Thread: Trouble with function

  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,336
    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 10: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,336
    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,336
    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,336
    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,336
    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,336
    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,336
    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

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, 12:06 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02: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, 07:15 AM