So what i'm supposed to do is accept user input into an array that i allocated space for. Then i'm supposed to display the difference between each value the user inputted and the smallest value they inputted. I figured out how to allocate space for it and all that but i'm having trouble doing the calculation to figure out the difference between each number entered and the smallest number entered. If someone could give me an example of that it would be appreciated! Thanks in advance :)
To find the smallest number, try iterating through the array. Set the first element as the smallest integer found so far. Compare every element after that. if ( currentElement[x] < smallest ) { smallest = currentElement[x]; } To figure the difference between each number, something like this might work for you
1 2 3 4
if ( x != 0 )
{
cout << currentElement[x] - currentElement[x - 1] << endl;
}
i did what you said and its showing me the difference between each of the numbers entered not the difference between the smallest and each number entered
Misslyss i'm having a bit of trouble understanding exactly what you're trying to do.
Do you want to sort the array from smallest to largest, and print the difference between each number?
Or do you want to sort the array from smallest to largest and print the difference between the smallest number and each individual number?
Or, do you want to just find the smallest number in the array, and find the difference between the smallest number and each individual number in the array?
Now you have the lowest value entered.
The difference between each element and loVal is: diff = a[i] - loVal;
Just iterate through the array and display this difference.
You can either keep track of the smallest value as they are entered if (newValue < smallest) smallest = newValue; or you can traverse the array afterwards doing the same thing if (arr[i]<smallest) smallest = ar[i];. Remember, you MUST set smallest to 0 first.
its showing me the difference between each of the numbers entered not the difference between the smallest and each number entered
then surely cout << currentElement[x] - smallest << endl; should spring to mind :)
Don't set smallest = 0 initially!! smallest = 1st entered value.
If you assign smallest = 0 and all entered numbers are > 0, then smallest remains = 0 and the smallest value will not be found.