GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Apr-2004, 17:18
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

Can someone check to see if my program is correct


I finally finished this program and some of the answers I am getting aren't correct and I don't know why. For example, Report 4 states:

The number of days in which the moving average of stock1 exceeded the moving ave
rage of stock2: 10 days

The number of days in which the moving average of stock2 exceeded the moving ave
rage of stock1: 6 days

The moving average of stock1 equals the moving average of stock2: 0 days

I know the last one is incorrect because for stock1, the 16 five-day average for 2-6 is: $35.50 and for stock2, the average price from 11-15 is: $35.50. The answer should be 1 and not 0. I don't know why I am getting an answer of 0.

One last thing......The output for the 16 five-day moving averages. Can the output for Report 3 look any better? I tried using %10.2f and but since it's in a loop, the changes effect everything that's printed within that loop.

I have my own header file and that's why I have PrintDate(); and PauseScreen();,

If the code can be improved, please let me know. Thanks for any help.


CPP / C++ / C 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 stock1[],int);	/*function prototype*/

void movAvg(float stock1[],int, float movingAvg[]);  /*function prototype*/

int avgDay(float movAvg1[],float movAvg2[],int);  /*function prototype*/

int movAvgSame(float movAvg1[],float movAvg2[],int);    /*function prototype*/
                                   
int main()
{
	system("CLS");			/*Clears the screen*/

	printf("Report 1                                                      ");

	PrintDate();
	printf("\n********");
		
	printf("\n\n");
	
	PrintTitle();

	
	 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};

	 float movingAvg1[16];
	 float movingAvg2[16];
	 

     printf("\n\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					              ");			

	 PrintDate();
	 printf("\n********");

	 printf("\n\nThe number of days in which stock1 exceeded it's average price:  %d days\n",  numAvg(stock1,20)); 

	 printf("\nThe number of days in which stock2 exceeded it's average price:  %d days\n\n",  numAvg(stock2,20));

	 PauseScreen();
	
	 system("CLS");

	 printf("Report 3                                                     ");

	 PrintDate();
	 printf("\n********");
	 printf("\n\n");

	 printf("16 Five-day moving averages of Stock1 and Stock2 \n\n");

	 printf("\t\t  Stock 1");
	 printf("\n\t\t*********\n\n");

 	 movAvg(stock1,20,movingAvg1);

	 printf("\n\n");

	 printf("\t\t  Stock 2");
	 printf("\n\t\t *********\n\n");

	 movAvg(stock2,20,movingAvg2);

	 PauseScreen();
	
	 system("CLS");
	 
	 printf("Report 4                                                     ");

	 PrintDate();
	 printf("\n********");

	 printf("\n\n");

	 printf("\nThe number of days in which the moving average of stock1 exceeded the moving average of stock2: %d days\n\n", avgDay(movingAvg1,movingAvg2,16));

     printf("\nThe number of days in which the moving average of stock2 exceeded the moving average of stock1: %d days\n\n", avgDay(movingAvg2,movingAvg1,16));

	 printf("\nThe moving average of stock1 equals the moving average of stock2: %d days\n\n", movAvgSame(movingAvg1,movingAvg2,16));
		 
	 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 stock1[],int size)
{
	float sum=0.;
	
	int index;

	int days;
	days=0;

	int avg;
	avg;

	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;
}
void movAvg(float stock1[],int size,float movingAvg[])
{
	
	int const days = 5; 
    float sum; 
    int index,n;
    

    for (index=0; index<size-days+1; index++)
    {
       sum = 0.;
       for (n=index;n<index+days;n++) 
       {
           sum=sum+stock1[n]; 
       	}
       movingAvg[index]=sum/days;
       printf("The average for prices %d through %d is: $%.2f\n",  index+1,index+days,movingAvg[index]); 
    } 

} 
int avgDay(float movAvg1[],float movAvg2[],int size)
{
	int index;

	int days;
	days=0;

	for (index = 0;index < size;index++)
	
	if (movAvg1[index] > movAvg2[index])
	{
		days++;
	}
	
	return days;
} 
int movAvgSame(float movAvg1[],float movAvg2[],int size)
{
	int index;

	int days;
	days=0;

	for (index = 0;index < size;index++)
	
	if (movAvg1[index] == movAvg2[index])
	{
		days++;
	}
	
	return days;
} 
*********************OUTPUT*********************** *****
Report 1 Apr 12 2004
********

Scott's Trading Company-----------------------

Stock1 has exceeded the price of stock2 by: 9 days

Stock2 has exceeded the price of stock1 by: 11 days

The number of days in which the price of the stocks were the same: 0 days

Please press any key to continue
================================================== ========
Report 2 Apr 12 2004
********

The number of days in which stock1 exceeded it's average price: 10 days

The number of days in which stock2 exceeded it's average price: 12 days

Please press any key to continue
================================================== ========
Report 3 Apr 12 2004
********

16 Five-day moving averages of Stock1 and Stock2

Stock 1
*********

The average for prices 1 through 5 is: $36.30
The average for prices 2 through 6 is: $35.50
The average for prices 3 through 7 is: $34.50
The average for prices 4 through 8 is: $34.40
The average for prices 5 through 9 is: $33.25
The average for prices 6 through 10 is: $34.60
The average for prices 7 through 11 is: $35.35
The average for prices 8 through 12 is: $35.25
The average for prices 9 through 13 is: $35.30
The average for prices 10 through 14 is: $35.30
The average for prices 11 through 15 is: $36.00
The average for prices 12 through 16 is: $37.50
The average for prices 13 through 17 is: $38.80
The average for prices 14 through 18 is: $39.55
The average for prices 15 through 19 is: $40.00
The average for prices 16 through 20 is: $38.80


Stock 2
*********

The average for prices 1 through 5 is: $35.45
The average for prices 2 through 6 is: $33.35
The average for prices 3 through 7 is: $33.15
The average for prices 4 through 8 is: $33.85
The average for prices 5 through 9 is: $34.10
The average for prices 6 through 10 is: $35.60
The average for prices 7 through 11 is: $36.50
The average for prices 8 through 12 is: $36.40
The average for prices 9 through 13 is: $35.65
The average for prices 10 through 14 is: $36.20
The average for prices 11 through 15 is: $35.50
The average for prices 12 through 16 is: $36.35
The average for prices 13 through 17 is: $36.45
The average for prices 14 through 18 is: $37.05
The average for prices 15 through 19 is: $37.20
The average for prices 16 through 20 is: $37.80
Please press any key to continue
================================================== ========
Report 4 Apr 12 2004
********


The number of days in which the moving average of stock1 exceeded the moving ave
rage of stock2: 10 days


The number of days in which the moving average of stock2 exceeded the moving ave
rage of stock1: 6 days


The moving average of stock1 equals the moving average of stock2: 0 days

Please press any key to continue
================================================== ========
Last edited by dsmith : 12-Apr-2004 at 17:30. Reason: Use [c] & [/c] for syntax highlighting (not [code])
  #2  
Old 12-Apr-2004, 20:17
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
CPP / C++ / C Code:
int movAvgSame(float movAvg1[],float movAvg2[],int size)
{
  int index;

  int days;
  days=0;

  for (index = 0;index < size;index++)
  
  if (movAvg1[index] == movAvg2[index])
  {
    days++;
  }
  
  return days;
}

in this function, youre actually comparing movAvg1 and movAvg2 according to their index, that means youre comparing moveAvg1[0] with moveAvg2[0], but not comparing moveAvg1[0] with moveAvg2[1] and so on. That's why you got 0 for

"The moving average of stock1 equals the moving average of stock2: 0 days"

Although I don't get what you are doing, I sense that this maybe the problem and I suggest you to use two for loop to compare all the item.
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #3  
Old 12-Apr-2004, 20:36
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Thank Max. I used two for loops and I got a 1 for the answer, which is correct. I appreciate your help. What I have done is calculated a 16 five-day moving average on stock1 and stock2. I have 20 prices for each stock. Once I calculated the 16 five-day moving averages, I put these in an array named movingAvg1 and movingAvg2. So, with the 2 for loops, I am comparing the index of movingAvg1 with the index of movingAvg2. Thanks again.....
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Modify a C program ayoub C Programming Language 3 15-Mar-2004 11:34
error during program rjd72285 C++ Forum 0 11-Nov-2003 18:49
one program access another? dgoulston C++ Forum 1 07-Oct-2003 11:26
Attention Resellers! Check this program out! Yippee Web Hosting Advertisements & Offers 0 22-Jul-2003 09:05
Attention Resellers! Check this program out! Yippee Web Hosting Advertisements & Offers 1 12-Jul-2003 18:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.