Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

How do I write a code in C++ to eliminate the 2 lowest numbers? For example, the numbers are: 42, 5.6 ,6.2, 3.6, 4.5, 5.5 ,4.56, 3.75, 4.65.

OpenStudy (e.mccormick):

Well, how are they stored? Individual number or in some sort of array? Sorted or not? Does it need to be able to handle random data types or not?

OpenStudy (anonymous):

The numbers are stored from an input file.

OpenStudy (anonymous):

So, array I think.

OpenStudy (anonymous):

And yes, it needs to be able to handle random data types.

OpenStudy (anonymous):

If it's being stored in an array it's a simple walk throughout the entire array while keeping track of the 2 lowest numbers, as well as their index. We can do this by first creating 5 variables: firstLowest secondLowest firstLowestIndex secondLowestIndex index = 1 //because we'll set our first value of the array to firstLowest firstLowest = arrayOfNum[0] firstLowestIndex = 0 while(arrayOfNum[index] != NULL)//let's walk through the array and keep track of the 2 lowest numbers { if(firstLowest > arrayOfNum[index]) { //if firstLowest is lower than the number on the index it's now considered secondLowest and arrayOfNum[index] is the firstLowest secondLowest=firstLowest secondLowestIndex=firstLowestIndex firstLowest=arrayOfNum[index] firstLowestIndex=index } }

OpenStudy (e.mccormick):

You coupld do basically what 114Bytes said, but while loading the file. Just do not pass the lowest number into it. However, the need for diferent types means you will be stuck dealing with it as some sort of floating point number or as strings.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!