subracting

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 1
Reputation: rangers10 is an unknown quantity at this point 
Solved Threads: 0
rangers10 rangers10 is offline Offline
Newbie Poster

subracting

 
0
  #1
Nov 8th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: subracting

 
0
  #2
Nov 8th, 2004
>i just need someone to point me in the right direction.
  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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Re: subracting

 
0
  #3
Nov 8th, 2004
Heres a quick algo I just threw together. There probably are more efficient ways but this gets the job done.

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: subracting

 
0
  #4
Nov 8th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 56
Reputation: jasweb2002 is an unknown quantity at this point 
Solved Threads: 2
jasweb2002 jasweb2002 is offline Offline
Junior Poster in Training

Re: subracting

 
0
  #5
Nov 8th, 2004
That was quick. Told ya there might be more efficient ways.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 744
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: subracting

 
0
  #6
Nov 8th, 2004
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 24
Reputation: yb1pls is an unknown quantity at this point 
Solved Threads: 1
yb1pls yb1pls is offline Offline
Newbie Poster

Re: subracting

 
0
  #7
Dec 8th, 2004
#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;

}
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,069
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: subracting

 
0
  #8
Dec 8th, 2004
Originally Posted by Narue
>i just need someone to point me in the right direction.
  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 ...
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,069
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: subracting

 
0
  #9
Dec 8th, 2004
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: subracting

 
0
  #10
Dec 8th, 2004
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC