Thread: return statement

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    40

    return statement

    Hi,

    Need little help on my homework assignment. Let's say I have the information on three stocks which include stock name, buying price, current price, number of shares bought and and yearly fees.

    I am supposed to calculate the following:

    1. initial cost which is number of share * buying price
    2. current cost, which is the number of shares * current price
    3. profit, which is current_cost - initial_cost - yearly_fees
    4. total profit = total + profit.

    I also need have the program figure out how many of the three stocks are generating positive profit, negative profit or break even and print out the number of each.

    Another requirement is to write with appropriate functions and paramenter passing (the reference/value stuff) and to use one void function and one function with return statement.
    -------------------

    I have completed assignment using only void functions, but wondering how to incorporate the return statement deal?

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Are you working with structures? If so do you know how to access the struct members? Each structure you create can represent each stock ticker you are working with.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    I haven't learned structures in class yet, therefore can't use it. Though as far as I understand from my book, structures make great for writing code to make databases.

    But what can I do? Still need help figuring out a function that involves using return statement.
    Last edited by x2x3i5x; 03-29-2010 at 02:00 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by x2x3i5x View Post
    I haven't learned structures in class yet, therefore can't use it. Though as far as I understand from my book, structures make great for writing code to make databases.

    But what can I do? Still need to incorporate that return statement.
    (1) In your own terms, can you explain what the return statement does?
    (2) Could we see your code for the function that calculates profit, f.ex.?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Here's my entire program code. Return statement basically feeds out a value to the main from the function so that main can use it. I attempted doing return *profit, but obviously that's just a wasted line of code as it does nothing. So here I am. Yeah, in the main I could've used a loop, but loops are saved for a later assignment so it's not implemented here.


    Code:
    #include <stdio.h>
    
    void input(char *stock_name, char *purchase_date, int *num_shares, float *buying_price, float *current_price, float *yearly_fees)
    {
    	printf("Enter stock name: ");
    	gets(stock_name);
    	
    	printf("Enter purchase date: ");
    	gets(purchase_date);
    
    	printf("Enter number of shares: ");
    	scanf("%d", &(*num_shares));
    
    	printf("Enter buying price per share: ");
    	scanf("%f", &(*buying_price));
    
    	printf("Enter current price per share: ");
    	scanf("%f", &(*current_price));
    
    	printf("Enter yearly fees: ");
    	scanf("%f", &(*yearly_fees));
    
    	fflush(stdin);
    }
    
    float calcProfit(float buying_price, float current_price, float *yearly_fees, float *initial_cost, int *num_shares, float *current_cost, float *profit, float *total)
    {
    	*initial_cost = *num_shares * buying_price;
    	*current_cost = *num_shares * current_price;
    	*profit = *current_cost - *initial_cost - *yearly_fees;
    	*total = *total + *profit;
    
    	return *profit;
    }
    
    void calcProfit2(float profit, int *pos_profit, int *neg_profit, int *even)
    {
    	if (profit>0)
    		(*pos_profit)++;
    	else if (profit<0)
    		(*neg_profit)++;
    	else 
    		(*even)++;
    }
    
    void print1(char *stock_name, char *purchase_date, float initial_cost, float current_cost) 
    {
    	printf("\nStock Name: %s\n", stock_name);
    	printf("Purchase Date: %s\n", purchase_date);
    	printf("Initial Cost: $%0.2f\n", initial_cost);
    	printf("Current Cost: $%0.2f\n", current_cost);
    }
    
    void print2(float total, int pos_profit, int neg_profit, int even)
    {
    	printf("Total Profit: $%0.2f\n", total);
    	printf("Number of stocks with positive profit: %d\n", pos_profit);
    	printf("Number of stocks with negative profit: %d\n", neg_profit);
    	printf("Number of stocks broke even: %d\n", even);
     }
    
    void main()
    {
    	char stock_name[9], purchase_date[9];
    	int num_shares, pos_profit=0, neg_profit=0, even=0;
    	float buying_price, current_price, yearly_fees, initial_cost=0, current_cost=0, profit=0, total=0;
    
    	input(stock_name, purchase_date, &num_shares, &buying_price, &current_price, &yearly_fees);
    	calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, &current_cost, &profit, &total);
    	
    	calcProfit2(profit, &pos_profit, &neg_profit, &even);
    	print1(stock_name, purchase_date, initial_cost, current_cost);
    	printf("Profit: $%0.2f\n\n", profit);
    	
    	input(stock_name, purchase_date, &num_shares, &buying_price, &current_price, &yearly_fees);
    	calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, &current_cost, &profit, &total);
    	calcProfit2(profit, &pos_profit, &neg_profit, &even);
    	print1(stock_name, purchase_date, initial_cost, current_cost, profit);
    
    	
    	input(stock_name, purchase_date, &num_shares, &buying_price, &current_price, &yearly_fees);
    	calcProfit(buying_price, current_price, &yearly_fees, &initial_cost, &num_shares, &current_cost, &profit, &total);
    	calcProfit2(profit, &pos_profit, &neg_profit, &even);
    	print1(stock_name, purchase_date, initial_cost, current_cost, profit);
    
    	print2(total, pos_profit, neg_profit, even);
    }

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Have you covered arrays yet?
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Bo sorry, haven't learned on arrays except for the char arrays, so a way to go for it?
    Last edited by x2x3i5x; 03-29-2010 at 02:24 PM.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I suspect with this assignment, your instructor is setting you up to teach structures - so you will see how nice they are to "package" related data together.

    Back to your current issue - you could have an array for each of your variables, stock_name, purchase_date, etc., etc., etc... That would be how I would do it. With all the data kept in arrays, it makes it easier to iterate through all the data to find, for instance, which stocks are good performers and which are dogs. You can sort, sum, do all kinds of operations if you keep the data around and don't overwrite the previous values each time you get new info from the user.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    ok, but the return statement function?

  11. #11
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by x2x3i5x View Post
    Here's my entire program code. Return statement basically feeds out a value to the main from the function so that main can use it. I attempted doing return *profit, but obviously that's just a wasted line of code as it does nothing.
    More specifically, the return statement returns a value to the calling function for future use. It doesn't have to be main().

    The return *profit; does nothing because you throw away the result. I.e. you do not assign the returned value to a variable. Also, you are saying that the return type for calcProfit() is float but you are returning a pointer to float.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    Quote Originally Posted by msh View Post
    More specifically, the return statement returns a value to the calling function for future use. It doesn't have to be main().

    The return *profit; does nothing because you throw away the result. I.e. you do not assign the returned value to a variable. Also, you are saying that the return type for calcProfit() is float but you are returning a pointer to float.
    I realized that the *profit return is just junk, but how else to fix the problem? Maybe profit is not the right variable to have the result returned back.
    Last edited by x2x3i5x; 03-29-2010 at 02:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM