Thread: function not sending back values

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    7

    function not sending back values

    Hey guys, I am trying to get all these functions work together and send the value of countX and countY back into main from function2() and function3() to be used by function4() later on.. But I keep getting 0 printed out and I am not quite sure why.. could anyone supply some ideas of how to fix this?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    #define maxrow 20  //defines maxrow as a constant of 20
    #define maxcol 30  //defines maxcol as a constant of 30
    
    
    void function1(char array[][maxcol]);
    int function2(char array[][maxcol]);
    int function3(char array[][maxcol]);
    void function4(int, int); //function to display the pairs count
    
    int main( void )
    { 
    
    int countX = 0;
    int countY = 0;
    srandom( (unsigned) time(NULL) );
    char array[maxrow][maxcol];
    
    function1(array);
    
    function2(array);
    
    function3(array);
    
    function4 (countX, countY);
    
        return ( 0 ) ;
    
    } 
    
    
    void function1(char array[][maxcol])
    	{
    	int x = 0;
    	int y = 0;
    
    	 for (x=0;x<maxrow;x++)
    	    {
        		 for (y=0;y<maxcol;y++)
           		    {
            		 array[x][y] = random() % 26 + 'A';
            		 printf("%c ", array[x][y]);
           		    }
       		 printf("\n");
       	    }
    	printf("\n");
    	}
    int function2(char array[][maxcol])
    	{
    	int col = 0;
    	int row = 0;
    	int countX = 0;
    
    	for (row=0;row<maxrow-1;row++)
            	{
             	for (col=0;col<maxcol; col++)
                       {
                    	 if (array[row][col] == array[row+1][col])
                      	   {
                        	    countX++;
                       	   }
                   	   }
           		 }
    	return(countX);
    	}
    int function3(char array[][maxcol])
    	{
    	int col = 0;
    	int row = 0;
    	int countY = 0;
    
    	for(col=0;col<maxcol-1;col++)
            {
             for (row=0;row<maxrow;row++)
                    {
                     if (array[row][col] == array[row][col+1])
                       {
                        countY++;
                       }
                    }
            }
    	return(countY);
    	}
    void function4(int countX, int countY)
    	{
    	printf("\nNumber of horizontal pairs: %d\n", countX);
    	printf("\nNumber of vertical pairs: %d\n\n", countY);
    	
    	}

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    You're not assigning the returned values to anything; e.g. you probably want countx = function2(array);

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no relationship between the variable countY in function3() and the variable countY in main(). They might have the same name, but are distinct variables (in a different scope or context).

    The statement "return(countY);" inside function3() causes the function to return the value of the countY inside function3(). It does not magically place that value into the variable named countY in main(), which is what you seem to be expecting.

    If you want the variable countX in main() to get the value countY returned by function3(), you will do
    Code:
        countX = function3(array);
    Note that this does not affect the variable countY that is defined in main() in any way. It only affects the variable countX.

    Similarly, if you want the value of countY returned by function3() to be stored in main()'s countY, you need to do
    Code:
        countY = function3(array);
    Unrelated to your problem, but the expression
    Code:
    array[x][y] = random() % 26 + 'A';
    is not guaranteed to store an uppercase letter (A-Z) in array[x][y]. Your code assumes a character set (such as ASCII) in which letters are contiguous. There are real-world compilers that support different character sets.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending values to text box in form
    By drkidd22 in forum C# Programming
    Replies: 13
    Last Post: 01-06-2011, 09:14 AM
  2. Sending Values and Scanning an Array
    By xxxixpats in forum C Programming
    Replies: 7
    Last Post: 11-05-2010, 11:32 PM
  3. getting values off the stack back to c++
    By Anddos in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2009, 06:48 AM
  4. Sending a two values to main from a function?
    By Zeratulsdomain in forum C++ Programming
    Replies: 15
    Last Post: 12-05-2005, 03:38 PM
  5. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM