1

I need some help with my assignment. I need to create an array amounts to store five elements and then two arrays of five elements with the names dollars and cents. My problem is that I cant understand how to store the whole number part of each value in the amounts array in the corresponding element of dollars and the fractional part of the amount as a two-digit integer in cents(like if I input 2.75 - store 2 in dollars array and 75 in cents array).Any suggestions on how to do this would be appreciated! Thanks

This is what I have for now:

void main()
{
float amounts[5];
long dollars[5];
long cents[5];
int i = 0;

printf("Enter five monetary values separated by spaces:\n");
for(i = 0; i<5 ; i++)
scanf("%f", &amounts[i]);

for (i = 0; i<5; i++){

printf ("\ni=[%d],  dollars: %.2f, cents: %.2f\n", i, dollars, cents);
}
printf("\nYou entered the values: \n");

for(i = 0; i<5 ; i++)
printf("$%.2f\n", amounts[i]);
printf("\n");

}
1
  • 2
    You are missing the function that converts amounts[i] to dollars and `cents'. How would you determine the whole number of a floating point number? When you have the whole number, how can you determine the fractional portion? Hope this helps. Commented Dec 2, 2013 at 1:34

1 Answer 1

1

dollars[i] = (long)(amounts[i]) will truncate it, that is, remove the fractional part.

cents[i] = (long)((amounts[i] - dollars[i]) * 100); will give you the fractional part as an integer.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.