Hello, all. I was assigned a new program to sort a list of stocks from a formatted data file. The data file (truncated) is as follows:
Code:
BBY 48.75 48.65 49.30 49.35 48.10 48.35
COP 69.05 68.36 67.35 67.10 67.75 68.10
DEL 23.05 23.10 23.20 23.19 23.31 23.31
HPQ 40.12 40.24 40.50 41.00 41.10 41.68
HD 37.12 36.65 37.38 37.63 37.90 38.00
There are about 15 stocks in the list. The first column is the stock symbol, the second column is the average price, and the next five columns are daily prices. I am trying to read the stock symbols into a two dimensional array so that I can sort it by average prices later. My problem is with actually reading the correct stock symbol into the array. Here is the code:
Code:
#include <stdlib.h>
#include <stdio.h>
#define MAX 20
#define MAX_NAME 3
#define MAX_DAYS 5
#define FILENAME "Stocks07.dta"
#define EOS '\0'
int main (void)
{
FILE *ifp;
char symbol[MAX][MAX_NAME];
double prev_avg[MAX],
daily[MAX][MAX_DAYS];
int cnt, // number of rows OR number of stocks
element; // counter for storing stock symbols and daily prices
if((ifp = fopen(FILENAME, "r")) != NULL)
{
for (cnt = 0; cnt < MAX; ++cnt)
{
for(element = 0; element < MAX_NAME; ++element)
fscanf(ifp,"%c", &symbol[cnt][element]);
symbol[cnt][element] = EOS;
fscanf(ifp,"%lf", &prev_avg[cnt]);
for(element = 0; element < MAX_DAYS; ++element)
fscanf(ifp,"%lf", &daily[cnt][element]);
fscanf(ifp,"%*c");
}
printf("The first symbol is %s", symbol[0]);
printf("\n\nPrev. avg is %5.2f", prev_avg[0]);
printf("\n\nWednesday's price for %s is %5.2f", symbol[4], daily[4][2]);
printf("\n\n15th Prev.avg is %5.2f\n\n", prev_avg[14]);
system("pause");
}
else
printf("\n\n\n\n %45s \n\n\n", "Missing Data File!");
return 0;
}
From the sample printf statements, my output is as follows:
Code:
The first symbol is BBYCOPDELHPQHD IBMINTLMTLOWLUFPEPPPCTINWMTXOM
Prev. avg is 48.75
Wednesday's price for HD IBMINTLMTLOWLUFPEPPPCTINWMTXOM is 37.63
15th prev. avg is 76.12
Can anyone tell me what I'm doing wrong or another method that doesn't involve using a lot of standard functions. We were told to use the methods we learned in class.