Computer Science Canada Average Help! |
Author: | Nikola [ Sat May 03, 2008 10:38 pm ] |
Post subject: | Average Help! |
I dont know whats wronge but my code is arranging the wronge value for int_avg ![]() Quote: #include <stdio.h>
#include <stdlib.h> int main() { char str_name [30]; char str_name2 [30]; int int_age; int int_age2; int int_age3; 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 <=10; int_num++) { printf("Please enter the name of person #%d: ", int_num); scanf(" %d", str_name2); printf("Please enter the age of person #%d: ", int_num); scanf(" %s", int_age2); int_age3 = int_age2 + int_age3; } int_avg = int_age3 + int_age; printf("Congratulations %s on being person #1.\n", str_name); printf("The average of all ages is %d\n", int_avg); system("PAUSE"); return 0; } |
Author: | Tony [ Sat May 03, 2008 10:53 pm ] | ||
Post subject: | 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...
|
Author: | syntax_error [ Sat May 03, 2008 11:18 pm ] |
Post subject: | RE:Average Help! |
just as a side note, why not use an array? |
Author: | Tony [ Sat May 03, 2008 11:32 pm ] |
Post subject: | 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. |
Author: | Nikola [ Sun May 04, 2008 8:53 am ] |
Post subject: | 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; ) |
Author: | Nikola [ Sun May 04, 2008 9:03 am ] | ||
Post subject: | Re: Average Help! | ||
Alright I'm sorry I gave you really bad code here is the update!
|
Author: | Nikola [ Sun May 04, 2008 9:08 am ] |
Post subject: | RE:Average Help! |
Ugh alright so i hate c ![]() ![]() |
Author: | Tony [ Sun May 04, 2008 12:50 pm ] |
Post subject: | RE:Average Help! |
int_age3 was uninitialized, though you have fixed that in the last version. but your int_avg is still not an average, it's just the sum of all the ages. Isn't it? |
Author: | Dragan [ Sun May 04, 2008 6:36 pm ] |
Post subject: | RE:Average Help! |
Exchange int_avg = int_age3 + int_age; with int_avg = (int_age3 + int_age)/10; |
Author: | btiffin [ Mon May 05, 2008 6:52 pm ] | ||
Post subject: | Re: Average Help! | ||
Nikola; Et al. Not trying to be mean, but buffer overflow bugs are a very common way of virus writers getting into systems. I know it's a little bit heavy while still learning, but learn it now to put a stop to the crackers.
Anyway, google up on buffer overflow. scanf("%29s", name) would be better but there are other ways of sanitizing input as well. Sorry for the interruption, but this is something that should be learnt early and often. It is advanced, but early and often in my humble opinion. Again, apologies for the interruption on an issue that just complicates things. Cheers |