finding sum,min,max with array
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.
I write the code for this assignment but i got error
#include <stdio.h>
int main(void)
{
float a;
float i;
short c;
float sum;
float nu[];
i=nu[];
printf("Number of values :");
scanf("%f",&i);
for (c=1;i>=c;c++)
{
printf("values=");
scanf("%f",&nu[c]);
sum = sum + nu[c];
// printf("Sum = %f\n",sum);
}
printf("sum = %f \n",sum);
// printf("Number of values :");
// scanf("%f",&i);
}
开发者_开发知识库
error is number.c: In function ‘main’: number.c:9: error: array size missing in ‘nu’ number.c:11: error: expected expression before ‘]’ token
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:
- if it's the first or greater the the current high, set the current high to it.
- if it's the first or less the the current low, set the current low to it.
- add it to a sum.
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 :-)
- Get rid of the line "i=nu[]".
- Ensure you're initialising the array you pass in with the relevant number of elements
You must at first input i:
scanf("%f",&i);
and then declare array:
float nu[i];
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);
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]);
}
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);
}
精彩评论