10 Replies - 4922 Views - Last Post: 24 December 2010 - 07:12 AM Rate Topic: -----

#1 progc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 progc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? -1
  • +
  • -

#4 Scottm  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 42
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 progc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Scottm  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 12:18 PM

View Postprogc, 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

Was This Post Helpful? 1
  • +
  • -

#7 progc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 Scottm  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 01:22 PM

View Postprogc, 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
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: How to find a maximum of entered numbers?

Posted 23 December 2010 - 03:03 PM

View Postprogc, 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.
Was This Post Helpful? 0
  • +
  • -

#10 progc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 hpfreak080  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 13
  • Joined: 16-November 09

Re: How to find a maximum of entered numbers?

Posted 24 December 2010 - 07:12 AM

View Postprogc, 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.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1