assigning char with parallel arrays

Jun 21, 2016 at 12:42am
Within my program i am supposed to assign one array (student_name) with grades by using a parallel array (grades). i'm not sure how to assign the student_name array with the grades by using a parallel array. in my program i have found out the average of each student so i know their scores and what their grades should be, but i'm not sure how to go about it using the parallel array. i've already made the parallel array and it's ready to be used. here's a sample of my code of how i think it's supposed to look

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  for (a = 0; a < 10; a++)
				{
					cout << left << setw(14) << student_name[a] << ": ";

					for (b = 0; b < 5; b++)
					{
						average += student_score[a][b];
					}

					average = average / 5;
					
					if (average <= 100 && average >= 90)
					{
						//student_name[a] receives A
					}


					else if (average < 90 && average >=80)

					{
						//student_name[a] receives B
					}


					else if (average < 80 && average >=70)
					{
						//student_name[a] receieves C
					}

					else if (average < 70 && average >= 60)
					{
						//student_name[a] receieves D
					}

					else if (average <= 50 )
					{
						//student_name[a] receives F
					}


					average = 0;
				}


at the end i assign average to 0 because the program would continue to add on number to the average. i would prefer to keep that there, it helps me and it works like that. but if there's a more cleaner way or more efficient way than assigning average to 0 at the end please give me your input. if it helps i'll also post the txt file.





Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,
Jun 21, 2016 at 2:34am
Hi,

I don't understand why are you using parallel arrays and complicating your code-logic.

You may just use a struct/class like this

1
2
3
4
5
6
7
8
9
10
11
class student
{
string name;
int grade[5];
char grade;
public:
//function to assign avg
..
..
//& other functions (initialize and all that stuff)
};


not only it will make the code easy but also more readable (a readable code is heaven to every coder)
Last edited on Jun 21, 2016 at 2:35am
Jun 21, 2016 at 2:45am
i would but the project outlines don't want us to use that yet. the next project i'll be doing would include struct/classes, but for now we can't use that
Jun 21, 2016 at 3:42am
can you at least use 2D arrays???
Jun 21, 2016 at 4:04am
closed account (48T7M4Gy)
score[][] at line 3 above is the 2d array.
Jun 21, 2016 at 5:01am
whoops! missed that

here's a slightly modified code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
float average[10]; //float as average could be decimal
  for (a = 0; a < 10; a++)
				{
					cout << left << setw(14) << student_name[a] << ": ";

					for (b = 0; b < 5; b++)
					{
						average[a] += student_score[a][b];
					}

					average[a] = average[a] / 5;
					if (average[a] <= 100 && average[a] >= 90)
					{
						//student_name[a] receives A
					}


					else if (average[a] < 90 && average[a] >=80)

					{
						//student_name[a] receives B
					}


					else if (average[a] < 80 && average[a] >=70)
					{
						//student_name[a] receieves C
					}

					else if (average[a] < 70 && average[a] >= 60)
					{
						//student_name[a] receieves D
					}

					else if (average[a] <= 50 )
					{
						//student_name[a] receives F
					}

				}


you see you can just use a parallel array for average too... this will help you in your code

although in real life stick with class/struct

hope it helps
Last edited on Jun 21, 2016 at 5:03am
Jun 22, 2016 at 9:54pm
i'm still a bit confused on how i'm going to assign grades to the student_name array by using the parallel array grades
Jun 23, 2016 at 3:02am
closed account (48T7M4Gy)
Parallel arrays are not the same concept as multi dimension arrays.
http://mathbits.com/MathBits/CompSci/Arrays/Parallel.htm

A multi dimension array with 2 dimensions arry[][] is an array of arrays, and it can be in parallel with other arrays, for example student_name. The link between the two is the value of the array index number.

So if there are 100 students and 5 grades then grade_array will have [100][5] elements. If you want to store averages after calculating them then declare grade_array to grade_array[100][6] and for student[x] the average will be grade_array[x][6]

Jun 23, 2016 at 4:51am
you just have to take a new array for grade and assign it inside the loop
Jun 23, 2016 at 5:46am
closed account (48T7M4Gy)
Keeping in mind also that the average and grade are derived values and probably best not saved at all because they need to be recalculated if the grades change. A function is more often than not the way to go in such cases.
Jun 23, 2016 at 5:53am
Keeping in mind also that the average and grade are derived values and probably best not saved at all because they need to be recalculated if the grades change. A function is more often than not the way to go in such cases.


+1 kemort
Topic archived. No new replies allowed.