Need to use decimals in program float or double?

Page 1 of 1

4 Replies - 5213 Views - Last Post: 06 October 2008 - 08:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=66389&amp;s=5541fbf042429d6a2f71a4db9ab1bf2c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 ability

Reputation: 2
• 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

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• 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.

#3 ability

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

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

Posted 06 October 2008 - 07:53 AM

AmitTheInfinity, 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.

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!

#5 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• 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.

#6 red_4900

• Code T(h)inkers

Reputation: 21
• 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.