Taking Array Inputs
Remember Kirti, Jubin, and Riya's example? If not then let me state it again.
Suppose there are 3 students: Riya, Jubin and Kirti. We need to write a program to input their marks and print their average. From what we have studied so far, we know that we can create an array of size 3 that'll store their marks. But who'll provide the marks?? Hence, we need to take input from the users themselves.
This is what we'll study in this article: How to take input from the user for the values of the array elements.
Example
Let's see now how to take inputs from the user:
Problem Statement
Create an array of size 5, take its elements as input from the user, and print the array elements.
Input
Enter the first element: 15
Enter the second element: 25
Enter the third element: 35
Enter the fourth element: 45
Enter the fifth element: 55
Code
#include <iostream>
using namespace std;
int main() {
// declare an array
int arr[5];
cout << "Enter the first element: ";
cin >> arr[0];
cout << "Enter the second element: ";
cin >> arr[1];
cout << "Enter the third element: ";
cin >> arr[2];
cout << "Enter the fourth element: ";
cin >> arr[3];
cout << "Enter the fifth element: ";
cin >> arr[4];
cout << "Array Elements:" << endl;
// access array using loop
for (int i = 0; i < 5; ++i) {
cout << arr[i] << endl;
}
return 0;
}
Output
Enter the first element: 15
Enter the second element: 25
Enter the third element: 35
Enter the fourth element: 45
Enter the fifth element: 55
Array Elements:
15
25
35
45
55
In this case, arr[index]
acts as separate variables for which we take inputs separately and then print them using a for loop. But can we use a loop to take inputs just like we did while printing outputs? Yes!
Taking array inputs using loop
Let's see the same example, but we'll take inputs using a loop this time.
Input
Enter the first element: 15
Enter the second element: 25
Enter the third element: 35
Enter the fourth element: 45
Enter the fifth element: 55
Code
#include <iostream>
using namespace std;
int main() {
// declare an array
int arr[5];
cout << "Enter array elements:" << endl;
for (int index = 0; index < 5; ++index) {
cin >> arr[index];
}
cout << "Array Elements:" << endl;
// access array using loop
for (int index = 0; index < 5; ++index) {
cout << arr[index] << endl;
}
return 0;
}
Output
Enter array elements:
15
25
35
45
55
Array Elements:
15
25
35
45
55
Problem
Now let's solve the initial problem that we discussed else the students might get sad : (
Problem Statement
Suppose there are n
students. Write a C++ program to input their marks and print their average using a marks array.
Input
No. of students = 3
Marks of student 1 = 89
Marks of student 2 = 67
Marks of student 3 = 95
Output
Average marks: 83.6667
Hint
Create an array of size '3' and data type 'double' as marks or their average could be in decimals as well.
Solution
#include <iostream>
using namespace std;
int main() {
int n; // Number of students
cout << "Enter the no. of students: " << endl;
cin >> n; // Input Number of students
double marks[n]; // Array to store marks of n students
// Input marks for each student
for (int i = 0; i < n; i++) {
cout << "Enter marks for student " << i + 1 << ": " << endl;
cin >> marks[i];
}
// Calculate the average
double sum = 0;
for (int i = 0; i < n; i++) {
sum += marks[i];
}
double average = sum / n;
// Print the average marks
cout << "Average marks: " << average << endl;
return 0;
}
Going great coders! Just keep revising the array concepts and we'll meet in the next article for more new topics : )