## C Programming Tutorial – 14 – Calculating the Average Age

GitHub – https://github.com/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/

• Views:137,624 views
• Rating:
• Categories:
• Tags:

MrTacCcCcC says:

It’s okay to do that, right?
int age1, age2, age3;
float average;

David Sho says:

finally I can do something interactive, thank you!

Isaiah Taylor says:

Just a bit of beef with how you explained assigning the same values to multiple variables… its not that the compiler is “looking right to left” and that b is equal to c… its that the statement c = 3 *returns* 3. Assignments return the assignee.

a = b = c = 3;

is just short hand for

a = (b = (c = 3));

That’s how its actually being processed. Each variable gets the result (or the return) of the assignment ahead of it.

Perhaps a clearer example:

int a;

printf(“%d”, (a = 3));

>>3
The above prints out 3… but certainly not because its trying to access the variable a. It’s printing out the result of that assignment.

This may seem like splitting hairs, but I think it’s actually pretty important that people new to the language understand what the compiler is doing and why. Knowing that assignments return the assignee is useful, and thinking that a = b = c = 3 is actually referencing those address spaces to inform each variable is misleading and confusing, and goes against how we should expect the compiler to operate.

Cheers.

Zeke Ramos says:

You’re amazing! I wish I would have given you several thousand bucks instead of having given it to my school xD

prashant gupta says:

I have a mac and whenever i run a c program it opens terminal but i cant see my program running. What can i do?

r4xe says:

Why do you use float for that? why not int? Is float required or are you just using it for the luls

IndiCoder says:

Can anyone please find my mistake here? :- I am getting weird result of 4200728.000000
#include
#include

int main()
{ int age1, age2, age3 ;
age1=age2= 12;
float avg = (age1+age2+age3)/3;
printf(“We have to boys each age of %d.n”, age1 );
scanf(“%d”, &age3);
printf(“Hi! Our team’s average age is %f”, avg);
return 0;
}

Hesham Sugail says:

Harsh Pherwani says:

42 isn’t that old for a kindergarden student though, who knows

Leverquin says:

one question at age3 he put & but later on average he didn’t. why?

broefkip says:

In that function with the ages, how do you make a sort of “security” that people enter floats (or in my case integers) in the input? So it will print out a line like if it’s not an float (or in my case an int): printf(“please enter a number”) or smth like this

JaneliMae says:

thanks bucky!

jack hammer says:

Damn u Emma!

Deutsch Fly says:

You’re damn awesssoommeeeee

Tarun Lakra says:

what is the meaning of %f and can someone tell me some tips of remembering different programming languages

Emtiaz Ahmed says:

How did the computer solve the value of age3?
And please, mention why did you put “&” in “&age3”

is average a function?or just a variable set by us

Seven Day says:

I was about to object to two “kindergarteners” being 4 years old, but then you wrote 42 and… Yeah, whatever. Shine on you crazy diamond.

james brown says:

im here now!! =D yay

Yicheng Wang says:

char Name [20];
scanf(%s, Name );
printf(“%s love this video!!!!a “, Name);
return 0;

Christopher Pilcher says:

-Another pro-tip:-

-int a, b, c;-

-a = b = c = 100;-

-That works, instead of having int written out 3 times.-

I spoke too soon.

chimchim says:

what if the question was make program to find the average of a set of numbers ..and didnt tell us how many elements ..plz i need answer soon .. like how to tell the user to enter the elements while i dont know how many are they ..!!

float age1[4] ,age2[4], age3,average;
printf (“what is age1?n”);
scanf (“%f”,age1);
printf (“what is age2?n”);
scanf (“%f”,age2);
printf (“what is 3?n”);
scanf (“%f”,&age3);
average = (age1 + age2 + age3) / 3;
printf(“the average age of the group is %f”,average);
it is givin an errer before average..
why

కిరణ్ కుమార్ బొక్కిసమ్ says:

hey, why aren’t you saying anything about the headers that will be required for every program??! It seems that these videos can’t be completely relied upon for learning the programming languages completely.

dannytheman says:

#include
#include

int main()
{
float age1, age2, age3, average;
age1 = age2 =4.0;

scanf(“%f”,&age3);

average =(age1 + age2 + age3);
printf(“the average age of a group is %d.”,&average);

return 0;
}

jorgen T says:

It’s sad that people are just watching these for school. People should just watched these for fun!

Aman Rustagi says:

Thanks a lot. Here is a similar program to print average marks of students.
http://www.techcrashcourse.com/2015/11/c-program-find-total-average-percentage-marks.html

192.168.0.13 says:

wow i actually did something faster than yours no need for average var and 1 line less. IM SUCH A GOOD PROGRAMMER!!!!!!
{

float age1, age2, age3;
age1 = age2 = 43.0;

printf(“how old are youn”);
scanf(“%f”, &age3);

printf(“the average is %f”, (age1 + age2 + age3)/3 );
return 0;
}

Yiannis Sarris says:

lets say you changed the value of age2 in line 8 (age2=5;) would that affect the value age1 or not? i mean because of the way you assigned their values in line 7..

Mark Abelardo says:

sir can you help me for the formula of motorcycle loan?? the interest.monthly.price of the motorcyle how can i code it sir?? it so confusing for me.

Lester Deguzman says:

Heyy, Can i use 3 floats in one Scanf?

russkydeutsch says:

Need some help with an error message in code blocks when I was doing some practice…
Process terminated with status -1073741510

this is my code

#include
#include

int main()
{
int currentyear;
int birthyear;
int age;
age = currentyear-birthyear;

printf(“when were you born? n”);
scanf(” %d”, &birthyear);

printf(“what year is it? n”);
scanf(” %d”, &currentyear);

printf(“you are %d years old”, age);

return 0;
}

maybe I’m doing something wrong.

i need money says:

wat kind of bagel was it

Ameya Pagrut says:

hi bucky , thanks for the lessons.
i have a question .
what i don’t understand is why do we use a float value for scanf if when the age entered is going to be an integer??it got me a bit confused , thanks

Sean Lin says:

how come we do not use &average? how come we don’t need the ampersand in front of average? even though it is not an array.

Eric Shin says:

I don’t understand why there is no ‘&’ sign before the float average. We learned that we need a & sign for everything except arrays right? Is average considered an array here? Even if we used %f?? Thank you I am a bit confused

warning: format specifies type ‘int’ but the argument has type ‘int *’ [-Wformat]
printf(“%d”, &inp);
~~ ^~~~
1 warning generated.

Nick Rameau says:

Enjoy this peeps:

#include
#include

int main()
{

int howMany, age, sumAges;
sumAges = 0;

printf(“%sn”, “How many are you in the room right now?”);
scanf(“%d”, &howMany);

for (int i = 0; i < howMany; ++i) { printf("%s %d?n", "How old is person", i+1); scanf("%d", &age); sumAges += age; } printf("%s %d.n", "Based on what you told me, the average age in the room is", sumAges / howMany); return 0; }