Tuesday, July 14, 2009

How do you find the least and greatest amount in C++?

I'm supposed to write a program where





1. The user can enter as many numbers as they can and to stop they would enter -99.





2. After they have entered -99 I'm supposed to display the highest number that they entered and the lowest number that they entered (which cannot be -99).








If you help me I will REALLY appriciate it thanks a lot!

How do you find the least and greatest amount in C++?
Just have a variable called max and min


for the first number then enter just set that to max and min


then for each additional number


if num %26gt; max then max=num


if num %26lt; min and num %26lt;%26gt; -99 then min=num
Reply:There are a lot of ways to do it. Here is one:





Every time the user type one number you store it in an array (Array). When he has finished the typing, you know the lenght of the array (ArrayLenght). Take it from the begining, make 2 variables (let's say MaxVal and MinVal) and assing each one with the first number in the array.





Go to the next value in the array (for i=2 to ArrayLenght)


If Array[i]%26gt;MaxVal then MaxVal=Array[i]


else If Array[i]%26lt;MinVal then MinVal=Array[i]





And so one until you reach the end the array.





That will be all.
Reply:I have seen 2-ans given to you, but i dont think that are right one.





Your problem can only be solved by using linklist-----1)you need a dynamic array which can grow dynamically


2)node creation process shud be recursive and end only when -99 enters.





For finding out greatest and least you can use any logic which involves less comparisons so that it wud be optimised.

song lyrics

No comments:

Post a Comment