Write a program that inputs number of values then inputs these values (double type) one by one in a loop and finally outputs their sum, the maximum value and the minimum value.
Or this
#include <stdio.h>
int main(void)
{
float temp;
int val,i,j,k;
double sum = 0;
double number[val];
printf("Enter the number of values: ");
scanf("%d", &val);
double number[val];
for(i=1; i <= val ;i++)
{
printf("enter a value: ");
scanf("%lf", &number[i]);
sum = sum + number[i];
}
for(i=1;i<=val;i++)
{
for(j=i+1;j<=val;j++)
{
if(number[i] > number[j])
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
}
printf("Sum = %.lf\n", sum);
printf ("Maximum element: %f\n",number[val]);
printf ("Minimum element: %lf\n", number[1]);
}
If you dont know the array dimension before, use dynamic allocation.
replace
float nu[];
i=nu[];
printf("Number of values :");
scanf("%f",&i);
with:
float *nu=0;
printf("Number of values :");
scanf("%f",&i);
nu=malloc(i*sizeof*nu); if(!nu) fprintf(stderr,"not enough memory"),exit(1);
...
free(nu);
Try this...
#include <stdio.h>
int main(void)
{
float temp;
int val,i,j,k;
double sum = 0;
double max,min;
printf("Enter the number of values: ");
scanf("%d", &val);
double number[val];
for(i=0; i < val ;i++)
{
printf("enter a value: ");
scanf("%lf", &number[i]);
sum = sum + number[i];
}
min = number[0];
max = number[0];
for(j=0;j<val;j++)
{
if(number[j] < min)
min = number[j];
}
for(k=0;k<val;k++)
{
if(number[k] > max)
max = number[k];
}
printf("Sum = %.lf\n", sum);
printf ("Maximum element: %.f\n",max);
printf ("Minimum element: %.lf\n",min);
}
In C, you need to specify the sizes of your arrays such as with float nu[100];
but you're barking up the wrong tree if you think you need to store all those values. The minimum, maximum and sum can all be calculated on the fly without any need to go back and retrieve any previous numbers.
All you need to do is enter them one at a time and, for each:
The sum by the way, should be initialised to zero to start with.
In terms of pseudo-code, start with this:
set sum, high and low all to zero
scan "number of values" into count
for curr = one to count, inclusive:
scan "current number" into num
set sum to sum + num
if curr is one, or num is less than low:
set low to num
if curr is one, or num is greater than high:
set high to num
output "Minimum = " low
output "Maximum = " high
output "Sum = " sum
And the best way to understand it is to get a (very non-tech) piece of paper and write up a table like this:
+-----+------+-----+-----+-------+------+
| sum | high | low | num | count | curr |
+-----+------+-----+-----+-------+------+
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
+-----+------+-----+-----+-------+------+
Then run that program step-by-step through your head, entering or changing the values in that table as you go. You'll even be able to detect when you're using uninitialised values such as if you came across set sum to sum + curr
if the sum
column was empty.
You'll be surprised how quickly you begin to think like a computer, just hope it doesn't push all those social skills out of your head in the process :-)
You must at first input i:
scanf("%f",&i);
and then declare array:
float nu[i];