-
float to int 2
Having trouble getting the right output out of this assignment.
The assignment is:
Write a C function named licquid() that is to accept an integer number and the addresses of the variables gallons, quarts, pints, and cups. The passed integer represents the total number of cups, and function is to determine the number of gallons, quarts, pints, and cups in the passed value. Using the passed addresses, the function should directly alter the respective variables in the calling funcion. Use the relationships of 2 cups to a pint, 4 cups to a quart, and 16 cups to a gallon.
Include the function in a program, printing the results after the function has run.
If the user enters 27 cups, the output should be:
1 gallon
2 quarts
1 pint
1 cup
This is what I have so far:
Code:
#include <stdio.h>
#include <math.h>
void liquid(int ,int*,int*,int*,int*);
int main()
{
int num1, gallons, quarts, pints, cups;
printf("Enter the number of cups:");
scanf("%2d",&num1);
liquid(num1, &gallons, &quarts, &pints, &cups);
return 0;
}
void liquid(int x, int *gallons, int *quarts, int *pints, int *cups)
{
if (x >= 16)
*gallons = (x / 16;
printf("The number of gallons is %d\n", *gallons);
if (x >= 8)
*quarts = x / 8;
printf("The number of quarts is %d\n", *quarts);
if (x >= 4)
*pints = x / 4;
printf("The number of pints is %d\n", *pints);
if (x < 4)
*cups = x;
printf("The number of cups is %d\n", *cups);
return;
}
My output after entering 27 cups is:
The number of gallons is 1
The number of quarts is 3
The number of pints is 6
The number of cups is 1345
I don't quite understand what i'm doing wrong could somebody please point me in the right direction.
-
Your variables for gallons, quarts, pints, and cups are not always given a value before you print them.
-----
Bonus hint:
Code:
if ( expr )
statement;
statement;
is not the same as
Code:
if ( expr )
{
statement;
statement;
}
-
Thanks Deckard that fixed some of my problems. Now I just need to figure out how to make x a static variable so that it decrements as its passed through the if statements.
-
got it to work
Code:
#include <stdio.h>
#include <math.h>
void liquid(int ,int*,int*,int*,int*);
int main()
{
int num1, gallons, quarts, pints, cups;
printf("Enter the number of cups:");
scanf("%2d",&num1);
liquid(num1, &gallons, &quarts, &pints, &cups);
return 0;
}
void liquid(int x, int *gallons, int *quarts, int *pints, int *cups)
{
static int y;
y = x;
if (y >= 16)
{
*gallons = (y / 16);
printf("The number of gallons is %3d\n", *gallons);
}
if (y - (*gallons * 16) >= 4)
{
*quarts = ((y - (*gallons * 16)) / 4);
printf("The number of quarts is %3d\n", *quarts);
}
if ((y - (*gallons * 16) - (*quarts * 4)) >= 2)
{
*pints = ((y - (*gallons * 16) - (*quarts * 4)) / 2);
printf("The number of pints is %3d\n", *pints);
}
if ((y - (*gallons * 16) - (*quarts * 4) - (*pints *2)) < 2 || y == 0)
{
*cups = (y - (*gallons * 16) - (*quarts * 4) - (*pints *2));
printf("The number of cups is %3d\n", *cups);
}
return;
}
yeah got it to work!
-
I'm glad to see you've got it figured out. For what it's worth, there's nothing wrong with tracking the remaining number of cups with x or cups (no need for y).
Code:
*cups = x;
*cups -= (*gallons = *cups / 16) * 16;
*cups -= (*quarts = *cups / 8) * 8;
*cups -= (*pints = *cups / 4) * 4;
printf("The number of gallons is %d\n", *gallons);
printf("The number of quarts is %d\n", *quarts);
printf("The number of pints is %d\n", *pints);
printf("The number of cups is %d\n", *cups);
You could eliminate x all together and write the user's input to cups if your instructor allowed it. Finally, take a moment to reread how the 'static' keyword effects local variables (you did not need to declare y as static).