Thread: Quick Understanding Question

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

    Quick Understanding Question

    I am given the following function that I am supposed to write.


    getaverage() - receives the sales array and the address of total and average as arguments. Use pointer syntax. It gets the total and average values from the sales array. It does NOT display the total and average. The total and average are displayed from the main() function in the final lines of code.
    My question (kinda dumb but I need a second opinion) is, in the function can I use printf to show the Average and Total and then put the function in main(), or is that exactly what I am not supposed to do? If so do I need to return the value to main() and then use printf()?

    Thanks!!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I read it as saying, "Do not use any sort of printf or other output function within getaverage. Print these things in main."

    And I don't think you're supposed to use a return value either, since it explicitly states that you pass these parameters to be updated within the function. (And a function can't return more than one value.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    It does NOT display the total and average.
    I think that makes it pretty clear. getaverage() can't print the total and average. This is probably an exercise in using pointer arguments to allow multiple "return" values, so you can return one and assign another to a pointer argument or return nothing and assign both to the corresponding pointer arguments.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    That's exactly what you're not supposed to do. You're supposed to let getaverage() assign values to total and average, and that's all. You pass addresses to them because that's the way to make it possible for the function to change their values.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    How about this code, I just got it to work and I think that it follows the directions?



    Code:
    Code:
    	
    	}
    while (choice != 'N');	
    	report(name, totsales);
    	
    	getaverage(totsales, &tot, &avg); 
    	
    	printf("\nTotal:%.2lf", tot);
    	printf("\nAverage:%.2lf\n", avg);
    
    return 0;
    }
    /*======================================================	*/
    void getaverage(double *sales, double *num1, double *num2)
    {
    	*num1 = *sales;
    	*num2 = (*sales / 5);
    	
    
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You're definitely in the right ballpark, but I'd imagine you'd need a loop to calculate sum and average.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Yes I must because I just ran the program a bunch of times and it is only using the first input for the total and average. Loop...hmmm. I will be back.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    ok how about this one. it seems to be working right.

    Code:
    int main(void )
    {
    	
    	
    	
    	char name[][100] = {"Larry Lister", "Sue Sales  ", "Eva Escrow  ",
    		"Morley Money", "Pete Profit"};
    	double totsales[6] = {0};
    	
    	double sale, tot, avg;
    	int code, choice;
    do
    	{
    	printf("\n\t\tRocklin Realty\n\n\n");
    	
    	code = getcode( );
    	sale = getsales( );
    	totsales[ code - 1 ] += sale;
    	choice = getyn("\nAnother");
    	
    	}
    while (choice != 'N');	
    	report(name, totsales);
    	
    	getaverage(totsales, &tot, &avg); 
    	
    	printf("\nTotal=\t\t\t\t%22.2lf", tot);
    	printf("\nAverage=\t\t\t%22.2lf\n", avg);
    
    return 0;
    }
    /*======================================================	*/
    void getaverage(double *sales, double *num1, double *num2)
    {
    	int i;
    
     for (i = 0; i < 5; i++)
    	{
    	*num1 += *(sales + i);
    	*num2 += (*(sales + i) / 5);
    	}
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You can write *(sales + i) as sales[i].

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by robatino View Post
    You can write *(sales + i) as sales[i].
    What is the benefit of doing that?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > What is the benefit of doing that?
    Well, that's what sales[i] means, and it's shorter. It's more readable since it's what most people expect to see. Normally, you don't see people writing an explicit dereference like that unless they're writing the loop using a pointer instead of an index.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Oh I really didnt know. But I think that I am supposed to use pointers. See the very first post. Does it seem to follow those directions??

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    leopardforest@g: Calculate the average after the loop; outside of it.
    robatino: Read the assignment.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    I am not challenging you. But I dont understand why I have to do that? What tells you that you have to calculate it outside of the loop? Am I missing something?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > robatino: Read the assignment.
    Forgot about that. But it's possible that what the instructor meant was for the loop to use a pointer, and not an int, for the variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM