4 Replies - 3360 Views - Last Post: 06 October 2008 - 08:36 AM Rate Topic: -----

#1 ability  Icon User is offline

  • New D.I.C Head

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

Need to use decimals in program float or double?

Posted 06 October 2008 - 06:50 AM

Hello,

I have written a program that will allow a user to input the number of stock prices that they want to enter and then their closing prices. From here the program will give the average price, high and low prices. I can get the program to work with the variables declared as int, but I cannot work with decimals, which I need to and am asking for help on doing this. I have tries various float and double types, but keep getting hung up with ill or non compatible types?

Here is my code


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


void stockPrice ( double *stockArray, int qty );
void average( double *stockArray, int qty);
void high( double stockArray[], int qty);
void low( double stockArray[], int qty);


int main()
{


double * stockArray;
int quantity;

printf("Please tell me how many stock prices that you will input?  :  ");

scanf("%d", &quantity);


stockArray = (double *) malloc( quantity *sizeof( int ) );

stockPrice ( stockArray, quantity );
average( stockArray, quantity);
high ( stockArray, quantity);
low ( stockArray, quantity);
free( (void *) stockArray );

return 0;
}

void stockPrice ( double *stockArray, int qty )
{
int i;

printf( "Enter the stock's closing price now:\n");

for ( i = 0; i < qty; i ++ )
{
printf("Stock Price %d:$ ", i + 1 );
scanf("%d", &stockArray[i] );
}
}


void average( double *stockArray, int qty)
{
int i;
double sum = 0;
double average = 0;

for ( i = 0; i < qty; i++)
	 sum = sum + stockArray[i];

	 average = sum / qty;

	  printf("Average $%5.2d\n", average);

}


void high ( double stockArray[], int qty)
{
int i;
double highest = stockArray[0];
for ( i = 1; i < qty; i++)
{
if( stockArray[i] > highest )
	highest = stockArray[i];
}

printf( "Largest $%d\n", highest );
}


void low ( double stockArray[], int qty)
{
int i;
double lowest = stockArray[0];
for ( i = 1; i < qty; i++)
{
if( stockArray[i] < lowest )
	lowest = stockArray[i];
}

printf( "Largest $%5.2d\n", lowest );
}




Thanks you and help is much appreciated!

JC

Is This A Good Question/Topic? 0
  • +

Replies To: Need to use decimals in program float or double?

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Need to use decimals in program float or double?

Posted 06 October 2008 - 07:15 AM

scanf("%d", &stockArray[i] ); if variable is a double then you shouldn't be using %d to accept the double value from user.

stockArray = (double *) malloc( quantity *sizeof( int ) );
you used a double pointer but allocated memory with size of integer. That can surely create problems.

I hope this will help you. :)
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: Need to use decimals in program float or double?

Posted 06 October 2008 - 07:53 AM

View PostAmitTheInfinity, on 6 Oct, 2008 - 07:15 AM, said:

scanf("%d", &stockArray[i] ); if variable is a double then you shouldn't be using %d to accept the double value from user.

stockArray = (double *) malloc( quantity *sizeof( int ) );
you used a double pointer but allocated memory with size of integer. That can surely create problems.

I hope this will help you. :)





Thanks for your responce, I really do appreciate your kindness. I have made a few changes; these being, scanf now accepts the %f vs %d, and fixing the pointer conflict.

I now get the what seems to be progress; however, I can set the dynamic array at, lets say 9. I am now prompted to input the first stock price and after that, it looks like the loop closes and the rest of the program executes?

Here is my code,


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


void stockPrice ( double *stockArray, double qty );
void average( double *stockArray, double qty);
void high( double stockArray[], double qty);
void low( double stockArray[], double qty);


int main()
{


double * stockArray;
double quantity;

printf("Please tell me how many stock prices that you will input?  :  ");

scanf("%f", &quantity);


stockArray = (double *) malloc( quantity *sizeof( double ) );

stockPrice ( stockArray, quantity );
average( stockArray, quantity);
high ( stockArray, quantity);
low ( stockArray, quantity);
free( (void *) stockArray );

return 0;
}

void stockPrice ( double *stockArray, double qty )
{
int i;

printf( "Enter the stock's closing price now:\n");

for ( i = 0; i < qty; i ++ )
{
printf("Stock Price %d:$ ", i + 1 );
scanf("%f", &stockArray[i] );
}
}


void average( double *stockArray, double qty)
{
int i;
double sum = 0;
double average = 0;

for ( i = 0; i < qty; i++)
	 sum = sum + stockArray[i];

	 average = sum / qty;

	  printf("Average $%5.2f\n", average);

}


void high ( double stockArray[], double qty)
{
int i;
double highest = stockArray[0];
for ( i = 1; i < qty; i++)
{
if( stockArray[i] > highest )
	highest = stockArray[i];
}

printf( "Largest $%5.2f\n", highest );
}


void low ( double stockArray[], double qty)
{
int i;
double lowest = stockArray[0];
for ( i = 1; i < qty; i++)
{
if( stockArray[i] < lowest )
	lowest = stockArray[i];
}

printf( "Largest $%5.2f\n", lowest );
}




Thanks again!
Was This Post Helpful? 1

#5 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Need to use decimals in program float or double?

Posted 06 October 2008 - 08:03 AM

make your quantity variable as integer. That might be a problem.
Was This Post Helpful? 1

#6 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,120
  • Joined: 22-February 08

Re: Need to use decimals in program float or double?

Posted 06 October 2008 - 08:36 AM

Look at your scanf. %f is for float type variable. %lf is for double type variable.

Hope that helps you. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1