Parallel Array reading and bubble sorting

I am having trouble initializing some parallel arrays using an input file.
I am only able to get the first two items out of the file and the rest is random numbers that are not even in the file. Also, the bubble array is supposed to sort by trSym, which is the first row in the input file. Not sure if my code is right for that. Any help and/or advice would be greatly appreciated. The file is below, and the code is below that.
1
2
3
4
5
6
7
MCD      McDonald's Corporation            35  45.63  54.76
BA       Boeing Co                        245  34.40  34.43
TGT      Target Corp.                      25  22.75  27.83
MOT      Motorola Inc                     100  12.25   3.75
HOG      Harley-Davidson, Inc             500   5.65  10.40
HD       Home Depot Inc                   312  33.50  20.50
DIS      Walt Disney Company                2  17.94  17.90 

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int ReadStkData(char trSym[][9], char cmpName[][31], int numShrs[], float pPrc[], float cPrc[], int maxSize);
void SortStkData(char trSym[][9], char cmpName[][31], int numShrs[], float pPrc[], float cPrc[], int size);

int main()
{
	// declarations
	const int max_Size = 15; // declaring array size
	char trSym[max_Size][9];
	char cmpName[max_Size][31];
	int numShrs[max_Size];
	float pPrc[max_Size];
	float cPrc[max_Size];

	//calling modules to perform actions
	ReadStkData(trSym, cmpName, numShrs, pPrc, cPrc, max_Size);
	SortStkData(trSym, cmpName, numShrs, pPrc, cPrc, max_Size);
	system("pause");
return 0;
}

int ReadStkData( char trSym[][9], char cmpName[][31], int numShrs[], float pPrc[], float cPrc[], int max_Size)
{

	ifstream inCode; // input file stream variable 
	char inFile[21]; // variable to store name of text file
	// get file name
	cout << "Enter the input file name: ";
	cin.getline(inFile, max_Size);

	// open file
	inCode.open(inFile);
	if (!inCode)
	{
		cout << "Unable to open the file " << inFile; // file not valid
		
		exit(EXIT_FAILURE);
	}
	// initialize variables using input file
	int i = 0;
	while (i < max_Size)
	{
        inCode >> trSym[i];
        inCode >> cmpName[i];
        inCode >> numShrs[i];
        inCode >> pPrc[i];
		inCode >> cPrc[i];

        i++;
	}

	inCode.close();
	// return variables
return trSym, cmpName, numShrs, pPrc, cPrc, max_Size;
}

void SortStkData(char trSym[][9], char cmpName[][31], int numShrs[], float pPrc[], float cPrc[], int size)
{
        int temp;

        // using a bubble sort
        for (int pass=0; pass < size -1; pass++) // passes
	        for (int i=0; i < size - i; i++) // one pass
		        if (trSym[i] > trSym[i+1]) // compare
		        {	
			        temp = trSym[i][9];
			        trSym[i][9] = trSym[i+1][9];
			        trSym[i+1][9] = temp;
		        }

         for(int index = 0; index < size; index++)
        {
		cout << setw(9) << trSym[index] 
		<< setw(31) << cmpName[index] << setw(10) << numShrs[index] << endl;
        }
}
Last edited on
Topic archived. No new replies allowed.