#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?
Method of Groove




"Hardcore without punk isn't music, it's a genre of porn.
Punk isn't a genre of music, it’s a thought process."
#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.
What do we tell our children? Look before you leap. Stop and think. Don't judge a book by its cover. We believe that we are always better off gathering as much information as possible and spending as much time as possible in deliberation.
#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.
#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...
#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;
}
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...
Gibson Les Paul Classic w/Stetsbar
Fender MIM Strat w/ EMG DG20 System
Mesa Boogie Express 5:25
EHX Holy Grail
EHX Memory Boy
Maxon OD9
EHX Big Muff
Vox Wah
EHX POG
#8
Quote by statopaul_mark
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.
Quote by Godzilla1969
I love you, Muphin. You have great taste in music.

Quote by Pacifica112J
Muphin > You

The Cooperation
#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;
}
Quote by Silentnight66
Because this thread has Hitler and being hyper in it here's the fact of the day. Hitler was a meth addict.
#10
Quote by aPIRATEwithADD
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
Quote by aPIRATEwithADD
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.
Dear God, do you actually answer prayers?

Yes, but only in a way indistinguishable from random luck or the result of your own efforts.
#12
Quote by bbunt302
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.
Quote by Silentnight66
Because this thread has Hitler and being hyper in it here's the fact of the day. Hitler was a meth addict.
#13
Vectors are one-dimensional arrays...
Dear God, do you actually answer prayers?

Yes, but only in a way indistinguishable from random luck or the result of your own efforts.