This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
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

average number and 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 two outputs: maximum number and average number
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. when I test the code, i enter the numbers and it print "enter the numbers:" non-stop. can someone help me?


 
Rancher
Posts: 234
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

olu idowu wrote:Note: the average function must have one argument only named num to pass numbers from the main function.

I think this means you are expected to use global variables (or static variables within the average function) to hold the total and counter values. Then they don't need to be passed in as arguments.

check your code using the following entries: 2.5, 33.1, 20, -1.

As has been pointed out in your other thread, a couple of those values are floating point numbers. So you must use the %f format string in your scanf(), and number must be a float (or %lf and double). Similarly your total value should be float/double, as should the max/min values in main().

You only want a while loop in main() to check for the -1 input value; you shouldn't have a while in average(). Your average function should just update the (global/static) total and counter values and calculate/return the new average value. At the moment you have an endless loop in average() because the condition (counter != 0) is not affected by any code within the loop body.
 
Look ma! I'm selling my stuff!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic