By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Contstructing Parallel Arrays question.

P: 16
I am trying to construct two, one-dimensional parallel arrays that will give me a letter grade for whatever score I enter. The two arrays I need are minimum score and grade. I don't understand how to build the score array. The scores are as follows: 0 - 299 should result in a F
300 - 349 should result in a D
350 - 399 should result in a C
400 - 449 should result in a B
450 - 500 should result in an A

I dont know how to enter the score data as a range of data.

Any kind of help/pointers on this would be greatly appreciated.
Sep 23 '06 #1
Share this Question
Share on Google+
12 Replies


Banfa
Expert Mod 5K+
P: 8,916
You will need an array for maximum score for grade and an array for minimum score for grade, plus the letter grade array gives 3 arrays.

However I would not do it like that, the min score, max score and letter grade are all related to ech other. I would create a structure containing min score, max score and letter grade and then have an array of structures

Expand|Select|Wrap|Line Numbers
  1. struct grading {
  2.     int iMinScore;
  3.     int iMaxScore;
  4.     char cGrade;
  5. };
  6.  
  7. struct grading scheme[5];
  8.  
Sep 23 '06 #2

P: 16
You will need an array for maximum score for grade and an array for minimum score for grade, plus the letter grade array gives 3 arrays.

However I would not do it like that, the min score, max score and letter grade are all related to ech other. I would create a structure containing min score, max score and letter grade and then have an array of structures

Expand|Select|Wrap|Line Numbers
  1. struct grading {
  2.     int iMinScore;
  3.     int iMaxScore;
  4.     char cGrade;
  5. };
  6.  
  7. struct grading scheme[5];
  8.  
Thank you for the tip, but the text specifically tells me that I must use only two one dimensional parallel arrays. Using an array of structures is not allowed and my hands are tied due to the wording of the question. I am not sure how I am going to build this. Is there anyway to store a range of data in each element of an array? LIke for example, could I have the following...
scores[5] = {0-299, 300-349, 350-399, 400-449, 450-500}

I have tried doing this but C++ recognizes the - as only a negative number or subtraction sign. Is there anyway to make it recognize it as meaning the number zero through 299? Thanks for any pointers.
Sep 24 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Only store either the minimum or maximum value, look at your data

scores[5] = {0-299, 300-349, 350-399, 400-449, 450-500};

in all cases the maximum limit of 1 index = the minimum limit of the next index - 1

i.e.
299 = 300 - 1
349 = 350 - 1
399 = 400 - 1
449 = 450 - 1

By trying to store the maximum and minimum for each grade you are duplicating data.

You can store the maximum (or minimum the choice is yours) for each grade and from that data construct an if - else if - else statement that correctly assignes the letter grade.
Sep 24 '06 #4

100+
P: 293
D_C
I think it would be much better to use several lines of code that calculates the letter grade. However, the text says to use arrays. It makes me wonder if my first programs were that inefficient.
Expand|Select|Wrap|Line Numbers
  1. if(grade == 500)
  2.   grade--;
  3.  
  4. grade -= 300;
  5. grade /= 50;
  6. grade *= -1;
  7. grade += 'A'+4;
Sep 24 '06 #5

P: 16
This is the code that I came up with and it is horrible! I don't know how to make it work to where the user can enter a number score and then the appropriate grade for that number range comes out. Any pointers with what is wrong with my code? I know there are a ton of errors. Any help is greatly appreciated!

displays a grade based on the total number of points
//entered by Ms. Jenkins
//Created/revised by Casey Dockins on 9/23/2006

#include <iostream>
#include <string>
#include <algorithm>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
//declare variables and arrays
int searchForScore = 0;
int minPoints[5] = {0, 300, 350, 400, 450};
string grades[5] = {"F", "D", "C", "B", "A"};

//get score to search for
cout << "Enter score (-1 to exit): ";
cin >> searchForScore;

while (searchForScore != -1)
{
//locate position of points in the points array
int y = 0; //keeps track of array subscripts
while (y < 5 && minPoints[y] != searchForScore)
y = y + 1;

//if score was found, display grade from grades array
if (y > 0 || y < 1)
cout << "grade: " << "F" << endl;
else if
(y > 1 || y < 2)
cout << "grade: " << "D" << endl;
else if
(y > 2 || y < 3)
cout << "grade: " << "C" << endl;
else if
(y > 3 || y < 4)
cout << "grade: " << "B" << endl;
else if
(y > 4 || y <= 500)
cout << "grade: " << "A" << endl;
//end ifs
} //end while


return 0;
} //end of main function
Sep 24 '06 #6

100+
P: 293
D_C
You can remove all the if else statements at the end. Once you have y, the grade they received is grade[y] (grade[0] = 'F', ... , grade[4] = 'A').
Sep 24 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
In this code

Expand|Select|Wrap|Line Numbers
  1.  int y = 0; //keeps track of array subscripts
  2. while (y < 5 && minPoints[y] != searchForScore)
  3. y = y + 1;
  4.  
The comparison != is wrong, have you considered what will happen here if you have entered a number that is not on one of the grade bounderies e.g. 341 ?

Also since you have chosen to put in minimum score boundaries you need to start the search from the top grade and work downwards using >=, if you used maximum score boundaries thaen you could start at the bottom and work upwards using <=.
Sep 24 '06 #8

P: 16
In this code

Expand|Select|Wrap|Line Numbers
  1.  int y = 0; //keeps track of array subscripts
  2. while (y < 5 && minPoints[y] != searchForScore)
  3. y = y + 1;
  4.  
The comparison != is wrong, have you considered what will happen here if you have entered a number that is not on one of the grade bounderies e.g. 341 ?

Also since you have chosen to put in minimum score boundaries you need to start the search from the top grade and work downwards using >=, if you used maximum score boundaries thaen you could start at the bottom and work upwards using <=.
I have tried my previous code and no matter what number I enter I get grade F over and over and over, I have to hit control C to stop the program. You said that != is wrong, so I changed that >= and <= and the program did the same thing as with the !=. I'm lost!
Sep 25 '06 #9

Banfa
Expert Mod 5K+
P: 8,916
I have tried my previous code and no matter what number I enter I get grade F over and over and over, I have to hit control C to stop the program. You said that != is wrong, so I changed that >= and <= and the program did the same thing as with the !=. I'm lost!
The reason you always get 'F' is because the comparision change is not enough by itself.

You have used lower bounds, this means you need to use >= but you are currently evaluating the possible grades in this order

0 - 299 should result in a F
300 - 349 should result in a D
350 - 399 should result in a C
400 - 449 should result in a B
450 - 500 should result in an A

Since you evaluate F first and everything is >= 0 you always get F

Using minimum bounds with a >= comparison you need to evaluate the grades in this order

450 - 500 should result in an A
400 - 449 should result in a B
350 - 399 should result in a C
300 - 349 should result in a D
0 - 299 should result in a F

That way it will find the first grade that meets the cirteria.


The reason that the program goes into a loop and needs Ctrl-C to exit is that you have the code you use to input the score

//get score to search for
cout << "Enter score (-1 to exit): ";
cin >> searchForScore;


Outside the main while loop instead of inside it.
Sep 25 '06 #10

P: 16
The reason you always get 'F' is because the comparision change is not enough by itself.

You have used lower bounds, this means you need to use >= but you are currently evaluating the possible grades in this order

0 - 299 should result in a F
300 - 349 should result in a D
350 - 399 should result in a C
400 - 449 should result in a B
450 - 500 should result in an A

Since you evaluate F first and everything is >= 0 you always get F

Using minimum bounds with a >= comparison you need to evaluate the grades in this order

450 - 500 should result in an A
400 - 449 should result in a B
350 - 399 should result in a C
300 - 349 should result in a D
0 - 299 should result in a F

That way it will find the first grade that meets the cirteria.


The reason that the program goes into a loop and needs Ctrl-C to exit is that you have the code you use to input the score

//get score to search for
cout << "Enter score (-1 to exit): ";
cin >> searchForScore;


Outside the main while loop instead of inside it.
Ok, I moved the //get score to search for piece of code to the outside of the main while loop and that did make the program stop looping. However, I am still unsure what to do in order to return the appropriate letter grade for whatever score I input. I still receive an F each time. Here is my updated code.

//Ch11AppE11.cpp displays a grade based on the total number of points
//entered by Ms. Jenkins
//Created/revised by Casey Dockins on 9/23/2006

#include <iostream>
#include <string>
#include <algorithm>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
//declare variables and arrays
int searchForScore = 0;
int minPoints[5] = {0, 300, 350, 400, 450};
string grades[5] = {"F", "D", "C", "B", "A"};

while (searchForScore != -1)
{
//get score to search for
cout << "Enter score (-1 to exit): ";
cin >> searchForScore;

//locate position of points in the points array
int y = 0; //keeps track of array subscripts
while (y < 5 && minPoints[y] >= searchForScore)
y = y + 1;
//if score was found, display grade from grades array
if (y > 0 || y < 300)
cout << "grade: " << "F" << endl;
else if
(y > 300 || y < 350)
cout << "grade: " << "D" << endl;
else if
(y > 350 || y < 400)
cout << "grade: " << "C" << endl;
else if
(y > 400 || y < 450)
cout << "grade: " << "B" << endl;
else if
(y > 450 || y <= 500)
cout << "grade: " << "A" << endl;
//end ifs

} //end while


return 0;
} //end of main function


Also, I am using the grades as string data so shouldn't I have to use the getline() somewhere in the code? Thanks for all of the pointers thus far....hopefully with a little more guidance I will finally realize how to make this things work.
Sep 25 '06 #11

P: 16
I finally figured it out! Thanks for all of the help on this one.
Sep 27 '06 #12

P: 3
i need help with this problem. do you still have the code with you? i am stuck with this problem
Dec 7 '11 #13

Post your reply

Sign in to post your reply or Sign up for a free account.