Create two arrays with 5 elements each: one will hold Strings and the second wil
ID: 3769397 • Letter: C
Question
Create two arrays with 5 elements each: one will hold Strings and the second will hold integers. Write a program 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 Joes Age: 25 Enter Marks Age: 24 Enter Marys Age: 35 Enter Michelles Age: 19 Enter Aarons Age: 65 Joe 25 Mark 24 Mary 35 Michelle 19 Aaron 65
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
//declare arrays
string Student_Name[5] = {"", "", "" ,"", ""};
int Student_Age[5] = {0};
//declare variables
string names = "";
int age = 0;
//Name entry
for (int i = 0; i < Student_Name.length; ++i)
{
cout << "Enter a student name: ";
cin >> Student_Name[i];
}
//Age entry
for (int age = 0; age < 5; age += 1)
{
cout << "Enter " << Student_Name[age] << "'s Age: ";
cin >> Student_Age[age];
}//end for loop
//Display Data
for (int i = 0; i < 5; ++i)
cout << Student_Name[i] << ' ';
for (int i = 0; i < 5; ++i)
cout << Student_Age[i] << ' ';
}
system("pause");
return 0;
}