Im trying to make this program for class that compares to array that represent stock values on different days. The arrays are these two below...
Code:
float stock1[ ] =
{34.25,40.50,36.50,40.00,
30.25,30.25,35.50,36.00,
34.25,37.00,34.00,35.00,
36.25,34.25,40.50,41.50,
41.50,40.00,36.50,34.50};
float stock2[ ] =
{40.25,38.50,34.50,33.50,
30.50,29.75,37.50,38.00,
34.75,38.00,34.25,37.00,
34.25,37.50,34.50,38.50,
37.50,37.25,38.25,37.50};
Theres four different reports that do a calculation (i.e. tally # of days stock 1 exceeds stock 2) then print out a report of the answers. The class is almost over and I have been doing very well but I am stumped on the third report.
"Report 3: Compute the five-day moving average for all stocks and display these averages."
So basically I have to make a loop calculating the average (stock0-4), then divide by 5, then assign it to an array value and repeat, but the 2nd loop totals stocks 1-5 and so on till its stock 16-20.
So, onto my code at the moment. Right now I am almost positive that the code is right, but for some reason instead of printing ANY of the results, the program crashes. Ive been in the class for awhile now and I have close to a 100%, so I should be able to figure this out myself, but I am completely stumped. So heres the code, hopefully someone can help me out.
Code:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include "util.h"
#define MAX 20
#define FIVEDAYAVG 5
int main()
{
int i=0, x=0;
int stock1Counter=0, stock2Counter=0, sameStockCounter=0; //Report1 Varibles
int stock1AvgCount=0, stock2AvgCount=0; //Report2 Varibles
double stock1Average=0, stock2Average=0, stock1Total=0, stock2Total=0; //Report2 Varibles
double average=0; //Report3 Varibles
int z=5, y=0; //Report 3 Varibles
float fiveDayStock1[ ] = {0}, fiveDayStock2[ ] = {0}; //Report3 Varibles
int fiveDay1Counter=0, fiveDay2Counter=0, sameFiveDayCounter=0; //Report4 Varibles
float stock1[ ] = {34.25,40.50,36.50,40.00,30.25,30.25,35.50,36.00,34.25,37.00,34.00,35.00,36.25,34.25,40.50,41.50,41.50,40.00,36.50,34.50};
float stock2[ ] = {40.25,38.50,34.50,33.50,30.50,29.75,37.50,38.00,34.75,38.00,34.25,37.00,34.25,37.50,34.50,38.50,37.50,37.25,38.25,37.50};
//REPORT 1
for (i=0; i < MAX; i++)
{
if (stock1[i] > stock2[i])
stock1Counter++;
else if (stock2[i] > stock1[i])
stock2Counter++;
else
sameStockCounter++;
}
//REPORT 2
for (i=0; i < MAX; i++)
{
stock1Total = (stock1Total + stock1[i]);
stock2Total = (stock2Total + stock2[i]);
}
stock1Average = stock1Total / MAX;
stock2Average = stock2Total / MAX;
for (i=0; i < MAX; i++)
{
if (stock1[i] > stock1Average)
stock1AvgCount++;
else if (stock2[i] > stock2Average)
stock2AvgCount++;
}
//REPORT 3
for (i=0; i<MAX; i++)
{
for (x=z-5; x<z; x++)
average = (average + stock1[x]);
average = (average / FIVEDAYAVG);
fiveDayStock1[i] = average;
average = 0; //clears average
z++;
}
z=5; //resets counter
average=0; //resets average
for (i=0; i<MAX; i++)
{
for (x=z-5; x<z; x++)
average = (average + stock2[x]);
average = (average / FIVEDAYAVG);
fiveDayStock2[i] = average;
average = 0; //clears average
z++;
}
//REPORT 4
for (i=0; i<MAX; i++)
{
if (fiveDayStock1[i] > fiveDayStock2[i])
fiveDay1Counter++;
else if (fiveDayStock2[i] > fiveDayStock1[i])
fiveDay2Counter++;
else
sameFiveDayCounter++;
}
//REPORT 1 RESULTS
printf("\nThe number of days on which stock 1 exceeds stock 2 is %d\n", stock1Counter);
printf("The number of days on which stock 2 exceeds stock 1 is %d\n", stock2Counter);
printf("The number of days on which both stocks are the same price is %d\n\n", sameStockCounter);
//REPORT 2 RESULTS
printf("\nThe average of stock 1 is %lf\n", stock1Average);
printf("The average of stock 2 is %lf\n", stock2Average);
printf("The number of times stock 1 exceeds the stock 1 average is %d\n", stock1AvgCount);
printf("The number of times stock 2 exceeds the stock 2 average is %d\n", stock2AvgCount);
//REPORT 3 RESULTS
printf("\n~~~STOCK 1 ~~~\n");
for (i=0; i<MAX; i++)
printf("Moving average #%d = %lf\n", i+1, fiveDayStock1[i]);
printf("\n~~~STOCK 2 ~~~\n");
for (i=0; i<MAX; i++)
printf("Moving average #%d = %lf\n", i+1, fiveDayStock2[i]);
//REPORT 4 RESULTS
printf("\nThe number of days stock 1's moving average exceeds stock 2's = %d\n", fiveDay1Counter);
printf("The number of days stock 2's moving average exceeds stock 1's = %d\n", fiveDay2Counter);
printf("The number of days stock 1 and stock 2's moving average was equal = %d\n", sameFiveDayCounter);
getch();
return 0;
}
In the report 3 calculations the outer for loop does the calculation 20 times and the inner loop does the adding of the 5 stocks, then computes the average and assigns it inside the outer for loop.