****How do I put my current code into a linked list**
Put the structures in a linked list. The linked list shall be a head pointer, and a Node structure for each CompanyData structure.
Each Node structure shall contain a pointer to one CompanyData structure, and a pointer to the next Node structure.
As soon as you read ONE line of data and put it into a structure, insert a new Node, with the CompanyData, into the linked list in the correct position to keep the linked list always in sorted order, alphabetically by company name.
Do NOT sort the linked list AFTER the CompanyData has been put into the linked list, it should already be sorted by the correct insertions.
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.
**I am having trouble putting into a linked list**
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 113
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CPNY 6
#define HOURS 6
typedef struct
{
char* name;
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[]);
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"};
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[])
{
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[])
{
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]);
}
}
|