473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Contstructing Parallel Arrays question.

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
12 5271
Banfa
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
D_C
293 100+
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
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
D_C
293 100+
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
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
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
I finally figured it out! Thanks for all of the help on this one.
Sep 27 '06 #12
i need help with this problem. do you still have the code with you? i am stuck with this problem
Dec 7 '11 #13

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

Similar topics

6
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within...
126
by: ramyach | last post by:
Hi friends, I need to write a parallel code in 'C' on the server that is running SGI Irix 6.5. This server supports MIPS Pro C compiler. I don't have any idea of parallel C languages. I looked...
1
by: kmuz08 | last post by:
Hello to everyone, I have to do a project in my discovering programming class based on creating parallel arrays for an order form. I am still not completely sure was a parallel array...
9
by: IamIan | last post by:
I'm using an array to store map features (name, lat, lon, caption, etc), from which the user can then select an individual feature. The problem is that when thousands of features are stored in the...
8
by: earla12 | last post by:
Hi, I'm working on a program that takes the following input from the user: Student Name, Student Number, and Student GPA The information is then supposed to be stored in three separate...
3
by: T. Crane | last post by:
Hi all, I have some data that I am using a vector<vector<double container to hold. The data is n sets of (x,y,z,intensity) data points. I can either group my data like this: ...
1
by: joor | last post by:
Hi I am a beginner and currently trying to create a small program. I seem to be stuck on the multiplication of their elements. Eg. I have an Array with 4 different prices for 4 different...
4
by: gw7rib | last post by:
I'm using a system in which TCHAR is typedef-ed to represent a character, in this case a wchar_t to hold Unicode characters, and LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume...
4
by: mh | last post by:
I want to interate over two arrays in parallel, something like this: a= b= for i,j in a,b: print i,j where i,j would be 1,4, 2,5, 3,6 etc.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.