tags:

views:

155

answers:

6

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

A: 
  • Get rid of the line "i=nu[]".
  • Ensure you're initialising the array you pass in with the relevant number of elements
Bobby Jack
A: 

You must at first input i:

scanf("%f",&i);

and then declare array:

float nu[i];
shk
Actually this will not work in C, as array size needs to be known at compile-time. You'd need to dynamically allocate memory, but this would be clearly outside the scope of this assignment.
ig2r
will only work in C99 and not in C89 where array lengths must be constant.
doron
Oh, C99 does allow this? In that case, disregard my former comment. :)
ig2r
Still not a good idea to do that as not all compilers would support it. I would do `float* nu = malloc(sizeof(float) * i);`
mathepic
I'm thinking maybe it's time for all those compilers not currently supporting c99, to think about upgrading. c1x is just around the corner and c89 is twenty years old. Leave it for legacy stuff but, in the name of all that's holy, please give us decent up-to-date tools for everything else :-)
paxdiablo
+9  A: 

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 to 0
set high to 0
set low to zero
scan number of values into count
for curr = 1 to count:
    scan number into num
    if curr is 1:
        set low to num
        set high to num
    if curr is less than low:
        set low to curr
    if curr is greater than high:
        set high to curr
    set sum to sum + curr
output "Minimum = " low
output "Maximum = " high
output "Sum     = " sum

And the best way to understand it is to get a 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 :-)

paxdiablo
+1 For recommending the old fashioned pencil and paper!
Thomas Matthews
A: 

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);
A: 

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);  

    }
Pavitar
A: 

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]);  

}
Pavitar