Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Average Help!
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nikola




PostPosted: 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 Sad Can anyone please help?

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;
}
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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...

code:

int_avg = int_age3 + int_age;
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
syntax_error




PostPosted: Sat May 03, 2008 11:18 pm   Post subject: RE:Average Help!

just as a side note, why not use an array?
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Nikola




PostPosted: 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;
)
Nikola




PostPosted: 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!
code:
#include <stdio.h>
#include <stdlib.h>

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 <=10; int_num++)
      {
        printf("Please enter the name of person #%d: ", int_num);
        scanf(" %s", str_name2);
        printf("Please enter the age of person #%d: ", int_num);
        scanf(" %d", &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;
}
Nikola




PostPosted: Sun May 04, 2008 9:08 am   Post subject: RE:Average Help!

Ugh alright so i hate c Sad i figured out the problem, i was missing a SINGLE '&' in the first scanf. Thanks anyways guys Smile
Tony




PostPosted: 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Dragan




PostPosted: 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;
btiffin




PostPosted: 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.
code:
name char [30];
scanf ("%s", name);
dangerous code. What if someone types 40 characters? Or a cracker handcrafts a packets with 30 chars and then some well chosen binary code that will overlay your code space, get executed and then open up your system to spam crooks.

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
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: