I have a function and I need multiple averages returned
I have a question about this program. I created the header file called util.h and I have completed most of the 3 out of 4 reports that need to be done. This program has 2 arrays, stock1 and stock2 and they each have 20 prices. I need to write functions (1 for stock1 and the other for stock2) to find the 16 5-day moving average of the price of the stocks. This means I would find the average price for prices 1-5, then the average price from 2-6, then 3-7, 4-8, 5-9, 6-10 and so on until I get to 16-20. I am not sure how to do this. I know how to find the moving average, but I don't know how to return the values, or if they need to be returned at all. Anybody have any ideas? Functions can only return 1 value. They can probably return more, but knowing this is beyond the scope of this course. I know that writing 16 functions is absurd. How about pointers, which I am not that good at writing yet? Or can we just have the printf statements within the function? Or am I wrong all together? LOL. Here's the program:
Code:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include "util.h"
int numDays1(float stock1[],float stock2[],int); /*function prototype*/
int numDays2(float stock2[],float stock1[],int); /*function prototype*/
int numSame(float stock1[],float stock2[],int); /*function prototype*/
int numAvg1(float stock1[],int); /*function prototype*/
int numAvg2(float stock2[],int); /*function prototype*/
float movAvg1(float stock1[],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",numDays1(stock1,stock2,20));
printf("\nStock2 has exceeded the price of stock1 by: %d days\n",numDays2(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", numAvg1(stock1,20));
printf("\nThe number of days in which stock2 exceeded it's price is: %d days\n", numAvg2(stock2,20));
PauseScreen();
system("CLS");
printf("Report 3\n");
printf("\nThe average for prices 1 through 5 are: %.2f\n", movAvg1(stock1,20));
getch();
return 0;
}
int numDays1(float stock1[],float stock2[],int size)
{
int index;
int days;
days;
days=0;
for (index = 0;index < size;index++)
if (stock1[index] > stock2[index])
{
days++;
}
return days;
}
int numDays2(float stock2[],float stock1[],int size)
{
int index;
int days;
days=0;
for (index = 0;index < size;index++)
if (stock2[index] > stock1[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 numAvg1(float stock1[],int size)
{
float sum;
sum=0;
int index;
int days;
days=0;
int avg;
avg=0;
for (index = 0;index < size;index++)
{
sum=sum+stock1[index];
}
avg=(int)sum/size;
for (index = 0;index < size;index++)
if (stock1[index]> avg)
{
days++;
}
return days;
}
int numAvg2(float stock2[],int size)
{
float sum;
sum=0;
int index;
int days;
days=0;
int avg;
avg=0;
for (index = 0;index < size;index++)
{
sum=sum+stock2[index];
}
avg=(int)sum/size;
for (index = 0;index < size;index++)
if (stock2[index]> avg)
{
days++;
}
return days;
}
float movAvg1(float stock1[],int)
{
float sum;
sum=0;
int index;
int days;
days=0;
float avg;
avg=0.0;
for (index=0;index<5;index++)
{
sum=sum+stock1[index];
avg=sum/5;
}
return avg;
}
The float movAvg1 will be the function that will find the 16 5-day moving average. I plan on having another function called float movAvg2 to create the 16 5-day moving average for stock2. I appreciate any help.....Thanks....Tommy
P.S.--If anyone has any suggestions on improving my code, I would like to know.