0

I am passing an array of exam grades to the examAverage function along with the size of the array. When I printf the array contents in the for loop they are correct, but when they are added together the sum is wrong. Even if I printf immediately after I set double sum = 0, it says sum = 2. By the time the function is complete I get an extremely large number. Any help is appreciated.

double examAverage(int arr[], int size){

  int    i;
  double exAvg=0;
  double sum =0;
  printf("Sum = %d\n", sum);

  for (i = 0; i < size; ++i)
  { 
    printf("Sum = %d\n", sum);
    sum = arr[i];
  }

  exAvg = sum / size;

  return exAvg;
}

Main

double examAverage(int arr[], int size);    
printf("How many exam grades would you like to enter?\n");
scanf("%d", &examGrad);    

int exams[examGrad];

printf("Enter exam grades \n");
for(int i=0; i<sizeof(exams)/sizeof(exams[0]); i++){
    scanf("%d", &exams[i]);
}

double exAvg;
exAvg= examAverage(exams, examGrad);
printf("Exam average = %d", exAvg);

Output

How many exam grades to enter?
2
Enter exam grades
100
50
Sum = 2
Sum = 10
Sum = 1079574528
Exam Average = 1081159680
1
  • sum = arr[i]; should be sum += arr[i];, and change the printf as the below answer indicates Commented Apr 9, 2020 at 18:59

2 Answers 2

2

Don't You mean sum += arr[i]; in the for loop in examAverage? Also, Your code labeled Main is not really valid at all, first, there is no declaration of examGrad, second the function declaration at the top does whoever knows what, third, sizeofs in the for loop can be replaced by the examGrad, the %d was already mentioned. If You ask questions about simple code just include all of it, If it is too much, cut out the unnecessary parts or ensure us that they are there, otherwise we don't know if the problem is the missing code or not.

Sign up to request clarification or add additional context in comments.

3 Comments

Good call, but you should add in the %f thing, since that is also a problem.
Yes, sorry I changed that to try and troubleshoot.
Yea there was a good amount of code I left out, but I figured you guys only needed to see the function and how I am calling it in this example. I am not sure what you mean by the function declaration at the top does whoever knows what? It is just passing my array and the size of the array to the examAverage function in a different file.
1

Do not use %d to print a double; that is for integers. Use %f. The different way in which integers and floating point numbers are stored accounts for the problems that you are seeing.

You should have seen a warning about this when you compiled, along the lines of:

format specifies type 'int' but the argument has type 'double'

2 Comments

Thanks, I can't believe I overlooked that. I do not have any warnings though which is odd.
@Socci if you're using gcc, compile your code with the -Wall flag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.