filling a multidimensional array with stock symbols and pricing

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2297 Views - Last Post: 29 November 2008 - 11:49 PM Rate Topic: -----

#1 ability  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 06-October 08

filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 05:19 PM

Hello

As we all seem to do, I have been working on a program that uses a multidimensional array so a user can input a stock symbol and the closing priced for each day of the week. Once done, the user will hit the enter key and a report will display that shows the symbol and some statistics of the stock.

I am now to the point that I can get the correct stock information but the symbol of the stock stays the same.

I cannot tell if I am loading it wrong or extracting it wrong. I'm totally stuck!

here is my


/*
JC Buschow
CSC 230
Ch 9 program 2
stock_calculator.c

This program will allow a user to input their stock names and closing values 
from their portfolio. Once inut is complete, the user will get statistics of
 the high low and change for that week. 
*/



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define GOT_ONE	   1
#define NONE_ENTERED  0
#define LGTH		128
#define MAX		  10
#define COLUMNS	   5


					/* Function Prototypes */
int input( char symbol[], float price[][COLUMNS], int portfolio ); 

float high_price( float price[][COLUMNS], int portfolio );
			   
float low_price( float price[][COLUMNS], int portfolio );
 
float net_change( float price[][COLUMNS], int portfolio );

void print_stocks( char symbol[], float stock_rep[][5], char date[], int portfolio );



int main(void)
{
	
	char date_report[LGTH];
	char *date;
	char symbol[LGTH];
	float price[MAX][COLUMNS];
	float stock_rep[MAX][5];
	int portfolio = 0;
	int repeat = 1;
	
	
	printf( "\t\tSTOCK INFORMATION PROGRAM\n" );
	printf( "\t\t----- ----------- -------\n" );
	
	printf( "This program will create a report on the weekly activity\n" );
	printf( "of any stocks that you wish.  You need only enter the stock\n" );
	printf( "name and the closing prices for Monday through Friday.\n" );
	printf( "Press RETURN at the stock name prompt when you are\n" );
	printf( "through.\n\n" );
	printf( "Ending date of the week of this report:  " );
	
	date = fgets( date_report, LGTH, stdin );
   
	
	while (repeat == GOT_ONE && portfolio++ < MAX )
	{
		  repeat = input( symbol, price, portfolio );
		  stock_rep[portfolio-1][0] = high_price( price, portfolio );
		  stock_rep[portfolio-1][1] = low_price( price, portfolio );
		  stock_rep[portfolio-1][2] = price[portfolio-1][COLUMNS-1];
		  stock_rep[portfolio-1][3] = net_change(price, portfolio );
		  
	}
									
	if ( portfolio == MAX )
	{
		 printf( "Program Full!.\n" );
	}
										

	print_stocks( symbol, stock_rep, date, portfolio );
	
	printf( "\n\n\t\t\tEND OF REPORT\n" );
  system("PAUSE");	
  return 0;
}



int input( char symbol[], float price[][COLUMNS],
				  int portfolio )
{
	int column = 0;
	float stock_price;
	char instring[LGTH];
	printf( "\nStock name (Press Return when done):  " );
	
	fgets( instring, LGTH, stdin );
	instring[ strlen( instring )-1 ] = '\0';
	if ( strlen( instring ) > 0 )	
	{
		 strncpy( symbol, instring, LGTH ); 
		 printf( "Monday's close:	" );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 column++;
		 
		 printf( "Tuesday's close:   " );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 column++;
		 
		 printf( "Wednesday's close: " );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 column++;
		 
		 printf( "Thursday's close:  " );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 column++;
		 
		 printf( "Friday's close:	" );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 
		 while (getchar() != '\n');  
		 return( GOT_ONE );
	}
	 else
		 return( NONE_ENTERED );			 
}

float high_price( float price[][COLUMNS], int portfolio )
{
	 int column;
	 int high = 0;
	 
	 for ( column = 0; column < COLUMNS; column++ )		 
		  if ( price[portfolio-1][high] < price[portfolio-1][column] )
			   high = column;			   
	  return price[portfolio-1][high];
}  

float low_price( float price[][COLUMNS], int portfolio )
{
	 int column;
	 int low = 0;
	 
	 for ( column = 0; column < COLUMNS; column++ )	 
		  if ( price[portfolio-1][low] > price[portfolio-1][column] )
			   low = column;
	  return price[portfolio-1][low];		 
}

float net_change( float price[][COLUMNS], int portfolio )
{
	  float change;
	  change = price[portfolio-1][COLUMNS-1] - price[portfolio=1][0];
	  return change;
}
	 
void print_stocks( char symbol[], float stock_rep[][5],
				   char date[], int portfolio )
{


	 int row, column;
	 printf( "\n\n\t\t  STOCK ACTIVITY REPORT\n" );
	 printf( "\t\tWeek Ending Friday %s\n\n", date );
	 printf( "-------------------------------------------------------------\n" );
	 printf( "Stock   |	High	|	Low	 |   Final	|   Net Change\n" );
	 printf( "-------------------------------------------------------------\n" );
	 
	 for (row = 1; row < portfolio; row++)
	 {
		 
		 printf( "%s\t|", symbol );
		 for (column = 0; column < 4; column++)
		 {
		 printf( "%9.3f   |", stock_rep[row][column]);
		 }
	 printf( "\n" );	
	 }
}






Thanks again for the help.

JC

Is This A Good Question/Topic? 0
  • +

Replies To: filling a multidimensional array with stock symbols and pricing

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 05:32 PM

The problem I see is that you only have one char array for symbols. So if you have more then one the stock it gets overwritten.

Here:

 char symbol[LGTH];

//inside function
fgets( instring, LGTH, stdin );
//code
 strncpy( symbol, instring, LGTH );
//overwrites whatever was already in that char array


Was This Post Helpful? 0
  • +
  • -

#3 ability  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 06-October 08

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 05:47 PM

View PostKYA, on 29 Nov, 2008 - 04:32 PM, said:

The problem I see is that you only have one char array for symbols. So if you have more then one the stock it gets overwritten.

Here:

 char symbol[LGTH];

//inside function
fgets( instring, LGTH, stdin );
//code
 strncpy( symbol, instring, LGTH );
//overwrites whatever was already in that char array





Thanks, I under stand that it is being over written, but I cannot vision why?

JC
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 05:54 PM

Let's say my first stock symbol is BCCX

The symbol char array has 'BCCX\0' in it

now when i enter another one say TOT

the char array now has 'TOT\0'

You strcpy the temp char array in the function to the symbol one, thus eliminating any previous entry.
Was This Post Helpful? 0
  • +
  • -

#5 ability  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 06-October 08

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 07:57 PM

View PostKYA, on 29 Nov, 2008 - 04:54 PM, said:

Let's say my first stock symbol is BCCX

The symbol char array has 'BCCX\0' in it

now when i enter another one say TOT

the char array now has 'TOT\0'

You strcpy the temp char array in the function to the symbol one, thus eliminating any previous entry.



I believe that you are trying to say that I need to remove the strncpy, but when I do that I only get so strange symbols that make up my stock symbols?

Not quite sure why

Thanks

JC
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5413
  • View blog
  • Posts: 27,429
  • Joined: 10-May 07

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 07:59 PM

** Renamed topic to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 07:59 PM

No what I'm saying is that you need to revisit how you do the symbols. This is all in C so I guess you can't do C++ strings? If you could I'd make an array of them and increment after each iteration adding the new symbol to the next index. In C you could do the same with a multidimensional array of chars.

The data does not persist to the end of the program which is a problem given the need to relate all the information at the end.
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5413
  • View blog
  • Posts: 27,429
  • Joined: 10-May 07

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 08:04 PM

View Postability, on 29 Nov, 2008 - 08:57 PM, said:

when I do that I only get so strange symbols that make up my stock symbols?

Are you assuring that the array is empty? When an array is declared, it is by the size of the array, & it is very possible (& very common) that it already has data at that designated memory location. Run it through a loop nulling it out. This will assure you that it's empty before you start filling it. If you are still getting "strange" characters in your array, then it would be something in your code is filling it incorrectly.
Was This Post Helpful? 0
  • +
  • -

#9 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 09:24 PM

/* One dimensional array of 20 characters, */
/* or 1 string of at most 19 characters */
char string[20];


/* Two dimensional array of 5 X 20 characters */
/* or 5 strings of a most 19 characters */
char strings[5][20];



You did have this working in a previous version.


Here

Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 09:26 PM

That was a silent change, I wonder what warranted it?
Was This Post Helpful? 0
  • +
  • -

#11 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 09:30 PM

Code beautification?
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 09:37 PM

By stripping out functionality? Terrible tradeoff in my humble opinion.
Was This Post Helpful? 0
  • +
  • -

#13 ability  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 06-October 08

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 10:08 PM

View PostKYA, on 29 Nov, 2008 - 08:37 PM, said:

By stripping out functionality? Terrible tradeoff in my humble opinion.




trial and error? I'm a bit out of control when it comes to arrays! :)

Here give this one a try, but my question is why is it when a 3 digit stock symbol is used the program skips a line, but if fine with a 4 digit stock symbol?


/*
JC Buschow
CSC 230
Ch 9 program 2
stock_calculator.c

This program will allow a user to input their stock names and closing values 
from their portfolio. Once input is complete, the user will get statistics of
 the high low and change for that week. 
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define GOT_ONE 1
#define NONE_ENTERED 0
#define LGTH 10
#define MAX 20
#define COLUMNS 4


int input( char symbol[][LGTH], float price[][COLUMNS],   int num_stock );  /* iput for MD arrays */
				
float high_price( float price[][COLUMNS], int portfolio );
   
float low_price( float price[][COLUMNS], int portfolio );
 
float price_change( float price[][COLUMNS], int portfolio );
 
void print_report( char symbol[][LGTH], float data[][4], char date[], int portfolio );

   
int main(void)
{
	char set_date[LGTH];		   
	char *date;
	char symbol[MAX][LGTH];
	float price[MAX][COLUMNS];
	float data[MAX][4];
	
	int portfolio = 0;
	int index = 0;
	int repeat = 1;
	
	printf( "\t\tSTOCK INFORMATION PROGRAM\n" );  
	printf( "\t\t----- ----------- -------\n" );
	printf( "This program will create a report on the weekly activity\n" );
	printf( "of any stocks that you wish.  You need only enter the stock\n" );
	printf( "name and the closing prices for Monday through Friday.\n" );
	printf( "Press RETURN at the stock name prompt when you are\n" );
	printf( "through.\n\n" );
	printf( "Ending date of the week of this report:  " );
	
	date = fgets( set_date, LGTH, stdin );				/* gets date */

	
	while (repeat == GOT_ONE && ++portfolio < MAX )   /* loop for functions */
	{
		  repeat = input( symbol, price, portfolio );
		  data[portfolio-1][0] = high_price( price, portfolio );
		  data[portfolio-1][1] = low_price( price, portfolio );
		  data[portfolio-1][2] = price[portfolio-1][COLUMNS-1];
		  data[portfolio-1][3] = price_change(price, portfolio );
	
	}
									
	if ( portfolio == MAX )								  /* checks if full */
	{
		 printf( "Program Full " );
		 printf( "\n" );
	}
	
	print_report( symbol, data, date, portfolio );	  /* display results call */
	
	printf( "\n\n\t\tEND OF REPORT\n" );
  system("PAUSE");	
  return 0;
} 

int input( char symbol[][LGTH], float price[][COLUMNS], int portfolio )
{
	int column = 0;
	int index = 0;
	float stock_price;
	char inchar;
	
	printf( "\nStock name (Press Return when done):  " );   
	
	while (inchar != '\n' )	
	{
		 inchar = getchar(); 
		 symbol[portfolio][index] = inchar;
		 index++;
	}
		 if (index > 1)
		 { 
		 printf( "Monday's close:	" );					/* fills array */
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 column++;
		 
		 printf( "Tuesday's close:   " );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 column++;
		 
		 printf( "Wednesday's close: " );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 column++;
		 
		 printf( "Thursday's close:  " );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
		 column++;
		 
		 printf( "Friday's close:	" );
		 scanf( "%f", &stock_price );
		 price[portfolio][column] = stock_price;
			 
		 while (getchar() != '\n');  
		 return( GOT_ONE );
	}
	 else
		 return( NONE_ENTERED );			 
}

float high_price( float price[][COLUMNS], int portfolio )
{
	 int column;
	 int high = 0;
	 
	 for ( column = 1; column < COLUMNS; column++ )		  /* sort for high */
		  if ( price[portfolio-1][high] < price[portfolio-1][column] )
			   high = column;			   
	  return price[portfolio-1][high];
}  

float low_price( float price[][COLUMNS], int portfolio )
{
	 int column;
	 int low = 0;
	 
	 for ( column = 1; column < COLUMNS; column++ )		  /* sort for low */
		  if ( price[portfolio-1][low] > price[portfolio-1][column] )
			   low = column;
	  return price[portfolio-1][low];		 
}

float price_change( float price[][COLUMNS], int portfolio )
{
	  float change;
	  change = price[portfolio-1][COLUMNS-1] - price[portfolio-1][0];
	  return change;
}
	 
void print_report( char symbol[][LGTH], float data[][4], char date[], int portfolio )
{
	 int row, column;
	 printf( "\n\n\t\t  STOCK ACTIVITY REPORT\n" );
	 printf( "\t\tWeek Ending Friday %s\n\n", date );
	 printf( "-------------------------------------------------------------\n" );
	 printf( "Stock   |	High	|	Low	 |   Final	|   Net Change\n" );
	 printf( "-------------------------------------------------------------\n" );
	 for (row = 1; row < portfolio; row++)
	 {
		 for (column = 0; column < 4; column++)
		 printf( "%c", symbol[row][column] );
		 printf( "\t|" );
	 
		 for (column = 0; column < 3; column++)
			 printf( "%9.3f   |", data[row][column] );
			 printf( "%9.3f\n", data[row][3] );
	 } 
}







Please let me know

JC
Was This Post Helpful? 0
  • +
  • -

#14 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 10:10 PM

I doubt that it was intensional.
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: filling a multidimensional array with stock symbols and pricing

Posted 29 November 2008 - 10:18 PM

Because of here : for (column = 0; column < 4; column++)

It's columns are 3 since it has three letters. Probably best to call size or such instead of a numerical constant
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2