0

This Question has been **Answered**

0

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

```
int main ( void )
{
return 0;
}
```

>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?

0

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

```
float min, max, temp, count;
count = 0;
cin >> max; // Read in first #
cin >>temp; // Read in second #
if (max > temp) // This if statement will basically initialize min and max
{
min = temp;
}
else
{
min = max;
max = temp;
}
for (int i=1; i <=4; i++) // Reads in next 4 #s
{
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;
}
```

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.

0

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

0

>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.

0

```
#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;
}
```

*Edited
by Dani*: Formatting fixed

0

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

`int main ( void ) { return 0; }`

>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 ...

0

>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:

```
// 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;
}
}
```

0

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?

0

Welcome back Narue, I hope you had a good vacation ...

...I don't think she's back. yb1pls keeps resurrecting old threads :(

This question has already been answered. Start a new discussion instead.

Recommended Topics

```
import tkinter
root = tkinter.Tk()
root.geometry("200x200")
buttons=[i for i in range(10)]
def getNext():
#If specific button is pressed, output "YES"
for num in buttons:
btn=tkinter.Button(root, text=num,command=getNext)
btn.pack(side=tkinter.TOP)
```

I have written a formal Deadlock prevention code but I'm having trouble implementing this algorithm. Any help is appreciated.

I have represented the algorithm below,

**Formal algorithm description**

Data structures ...

```
from tkinter import*
import tkinter
import sqlite3
import sys
conn = sqlite3.connect('REVISION CARD.db')
c=conn.cursor()
def load():
root=Tk()
root.title("REVION CARDS")
root.geometry("700x400+100+100")
w1=Label(root, text="Question")
w1.pack()
e=Entry(root,width=40)
e.pack()
w2=Label(root,text="Answer")
w2.pack()
e1=Entry(root,width=40)
e1.pack()
toolbar=Frame(root) ...
```