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]);
}
}
|