Parallel Arrays

Pages: 123
Im doing a program and it is asking me to do this.

Make a function named getData to interactively input data values into the variable listSize and then values into two parallel arrays, studentName and testScore, both of size listSize and return these values to the calling function. (Data validation: do not accept test score values outside the acceptable range of 0 through 100.)

The names are John Kathy Susan Alice and Bill
their test scores are 100 95 67 87 98

The output should look like this
The dialog generated by your program should be similar to the one below:

Enter size for arrays: 3
Enter 1th student name: Joanne
Enter test score for Joanne: -2
Re-enter test score for Joanne: 109
Re-enter test score for Joanne: 100
Enter 2th student name: Amy
Enter test score for Amy: 89
Enter 3th student name: Zack
Enter test score for Zack: 95

I dont really know how to start this at all. It would be nice if someone can help me start this, Thanks.

You will need to make arrays of a size that is determined at runtime, so you should be using the new keyword.
http://www.cplusplus.com/doc/tutorial/dynamic/

You'll then be using loops to get each input, one for each student/grade and another for re-entering bad input.
http://www.cplusplus.com/doc/tutorial/control/

Also, you may want to go above and beyond and fix the problem of firth, seconth, and thirth by using an array of suffixes to use for the last digit in a number.

Try writing some code and when you get stuck, post it and state what you are stuck on.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

void getData();

void getData() {

const char STUDENT_NAME[5][6] = {"John", "Kathy", "Susan", "Alice", "Bill"};
const int TEST_SCORE[5][1] = {100, 95, 67, 87, 98};

am i on the right track?
No, the user will give you the names and scores - you don't write them in the code yourself.
Last edited on
So how should i start this then?
Start by asking the first prompt from the example output/input:
Example Output wrote:
Enter size for arrays:
Last edited on
void getData() {
int size, studentName;

cout << "Enter size for arrays: ";
cin >> size;

now i need to do a for loop asking the name of the student and their test score
Yes, but before the for loop, you actually need the arrays!
Recall the tutorial I gave you a link to about how to do this: http://www.cplusplus.com/doc/tutorial/dynamic/
Please read it.
we havent learned that in class so im not allowed to use that
Well, consider this (and possibly ask your teacher): what if you make the arrays some size like 100, what happens when the user wants to enter 101 students?

You need to get information from your teacher about how many students you should use as a maximum.
There are only five students
OK, then make your array of size five and then ask your user to tell the student names and grades "size" times. Remember that "size" is the variable you made when you asked the user how many students they wanted to enter.
void getData() {
const int SIZE = 5;
int size, testScore[SIZE], index;


cout << "Enter size for arrays: ";
cin >> size;

for (index = 0; index <= size -1; index++)
{
cout << "Enter test score number "
<< (index +1) << ": ";
cin >> testScore[index];
}

}

i know this is wrong but idk what else to do
You're very close, you just need an array for the student names and you also need to ask the user to input the student names.

Remember, to have an array of student names, that will be a 2D array, because you will have an array OF character arrays.
void getData() {
const int SIZE = 5;
int size, testScore[SIZE], index, studentName[SIZE];


cout << "Enter size for arrays: ";
cin >> size;

for (index = 0; index <= size -1; index++)
{
cout << "Enter test score number "
<< (index +1) << ": ";
cin >> testScore[index];
}

for (index = 0; index <= size -1; index++)
{
cout << "Enter 1th student name: "
<< (index +1) << ": ";
cin >> studentName[SIZE];
}
}
Last edited on
According to the sample in your first post, you need to ask for the student name BEFORE the student grade. Also, you need to use index and not 1 where you wrote 1th.

Additionally, you still have a one dimensional integer array instead of a two dimensional character array:
char studentName[SIZE][6];

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

void getData();

void getData() {
const int SIZE = 5;
int size, testScore[SIZE], index, studentName[SIZE];


cout << "Enter size for arrays: ";
cin >> size;

for (index = 0; index <= size -1; index++)
{
cout << "Enter" <<(index +1) << "th student name: "
<< ": ";
cin >> studentName[SIZE];
}

for (index = 0; index <= size -1; index++)
{
cout << "Enter test score for " << studentName[index]
<< ": ";
cin >> testScore[index];



while (0 < testScore[index] && testScore[index] < 100){
cout << "Re-enter test score for " << studentName[index]
<< ": ";
cin >> testScore[index];
}
}
}


okay i got this now but it tells me i have an unresolved externals error
The error is because you forgot the main function ;)

Also, you still didn't make studentName a two dimensional array that holds characters and not integers. I even gave you code you could copy and paste:
char studentName[SIZE][6];
Last edited on
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

void getData(int size, int [], int []);

void main(){

}

void getData(int size,int testScore[],int studentName[]) {

const int SIZE = 5;
int index;


cout << "Enter size for arrays: ";
cin >> size;

for (index = 0; index <= size -1; index++)
{
cout << "Enter" <<(index +1) << "th student name: "
<< ": ";
cin >> studentName[SIZE];
}

for (index = 0; index <= size -1; index++)
{
cout << "Enter test score for " << studentName[index]
<< ": ";
cin >> testScore[index];



while (0 < testScore[index] && testScore[index] < 100){
cout << "Re-enter test score for " << studentName[index]
<< ": ";
cin >> testScore[index];
}
}
}

okay now somehow i need to return this data into the calling function. So the data back into void main()
how do i do that?
void getData(int size,int testScore[],int studentName[]) {
Delete int size
Change int studentName[] to char studentName[][]

Delete const int SIZE = 5;

Move these lines into main:
1
2
cout << "Enter size for arrays: ";
cin >> size;


cin >> studentName[SIZE];
Change SIZE to size

while (0 < testScore[index] && testScore[index] < 100){
Change both < to >
change && to ||

In main, you need to make int Scores[5]; and char Students[5][6];
Then you call [code]getData(size, Scores, Students);
Pages: 123