#1

for school i have to write a program in c++ that uses a vector to:

1. takes 8 numbers from the user

2. drops the highest and lowest numbers

3. average the remaining numbers

im completely retarded when it comes to vectors so can someone help me out here?

1. takes 8 numbers from the user

2. drops the highest and lowest numbers

3. average the remaining numbers

im completely retarded when it comes to vectors so can someone help me out here?

#2

It's been a while since I've messed with C++. Step 1 is easy, just have the user input 8 choices. I would do Step 2 manually, the long way. Have the program compare each number to each other, which would be 8! (or 8 factoral), so that might take a while, but it'd work. I don't know how you would incorporate a vector into this.

#3

Vectors is another term for values in an array. What you are going to do here in create and area with 8 variables and have a MIN and MAX value. Make a simple search algorithm to parse through the values in the array and find the lowest int and the highest int and store them into MIN and MAX. Then add up and divide the remaining ints by 6. Make sure that you exclude MIN and MAX from your averaging.

#4

and i do this how?

#5

deffine an array like so:

int arr[8],m,n1,n2,i;

*insert code for user to input numbers*

//find max

m=0

for (i=0; i<8; i++)

{

if (arr>m)

{

m=arr;

n1=i;

}

}

//find min

m=a[0]

n2=0;

for (i=1; i<8; i++)

{

if (arr<m)

{

m=arr;

n2=i;

}

}

a[n1]=0;

a[n2]=0;

sum=0;

for(i=0;i<8;i++)

{

sum+=a;

}

sum=sum/6;

EDIT: if you have a vector of all the same values, it won't work.I think...

int arr[8],m,n1,n2,i;

*insert code for user to input numbers*

//find max

m=0

for (i=0; i<8; i++)

{

if (arr>m)

{

m=arr;

n1=i;

}

}

//find min

m=a[0]

n2=0;

for (i=1; i<8; i++)

{

if (arr<m)

{

m=arr;

n2=i;

}

}

a[n1]=0;

a[n2]=0;

sum=0;

for(i=0;i<8;i++)

{

sum+=a;

}

sum=sum/6;

EDIT: if you have a vector of all the same values, it won't work.I think...

#6

It can be done without ever having to step through the array any time other than the initial read. Like so:

const int NUM_OF_VALUES = 8;

int main()

{

int values[NUM_OF_VALUES];

int minValue, maxValue;

int sum = 0;

for (int i = 0; i < NUM_OF_VALUES; i++ )

{

std::cout << "Enter value #" << i+1 << ": " << std::endl;

std::cin >> values[i];

maxValue = values[i] < minValue )

{

minValue = values[i] > maxValue I)

{

maxValue = values[i];

}

sum = sum - maxValue - minValue;

float average = (float)sum / (float)(NUM_OF_VALUES - 2.0);

std::cout << std::endl << "The average is " << average << std::endl;

return 0;

}#include <iostream>

const int NUM_OF_VALUES = 8;

int main()

{

int values[NUM_OF_VALUES];

int minValue, maxValue;

int sum = 0;

int minCount = 1;

int maxCount = 1;

for (int i = 0; i < NUM_OF_VALUES; i++ )

{

std::cout << "Enter value #" << i+1 << ": " << std::endl;

std::cin >> values[i];

maxValue = values[i] < minValue )

{

minValue = values[i] == minValue )

{

minCount++;

}

if ( values[i] > maxValue )

{

maxValue = values[i];

maxCount = 1;

}

else if ( values[i] == maxValue )

{

maxCount++;

}

}

sum += values[i];

}

sum = sum - (maxValue * maxCount) - (minValue * minCount);

float average = (float)sum / (float)(NUM_OF_VALUES - maxCount - minCount);

std::cout << std::endl << "The min value is " << minValue;

std::cout << std::endl << "The max value is " << maxValue;

std::cout << std::endl << "The sum is " << sum;

std::cout.setf(std::ios::fixed);

std::cout << std::endl << "The average is " << average << std::endl;

return 0;

}

const int NUM_OF_VALUES = 8;

int main()

{

int values[NUM_OF_VALUES];

int minValue, maxValue;

int sum = 0;

for (int i = 0; i < NUM_OF_VALUES; i++ )

{

std::cout << "Enter value #" << i+1 << ": " << std::endl;

std::cin >> values[i];

maxValue = values[i] < minValue )

{

minValue = values[i] > maxValue I)

{

maxValue = values[i];

}

sum = sum - maxValue - minValue;

float average = (float)sum / (float)(NUM_OF_VALUES - 2.0);

std::cout << std::endl << "The average is " << average << std::endl;

return 0;

}#include <iostream>

const int NUM_OF_VALUES = 8;

int main()

{

int values[NUM_OF_VALUES];

int minValue, maxValue;

int sum = 0;

int minCount = 1;

int maxCount = 1;

for (int i = 0; i < NUM_OF_VALUES; i++ )

{

std::cout << "Enter value #" << i+1 << ": " << std::endl;

std::cin >> values[i];

maxValue = values[i] < minValue )

{

minValue = values[i] == minValue )

{

minCount++;

}

if ( values[i] > maxValue )

{

maxValue = values[i];

maxCount = 1;

}

else if ( values[i] == maxValue )

{

maxCount++;

}

}

sum += values[i];

}

sum = sum - (maxValue * maxCount) - (minValue * minCount);

float average = (float)sum / (float)(NUM_OF_VALUES - maxCount - minCount);

std::cout << std::endl << "The min value is " << minValue;

std::cout << std::endl << "The max value is " << maxValue;

std::cout << std::endl << "The sum is " << sum;

std::cout.setf(std::ios::fixed);

std::cout << std::endl << "The average is " << average << std::endl;

return 0;

}

*Last edited by bbunt302 at Jan 7, 2007,*

#7

Looking at this thread makes me realize I forgot everything I knew about C++. Ah well...

#8

Looking at this thread makes me realize I forgot everything I knew about C++. Ah well...

Me too Oh well, I had some fun messing with it.

#9

It can be done without ever having to step through the array any time other than the initial read. Like so:

const int NUM_OF_VALUES = 8;

int main()

{

int values[NUM_OF_VALUES];

int minValue, maxValue;

int sum = 0;

for (int i = 0; i < NUM_OF_VALUES; i++ )

{

std::cout << "Enter value #" << i+1 << ": " << std::endl;

std::cin >> values[i];

maxValue = values[i] < minValue )

{

minValue = values[i] > maxValue I)

{

maxValue = values[i];

}

sum = sum - maxValue - minValue;

float average = (float)sum / (float)(NUM_OF_VALUES - 2.0);

std::cout << std::endl << "The average is " << average << std::endl;

return 0;

}#include <iostream>

const int NUM_OF_VALUES = 8;

int main()

{

int values[NUM_OF_VALUES];

int minValue, maxValue;

int sum = 0;

int minCount = 1;

int maxCount = 1;

for (int i = 0; i < NUM_OF_VALUES; i++ )

{

std::cout << "Enter value #" << i+1 << ": " << std::endl;

std::cin >> values[i];

maxValue = values[i] < minValue )

{

minValue = values[i] == minValue )

{

minCount++;

}

if ( values[i] > maxValue )

{

maxValue = values[i];

maxCount = 1;

}

else if ( values[i] == maxValue )

{

maxCount++;

}

}

sum += values[i];

}

sum = sum - (maxValue * maxCount) - (minValue * minCount);

float average = (float)sum / (float)(NUM_OF_VALUES - maxCount - minCount);

std::cout << std::endl << "The min value is " << minValue;

std::cout << std::endl << "The max value is " << maxValue;

std::cout << std::endl << "The sum is " << sum;

std::cout.setf(std::ios::fixed);

std::cout << std::endl << "The average is " << average << std::endl;

return 0;

}

const int NUM_OF_VALUES = 8;

int main()

{

int values[NUM_OF_VALUES];

int minValue, maxValue;

int sum = 0;

for (int i = 0; i < NUM_OF_VALUES; i++ )

{

std::cout << "Enter value #" << i+1 << ": " << std::endl;

std::cin >> values[i];

maxValue = values[i] < minValue )

{

minValue = values[i] > maxValue I)

{

maxValue = values[i];

}

sum = sum - maxValue - minValue;

float average = (float)sum / (float)(NUM_OF_VALUES - 2.0);

std::cout << std::endl << "The average is " << average << std::endl;

return 0;

}#include <iostream>

const int NUM_OF_VALUES = 8;

int main()

{

int values[NUM_OF_VALUES];

int minValue, maxValue;

int sum = 0;

int minCount = 1;

int maxCount = 1;

for (int i = 0; i < NUM_OF_VALUES; i++ )

{

std::cout << "Enter value #" << i+1 << ": " << std::endl;

std::cin >> values[i];

maxValue = values[i] < minValue )

{

minValue = values[i] == minValue )

{

minCount++;

}

if ( values[i] > maxValue )

{

maxValue = values[i];

maxCount = 1;

}

else if ( values[i] == maxValue )

{

maxCount++;

}

}

sum += values[i];

}

sum = sum - (maxValue * maxCount) - (minValue * minCount);

float average = (float)sum / (float)(NUM_OF_VALUES - maxCount - minCount);

std::cout << std::endl << "The min value is " << minValue;

std::cout << std::endl << "The max value is " << maxValue;

std::cout << std::endl << "The sum is " << sum;

std::cout.setf(std::ios::fixed);

std::cout << std::endl << "The average is " << average << std::endl;

return 0;

}

#10

Thats brilliant right there, but why std::cout? The IOstream library only needs cout and cin to function correctly. and in reality i don't think you used vectors and just stuck with basic arrays. the point of vectors is to be a replacement of array's just like strings are replacement for the char[x] array. there is no real limit on the vector or the string so they don't fall into the keyboard buffer and risk the possibility of crashing the program if its a much larger program.

I take it you are used to throwing a "using namespace std;" up there?

If you don't tell the compiler what namespace to use, it will error. If you use a using directive, it knows to look in the std namespace. Otherwise, you must specify which namespace to use. I'm just not used to using the "using" directive. Man, that's confusing.

As far as vectors are concerned, sure I could have used the std::vector. There's no point in doing so for something this simple though. I know that the Standard Template Library is built with efficiency in mind, but it is still unnecessary overhead for something this simple.

#11

Thats brilliant right there, but why std::cout? The IOstream library only needs cout and cin to function correctly. and in reality i don't think you used vectors and just stuck with basic arrays. the point of vectors is to be a replacement of array's just like strings are replacement for the char[x] array. there is no real limit on the vector or the string so they don't fall into the keyboard buffer and risk the possibility of crashing the program if its a much larger program.

The datatypes string and char[] are essentially the same thing (yes I realize there's no "string" datatype in c++, but in other languages you do have that). Variables of these types are basically a *char which points to the address of the first variable in the array. Wether these are allocated statically or dynamically is another story.

#12

I take it you are used to throwing a "using namespace std;" up there?

If you don't tell the compiler what namespace to use, it will error. If you use a using directive, it knows to look in the std namespace. Otherwise, you must specify which namespace to use. I'm just not used to using the "using" directive. Man, that's confusing.

As far as vectors are concerned, sure I could have used the std::vector. There's no point in doing so for something this simple though. I know that the Standard Template Library is built with efficiency in mind, but it is still unnecessary overhead for something this simple.

no no no...what i was saying is that the guy who had the original question needed a program that used vectors, even though with thing like this, arrays are much better to use.

#13

Vectors are one-dimensional arrays...