Trouble with function

This is a discussion on Trouble with function within the C Programming forums, part of the General Programming Boards category; Heres what needs to be accomplished: "Write a function to print the day of the week and number of the ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    48

    Trouble with function

    Heres what needs to be accomplished:
    "Write a function to print the day of the week and number of the week on which the minimum power output occurred. If there are several days with the minimum power output, print the information for each day."
    Heres what I have
    Code:
    /*----------------------------------------
    This function finds minimum output of array and prints
    out the day and week #*/
    void min(int power[NROWS][NCOLS])
    {
    	/*Declare and intialize variables*/
    	int i, j;
    	double min_x;
    
    	/*Determine minimum value in the array.*/
    	min_x=power[NROWS][NCOLS];
    	for(i=1; i<NROWS; i++)
    	{
    		for(j=1; j<NCOLS; j++)
    			if(power[i][j] > min_x)
    				min_x = power[i][j];
    		printf("Minimum Power Output \n Outage: %f \n Week: %d \n Day: %d \n",min_x, i+1, j+1); 
    	}
    }
    Just looking at my code and not worrying about what exactly is in my array, will the right things be printed out and if not how do I fix it. Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It would print something out every time through a row. It would claim that it was always the last day of the week, even if it wasn't.

    "Print every time this minimum occurs" == "You will need a separate for loop to print the minimum, in case there's more than one."

    Edit: Also, you fail to consider the first row or first column of the array.
    Last edited by tabstop; 11-09-2008 at 09:32 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    ??? the conditions im my for loop should be how exactly, maybe like this
    [code]for(min_x = 0; min_x = power[NROWS][NCOLS]; min_x++)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That makes even less sense. But you do need i and j to start at 0, otherwise you will fail to consider the first row or first column of the array.

    Also I forgot to mention that power[NROWS][NCOLS] fails to exist, so your attempt to set min_x to that number at the start is doomed.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    ok so intialize i and j to 0 got it, but even tho im passing that array down from my main function it still doesnt exist, how or why?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The array exists. The array element power[NROWS][NCOLS] does not.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    oh so when i first declare that my min_x = power[NROWS][NCOLS] they should be replaced with 0 so maybe it should look like this at first
    Code:
    min_x=power[0][0];

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    ok so now all that crap is fixed i need to put my printf statement in a loop? what if i just put my print stament under my second for loop what exactly would that outcome be?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Under the second for loop means it would only print one time, regardless of how many times it needs to print. Also the values for i and j would be meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    ok so it needs to be outside both loops and needs to have its own loop is that what you said earlier?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    yea still no clue on what the conditions should be in my for loop? sorry

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why would they be different from before? You still need to go through the entire array.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    well i dont see how this would be any different from what i had if i did this
    Code:
    for(i=1; i<NROWS; i++)
    for(j=1; j<NCOLS; j++)
    printf("Minimum Power Output \n Outage: &#37;f \n Week: %d \n Day: %d \n",min_x, i+1, j+1);
    i mean does this work or is this even what your talking about

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 11:06 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

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