# Thread: Logical error with array

1. ## 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 )) {
}

else if (( examScores[ i ] >= 80 ) && ( examScores[ i ] <= 89 )) {
}

else if (( examScores[ i ] >= 70 ) && ( examScores[ i ] <= 79 )) {
}

else if (( examScores[ i ] >= 60 ) && ( examScores[ i ] <= 69 )) {
}

else if (( examScores[ i ] >= 0 ) && ( examScores[ i ] <= 50 )) {
}
}
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.

2. Why do you have two loops?

Also:
Code:
```			if (( examScores[ i ] >= 90  ) && ( examScores[ i ] <= 100 )) {
}

else if (( examScores[ i ] >= 80 ) && ( examScores[ i ] <= 89 )) {
}```
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

3. Originally Posted by matsp
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. Originally Posted by EVOEx
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

5. 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