tags:

views:

140

answers:

5

user have to input 10 numbers and the program should read the largest number.the largest should be done in function..i can do the looping make user input 10 numbers but i don't know how to do the function to determine the largest..please help me!!! here is my code..

#include <stdio.h>

int main ()

{
int largest;
double total=0;
int x;
int i;

printf("Please key in 10 numbers\n");

    for(i=0; i<10; i++)
    {
    printf("Key in your number:");
    fflush(stdin);
    scanf("%f", &x);
    }



return 0;

}
+2  A: 

think of an algorithm first, write it down in steps in your moder tongue then try a few examples and see if the algorithm works. then think about how it should look in c-code

Anders K.
@Anders: Dude, sorry but it's "Mother tongue" (unless you were joking of course)
Binary Worrier
A: 

You could have 10 variables to store the numbers, but this would be very clumsy.

It's a pity there isn't some way you can declare one variable that can hold multiple values, possibly by specifying an index and storing a value at that index.

You'd think with the large array of variable types in c, that there would be some way to do this . . .

Binary Worrier
I can think of a way of doing this without needing an array of variables.
JeremyP
@JeremyP: The way I read the question, you collect the 10 values, then find the largest.
Binary Worrier
@Binary Worrier: The way I read it the spec is that the program reads in 10 numbers from the user and then spits out the largest one. There doesn't seem to be any need to store the other nine.
JeremyP
A: 

I guess your problem is how to design the function signature. That's easy: use pointers.

void correctMax( int* currentMax, int newValue )
{
    if( newValue > *currentMax ) {
       *currentMax = newValue;
    }
}

then you use like this:

int maxValue;
maxValue = INT_MIN;
for( /*your loop for entering values*/ ) {
   int newValue;
   newValue = obtainValue();
   correctMax( &maxValue, newValue );
}
sharptooth
Better teach a man to fish :)
Igor Oks
@Igor Oks: IMO the code above is enough "preudocode" for a newbie. We don't want people break to death while learning to fish, do we?
sharptooth
What's wrong with returning a value instead of passing pointers?
JeremyP
@JeremyP: Nothing, except IMO it looks wrong - if the value is to be retained it should not be reassigned to itself.
sharptooth
bro,i havent learn pointers yet..so i dont know how to do pointers in c
pervin
-1 For even suggesting points for this problem.
Brian
+1  A: 
  1. If you know how to input numbers then you can use an array to store 10 numbers.
  2. Use scanf and for loop to get the numbers
  3. Use another for loop to compare from 0 to 9 index of the array and get the max number. You can use the comparison operator "<" and if condition.

This can later be optimized, you can just have 2 variables, input and max and a single for loop.

After you are done with steps 1-3 please paste the code if you are unable to get the right output. Good Luck!!

Praveen S
A: 

If you don't want to use pointers, you could do something like the following:

#include <stdio.h>

int get_largest(int largest_so_far, int candidate)
{
    if (largest_so_far < candidate)
      return candidate;

    return largest_so_far; //Thank you Brian
}

int main ()   
{
    int largest = 0;
    double total=0;
    int x;
    int i;

    printf("Please key in 10 numbers\n");

    for(i=0; i<10; i++)
    {
      printf("Key in your number:");
      fflush(stdin);
      scanf("%f", &x);

      largest = get_largest(largest, x);
      printf("Largest number so far:%d\n", largest);
    }

    printf("Largest number:%d\n", largest);    

    return 0;
}

Its not beautiful, I don't know if it will compile and certainly its not how I would do it, but its easier to understand!

Since you are already familiarized with the code, I didn't put any comments.

karlphillip
`get_largest` won't always return.
Brian