Contstructing Parallel Arrays question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lifeshortlivitup
    New Member
    • Sep 2006
    • 16

    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.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #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

    Code:
    struct grading {
        int iMinScore;
        int iMaxScore;
        char cGrade;
    };
    
    struct grading scheme[5];

    Comment

    • lifeshortlivitup
      New Member
      • Sep 2006
      • 16

      #3
      Originally posted by Banfa
      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

      Code:
      struct grading {
          int iMinScore;
          int iMaxScore;
          char cGrade;
      };
      
      struct grading scheme[5];
      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.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        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.

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          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.
          Code:
          if(grade == 500)
            grade--;
          
          grade -= 300;
          grade /= 50;
          grade *= -1;
          grade += 'A'+4;

          Comment

          • lifeshortlivitup
            New Member
            • Sep 2006
            • 16

            #6
            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

            Comment

            • D_C
              Contributor
              • Jun 2006
              • 293

              #7
              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').

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                In this code

                Code:
                 int y = 0; //keeps track of array subscripts
                while (y < 5 && minPoints[y] != searchForScore)
                y = y + 1;
                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 <=.

                Comment

                • lifeshortlivitup
                  New Member
                  • Sep 2006
                  • 16

                  #9
                  Originally posted by Banfa
                  In this code

                  Code:
                   int y = 0; //keeps track of array subscripts
                  while (y < 5 && minPoints[y] != searchForScore)
                  y = y + 1;
                  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!

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by lifeshortlivitu p
                    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.

                    Comment

                    • lifeshortlivitup
                      New Member
                      • Sep 2006
                      • 16

                      #11
                      Originally posted by Banfa
                      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....hopefull y with a little more guidance I will finally realize how to make this things work.

                      Comment

                      • lifeshortlivitup
                        New Member
                        • Sep 2006
                        • 16

                        #12
                        I finally figured it out! Thanks for all of the help on this one.

                        Comment

                        • quashie
                          New Member
                          • Dec 2011
                          • 3

                          #13
                          i need help with this problem. do you still have the code with you? i am stuck with this problem

                          Comment

                          Working...