Hello everyone i'm writing a program of a stock market where i read from a file and sort with symbols and percent gain/loss. I have completed sorting with symbols but having trouble establishing the percent gain loss. Basically i am instructed to use vectors. We are required to produce the list ordered by percent gain/loss and i need to sort the stock list by this component. However i'm not to physically sort the list by component percent gain/loss; instead provide a logical ordering with respect to this component. so basically i added a data member, a vector to hold the indices of the stock list ordered by the component percent gain/loss. i called it array indexByGain. so when i print the list ordered by the percent gain/loss, i use the array indexByGain to print the list. my problem is an i need help on how to start if someone could help me explain how to go about it or explain the sample code they did using arrays would be of great assistance. below is a sample code sorting percent gain/loss:
/* Name: stock_sort() */
/* Purpose: Sort stocks (stockListType) by percent gained or loss, ascending order */
/* Parameters: None */
/* Preconditions: list of stocks should be created and initialized as well as
sortIndicesGainLoss array */
/* Postconditions: sortIndicesGainLoss holds the indices of stockList sorted
by gain/loss */
void stockListType::stock_sort() {
int i, j;
int min;
int temp1, temp2;
for(i = 0; i < length; i++)
sortIndicesGainLoss[i] = i;
for(i = 0; i < length; i++)
{
min = i;
for(j = i + 1; j < length; ++j)
if(list[sortIndicesGainLoss[min]].percent_gain()>list[sortIndicesGainLoss[j]].percent_gain())
min = j;
temp1 = sortIndicesGainLoss[i];
sortIndicesGainLoss[i] = sortIndicesGainLoss[min];
sortIndicesGainLoss[min] = temp1;
}
}
How do you go about this using vectors. i'm really having trouble trying to use vectors to sort out the stocks as i confused how about go about it. If someone could assist me i will appreciate it. Thanks