944,053 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2717
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2004
0

subracting

Expand Post »
hi, just a quick question

i have created a program to input 6 numbers, but the problem is that i need it to subract the highest and lowest numbers then add the remaining numbers left.

im new to "c" and cant figure this out

i just need someone to point me in the right direction.

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rangers10 is offline Offline
1 posts
since Nov 2004
Nov 8th, 2004
0

Re: subracting

>i just need someone to point me in the right direction.
C++ Syntax (Toggle Plain Text)
  1. int main ( void )
  2. {
  3. return 0;
  4. }
>but the problem is that i need it to subract the highest and lowest numbers then add the remaining numbers left.
What have you tried so far? Have you even thought about it?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 8th, 2004
0

Re: subracting

Heres a quick algo I just threw together. There probably are more efficient ways but this gets the job done.

C++ Syntax (Toggle Plain Text)
  1. float min, max, temp, count;
  2. count = 0;
  3.  
  4. cin >> max; // Read in first #
  5. cin >>temp; // Read in second #
  6.  
  7. if (max > temp) // This if statement will basically initialize min and max
  8. {
  9. min = temp;
  10. }
  11. else
  12. {
  13. min = max;
  14. max = temp;
  15. }
  16.  
  17. for (int i=1; i <=4; i++) // Reads in next 4 #s
  18. {
  19. cin >> temp;
  20. if (temp > max)
  21. {
  22. count += max;
  23. max = temp;
  24. }
  25. else if(temp < min)
  26. {
  27. count += min;
  28. min = temp;
  29. }
  30. else
  31. {
  32. count += temp;
  33. }
  34. }
  35.  
  36. cout << "The max is " << max << endl;
  37. cout << "The min is " << min << endl;
  38. cout << "The difference is " << max-min << endl;
  39. cout << "The sum of the remainding four numbers is " << count << endl;
  40.  
  41. }
As I said that will get the job done based on what you gave. There are some possible issues. For example it never keeps the four numbers that are not max or min, just tallies them. If you want to keep track of the numbers you will want to implement something like an array. Also this program doesn't really do anything to handle duplicates. But hey, you never said that was an issue.
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Nov 8th, 2004
0

Re: subracting

Hello,

I would enter the numbers into an array, and then have the computer do a bubble sort on them. With the Bubble, the largest value will be on the top ( array[0] ) and the largest value would be on the bottom ( array [5] ) .

Code for a bubble sort is around the internet somewhere. Granted, it is not the most efficient code, but you are looking at only a handful of elements.

Then you can whack off the [0] and the [5] elements, and add the rest and get your answer.

Christian
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Nov 8th, 2004
0

Re: subracting

That was quick. Told ya there might be more efficient ways.
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
jasweb2002 is offline Offline
56 posts
since Sep 2004
Nov 8th, 2004
0

Re: subracting

>Told ya there might be more efficient ways.
How is that more efficient? Handling the numbers as they come is a linear operation, bubble sort is quadratic in all but the best case.

>then have the computer do a bubble sort on them.
You mean insertion sort. There's no excuse for using bubble sort. Insertion sort is faster, easier to understand, and the implementation is shorter and more clear.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 8th, 2004
0

Re: subracting

#include <iostream.h>

void main()
{
float min, max, temp, count;
count = 0;

cout << "enter a number" << endl;
cin >> max;


cin >>temp;

if (max > temp)
{
min = temp;
}
else
{
min = max;
max = temp;
}

for (int i=1; i <=4; i++)
{
cin >> temp;
if (temp > max)
{
count += max;
max = temp;
}
else if(temp < min)
{
count += min;
min = temp;
}
else
{
count += temp;
}
}

cout << "The max is " << max << endl;
cout << "The min is " << min << endl;
cout << "The difference is " << max-min << endl;
cout << "The sum of the remainding four numbers is " << count << endl;

}
Reputation Points: 11
Solved Threads: 1
Newbie Poster
yb1pls is offline Offline
24 posts
since Dec 2004
Dec 8th, 2004
0

Re: subracting

Quote originally posted by Narue ...
>i just need someone to point me in the right direction.
C++ Syntax (Toggle Plain Text)
  1. int main ( void )
  2. {
  3. return 0;
  4. }
>but the problem is that i need it to subract the highest and lowest numbers then add the remaining numbers left.
What have you tried so far? Have you even thought about it?
Welcome back Narue, I hope you had a good vacation ...
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 8th, 2004
0

Re: subracting

Quote originally posted by Narue ...
>Told ya there might be more efficient ways.
How is that more efficient? Handling the numbers as they come is a linear operation, bubble sort is quadratic in all but the best case.

>then have the computer do a bubble sort on them.
You mean insertion sort. There's no excuse for using bubble sort. Insertion sort is faster, easier to understand, and the implementation is shorter and more clear.
Just a snippet for an insertion sort:
[php]
// returns the sorted array of numbers (this case int)
void insertionSort(int numbers[], int array_size)
{
int i, j, index;

for (i=1; i < array_size; i++) {
index = numbers[i];
j = i;
while ((j > 0) && (numbers[j-1] > index)) {
numbers[j] = numbers[j-1];
j = j - 1;
}
numbers[j] = index;
}
}
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 8th, 2004
0

Re: subracting

You need no sorting at all in this scenario. Just keep an array of 4 ints and 2 ints.
For each int read check if it's larger than the one in which indicates the smallest and smaller than the one that indicates the largest.
If so, insert it into the array.
If not, replace the largest (or smallest) with the new value and put that old largest (or smallest) into the array.
When all are read add up the elements of the array and you're done.

There's no requirement to show the numbers from smallest to largest after all so why go to the trouble of getting them in that order in the first place?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with a function
Next Thread in C++ Forum Timeline: Error Message Concerning Reading File From A Drive





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC