1

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

1
  • You should have a min val counter and a max val counter as well, init them both to the first passed value then update them inside the loop as appropriate. This saves you having to loop values again. Commented Aug 23, 2010 at 12:49

6 Answers 6

10

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

You must at first input i:

scanf("%f",&i);

and then declare array:

float nu[i];

5 Comments

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.
will only work in C99 and not in C89 where array lengths must be constant.
Oh, C99 does allow this? In that case, disregard my former comment. :)
Still not a good idea to do that as not all compilers would support it. I would do float* nu = malloc(sizeof(float) * i);
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 :-)
0

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

Comments

0

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

}

Comments

0

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

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.