tags:

views:

190

answers:

6

i wrote the code but i dont get max value how to find max value

#include <stdio.h>
int main(void)
{
        double temp[0];
        float max,min;
        float i;
        short c,j,k;
        float sum=0;
        float nu[c];

        printf("Number of values :");
        scanf("%f",&i);

        for (c=1;i>=c;c++)
        {
          printf("values=");
          scanf("%f",&nu[c]);

          nu[c]=temp[0];

          for (j = 0; i>=j; j++)
          {
             if (temp[0]> nu[c])
             {
             //      nu[c]=temp[0];
                  max = temp[0];

             }
        }
        sum = sum + nu[c];
      }
      printf("sum = %f \n",sum);
      printf("maximum value = %f\n",max);
}
A: 

remove the temp (cant see the point of it)

set max to some initial value (e.g. first element of the array)

and change if statement to:

  if (nu[c]> max)
             {
                  max = nu[c];

             }
JohnSmith
A: 

This may not solve your question but nu[c] seems bad allocated to me since c value is undefined

rano
+1  A: 

You need a single for loop and not nested for loop.

  1. Get input using the first for loop
  2. Assign first element of the input array to variable, call this max
  3. Compare each element of the array using a for loop again
  4. If you get a value greater than max then change max to this value using the below code

    if(max< a[i]) max=a[i];

At the end of these steps you can get max value.

Try to put these steps into a C code and it should be fine. There are some problems with the code you have written. I have written a small snippet for you to get input for number of elements and store them into your array for floats.

int number_of_elements;
printf("Enter the number of elements:\n");
scanf("%d", &number_of_elements);
for(i=0;i<number_of_elements;i++)
scanf("%f",&a[i]);
Praveen S
+1  A: 

Here's the code with some helpful pointers:

#include <stdio.h>
int main(void)
{
        double temp[0];
        float max,min;
        float i;
        short c,j,k;                     // k isn't used anywhere.
        float sum=0;
        float nu[c];                     // c isn't set at this point so the
                                         //   runtime couldn't create an
                                         //   array anyway.

        printf("Number of values :");
        scanf("%f",&i);

        for (c=1;i>=c;c++)
        {
          printf("values=");
          scanf("%f",&nu[c]);
                                         // You should set min/max to nu[c]
                                         //   if c is 1 regardless.
          nu[c]=temp[0];                 // Why are you scanning into nu[c]
                                         //   then immediately overwriting?

          for (j = 0; i>=j; j++)         // Why figure out the max *every*
          {                              //   time you enter a value?
             if (temp[0]> nu[c])
             {
             //      nu[c]=temp[0];
                  max = temp[0];

             }
        }
        sum = sum + nu[c];
      }
      printf("sum = %f \n",sum);
      printf("maximum value = %f\n",max);
}

I'd also suggest you go back to my original answer and create your code from that. It really is a much cleaner way of doing it than trying to fiddle around with arrays.

Let me emphasise that: if you're using an array, you're doing it the hard way!

paxdiablo
+1  A: 

If you run your compiler with a -Wall (as you should always do), you should have seen the problems :

gcc -Wall -o foo foo.c
foo.c: In function 'main':
foo.c:35:14: warning: unused variable 'k'
foo.c:33:14: warning: unused variable 'min'
foo.c:62:1: warning: control reaches end of non-void function
foo.c:37:4: warning: 'c' is used uninitialized in this function

Here is the source code of a program that works :

#include <stdio.h>

int main(void)
{
   float max = 0; // hypothesis : numbers keyed by the users are > 0
   float min = 0; // hypothesis : numbers keyed by the users are > 0
   int i, c;
   float sum = 0;
   float nu[100]; // hypothesis : no more than 100 numbers keyed by user
                  // hypothesis : float type is enough to store numbers keyed by user
   printf("Number of values :");
   scanf("%d",&i);

   for (c = 0; c < i; c++)
   {
         printf("values=");
         scanf("%f",&(nu[c]));

         if (nu[c] > max) {
            max = nu[c];
         }

         if (min == 0) {
            min = nu[c];
         } else if(nu[c] < min) {
            min = nu[c];
         }

         sum = sum + nu[c];
   }
   printf("sum = %f \n",sum);
   printf("maximum value = %f \n", max);
   printf("minimum value = %f \n", min);

   return 0;
}
Jérôme Radix
thanks i got the answer for maximum value but i dont get answer for minimum value
amberkar.sneha
I updated the source code to compute min value and correct the iteration (c should start at 0 as arrays in C always starts at 0)
Jérôme Radix
thanks i got the answer
amberkar.sneha
+1  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