3 Replies - 989 Views - Last Post: 07 August 2009 - 07:42 AM Rate Topic: -----

#1 diturralde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-August 09

Question about getting average of an array in for loop

Post icon  Posted 06 August 2009 - 09:24 PM

Hi,

I'm just learning C and have a doubt in a problem, here it is:


#include <stdio.h>

int main(){

	 float ave = 0.00f, ave_a = 0.00f, ave_f = 0.00f; 
	 int s = 0, s1 = 0, s2 = 0;
	 int i, grades[4];

	 puts("Please type 4 grades: ");

	 for (i = 0; i < 4; i++){

		   scanf("%d", &grades[i]);
	 }

	 for (i = 0; i < 4; i++){
		   if (grades[i] > 13){ //This is the min to pass.
			   s += 1;
			   ave_a = (ave_a + grades[i]) / s;
		   }else{
				s1 += 1;
				ave_f = (ave_f + grades[i]) / s1;
		   }

		s2 += 1;
		ave = (ave + grades[i]) / s2; //this is the problem....not getting correct average
		}

	 printf("You passed %d grades: \n", s);
	 printf("You failed %d grades: \n", s1);
	 printf("The average of the grades passed is: %.2f\n", ave_a);
	 printf("The average of the grades failed is: %.2f\n", ave_f);
	 printf("The average of all the grades is: %.2f\n", ave);

	 getchar();
	 return 0;
}



Output:

Please type 4 grades:
15
18
11
9
You passed 2 grades:
You failed 2 grades:
The average of the grades passed is: 16.50
The average of the grades failed is: 10.00
The average of all the grades is: 4.54

My question is why can't I get the average of all the elements in the array as I'm doing within the if, I get a wrong average. I doing it within the loop, it should get me a correct average???

Is This A Good Question/Topic? 0
  • +

Replies To: Question about getting average of an array in for loop

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Question about getting average of an array in for loop

Posted 06 August 2009 - 10:29 PM

View Postditurralde, on 7 Aug, 2009 - 03:24 AM, said:

Hi,

I'm just learning C and have a doubt in a problem, here it is:


Output:

Please type 4 grades:
15
18
11
9
You passed 2 grades:
You failed 2 grades:
The average of the grades passed is: 16.50
The average of the grades failed is: 10.00
The average of all the grades is: 4.54

My question is why can't I get the average of all the elements in the array as I'm doing within the if, I get a wrong average. I doing it within the loop, it should get me a correct average???

you a dividing within the loop (are you trying to calculate a running average?)
		s2 += 1;
		ave = (ave + grades[i]) / s2; //this is the problem....not getting correct average
		}



for a normal average sum within the loop and divide when you have finished, e.g.
		s2 += 1;
		ave = (ave + grades[i]); //this is the problem....not getting correct average
		}

	

	 printf("The average of all the grades is: %.2f\n", ave/s2);  // divide here!



Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Question about getting average of an array in for loop

Posted 07 August 2009 - 01:02 AM

int size = 10;
int myArray[size];
int average = 0;
for{int i=0; i<size; i++)
	average += myArray[i];
average/=size;



how to find the average of an array

This post has been edited by ImaSexy: 07 August 2009 - 01:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 diturralde  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-August 09

Re: Question about getting average of an array in for loop

Posted 07 August 2009 - 07:42 AM

got it.

thanks everyone for the help...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1