float to int 2

This is a discussion on float to int 2 within the C Programming forums, part of the General Programming Boards category; Having trouble getting the right output out of this assignment. The assignment is: Write a C function named licquid() that ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Question 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.

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    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;
    }
    Last edited by Deckard; 08-14-2005 at 05:35 PM. Reason: Bonus hint
    Jason Deckard

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    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.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Smile 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!

  5. #5
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    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).
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 11:28 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21