Logical error with array

This is a discussion on Logical error with array within the C++ Programming forums, part of the General Programming Boards category; Hey guys. Im so close to finishing this hw but im stumped on this part. Im trying to add a ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Logical error with array

    Hey guys.

    Im so close to finishing this hw but im stumped on this part.

    Im trying to add a letter grade to the array element depending on what exam score is in the condition of the if statement. Now, it all compiles ok how it is but when I run it, it only prints the first grade for the first result and
    then duplicates this all the way down the list.

    Here is my code:

    Code:
    // function to calculate the letter grade for each student
    void calculateGrade ( char letterGrades[], double examScores[], const int SIZE ) {
    	for ( int i = 0; i < SIZE; ) {
    		for ( int j = 0; j < SIZE; j++ ) {
    			if (( examScores[ i ] >= 90  ) && ( examScores[ i ] <= 100 )) {
    				letterGrades[ j ] = 'A';
    			}
    
    			else if (( examScores[ i ] >= 80 ) && ( examScores[ i ] <= 89 )) {
    				letterGrades[ j ] = 'B';
    			}
    
    			else if (( examScores[ i ] >= 70 ) && ( examScores[ i ] <= 79 )) {
    				letterGrades[ j ] = 'C';
    			}
    
    			else if (( examScores[ i ] >= 60 ) && ( examScores[ i ] <= 69 )) {
    				letterGrades[ j ] = 'D';
    			}
    
    			else if (( examScores[ i ] >= 0 ) && ( examScores[ i ] <= 50 )) {
    				letterGrades[ j ] = 'F';
    			}
    		}
    		i++;
    	}
    }
    I think i have the i++ in the wrong place, or am I looping
    inccorectly? Btw both arrays were initlized to empty before this function was called.

    Basically an output would be:

    Code:
    letter grades for exam:
    A
    A
    A
    A
    A
    A
    A
    A
    A
    A
    When it should always be diff for each one pending the value - so it is deffinatly this function I am having the logical error - any help really appriciated.
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why do you have two loops?

    Also:
    Code:
    			if (( examScores[ i ] >= 90  ) && ( examScores[ i ] <= 100 )) {
    				letterGrades[ j ] = 'A';
    			}
    
    			else if (( examScores[ i ] >= 80 ) && ( examScores[ i ] <= 89 )) {
    				letterGrades[ j ] = 'B';
    			}
    When you get to the second if, you already KNOW that it's less than 90, so you don't really need to check that. What do you do if the score is actually more than 100 (your current code does "nothing")?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by matsp View Post
    When you get to the second if, you already KNOW that it's less than 90, so you don't really need to check that. What do you do if the score is actually more than 100 (your current code does "nothing")?
    Actually... The datatype is a double. So it could be 89.5.
    So the code does something, although it's probably not what the OP expected.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by EVOEx View Post
    Actually... The datatype is a double. So it could be 89.5.
    So the code does something, although it's probably not what the OP expected.
    Ok, fair enough. Which would make it even BETTER to not check both ends with integer values, don't you think?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    If
    Code:
     
    double examScores[]
    holds the exam scores of students and letter grades hold the corresponding student grade
    (i assume for 'i'th element of examScores[] ,the grade is stored in 'i'th element of
    letterGrades[]),if that's what you want then a single outer for loop will suffice)

    Given your code it seems ,for a particular examScores[], you are putiing the same grade in
    letterGrades[]
    If suppose your examScores[0] = 91,then all the elements in letterGrades (upto SIZE) will be filled by the same grade,that's why you get all the "A" in letterGrades[] and i guess that is not what is expected to be in lettterGrades[].

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Thanks for your help everyone. The single loop did the trick.

    And i will take note matsp of the value checking in my if statements too, and decide on a better method. Thanks again for all the help.
    I'm just trying to be a better person - My Name Is Earl

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by matsp View Post
    Ok, fair enough. Which would make it even BETTER to not check both ends with integer values, don't you think?
    Of course :P. That's why it was a joke :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21