Code:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include "util.h"
int numDays(float stock1[],float stock2[],int); /*function prototype*/
int numSame(float stock1[],float stock2[],int); /*function prototype*/
int numAvg(float stock[],int); /*function prototype*/
void movAvg(float stock1[],int); /*function prototype*/
int avgDay(float stock1[],float stock2[],int); /*function prototype*/
int movAvgSame(float stock1[],float stock2[],int); /*function prototype*/
int main()
{
system("CLS"); /*Clears the screen*/
printf("Report 1\n");
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, /*Arrays are declared and initialized*/
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};
printf("\nStock1 has exceeded the price of stock2 by: %d days\n",numDays(stock1,stock2,20));
printf("\nStock2 has exceeded the price of stock1 by: %d days\n",numDays(stock2,stock1,20));
printf("\nThe number of days in which the price of the stocks were the same: %d days\n\n",numSame(stock1,stock2,20));
PauseScreen();
system("CLS");
printf("Report 2\n");
printf("\nThe number of days in which stock1 exceeded it's price is: %d days\n", numAvg(stock1,20));
printf("\nThe number of days in which stock2 exceeded it's price is: %d days\n", numAvg(stock2,20));
PauseScreen();
system("CLS");
printf("Report 3\n");
printf("\n\nstock 1\n\n");
movAvg(stock1,20);
printf("\n\nstock 2\n\n");
movAvg(stock2,20);
PauseScreen();
system("CLS");
printf("Report 4");
printf("\nThe number of days in which the moving average of stock1 exceeds the moving average of stock2: %d days\n\n", avgDay(stock1,stock2,20));
printf("\nThe number of days in which the moving average of stock1 exceeds the moving average of stock2: %d days\n\n", avgDay(stock2,stock1,20));
printf("\nThe moving average of stock1 equals the moving average of stock2: %d days\n\n", movAvgSame(stock1,stock2,20));
PauseScreen();
return 0;
}
int numDays(float stock1[],float stock2[],int size)
{
int index;
int days;
days=0;
for (index = 0;index < size;index++)
if (stock1[index] > stock2[index])
{
days++;
}
return days;
}
int numSame(float stock1[],float stock2[],int size)
{
int index;
int days;
days=0;
for (index = 0;index < size;index++)
if (stock1[index] == stock2[index])
{
days++;
}
return days;
}
int numAvg(float stock[],int size)
{
float sum;
sum=0;
int index;
int days;
days=0;
int avg;
avg=0;
for (index = 0;index < size;index++)
{
sum=sum+stock[index];
}
avg=(int)sum/size;
for (index = 0;index < size;index++)
if (stock[index]> avg)
{
days++;
}
return days;
}
void movAvg(float stock1[],int size)
{
int days=5;
float sum;
int index,n;
float avg=0;
for (index=0;index<size-days+1;index++)
{
sum=0;
for (n=index;n<index+days;n++)
{
sum=sum+stock1[n];
avg=sum/days;
}
printf("The average for prices %d through %d is: %.2f\n", index+1, index+days,avg);
}
}
int avgDay(float stock1[],float stock2[],int size)
{
int days=5;
float sum,sum1;
int index,n;
float avg,avg1;
for (index=0;index<size-days+1;index++)
{
sum=0.;
sum1=0.;
for (n=index;n<index+days;n++)
{
sum=sum+stock1[n];
avg=sum/days;
sum1=sum1+stock2[n]
avg1=sum1/days;
}
if (avg > avg1)
{
days++;
}
}
return days;
}
int movAvgSame (float stock1[],float stock2[],int size)
{
int days=5;
float sum,sum1;
int index,n;
float avg,avg1;
for (index=0;index<size-days+1;index++)
{
sum=0.;
sum1=0.;
for (n=index;n<index+days;n++)
{
sum=sum+stock1[n];
avg=sum/days;
sum1=sum1+stock2[n]
avg1=sum1/days;
}
if (avg == avg1)
{
days++;
}
}
return days;
}
The last 2 functions are not correct and I have no idea why. I also get different results when I move the return days in either or both functions down 1 bracket. Not sure why this happens either. Thanks again for your help. Tommy