Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Finding the Maximum number

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ask the user to enter unlimited amount of numbers but will terminate when the sentinel (flag) value -1 is entered (use WHILE statement).
Your program will print one outputs: maximum number

Can someone help whenever I enter -1, nothing happens.

 
Master Rancher
Posts: 3692
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

olu idowu wrote:


The return value of scanf represents the number of arguments which have been successfully read, or EOF.  But you're not actually reaching any EOF here, you're entering "-1" which is presumably being successfully read into the variable "number", 1 value.  So scanf() is returning 1,and you're comparing it to -1, which never happens.  Try comparing to the value of number, instead.
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed my code to this but when I enter the numbers: 2.5, 33.1, 20, -1, It should show 20 but it is showing 2 and the line "int max=INT_MIN;" is giving me error.
 
Mike Simmons
Master Rancher
Posts: 3692
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that %d is used for a decimal integer.  If your input may be floating-point, like 2.5, you need to consult the scanf documentation to find a different format specifier.

olu idowu wrote:and the line "int max=INT_MIN;" is giving me error.



Well, what does the error say?
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:Well, what does the error say?



I can't see the reason, it's not show me the reason
 
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does INT_MIN come from? Do you need another #include?
 
Mike Simmons
Master Rancher
Posts: 3692
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good call - it looks like it's from limits.h.  

But I'm also curious how it "gives an error" but there's nothing to see.  Is there any sort of message at all?  What exactly does it look like when it "gives an error"?
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The maximum number can be calculated within the main function, whereas the average value must be calculated using a separate function callable by the main function and located within the same source file (only once source file needed). Note: the average function must have one argument only named num to pass numbers from the main function.

check your code using the following entries: 2.5, 33.1, 20, -1. If the code is correct you should get the results: maximum=20, average=18.533
I checked my code and it it still giving me a different answer: maximum = 2 and average = 2. My prof wants the average to be in a while loop and the line is a wrong statement and wants it to be changed to how a beginner should write. can someone help me please? I have been stuck on this assignment for so long.

this is my newly update code:

 
You may have just won ten million dollars! Or, maybe a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic