
-----------------------------------
Nikola
Sat May 03, 2008 10:38 pm

Average Help!
-----------------------------------
I dont know whats wronge but my code is arranging the wronge value for int_avg :( Can anyone please help?

#include 
#include 

int main()
{
      char str_name 

-----------------------------------
Tony
Sat May 03, 2008 10:53 pm

RE:Average Help!
-----------------------------------
besides the fact that you are adding an uninitialized (so random memory dump) value to the sum, perhaps you can add a comment to how int_avg is calculated...


int_avg = int_age3 + int_age; 


-----------------------------------
syntax_error
Sat May 03, 2008 11:18 pm

RE:Average Help!
-----------------------------------
just as a side note, why not use an array?

-----------------------------------
Tony
Sat May 03, 2008 11:32 pm

RE:Average Help!
-----------------------------------
One wouldn't use an array in this case because we don't care to store any individual's age, and only care about the total.

Though with that in mind, asking for a person's name is quite redundant.

-----------------------------------
Nikola
Sun May 04, 2008 8:53 am

RE:Average Help!
-----------------------------------
I understand the basis of my code and im fine with it but i just need help with the average. I get the average by adding int_age2 to int_age 3 everytime the loop occurs. Than this total age is added with the age enter outside the loop (int_age) which is finally all posted by a variable called int_avg. Also I dont think i have an uninitialized value. All of my values are initialized. I did a few variable traces but everything still looks like it should work. Here is a list of the values of each variable (which affect the average):

int_age - is defined by the user
int_age2 - is defined at the start as 0 as to ensure that that is where the counting starts.
And the rest are just like int_age2
(
int int_age;
int int_age2;
int int_age3;
int int_avg=0;
int int_num=0; 
)

-----------------------------------
Nikola
Sun May 04, 2008 9:03 am

Re: Average Help!
-----------------------------------
Alright I'm sorry I gave you really bad code here is the update!
#include 
#include 

int main()
{
      char str_name [30];
      char str_name2 [30];
      int int_age;
      int int_age2=0;
      int int_age3=0;
      int int_avg=0;
      int int_num=0;
      printf("Please enter the name of person #1: ");
      scanf(" %s", str_name);
      printf("Please enter the age of person #1: ");
      scanf(" %d", int_age);
      for(int_num = 2; int_num 