got it thanks

Im working on this assignemt that neeeds to get data from a file and for some reason im having errors with "HOURS" and "CPNY" and reading the file, I am using xcode so I have to designate where the file is, please help.

The following data is the hourly price of each specified stock. The price is shown as of 10AM, 11AM, NOON, 1 PM, 2 PM, and 3 PM.
NOTE that the company names are different than in lab 2, but the prices are the same.

51.41 52.07 52.01 51.22 50.44 49.97 Coal Diggers
77.26 78.33 78.29 78.12 77.09 75.74 Airplane Flyers
31.25 31.44 31.43 31.09 31.01 30.92 Oil Fracting and Pumping
2.03 2.02 2.04 2.00 1.98 1.97 Big Bank
44.21 44.32 44.29 43.98 43.82 43.71 Rail Container Shipping
93.21 93.11 93.02 93.31 92.98 92.89 Gold Bugs
Type this data, exactly as shown, into a file, or copy your data from lab 2 and change the company names.

There will be ONE array of companies. Each company data will be kept in a structure. The structure will contain: - A pointer to heap space containing the company name - An array of 6 hourly prices - The average price for the day These structures shall be kept in an array of 20 structures.

Read the data from the file, and put the data from each line into the next structure in the array of structures.

For each company compute and print the average of all of the six prices for that company. Store the average in the structure.

Sort the array of structures alphabetically, by company name.

Print all the data from each company structure in a nice format. Use the hours in the heading for each column of hourly price data.
Do NOT print the unused entries at the end of the array.

For each hour compute and print the average of all the stock prices for that hour. Print all these average hourly prices in a nice format.



#include <stdio.h>
#include <stdlib.h>


typedef struct
{
char* name; //pointer to character array, which is the index of a name array
float prices[HOURS];
float avg;//average price for all hours
}company;

void openFile(FILE** fp);
void readData(FILE* fp, char names[CPNY][50], company companies[CPNY], float hourAvg[HOURS]);
void sortCompany(char names[CPNY][50], company companies[CPNY]);
void print(char names[CPNY][50], company companies[CPNY], char times[][50],float hourAvg[HOURS]);

int main()
{

char times[HOURS][50] = {{"10AM"},{"11AM"},{"NOON"},{"1PM"},{"2PM"},{"3PM"}};
float hourAvg[HOURS];
FILE* fp;
openFile(&fp);
char names[CPNY][50];
company companies[CPNY];
readData(fp, names, companies, hourAvg);
sortCompany(names, companies);
print(names, companies,times, hourAvg);
return 0;

}

void openFile(FILE** fp)
{
fp = fopen("/Users/ASSYRIA/Desktop/assign2.txt", "r");
if(fp == NULL)
{
printf("ERROR OPENING FILE");
system("pause");
exit(100);
}
}
void readData(FILE* fp, char names[CPNY][50], company companies[CPNY], float hourAvg[HOURS])
{
int row;
int col;
float avg;//company avg
for(col = 0; col < HOURS; col++)
{
hourAvg[col] = 0;
}
for(row = 0; row < CPNY; row++)
{
avg = 0;
for(col = 0; col < HOURS; col++)
{
fscanf(fp,"%f",&companies[row].prices[col]);
avg += companies[row].prices[col];
hourAvg[col] += (companies[row].prices[col] / CPNY);
}
avg = avg / HOURS;
fscanf(fp, "%*c%*c%[^\n]s",&names[row]);
companies[row].name = names[row];
companies[row].avg = avg;
}

}
void sortCompany(char names[CPNY][50], company companies[CPNY])
{
int i,j;
company swap;
for(i = CPNY - 1; i > 0; i--)
{
for(j = 1; j <= i;j++)
{
if(strncmp(companies[j-1].name,companies[j].name,50) > 0)
{
swap = companies[j-1];
companies[j-1] = companies[j];
companies[j] = swap;
}
}
}
}

void print(char names[CPNY][50], company companies[CPNY], char times[][50],float hourAvg[HOURS])
{
int row, col;
printf("COMPANIES ");
for(col = 0; col < HOURS; col++)
{
printf("%5s ", times[col]);
}
printf(" AVGS\n");

for(row = 0; row < CPNY; row++)
{
printf("%-16s",companies[row].name);
for(col = 0; col < HOURS; col++)
{
printf("%5.2f ", companies[row].prices[col]);
}
printf("%5.2f\n", companies[row].prices[col]);
}
printf(" -----------------------------------\nHourly Averages ");
for(col = 0; col < HOURS; col++)
{
printf("%5.2f ", hourAvg[col]);
}
}
Last edited on
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //<-------------------------------------------------added this header file for the use of strcmp
#define CPNY 6 //<-----------------------------------------------------added global variables CPNY and HOURS
#define HOURS 6                                                       //not saying you should use global variables
typedef struct                                                         //just saying it solved the problem
{
	char* name; //pointer to character array, which is the index of a name array
	float prices[HOURS];
	float avg;//average price for all hours
}company;

void openFile(FILE** fp);
void readData(FILE* fp, char names[][50], company companies[], float hourAvg[]);          //removed HOURS and CPNY from your prototypes
void sortCompany(char names[][50], company companies[]);
void print(char names[][50], company companies[], char times[][50],float hourAvg[]);

int main()
{

	
	char times[HOURS][50] = {"10AM","11AM","NOON","1PM","2PM","3PM"};   //removed some of the braces
	float hourAvg[HOURS];
	FILE* fp;
	openFile(&fp);
	char names[CPNY][50];
	company companies[CPNY];
	readData(fp, names, companies, hourAvg);
	sortCompany(names, companies);
	print(names, companies,times, hourAvg);
	return 0;

}

void openFile(FILE** fp)
{
	*fp = fopen("my.txt", "r");  //added a * to fp
	if(fp == NULL)
	{
		printf("ERROR OPENING FILE");
		system("pause");
		exit(100);
	}
}
void readData(FILE* fp, char names[][50], company companies[], float hourAvg[])  //removed HOURS and CMPNY from function header
{
	int row;
	int col;
	float avg;//company avg
	for(col = 0; col < HOURS; col++)
	{
		hourAvg[col] = 0;
	}
	for(row = 0; row < CPNY; row++)
	{
		avg = 0;
		for(col = 0; col < HOURS; col++)
		{
			fscanf(fp,"%f",&companies[row].prices[col]);
			avg += companies[row].prices[col];
			hourAvg[col] += (companies[row].prices[col] / CPNY);
		}
		avg = avg / HOURS;
		fscanf(fp, "%*c%*c%[^\n]s",&names[row]);
		companies[row].name = names[row];
		companies[row].avg = avg;
	}

}
void sortCompany(char names[][50], company companies[])//removed HOURS and CMPNY from function header
{
	int i,j;
	company swap;
	for(i = CPNY - 1; i > 0; i--)
	{
		for(j = 1; j <= i;j++)
		{
			if(strncmp(companies[j-1].name,companies[j].name,50) > 0)
			{
				swap = companies[j-1];
				companies[j-1] = companies[j];
				companies[j] = swap;
			}
		}
	}
}

void print(char names[][50], company companies[], char times[][50],float hourAvg[])//removed HOURS and CMPNY from function header
{
	int row, col;
	printf("COMPANIES ");
	for(col = 0; col < HOURS; col++)
	{
		printf("%5s ", times[col]);
	}
	printf(" AVGS\n");

	for(row = 0; row < CPNY; row++)
	{
		printf("%-16s",companies[row].name);
		for(col = 0; col < HOURS; col++)
		{
			printf("%5.2f ", companies[row].prices[col]);
		}
		printf("%5.2f\n", companies[row].prices[col]);
	}
	printf(" -----------------------------------\nHourly Averages ");
	for(col = 0; col < HOURS; col++)
	{
		printf("%5.2f ", hourAvg[col]);
	}
}




the output I got
COMPANIES  10AM  11AM  NOON   1PM   2PM   3PM  AVGS
ail Container Shipping44.21 44.32 44.29 43.98 43.82 43.71 44.05
ig Bank          2.03  2.02  2.04  2.00  1.98  1.97  2.01
il Fracting and Pumping31.25 31.44 31.43 31.09 31.01 30.92 31.19
irplane Flyers  77.26 78.33 78.29 78.12 77.09 75.74 77.47
oal Diggers     51.41 52.07 52.01 51.22 50.44 49.97 51.19
old Bugs        93.21 93.11 93.02 93.31 92.98 92.89 93.09
 -----------------------------------
Hourly Averages 49.90 50.22 50.18 49.95 49.55 49.20
Last edited on
Topic archived. No new replies allowed.