I'm a beginner with programming in C and I have a problem to solve at which point I get stuck.
So my program reads numbers until the value 0 then it calculates the sum of numbers. Then I have to write a function which displays the sum. Also, my program read a number "y" from keyboard and I need to find the result of the sum/y.For example if the result of the sum is 10 and I enter y=3 the function result should return the result of 10/3.
My program need to be built with functions. I received the functions name with parameters and I built them like this:
#include <stdio.h>
// Shows a message with what the program is doing.
void ShowIntroduction(void)
{
printf("My program finds a sum etc");
}
// find the sum of the numbers enter until 0 value
int sum(void)
{
int s=0,n;
do
{
scanf("%d",&n);
if (n > 0)
s=s+n;
}
while(n != 0);
return s;
}
// show the result
void sumResult(int a)
{
printf("The sum is %d", a);
}
// find the result of sum/y
double result(int s,int y)
{
double res;
res=s/(double)(y);
return res;
}
int main()
{
int y;
scanf("%d",&y);
ShowIntroduction();
sumResult(sum());
result(sum(),y);
return 0;
}
The problem is that my program finds the sum but when it should display the result of the sum / y it asks me to give the values again for the sum until 0 value then the program closes.I think I apelate the result function in a wrong way.How to correct this?