array

The findLargestDollarLoss function keeps getting the output of 0, did I mess up somewhere in the function? I tried setting largestLoss_so_far to equal dollarProfitLoss[d], but it got the same result.

The input file looks like this
IBM 100 93.2 98.6
MER 200 67.2 43.89
QQQ 300 78.9 70.0
C 200 35.78 50.02
CSCO 400 45.67 57.23


And the output file looks like this now.
//In this order: Name, shares, buy price, current price, dollar profit loss, percent loss,

IBM 100.00 93.20 98.60 540.00 5.79
MER 200.00 67.20 43.89 -4662.00 -34.69
QQQ 300.00 78.90 70.00 -2670.00 -11.28
C 200.00 35.78 50.02 2848.00 39.80
CSCO 400.00 45.67 57.23 4624.00 25.31

Largest gain: $4624.00
Largest loss: $0.00


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
double setDollarProfitLoss(double[], double[], double[], int);
double setPercentProfitLoss(double[], double[], double[], int);
double findLargestDollarGain(double[],double[], double[], int);
double findLargestDollarLoss(double[],double[], double[], int);


int main()
{
string name[5];
double shares[5], bprice[5], cprice[5];
double largestGain;
double largestLoss;

	ifstream inFile;
	ofstream outFile;

	inFile.open("C:homework1.txt");
	outFile.open("C:homework2.txt");

	if(!inFile.good())
		cout<<"error"<<endl;
	else
	{	for(int i=0; i<5; i++)
			inFile>>name[i]>>shares[i]>>bprice[i]>>cprice[i];				
	}
	
	for(int h=0; h < 5; h++)
	{
		setDollarProfitLoss(shares, bprice, cprice, h);
		setPercentProfitLoss(shares, bprice, cprice, h);
		largestGain = findLargestDollarGain(shares, bprice, cprice, h);
		largestLoss = findLargestDollarLoss(shares, bprice, cprice, h);
	}
	for (int k=0; k<5; k++)
		outFile<<name[k]<< setw(12) << fixed << setprecision(2) << shares[k]<< setw(10) << fixed << setprecision(2) << bprice[k]<< setw(10) << fixed << setprecision(2) << cprice[k]<< setw(12) << fixed << setprecision(2) << setDollarProfitLoss(shares, bprice, cprice, k) << setw(10) << fixed << setprecision(2) <<  setPercentProfitLoss(shares, bprice, cprice, k) << endl;

	outFile<<endl;
	outFile<<"Largest gain: $"<< fixed << setprecision(2) << largestGain << endl;
	outFile<<"Largest loss: $"<< fixed << setprecision(2) << largestLoss << endl;
	

inFile.close();
outFile.close();
return 0;
}

double setDollarProfitLoss(double shares1[], double bprice1[], double cprice1[], int count)
{
	double profitloss;
	profitloss = ((cprice1[count] - bprice1[count]) * shares1[count]);
	return profitloss;
}

double setPercentProfitLoss(double shares2[], double bprice2[], double cprice2[], int counter)
{
	double percentloss; 
	percentloss= (((cprice2[counter] - bprice2[counter]) * shares2[counter])/ (bprice2[counter] * shares2[counter])) * 100;
	return percentloss;
}

double findLargestDollarGain(double shares3[], double bprice3[], double cprice3[], int numcount)
{
	double dollarProfitGain[5];
	double largestGain_so_far = 0.0;
	
	for(int g=0; g<5; g++)
	{	
	dollarProfitGain[g] = ((cprice3[numcount] - bprice3[numcount]) * shares3[numcount]);
			if (largestGain_so_far < dollarProfitGain[g])
				largestGain_so_far = dollarProfitGain[g];
	}
	return largestGain_so_far;
}

double findLargestDollarLoss(double shares4[], double bprice4[], double cprice4[], int ncount)
{
	double dollarProfitLoss[5];
	double largestLoss_so_far = 0.0;

	for(int d=0; d<5; d++)
	{	
	dollarProfitLoss[d] = ((cprice4[ncount] - bprice4[ncount]) * shares4[ncount]);
		if(largestLoss_so_far > dollarProfitLoss[d])
			largestLoss_so_far = dollarProfitLoss[d];
	}
	return largestLoss_so_far;
}
Last edited on
Too many arrays. There is a reason stuff like vectors, structs etc exists.

As to your error:

largestLoss_so_far > dollarProfitLoss[d]


I can derive it from the context, but if you calculate loss as a positive value you'd have to use < instead of >.
To find the largest number in an array:
1.) Iterate through the array using a loop and assign the first element (ie array[0]) the value 'largest'.
2.) for the remaining elements if the value is greater than 'largest' assign it the value 'largest'
3.)after exiting the loop print out the value 'largest'
4.)Use exactly the same principle for finding 'smallest' or the greatest negative number
closed account (zb0S216C)
I wrote a template method to find the largest value. This is it, exactly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//---------------------------------------------------------------------------------------
// Function Name: GetLargeOfArray( )
//---------------
// Description: Returns the biggest value in an array.
//---------------------------------------------------------------------------------------
template < typename ArrayType >
ArrayType GetLargeOfArray( ArrayType ThisArray[ ], int ArraySize = 0 )
{
          ArrayType CurrentArrayValue = 0;  // Current value in the array
          ArrayType BiggestArrayValue = 0;  // Biggest value found in the array
          
          // Search for the biggest value
          for( int Done = 0; Done < ArraySize; Done++ )
          {
               // Give CurrentArrayValue the current value found in the array
               CurrentArrayValue = ThisArray[ Done ];
               
               // If the current array value is bigger than BiggestArrayValue then give
               // BiggestArrayValue the current value
               if( CurrentArrayValue > BiggestArrayValue )
               {
                   // Assign
                   BiggestArrayValue = CurrentArrayValue;
                   
                   // Clear out the current value
                   CurrentArrayValue = 0;
               }
          }
          
          // Clear the current value variable
          CurrentArrayValue = 0;
          
          // Return the biggest value found in the array
          return BiggestArrayValue;
}

It's pretty old and I'm sure there are faster ways of doing this, but it works. However, it only works with numerical types.
Framework - you do realize that there is an STL algorithm called max_element that just does that, just faster and without the need to provide the array size and without the restriction to types that implement the > operator, do you?
closed account (zb0S216C)
Nope, never knew that. Thanks for telling me that by the way. Why didn't you mention it before?
Topic archived. No new replies allowed.