Reputation: 3
This program will determine the count, minimum, maximum, sum and average of all valid numbers entered. It will stop asking for new values when zero is entered and don't accept negative values. After asking for 1st number it just stops working. I will be very happy if anyone can give me some tips how to write it more efficiently and some for just coding.
#include <stdio.h>
#include <stdlib.h>
void display(void);
void menu(void);
int main(void)
{
display();
menu();
system("pause");
return 0;
}
void display(void)
{
printf("program will determine the count, minimum, maximum, sum and average of all valid numbers entered.\n");
return;
}
void menu(void)
{
int count;
float min = 0;
float max = 0;
float sum;
float avg;
float user = 0;
int index;
printf("Please enter the number ==> ");
do
{
// I think I scew up here plz check it.
scanf("%f", user);
if (user > 0)
{
if (user > max)
{
max = user;
}
if (user < min)
{
min = user;
}
}
else
{
printf("Please enter a positive number");
}
sum += user;
count = index;
index++;
} while (user != 0);
avg = sum / count;
printf("The minimum number ==> %f", min);
printf("The maximum number ==> %f", max);
printf("The number of enteries ==> %i", count);
printf("The sum of all values ==> %f", sum);
printf("The average of all values ==> %f", avg);
}
Upvotes: 0
Views: 83
Reputation: 1221
scanf("%f", user);
should be scanf("%f", &user);
You'd not want to increase these if input - user
is not correct:
sum += user;
count = index;
index++;
Moreover, your count
is not being computed correctly. To be specific, you're calculating 1 less than actual count. So put this block inside if
statement like this
if (user > 0)
{
if (user > max)
{
max = user;
}
if (user < min)
{
min = user;
}
sum += user;
count = index + 1;
index++;
}
else
{
printf("Please enter a positive number");
}
Be cautious when using %i
. %d
and %i
are same when used in printf
but not in scanf
.
printf("The number of enteries ==> %d", count); //Notice %d
Upvotes: 1
Reputation: 223689
Here's your error:
scanf("%f", user);
The %f
format specifier to scanf
expects the address of a float
, i.e. a float *
. All function parameters in C are pass by value, meaning that changing a parameter isn't reflected in the calling function. By passing a pointer, the function can dereference the pointer to write to the location it points to.
So change the function call to pass in the address of user
:
scanf("%f", &user);
Upvotes: 1
Reputation: 8356
This line is wrong:
scanf("%f", user);
You are not storing the input into user
. You can fix it like this:
scanf("%f", &user);
Upvotes: 1