..........
..........
Last edited by [Student]; 02-15-2011 at 04:56 PM.
First you need to learn to use code tags when posting...
<< !! Posting Code? Read this First !! >>
Next.. you are using the variable a for your inputs, you should sit down with a pen and paper and follow the value of a through your code, step by step... your program doesn't do what you think it does...
(Hint: How many values can a variable have?)
arrarys! learn live love em
since it's just 5 numbers you were testing i kinda cheated xD. what i did was declare an array for 5 integers and made the do while loop execute 5 times. (x < 5). it starts at 0 so that you can access the first element of the array. (a[0]). then i passed the address of the first element of the array to a pointer in add and average. since we know it's 5 elements we could simply use a[0], a[1], ... a[4].
Code:#include<stdio.h> #include<conio.h> #include<stdlib.h> void add(int * a){ int sum=0; printf("\nSum is : %d",sum=a[0]+a[1]+a[2]+a[3]+a[4]); } void average(int * a){ double average; printf("\nAverage is : %d",(a[0]+a[1]+a[2]+a[3]+a[4])/5); } main(){ printf("Enter 5 #s: "); int a[5]; int x=0; do{ scanf("%d",&a[x]); x++; } while(x<5); add(&a[0]); average(&a[0]); int choice; printf("\n\nRepeat the process?Press 1 for yes,other #s to exit: "); scanf("%d",&choice); if(choice==1){ system("cls"); return main(); } else { return 0; } getch(); }
Last edited by codeprada; 02-15-2011 at 08:27 AM.
We shouldn't be quick to criticize unless we can do better.
Code:public function __clone() { die ( "I'm one of a kind" ); }
tnx alot codeprada!.... ;D i'll study about this...what did you do to this code!....
one last question Mr. Codeprada.....how to change my average into float...so i can know the decimal values!....if i convert it'll show syntax error!...
Here's the loop where I think you think everything happens. In this loop, you will change a with scanf() five times. But the problem is, nothing besides that happens. By the time you get toCode:do{ scanf("%d",&a); x++; } while(x<=5);
You add the last a in printf's argument five times. If a is 5 then that is the same as 5*5.Code:add(a); /* pasted your implementation of it here */ void add(int a){ int sum=0; printf("\nSum is : %d",sum=a+a+a+a+a); }
So I hope your error makes sense. You should rewrite add() so that you can include it in the loop, after the scanf line. Mainly, add's job would be to accept a couple of arguments, add them, and return the result.
ahh...ok2x... tnx
How to convert it to float!...?....
so i can know the decimal values!.....plz help!...
Code:void average(int * a){ double average; printf("\nAverage is : %d",(a[0]+a[1]+a[2]+a[3]+a[4])/5); }
and so i can know the average! with decimal places!....
never mind.....i got it!...tnx to all of you for help!...
Way to go codeprada...
How much do you think our friend learned from you simply handing him the answer?
You didn't help this person at all... In fact you delivered exactly the wrong message: "Don't worry there's always someone out there who's stupid enough to do it for you"...
http://cboard.cprogramming.com/c-pro...-homework.html
Last edited by CommonTater; 02-15-2011 at 09:53 AM.
Hey!.... don't fight plz!.......ok i'm sorry!....but don't worry i'll study it!....this is not a home work...I'm just practicing!.....and i have problem on that line....so i need help!....i didn't copy paste the code!...i just analyze the code what codeprada do to this problem!.....