# How to find a maximum of entered numbers?

Page 1 of 1

## 10 Replies - 7147 Views - Last Post: 24 December 2010 - 07:12 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=206748&amp;s=1cca328e43f7b22181a5c20612da2447&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 progc

Reputation: 0
• Posts: 14
• Joined: 04-February 10

# How to find a maximum of entered numbers?

Posted 23 December 2010 - 11:35 AM

My goal is that the program asks floating-point numbers until the user enters 0 or negative number and then it has to find the maximum value of those and print the maximum to the screen. I've come up with this solution, but it is not entirely correct. Can someone help me with this problem? Thanks!

```#include <stdio.h>

int main(float n, float m, float max){

m = 1;
do{
printf("Enter a number:");
scanf("%f", &n);
if(n>m){
max = n;
}
m = n;
}
while(n>0);

printf("The largest entered number was %f\n", max);

getch();
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: How to find a maximum of entered numbers?

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 11:39 AM

```int main(float n, float m, float max)
```

That's not how main is defined. It's defined as
```int main(void)
```
or
```int main(int argc, char *argv[])
```

You only need the second if you're using command line arguments, which you are not.

Define your variables within the main function.

### #3 progc

Reputation: 0
• Posts: 14
• Joined: 04-February 10

## Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 11:42 AM

Thats not the problem of the code! I have find that there is no difference where they are declared, it may be wrong, but it works as long as they are the same type.

### #4 Scottm

Reputation: 42
• Posts: 238
• Joined: 11-February 10

## Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 12:01 PM

Here's a tutorial on how to find the max number of an Array::
http://www.dreaminco...rs-in-an-array/

I would suggest setting the numbers up in an array so it is easier to deal with.

But if you want to do it your way try this::
```if(n>m){
m = n;
}
```

Now what that did, was make it so if the value in n is greater than m( which is 1), it will make that value m. So say n is 2. 2 is greater than m, now m = 2. It goes back through say with the number 5. 5 is greater and 2, therefore making m 5.

Hope this helps!

This post has been edited by Scottm: 23 December 2010 - 12:16 PM

### #5 progc

Reputation: 0
• Posts: 14
• Joined: 04-February 10

## Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 12:16 PM

OK I'll try it.

This post has been edited by progc: 23 December 2010 - 12:17 PM

### #6 Scottm

Reputation: 42
• Posts: 238
• Joined: 11-February 10

## Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 12:18 PM

progc, on 23 December 2010 - 11:16 AM, said:

OK I'll try it.

Please read above:) I edited it. And mainly I posted that to show you how finding the max number works. Not so much of an array.
It generally is the same throughout

----------------
And also, m becomes your max value. So by cleaning up your code, you can get rid of the second m =n;, and replace max = n; with m = n; And print out m.

This post has been edited by Scottm: 23 December 2010 - 12:21 PM

### #7 progc

Reputation: 0
• Posts: 14
• Joined: 04-February 10

## Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 12:37 PM

Thanks! That worked just fine, and it was exactly what i was looking for. Strange i didn't saw it myself, but then again, im just a starter at c.

This post has been edited by progc: 23 December 2010 - 12:39 PM

### #8 Scottm

Reputation: 42
• Posts: 238
• Joined: 11-February 10

## Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 01:22 PM

progc, on 23 December 2010 - 11:37 AM, said:

Thanks! That worked just fine, and it was exactly what i was looking for. Strange i didn't saw it myself, but then again, im just a starter at c.

No prob man. Glad I can help

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 03:03 PM

progc, on 23 December 2010 - 12:42 PM, said:

Thats not the problem of the code! I have find that there is no difference where they are declared, it may be wrong, but it works as long as they are the same type.

With that sort of attitude towards coding -- "it may be wrong, but it works" -- you're in for a heap of trouble.

### #10 progc

Reputation: 0
• Posts: 14
• Joined: 04-February 10

## Re: How to find a maximum of entered numbers?

Posted 24 December 2010 - 12:54 AM

My question wasn't about the deklaration, it was about the codes loop, why couldn't you just answer the question. And by the way thanks for -1, the reputation was indeed to high. What a great way for the moderator to greet a rookie programmer, who is looking for help.

### #11 hpfreak080

Reputation: 5
• Posts: 14
• Joined: 16-November 09

## Re: How to find a maximum of entered numbers?

Posted 24 December 2010 - 07:12 AM

progc, on 23 December 2010 - 11:54 PM, said:

My question wasn't about the deklaration, it was about the codes loop, why couldn't you just answer the question. And by the way thanks for -1, the reputation was indeed to high. What a great way for the moderator to greet a rookie programmer, who is looking for help.

Yes, and you're treating him so much better.

Your specific question may not have been about the function declaration, but JackOfAllTrades was trying to tell you how to format your function in a way that is standard so that it will be more applicable in the real world. Declaring your function like that now might not hurt anything, but if you were to take a job programming, you will most certainly not be using a lot of nonstandard definitions.

It was a tip that was meant to help you in the long run and, in the end, you got the answer to your specific question too. Take the information and learn from it. That's what we all have to do sometimes.