Create two arrays with 5 elements each: one will hold Strings and the second wil
ID: 3814757 • Letter: C
Question
Create two arrays with 5 elements each: one will hold Strings and the second will hold integers. Write a program in C++ to ask the user to enter 5 student names and their ages. Output the data from the parallel arrays. Sample Run: Your program must run exactly like the example below. Enter a student name: Joe Enter a student name: Mark Enter a student name: Mary Enter a student name: Michelle Enter a student name: Aaron Enter Joe’s Age: 25 Enter Mark’s Age: 24 Enter Mary’s Age: 35 Enter Michelle’s Age: 19 Enter Aaron’s Age: 65 Joe 25 Mark 24 Mary 35 Michelle 19 Aaron 65
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
string names[5];
int age[5];
for(int i=0; i<5; i++){
cout<<"Enter a student name: ";
cin >> names[i];
}
for(int i=0; i<5; i++){
cout<<"Enter "<<names[i]<<"’s Age: ";
cin >> age[i];
}
for(int i=0; i<5; i++){
cout<<names[i]<<" "<<age[i]<<endl;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter a student name: Joe
Enter a student name: Mary
Enter a student name: Michlle
Enter a student name: Suresh
Enter a student name: Sekhar
Enter Joe’s Age: 12
Enter Mary’s Age: 22
Enter Michlle’s Age: 33
Enter Suresh’s Age: 44
Enter Sekhar’s Age: 55
Joe 12
Mary 22
Michlle 33
Suresh 44
Sekhar 55