Can someone give me an example of this?

Feb 14, 2015 at 6:46pm
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 :)
Feb 14, 2015 at 7:25pm
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;
}
Last edited on Feb 14, 2015 at 7:25pm
Feb 15, 2015 at 1:32am
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
Last edited on Feb 15, 2015 at 1:45am
Feb 15, 2015 at 1:48am
closed account (2UD8vCM9)
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?
Feb 20, 2015 at 3:49pm
pindrought i just want to find the smallest number and find the difference between each individual number in the array and the smallest number
Feb 20, 2015 at 4:28pm
closed account (D80DSL3A)
Are you having trouble finding the smallest value? Or just the difference part?

Finding smallest value from the already filled array.
Assuming the array is named a[] and has sz elements of type int, then:
1
2
3
int loVal = a[0];
for(int i=1; i<sz; ++i)
    if( a[i] < loVal ) loVal = a[i];

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.
Feb 20, 2015 at 4:38pm
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 :)
Feb 21, 2015 at 4:10pm
closed account (D80DSL3A)
Remember, you MUST set smallest to 0 first.

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.
Last edited on Feb 21, 2015 at 4:11pm
Feb 23, 2015 at 9:10am
oops!

As fun2code says setting smallest=0 zero is wrong, my bad. Setting it to INT_MAX or the first item in the list is far more appropriate.

Topic archived. No new replies allowed.