any idea how i would take a group of numbers, entered in the comand window, and make it spit out the average? I JUST started C so im very newb atm.
any idea how i would take a group of numbers, entered in the comand window, and make it spit out the average? I JUST started C so im very newb atm.
Could you help me whith it please?Originally Posted by kermit
why cant i enter an age in the command window with this? i hit enter and type a # then hit enter again and it closes.Code:#include <stdio.h> int main() /* Most important part of the program! */ { int age; /* Need a variable... */ printf( "Please enter your age" ); /* Asks for age */ scanf( "%d", &age ); /* The input is put in age */ if ( age < 100 ) { /* If the age is less than 100 */ printf ("You are pretty young!\n" ); /* Just to show you it works... */ } else if ( age == 100 ) { /* I use else just to show an example */ printf( "You are old\n" ); } else { printf( "You are really old\n" ); /* Executed if no other statement is */ } return 0; }
Is this your problem?
Hope this helps:Originally Posted by rock4christ
Also look up this excellent link (slide 4 onwards explains the algorithm and program): http://www.csse.monash.edu.au/course...ect/lect10.pptCode:input limit sum=0 count=0 while (count<limit) { read num add num to sum increment count } average = sum/count print average
Last edited by noodles; 09-08-2006 at 07:19 PM.
i actually remembered another way i was shown, that works on mine:
at end of code
getch ();
return0;
but could you help on the averaging question?
Hey,
Enter your code within a while loop. You are getting only one age as input.
insert this while loop.
total = 0;
count = 0;
do
{
//your code here.
count ++;
total += age;
printf("Do you want to continue(y/n) :");
res = getc()
}while( res == 'y' )
average = total / count;
Jag
Read post#5 above and see the external link as well.Originally Posted by rock4christ
getch() is non-standard. Meaning it may exist on your compiler but not on others. It's best for you to use a standard function like getchar() while you have one.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
HWJR?
How would Jesus ROCK? HAHAHA You guys crack me up.
When you're not creeping me out that is.
Quzah.
Hope is the first step on the road to disappointment.
What I don't get: how's the code copied and pasted from a tutorial related to your problems? What does a program that decides if a person is pretty yound, old or very old have to do with averaging input?
People like it if you show your efforts, not just any piece of code you can find.
they were 2 seperate questions, i just didnt want to make another thread.
The way to approach this, and certainly more taxing programs, is to think how would you get the average of a sequence of numbers? Add up all the numbers, and divide by the number of 'em, right? So,
That's not real C, obviously, but it puts you in the thinking mode about how to accomplish the task. (as #7 said!!!)Code:int NumOfNumbers = 0; int Total = 0; while ( You still wish to add numbers ) { print - "do you want to add more numbers"; get - answer if ( yes ) { NumOfNumbers++; Get the number; Number += The Gotten Number; } } if ( NumOfNumbers isn't 0 ) { average = Number/NumberOfNumbers; }