This program gets 5 numbers only from the user then
Store them in an array. Get the min, max, and the average of the numbers inputted. Here's the code I made:
#include <stdio.h>
#include <conio.h>
int main()
{
int num[5];
int min, max=0;
int counter;
float average, total;
max = num[0];
min = num[2];
for(counter=0; counter<=4; counter++)
{
printf("Enter a number: ");
scanf("%d", &num[counter]);
if(num[counter]>max)
{
max = num[counter];
}
if (num[counter]<min)
{
min = num[counter];
}
}
total = max+min;
average = total/2;
printf("The maximum number is: %d\n", max);
printf("The minimum number is: %d\n", min);
printf("The average is: %d", average);
getch();
return 0;
}
FInally fixed my error with the min and max and now I'm having trouble with the average. I should only get the average of the min and max numbers but it keeps on showing an average of zero. Can someone help? Thankkyouuu.
//get memory address and store value in it.
void getValue(int *ptr)
{
printf("Enter a number: ");
scanf("%d", ptr);
}
int main()
{
//initialized n=5 as per your requirement. You can even get the value at run time.
int min, max, counter,n=5;
int num[n];
float average,total;
getValue(num); //get num[0]
min=max=total=num[0]; //Initialize min,max,total with num[0]
for(counter=1; counter<n; counter++)
{
getValue(num+counter); //get num[counter]
num[counter]>max?max = num[counter]:max; //find max
num[counter]<min?min = num[counter]:min; //find min
total+=num[counter]; // total = total + num[counter]
}
average = total/n;
printf("The maximum number is: %d\n", max);
printf("The minimum number is: %d\n", min);
printf("The total is: %f\n", total);
printf("The average is: %f\n", average);
return 0;
}
Your calculation of average is wrong; you need to use total/num (remember use float):
total += num[counter];
max and min were incorrectly initialized: num[0], num[2] may be anything when you initialize them.
Aside from your calculation of average being wrong (it's not just total/2), you need to use the correct format specifier in the printf:
printf("The average is: %g", average);
You are using %d which tells printf to expect an integer, but you're giving it a float.
1 Min and max should be initialized
int min = INT_MAX;
int max = INT_MIN;
2 You need to keep a running total of your numbers
int total = 0;
...
// inside loop
scanf("%d", &num[counter]);
total += num[counter];
3 At the end print the average, recommend going to floating point.
printf("The average is: %.1f", (double)total/counter);
Related
i need to assign these values to the array but i keep receiving assignment to expression error, I'm wondering how I can assign values to those arrays. i try assigning the inputted value of arr into num but i believe I am missing something in that expression.
this codes job is to read the input of a temperature assign the input into a category of the type of day and give the average of the temperature
Enter a high temp reading (-99 to quit)> 100
Enter a high temp reading (-99 to quit)> 0
Enter a high temp reading (-99 to quit)> 50
Enter a high temp reading (-99 to quit)> -99
Hot days: 1
Pleasant days: 0
Cold days: 2
The average temperature was 50.00 degrees.
this is what an output should look like
#include <stdio.h>
int main(void){
//declare variables
int num[30];
int Cday[30];
int Pday[30];
int Hday[30];
int total;
int total_ave;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
int arr;
//ask input store into num, then decides if the number goes into which array
do{
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &arr);
num = arr;
if(arr == -99)
{
break;
}
else if(arr<=60 && arr>=0){
Cday = num;
}
else if(arr>=60 && arr<=84){
Pday = num;
}
else if(arr<=84 && arr>=200){
Hday = num;
}
}while(num >=0);
//calculating the average
total = sizeof(num);
for(int i = 0;i< total; i++){
total_ave = total_ave + num[i];
}
ave = total_ave / total;
//to print the amount of times each day was in a category
NHday = sizeof(Hday)/sizeof(Hday[0]);
NPday = sizeof(Pday)/sizeof(Pday[0]);
NCday = sizeof(Cday)/sizeof(Cday[0]);
//printing the final statement once all the values are calculated
printf("\nHot days:\t %d\n", NHday);
printf("Pleasant days:\t %d\n", NPday);
printf("Cold days:\t %d\n\n", NCday);
printf("The average temperature was %.2f degrees.", ave);
//stops compiling when -99 is entered not collected as information
//
//
return(0);
}
i would also appreciate to know if how i am calculating how many items are in the array with the NHday CDay and Pday Calculations is the correct way to do it.
i am also wondering if my calculation for the average is correct. any help is appreciated thank you.
You don't need arrays for the different types of days, just counter variables, which you increment based on the temperature range.
total should not be sizeof(num) for two reasons:
sizeof counts bytes, not array elements.
You only want the count of inputs, not the total size of the array. You can get the total by adding the counters of each type of day.
You don't have to put the temperatures in an array. Just add each day's temperature to the total_ave variable after the user enters it.
When you're calculating ave, you need to cast one of the variables to double so that floating point division will be used instead of integer division.
#include <stdio.h>
int main(void){
//declare variables
int num;
int total;
int total_ave = 0;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
//ask input store into num, then decides if the number goes into which array
while (1) {
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &num);
if(num == -99)
{
break;
}
else if(num<=60){
NCday++;
}
else if(num<=84){
NPday++;
}
else {
NHday++;
}
total_ave += num;
}
//calculating the average
total = NHday + NPday + NCday;
ave = (double)total_ave / total;
//printing the final statement once all the values are calculated
printf("\nHot days:\t %d\n", NHday);
printf("Pleasant days:\t %d\n", NPday);
printf("Cold days:\t %d\n\n", NCday);
printf("The average temperature was %.2f degrees.", ave);
return(0);
}
You are assigning arr value to num array without using index. Your while loop should be like
while(i<30){
printf("Enter a high temp reading (-99 to quit)>");
scanf ("%d", &arr);
if(arr==-99){
break;
}
num[i] = arr;
i++;
total++;
if(arr<=60 && arr>=0){
NCday++;
}
else if(arr>=60 && arr<=84){
NPday++;
}
else if(arr>=84 && arr<=200){
NHday++;
}
}
Now
total = sizeof(num);
This will give you the total size of num array which is 30*4=120 as int data type has 4 byte size.
If you are not filling all 30 values then you need to know the number of values your are going to use from that array. That's why I am incrementing total as I am inserting values in num array which will help later.
for(int i = 0;i< total; i++){
total_ave = total_ave + num[i];
}
ave = total_ave / total;
If you just want to know the count of cold, hot days then you can increment them in while loop and print those.
printf("\nHot days:\t %d\n", NHday);
printf("Pleasant days:\t %d\n", NPday);
printf("Cold days:\t %d\n\n", NCday);
And first initialize the variables.
int num[30];
int total=0;
int total_ave=0;
double ave;
int NHday=0;
int NPday=0;
int NCday=0;
int arr=0;
int i=0;
In my C Program, I want to get the average of the sum of numbers being entered until the program stops. What should I add to check the average? Thank you!
#include <stdio.h>
int main (void)
{
int x;
int sum = 0;
int average;
int testEOF;
//Statements
printf("Enter your numbers: <EOF> to stop.\n");
do
{
testEOF = scanf("%d", &x);
if (testEOF !=EOF)
sum +=x;
} while (testEOF !=EOF);
printf ("\nTotal: %d\n", sum);
printf ("\nAverage: %d\n", average);
return 0;
//main
}
As other people explained, you have to initialize sum and count, they are never given their initial values. No need to initialize average and x because you assign sum/count to average and users will assign any value to x.
You have to put everything you want your if statement to do in {...}. But you didn't. Your if statement only does sum +=x and does not work for count++ and average = sum / count. So your program increases the value of count even after EOF. So you found 14/6 rather than 14/5.
You checked testEOF != EOF twice. One is in the if statement, other is in do-while.
I put the code below:
#include <stdio.h>
int main (void){
float x;
float sum = 0;
float count = 0;
float average;
float testEOF;
printf("Enter your numbers: <EOF> to stop.\n");
while(1){
testEOF = scanf("%f", &x);
if (testEOF ==EOF){
break;
}
sum +=x;
count++;
average = sum / count;
}
printf ("\nTotal: %f\n", sum);
printf ("\nAverage: %.2f\n", average);
return 0;
}
#include <stdio.h>
int main (void)
{
float x;
float sum;
float count;
float average;
float testEOF;
//Statements
printf("Enter your numbers: <EOF> to stop.\n");
do
{
testEOF = scanf("%f", &x);
if (testEOF !=EOF)
sum +=x;
count++;
average = sum / count;
} while (testEOF !=EOF);
printf ("\nTotal: %f\n", sum);
printf ("\nAverage: %.2f\n", average);
return 0;
}
This is now almost correct. The only thing is that the number is not getting divided by the number of values entered.
Example:
2
3
3
3
3
Sum is 14
Average: 2.3 (should be 2.8)
This seems that count is being added by 1 every time I get the average.
Well if you want your output to be printed with decimal point you can do the following , in that case
for inputs like 0, 1, 1 the out would be 0.666667 otherwise it will be simply 0 ignoring the decimal part.
the while part can be optimized as suggested by #David C. Rankin
double sum = 0;
double average = 0;
unsigned int count = 0;
//Statements
printf("Enter your numbers: <EOF> to stop.\n");
do
{
testEOF = scanf("%d", &x);
if (testEOF != EOF)
{
sum += x;
count++;
}
} while (testEOF != EOF);
printf ("\nTotal: %f\n", sum);
average = sum / count;
printf ("\nAverage: %f\n", average);
disclaimer: I'm new to programming
I'm working on this problem
so far ive written this which takes user inputs and calculates an average based on them
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, average;
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
}
I'd like the user to enter -1 to indicate that they are done entering data; I can't figure out how to do that. so if possible can someone explain or give me an idea as to how to do it
Thank you!
#include <stdio.h>
int main()
{
int i = 0;
float num[100], sum = 0.0, average;
float x = 0.0;
while(1) {
printf("%d. Enter number: ", i+1);
scanf("%f", &x);
if(x == -1)
break;
num[i] = x;
sum += num[i];
i++;
}
average = sum / i;
printf("\n Average = %.2f", average);
return 0;
}
There is no need for the array num[] if you don't want the data to be used later.
Hope this will help.!!
You just need the average. No need to store all the entered numbers for that.
You just need the number inputs before the -1 stored in a variable, say count which is incremented upon each iteration of the loop and a variable like sum to hold the sum of all numbers entered so far.
In your program, you have not initialised n before using it. n has only garbage whose value in indeterminate.
You don't even need the average variable for that. You can just print out sum/count while printing the average.
Do
int count=0;
float num, sum = 0;
while(scanf("%f", &num)==1 && num!=-1)
{
count++;
sum += num;
}
to stop reading at -1.
There is no need to declare an array to store entered numbers. All you need is to check whether next entered number is equal to -1 and if not then to add it to the sum.
Pay attention to that according to the assignment the user has to enter integer numbers. The average can be calculated as an integer number or as a float number.
The program can look the following way
#include <stdio.h>
int main( void )
{
unsigned int n = 0;
unsigned long long int sum = 0;
printf("Enter a sequence of positive numbers (-1 - exit): ");
for (unsigned int num; scanf("%u", &num) == 1 && num != -1; )
{
++n;
sum += num;
}
if (n)
{
printf("\nAverage = %llu\n", sum / n);
}
else
{
puts("You did not eneter a number. Try next time.");
}
return 0;
}
The program output might look like
Enter a sequence of positive numbers (-1 - exit): 1 2 3 4 5 6 7 8 9 10 -1
Average = 5
If you need to calculate the average as a float number then just declare the variable sum as having the type double and use the corresponding format specifier in the printf statement to output the average.
I'm trying to work on a program in C that gets 5 input numbers and then store these in an array. After getting the the 5 numbers, I must be getting the min, max and the average of the MINIMUN AND MAXIMUM numbers inputted and not all of the five. So here's the code that I made. When I get the maximum number, it seems to be working fine. But when it come's to the min, it's still same as the maximum and so I'll be getting a different average.
#include <stdio.h>
#include <conio.h>
int main()
{
int num[5];
int counter, min, max=0;
float average, total;
min=num;
for(counter=1; counter<=5; counter++)
{
printf("Enter a number: ");
scanf("%d", &num[5]);
if(num[5]>max)
{
max = num[5];
}
if (num[5]<min)
{
min = num[5];
}
}
total = min + max;
average = total/2;
printf("The maximum number is: %d\n", max);
printf("The minimum number is: %d\n", min);
printf("The average is: %d", average);
getch();
return 0;
}
Since this is a learning exercise, I wouldn't correct your code, but point out what needs to be fixed:
Arrays in C are indexed from zero, not from one, so the counter should go from 0 to 4, inclusive
min is an int, while num is an array, so the assignment min=num is invalid
scanf should put the data into &num[count], not &num[5]
In the way that you coded your loop you do not need an array at all: you need the last number entered.
total cannot be computed as min+max; you need to keep a running total, updating it on each iteration.
This program to calculate sum,min and max of the sum of array elements
Max value is the problem, it is always not true.
void main(void)
{
int degree[3][2];
int min_max[][];
int Max=min_max[0][0];
int Min=min_max[0][0];
int i,j;
int sum=0;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("\n enter degree of student no. %d in subject %d:",i+1,j+1);
scanf("%d",°ree[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("\n Student no. %d degree in subject no. %d is %d",i+1,j+1,degree[i][j]);
}
}
for(i=0;i<3;i++)
{
sum=0;
for(j=0;j<2;j++)
{
sum+=degree[i][j];
}
printf("\n sum of degrees of student no. %d is %d",i+1,sum);
min_max[i][j]=sum;
if(min_max[i][j] <Min)
{
Min=min_max[i][j];
}
else if(min_max[i][j]>Max)
{
Max=min_max[i][j];
}
}
printf("\nThe minimum sum of degrees of student no. %d is %d",i,Min);
printf("\nThe maximum sum of degrees of student no. %d is %d",i,Max);
getch();
}
The problem is that you are initialising Min and Max to min_max[0][0] before assigning any values to min_max, so their content is actually undefined.
Put the assignments Min=min_max[0][0] and Max=min_max[0][0] AFTER the scanf calls.
The lines
printf("\nThe minimum sum of degrees is %d",i,Min);
printf("\nThe maximum sum of degrees is %d",i,Max);
will print the only value of i, not Min or Max. Try this instead:
printf("\nThe minimum sum of degrees for %d is %d",i,Min);
printf("\nThe maximum sum of degrees for %d is %d",i,Max);
The line
min_max[i][j]=sum;
will always have a value of 2 for j because it is outside of the for loop. Also, I'm not clear why you would want to store the partial sum of degrees in the min_max array?