Addresses and pointers help...

This is a discussion on Addresses and pointers help... within the C Programming forums, part of the General Programming Boards category; I have a questions here from my programming class that is completely throwing me in circles. I don't even understand ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    133

    Addresses and pointers help...

    I have a questions here from my programming class that is completely throwing me in circles. I don't even understand what it's asking if anyone can help I'd be very great full!

    Write a C function named liquid() that is to accept an integer number and the addresses of the variables gallons, quarts, pints, and cups. The passed integers represents the total number of cups, and the 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 function. use the relationship of 2 cups to a pint, 4 cups to a quart, and 16 cups to a gallon.

    P.S. I know you guys hate it when people post questions on here without any attempted code but usually I do; this problem though is just completely out there for me. I really don't understand what it's even asking or how to pass the address.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You should know how to pass the address of a variable to a function if you've ever used scanf().

    Code:
    int x;
    ...
    scanf("%d",&x);
    Note that when you put a '&' in front of a variable, you're taking the address of that variable, not its value. This means that scanf(), or any other function, now has the address of where x is at in memory and can write to its location. This is how the variable x gets assigned a value from scanf().

    This question is basicaly asking you to write a function where you accept a number and the addresses of three variables. Using the first number, you'll be adjusting the values that the three addresses point to. This means that when your function is called, the values of the variables given to it will be changed by your function to their correct values.

    The math you should be using to adjust everything is given inside the question. First make sure you understand the question: Given a certain amount of cups of a liquid, how many pints, quarts, and gallons does that come out to be (using the least amounts of units)?

    This is similar to the "make change" type of questions where you're given an amount of money in cents or dollars or some other currency and asked to make change for a purchase.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you have a function and you pass it an argument like below, the function gets a copy of the original variable. Whatever you do with this variable inside the function, won't do anything to the variable that you passed into the function (from main() here).
    Code:
    void foo(int n)
    {
        n = 100;
    }
    
    int main(void)
    {
        int n;
        n = 42;
        foo(n);
        assert(n == 42); /*that's true*/
    There are however times, such as in your assignment, where you want to be able to modify the value of the passed variable. This you can achieve if you don't pass a copy of the original variable to the function, but the address of the variable. To receive it in the function you need to store the address in something called a pointer.

    The rest.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    could someone possible give me an example as to how to start this program. i.e. the first function... Thanks

    So does it want me to find this?

    How many quarts, pints, then cups are in a gallon?
    How many pints, then cups are in a quart?
    and how many cups are in a pint?

    lol I don't know if someone has the time could the give me and example I'm new to this stuff and this question is just confusing like you said it's all about understanding the question...
    Last edited by GCNDoug; 04-06-2007 at 06:57 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you wanted the function foo() to actually change n (for example increment it by 1), you could write it as
    Code:
    void foo(int *np) {
      (*np)++;
    }
    
    int main(void) {
      int n = 42;
      foo(&n);
      printf("n = %d\n", n); // prints 43
      return 0;
    }
    Here foo() takes as its argument the address of n, and uses it to increment n itself.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by GCNDoug View Post
    could someone possible give me an example as to how to start this program. i.e. the first function... Thanks

    So does it want me to find this?

    How many quarts, pints, then cups are in a gallon?
    How many pints, then cups are in a quart?
    and how many cups are in a pint?

    lol I don't know if someone has the time could the give me and example I'm new to this stuff and this question is just confusing like you said it's all about understanding the question...
    Code:
    while you don't understand
    	Pause for a minute.
    	Read the assignment.
    	Read the posts here.
    end while
    start work on assignment.
    They tell you the relationship between cups, pints, gallons, and whatever else. That's already done for you.

    If I purchase something from you and then I need $18.52 in USD back in change, how would you break it down and give me the change? Something perhaps like this:

    • 1 $10 bill.
    • 1 $5 bill.
    • 3 $1 bills.
    • 2 quarters.
    • 2 pennies


    You can do that because you know the relationship of the different units. Same with this assignment of yours. If I say I have X cups of any liquid, how many pints, quarts, gallons, and cups does that make? Think of it how you could break money down to make change or anything else similar to it.

    And btw, I made a mistake in my last post. You need 4 addresses instead of 3.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Ok so if I had,

    5 gallons
    2 quarts
    3 pints
    and 5 cups

    The question is asking for the total number of cups (90) then by using the addresses it wants me to state the number of of each liquid measurement again and do it using the addresses instead of making new variables?

    or

    when I print the output would it want me to change it to

    5 gallons
    3 quarts
    3 cups

    this would reduce it more efficiently.
    Last edited by GCNDoug; 04-06-2007 at 07:34 PM.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Your input will only be the amount of liquid in the amount of cups.

    You need to set the number of gallons, quarts, pints, and cups that number comes out to.

    This means, given 29 cups.... Break it down:

    Gallons = ?
    Quarts = ?
    Pints = ?
    Cups = ?

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    1 gallon
    3 quarts
    1 cup

    Now how would I go about starting the code if you don't mind. Should I do it all in one function, for instance do I pass all the address to a certain function and than do the calculation in there?

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    See my sig?

    Read the assignment again with your new knowledge. You'll see that it asks you to make a function called liquid() that accepts the addresses of variables that represent the final answer. All you have to do inside this function, is alter the values that each address points to. So if you receive 29 as the amount of liquid in cups, you would change the value that the variable that represents gallons points to, to 1, the quarts to 3, pints to 0, and the cups to 1.

    I would do it all inside the liquid() function, without calling other functions (since I think it's simple enough to merit only one function).

    Since, hopefully by now you understand the question, I would start working on this:

    1) Work out with a function prototype. What should liquid() receive and what should it return. Hint: The answer is given inside the assignment description.
    2) Work out manually, perhaps on paper, the logic that you would use to arrive at the answer. Just simply describe each step in English or some other equivalent natural language.
    3) Work on how to transfer the result of #2 to C.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    Code:
     
    
    #include <stdio.h>
    #include <conio.h>
     
    int liquid = "0";
    
    int main()
    {
    
    	printf("Please enter the total amount of liquid (in cups): ");
    	scanf("&#37;d", &liquid);
    
    	liquid(int, int, int, int,)
    	int gallons = 0;
    	int quarts = 0;
    	int pints = 0;
    	int cups = 0 ;
    
    
    liquid(int *cups, int *pint, int *quart, int *gallon)
    
    {
    	*gallon = cups >> 4
    }
    
    
    _getch();
    
    return 0;
    
    }
    This is what I've started with, my friend said I should use something called a bit shift or something I don't know we haven't learned it yet. Lol am I working in the right direction?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The function liquid(), if I read the assignment correctly, is supposed to take FIVE arguments. Four are the ones you're using now, and the fifth (which I would make the first argument) is the input number of cups. Since that argument is the input, you can just pass it in the normal way (not via pointer as with the others). BTW, you're using "liquid" now for both a variable and a function, which you shouldn't do. It probably won't compile.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    133
    This is little better?


    Code:
    #include <stdio.h>
    #include <conio.h>
     
    int liquid = "0";
    
    int main()
    {
    
    	printf("Please enter the total amount of liquid (in cups): ");
    	scanf("%d", &liquid);
    
    	total(int liquid, int, int, int, int,);
    	int gallon = 0;
    	int quarts = 0;
    	int pints = 0;
    	int cups = 0 ;
    
    
    liquid(int liquid int *cups, int *pint, int *quart, int *gallon);
    
    {
    	*gallon = cups >> 4;
    }
    
    
    _getch();
    
    return 0;
    
    }
    where would I go from here?

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    No, you're not working in the right direction.

    Have you ever written a C program in your life? That doesn't even compile, and it does nothing close to what you want it to do.

    If this course of yours that you're taking is near the end, you're going to fail it unless you get serious about it. Don't waste money on a college education if it's of no importance to you. A number of very talented people on these forums could do your assignment for you, including someone as untalented as myself, but even if we do such a task, it does nothing to help you learn, which is the point of the assignment.

    Now let's take a look at some of what you're doing:

    Code:
    #include <conio.h>
    This is not portable since it is not a part of the C standard, and therefore never guarenteed to work the way you expect it to on all systems. If that is not a concern of yours (ie. your professor says you must use it for some reason), then ignore this.

    Code:
    int liquid = "0";
    You're defining an integer named liquid and assigning it what value? 0? If you're assigning it 0, don't put quotes around it. Quotes are for string constants. This will most likely cause the compiler to cry for help.

    Code:
    liquid(int, int, int, int,)
    No idea what you're trying to do here. Is this a function call? A function prototype? No matter what it is, it's wrong.

    Code:
    liquid(int *cups, int *pint, int *quart, int *gallon)
    
    {
    *gallon = cups >> 4
    }
    First of all, you left this blob of a function inside main(). Secondly, it's not defined correctly per your assignment. It's supposed to take one integer and the addresses of 4 integers.

    I'm not sure what the logic inside the function is. Your friend, who is probably also failing the course, suggests bit shifting, so in your haste to implement it, you manage to take the address that the variable cups points to, shift it over by 4, and then assign that to the value of the variable that gallons points to.

    Needless to say, that is pointless.

    Code:
    _getch();
    I imagine this is supposed to be from conio.h. See my above comment on conio.h. A more portable version would be something like getchar() I believe.

    I know I'm being relatively rough here, and that's because I don't believe you gave an honest effort at all, but you just want people to do your homework for you, so you slapped together some garbage to look like C.

    If this is not the case, and you've been working quite hard throughout the entire time you've taken the course, then you either have to work harder, or work on something else. If I were you, I'd rush off to go see the professor or someone else that can help you. In addition, you need to keep writing programs in order to get better. You can't just rely on your memory to learn C. You need experience.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. char pointers and their addresses.
    By ens_leader in forum C Programming
    Replies: 5
    Last Post: 12-31-2007, 12:48 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Pointers, addresses, and new
    By WarBaboon in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2003, 10:12 PM
  5. Pointers and their Addresses
    By incognito in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2001, 06:16 PM

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