Thread: not so much fun with pointers

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    6

    not so much fun with pointers

    I'm having a hard time understanding how to use pointers. In my code below I an computing the compounded quarterly interest (5.3%) on a given savings account balance. I can get the first quarter values but am stuck on the second and subsequent values . Yes, homework.



    Code:
    /* This program will read a given savings account balance input by the user and calculate
    quarterly interest. It will print quarterly and yearly interest earned, beginning and ending savings balances
    in columned format with appropriate headings.
    Written by Patricia Stephens
    Date: June 2009 
    */
    
    
    #include "stdafx.h"
    #define interest_rate .053
    
    //Function Declarations
    void getData(float* beg_balance);
    float multiply(float beg_balance);
    float add(float beg_balance, float earned_interest);
    void printRes(float earned_interest,float new_balance);
    
    
    int main(void)
    
    {
    
    // Local Declarations
    float beg_balance;
    float earned_interest;
    float new_balance;
    
    
    
    
    		
    // Statements
    	getData(&beg_balance);	
    	earned_interest = multiply(beg_balance);
    	new_balance = add(beg_balance,earned_interest);
    	printRes(earned_interest, new_balance);
    	return 0;
    } // main
    
    /* ===================================getData=============================
    This function reads the savings balance input by the user at the keyboard
    Pre parameter save balance is an address
    Post Data read into parameter address
    */
    
    void getData(float* beg_balance)
    
    {
    
    		//statements
    	
    	printf("Please Enter your savings account balance: \n");
    	scanf_s("%f", beg_balance);
    	
    	return ;
    
    } //getData
    
    
    /*====================================multiply=============================================
    this function will calculate the quarterly interest on a given savings amount
    pre float beg_balance sav_balance & earned_interest are variables where values to be calculated will be stored
    post value in end_balance will be stored
    */
    
    float multiply(float beg_balance)
    
    {
    //local declarations 
    
    float earned_interest;
    
    
    
    //statements
    
    earned_interest = (beg_balance * interest_rate);
    
    
    //printf("**multiplyYou earned $%8.2f this month\n", earned_interest);
    //printf("**multiplyYou earned $%8.2f for the second quarter\n", second_quarter_interest);
    
    return earned_interest;
    
    } //multiply
    
    /* ==================================add=============================================
    this function will add beg_balance to earned interest and print results
    pre interest_earned and sav_balance contain variables
    post print new balance
    */
    float add (float beg_balance, float earned_interest)
    {
    //local definitions
    
    float new_balance;
    
    
    //Statements
    new_balance = beg_balance + earned_interest;
    //printf("**addYOur new balance is $%8.3f\n", new_balance);
    return new_balance;
    }
    
    /* =================================printRes=======================================
    Print Savings Balance
    Pre beg_balance contains number to be printed
    Post value in sav_balance printed*/
    
    void printRes(float earned_interest, float new_balance)
    {
    	//Statements
    	printf("\t\t\Interest Earned\t\t\t\t New Balance\n");
    	printf("First Quarter: $%8.2f\t\t\t\t$%8.2f\n", earned_interest, new_balance);
    
    	//printf("**printYou earned $%8.2f\n in interest this month\n", earned_interest);
    	//printf("**printYour new balance is $%8.2f\n", new_balance);
    	//printf("**printYour second quarter earnings are $%8.2f\n", second_quarter_interest);
    	return;
    } //printRes

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'm not sure what this problem has to do with pointers. It seems like your problem is not a C problem, but more of a math problem. If interest is compounded quarterly, then you need to compute it 4 times per year (so each calculation uses the result of the previous one).

    You should start off by first writing out the equations needed to compute the interest. In other words, how would you solve this with a pen and a piece of paper? Once you have that worked out, you will need to translate that algorithm to code. We can help you with that part (it will involve a loop, but no pointers).

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bithub View Post
    I'm not sure what this problem has to do with pointers.
    Still, it is a great title.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    repeat the following as often as necessary:

    "Pointers are my friend
    I love pointers"
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    6

    some more info on assignment

    I want to be able to use the var(s) new_balance and _earned_interest to print the results of each month's calculation.

    Haven't gotten to loops yet. I wanted to use pointers to change the above var(s)
    Last edited by beone2; 06-08-2009 at 05:37 PM. Reason: not fully replied

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I want to be able to use the var(s) new_balance and _earned_interest to print the results of each month's calculation.
    Aren't you already doing that?

    Haven't gotten to loops yet. I wanted to use pointers to change the above var(s)
    Pointers don't work that way. At any rate, what sort of class teaches you about pointers before loops?

    If you can't use loops, you could always duplicate code:
    Code:
    // compute first quarter
    earned_interest = multiply(beg_balance);
    new_balance = add(beg_balance,earned_interest);
    printRes(earned_interest, new_balance);
    
    // compute second quarter
    earned_interest = multiply(new_balance);
    new_balance = add(new_balance,earned_interest);
    printRes(earned_interest, new_balance);
    
    // compute third quarter
    earned_interest = multiply(new_balance);
    new_balance = add(new_balance,earned_interest);
    printRes(earned_interest, new_balance);
    
    // compute fourth quarter
    earned_interest = multiply(new_balance);
    new_balance = add(new_balance,earned_interest);
    printRes(earned_interest, new_balance);
    Now the only problem with the code is that it computes the wrong new balance. This is due to a problem in the computation algorithm (When interest is compounded quarterly, you can't use the yearly interest rate each quarter).

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    some more info on "not fully replied"

    Quote Originally Posted by beone2 View Post
    I want to be able to use the var(s) new_balance and _earned_interest to print the results of each month's calculation.

    Haven't gotten to loops yet. I wanted to use pointers to change the above var(s)
    You are a disorganized and incoherent mess. The truth is, you will have to learn to use (eg) pointers (and maybe loops) *before* you apply them to your assignment.

    So step back from it and try to come up with a short, simple program that accomplishes something parallel to your goal (in this case, "using pointers to change variables"). At the very least, you will be able to clarify your intentions and ask more specific questions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by beone2 View Post
    Haven't gotten to loops yet.
    Probably a good time to study them.

    Might want to make sure you have a good C book handy, too.

    Quote Originally Posted by bithub View Post
    At any rate, what sort of class teaches you about pointers before loops?
    Universities and colleges must be getting even worse in their teaching of C, and programming in general.

    Quote Originally Posted by MK27 View Post
    You are a disorganized and incoherent mess. The truth is, you will have to learn to use (eg) pointers (and maybe loops) *before* you apply them to your assignment.

    So step back from it and try to come up with a short, simple program that accomplishes something parallel to your goal (in this case, "using pointers to change variables"). At the very least, you will be able to clarify your intentions and ask more specific questions.
    I see you've taken over pizza night. Remember to get some that are simply plain cheese.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM
  4. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM