Thread: error compiling with program to convert 1D array - 2D array

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    66

    error compiling with program to convert 1D array - 2D array

    Im having issues with my function for converting a 1D array of 36 elements into a 2D array of 6x6 matrix.

    Code:
    #include<stdlib.h>#include<stdio.h>
    #include<unistd.h>
    
    
    #define SIZE 36		// will be used for the 1D array
    #define ROWS 6		// will be used for the 2D array
    #define COLS 6		// will be used for the 2D array
    
    
    // user defined functions prototypes
    void Input_1D_Array (int array_1D[], int size);
    int Convert_1D_to_2D_Array (int array_1D[]);
    void Print_2D_Array (int array_2D[ROWS][COLS], int rows, int cols);
    
    
    int main(void)
    {
    	// setting variables
    	int array_1D[SIZE], array_2D[ROWS][COLS], rows, cols, size;
    
    
    	// setting the value as defined for the following
    	//size = SIZE;
    	//rows = ROWS;
    	//cols = COLS;
    
    
    	// function calls.
    	Input_1D_Array (array_1D, SIZE);
    	Covert_1D_to_2D(array_1D);
    	Print_2D_Array (array_2D, rows, cols);
    
    
    }
    
    
    //  standard logic for user input of int for array
    void Input_1D_Array (int array_1D[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &array_1D[i]);
    }
    
    
    int Convert_1D_to_2D_Array (int array_1D[])
    {
    	int i = 0, j = 0, array_2D[ROWS][COLS];	// set local variables for function
    
    
    	for (i = 0; i < ROWS; i++)	// starting first loop for i (rows)
    	{
    		for (j = 0; j < COLS; j++)	// starting inner loop for j (cols)
    		{
    			array_2D[i][j] = array_1D[j++];	// combining the information from
    						// 1D array into the new 2D array
    		}
    	}
    	return array_2D[i][j];  // returning the new array_2D to the main function
    }
    
    
    void Print_2D_Array (int array_2D[ROWS][COLS], int rows, int cols)
    {
    	// setting local variables
    	int i, j;
    
    
    	
    	printf("The conversion of your 1D matrix into your 6 x 6 matrix is : \n");
    	for (i = 0; i < COLS; i++)
    	for (j = 0; j < ROWS; j++)
    	{
    		printf("%d\t", array_2D[i][j]);	// double looping to printout a 6X6 matrix (2D array)
    		if(j == ROWS-1)
    			printf("\n\n");
    	}
    }
    the output im getting when i try to compile is the following and i have no clue what this means:

    Code:
    ssma-imac:ENG-3211 ssma$ gcc -o convert convert.c Undefined symbols:
      "_Covert_1D_to_2D", referenced from:
          _main in ccadv9ks.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    help is greatly appreciated.

    i think my issues is the bolded section of code above:

    im just not sure how to properly increment that step.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where did you define a function name Covert_1D_to_2D()? Maybe you meant Convert_1D_to_2D_Array()?

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Pass the 2d array as a parameter to the convert function. Currently the convert function uses a local 2d array, rather than one passed to it from main().

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by jimblumberg View Post
    Where did you define a function name Covert_1D_to_2D()? Maybe you meant Convert_1D_to_2D_Array()?

    Jim
    yup, you are right i meant Convert_1D_to_2D_Array(), but still getting compiling error:

    Code:
    Undefined symbols:  "_Covert_1D_to_2D_Array", referenced from:
          _main in ccCHnNSb.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    main() needs a return(0); or a return 0; at the end of it.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You still can't spell Convert properly.

    Covert is the undercover version you can't see
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    the first off, yeah i didnt notice the spelling either! lol

    on top of that:
    C:\Users\crossfire\Desktop\array convert\array convert.c(20): warning #2114: Local 'size' is not referenced.
    C:\Users\crossfire\Desktop\array convert\array convert.c(20): warning #2116: Local 'rows' is used without being initialized.
    C:\Users\crossfire\Desktop\array convert\array convert.c(20): warning #2116: Local 'cols' is used without being initialized.

    you defined them as caps, yet trying to use them as lowercase.

    these are simple compilier errors, you need to turn up your warnings in your compilier

  8. #8
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by rcgldr View Post
    Pass the 2d array as a parameter to the convert function. Currently the convert function uses a local 2d array, rather than one passed to it from main().
    could you please provide an example of what you are talking about. are you saying i need to create a 2D array in main(), if so im not sure how to populate it with the values i need.

    FYI thank you for pointing out my typo... i just didnt see that. i have the conversion semi working as it does take in 36 elements and puts them into a 6x6, but its full of jibberish, not the data i need:

    Code:
    Please enter 36 integer numbers separated by spaces:1 2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    The conversion of your 1D matrix into your 6 x 6 matrix is : 
    1606418432	32767	1606419408	32767	7	0	
    
    
    1606664232	32767	850045863	0	0	0	
    
    
    0	0	-818806770	403145127	0	0	
    
    
    9	0	1606419960	32767	7	0	
    
    
    1606664232	32767	1606416032	32767	1606415968	32767	
    
    
    1606424274	32767	0	0	1606418464	32767

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by lleb View Post
    Are you saying i need to create a 2D array in main()
    You've already created a 2D array in main:

    Code:
    int main(void)
    {
    	// setting variables
    	int array_1D[SIZE], array_2D[ROWS][COLS], rows, cols, size;
    Then change the convert function to take both arrays as parameters:

    Code:
    int Convert_1D_to_2D_Array (int array_2D[][COLS], int array_1D[])

  10. #10
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by rcgldr View Post
    You've already created a 2D array in main:

    Code:
    int main(void)
    {
        // setting variables
        int array_1D[SIZE], array_2D[ROWS][COLS], rows, cols, size;
    Then change the convert function to take both arrays as parameters:

    Code:
    int Convert_1D_to_2D_Array (int array_2D[][COLS], int array_1D[])
    i take it im wrong in doing: [ROWS][COLS] should i just be [][COLS] for the 2D array? also im still getting junk output:

    Code:
    #include<stdlib.h>#include<stdio.h>
    #include<unistd.h>
    
    
    #define SIZE 36		// will be used for the 1D array
    #define ROWS 6		// will be used for the 2D array
    #define COLS 6		// will be used for the 2D array
    
    
    // user defined functions prototypes
    void Input_1D_Array (int array_1D[], int size);
    int Convert_1D_to_2D_Array (int array_1D[], int array_2d[ROWS][COLS]);
    void Print_2D_Array (int array_2D[ROWS][COLS], int rows, int cols);
    
    
    int main(void)
    {
    	// setting variables
    	int array_1D[SIZE], array_2D[ROWS][COLS], rows, cols, size;
    
    
    	// setting the value as defined for the following
    	size = SIZE;
    	rows = ROWS;
    	cols = COLS;
    
    
    	// function calls.
    	Input_1D_Array (array_1D, SIZE);
    	Convert_1D_to_2D_Array (array_1D, array_2D);
    	Print_2D_Array (array_2D, rows, cols);
    
    
    return (0);
    
    
    }
    
    
    //  standard logic for user input of int for array
    void Input_1D_Array (int array_1D[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &array_1D[i]);
    }
    
    
    int Convert_1D_to_2D_Array (int array_1D[], int array_2D[ROWS][COLS])
    {
    	int i = 0, j = 0;	// set local variables for function
    
    
    	for (i = 0; i < ROWS; i++)	// starting first loop for i (rows)
    	{
    		for (j = 0; j < COLS; j++)	// starting inner loop for j (cols)
    		{
    			array_2D[i][j] = array_1D[j++];	// combining the information from
    						// 1D array into the new 2D array
    		}
    	}
    	return array_2D[i][j];  // returning the new array_2D to the main function
    }
    
    
    void Print_2D_Array (int array_2D[ROWS][COLS], int rows, int cols)
    {
    	// setting local variables
    	int i, j;
    
    
    	
    	printf("The conversion of your 1D matrix into your 6 x 6 matrix is : \n");
    	for (i = 0; i < COLS; i++)
    	for (j = 0; j < ROWS; j++)
    	{
    		printf("%d\t", array_2D[i][j]);	// double looping to printout a 6X6 matrix (2D array)
    		if(j == ROWS-1)
    			printf("\n\n");
    	}
    }
    Code:
    Please enter 36 integer numbers separated by spaces:1
    2
    3
    4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    The conversion of your 1D matrix into your 6 x 6 matrix is : 
    1	32767	3	32767	5	0	
    
    
    1	32767	3	0	5	0	
    
    
    1	0	3	994963290	5	0	
    
    
    1	0	3	32767	5	0	
    
    
    1	32767	3	32767	5	32767	
    
    
    1	32767	3	0	5	32767

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Create a text file and use that for you input to save some time debugging this. In the covert function, you're using j to index the 1D array and also to index the columns of the 2D array. You need to use separate variable to do the indexing (like use "k" for the 1D array).

  12. #12
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by rcgldr View Post
    Create a text file and use that for you input to save some time debugging this. In the covert function, you're using j to index the 1D array and also to index the columns of the 2D array. You need to use separate variable to do the indexing (like use "k" for the 1D array).
    i have not learned how to use a text file with C yet. so you are saying because im using i and j for the 2D, i can not use either of those to increment the 1D in my function?

    That was exactly what I needed:

    Code:
    #include<stdlib.h>#include<stdio.h>
    #include<unistd.h>
    
    
    #define SIZE 36		// will be used for the 1D array
    #define ROWS 6		// will be used for the 2D array
    #define COLS 6		// will be used for the 2D array
    
    
    // user defined functions prototypes
    void Input_1D_Array (int array_1D[], int size);
    int Convert_1D_to_2D_Array (int array_1D[], int array_2d[ROWS][COLS]);
    void Print_2D_Array (int array_2D[ROWS][COLS], int rows, int cols);
    
    
    int main(void)
    {
    	// setting variables
    	int array_1D[SIZE], array_2D[ROWS][COLS], rows, cols, size;
    
    
    	// setting the value as defined for the following
    	size = SIZE;
    	rows = ROWS;
    	cols = COLS;
    
    
    	// function calls.
    	Input_1D_Array (array_1D, SIZE);
    	Convert_1D_to_2D_Array (array_1D, array_2D);
    	Print_2D_Array (array_2D, rows, cols);
    
    
    return (0);
    
    
    }
    
    
    //  standard logic for user input of int for array
    void Input_1D_Array (int array_1D[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &array_1D[i]);
    }
    
    
    int Convert_1D_to_2D_Array (int array_1D[], int array_2D[ROWS][COLS])
    {
    	int i = 0, j = 0, k = 0;	// set local variables for function
    
    
    	for (i = 0; i < ROWS; i++)	// starting first loop for i (rows)
    	{
    		for (j = 0; j < COLS; j++)	// starting inner loop for j (cols)
    		{
    			array_2D[i][j] = array_1D[k++];	// combining the information from
    						// 1D array into the new 2D array
    		}
    	}
    	return array_2D[i][j];  // returning the new array_2D to the main function
    }
    
    
    void Print_2D_Array (int array_2D[ROWS][COLS], int rows, int cols)
    {
    	// setting local variables
    	int i, j;
    
    
    	
    	printf("The conversion of your 1D matrix into your 6 x 6 matrix is : \n");
    	for (i = 0; i < COLS; i++)
    	for (j = 0; j < ROWS; j++)
    	{
    		printf("%d\t", array_2D[i][j]);	// double looping to printout a 6X6 matrix (2D array)
    		if(j == ROWS-1)
    			printf("\n\n");
    	}
    }
    Code:
    Please enter 36 integer numbers separated by spaces:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    The conversion of your 1D matrix into your 6 x 6 matrix is : 
    1	2	3	4	5	6	
    
    
    7	8	9	10	11	12	
    
    
    13	14	15	16	17	18	
    
    
    19	20	21	22	23	24	
    
    
    25	26	27	28	29	30	
    
    
    31	32	33	34	35	36
    Last edited by lleb; 07-31-2013 at 07:30 PM.

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The convert function transfers the data from the 1D array to the 2D array, it doesn't need to return anything, so it can be a void function:

    Code:
    void Convert_1D_to_2D_Array (int array_1D[], int array_2D[ROWS][COLS])
    {
    /* ... */
    	return;
    }

  14. #14
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by rcgldr View Post
    The convert function transfers the data from the 1D array to the 2D array, it doesn't need to return anything, so it can be a void function:

    Code:
    void Convert_1D_to_2D_Array (int array_1D[], int array_2D[ROWS][COLS])
    {
    /* ... */
        return;
    }

    ok, does it matter what the return type is in this case?

  15. #15
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by lleb View Post
    ok, does it matter what the return type is in this case?
    void means there is nothing returned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Three-Dimensional Array... Error while compiling
    By adarpodracir in forum C Programming
    Replies: 7
    Last Post: 04-04-2012, 01:21 AM
  2. why a[i] is not compiling . array compilation error
    By nkrao123@gmail. in forum C Programming
    Replies: 17
    Last Post: 09-06-2011, 08:49 AM
  3. Convert int array to char array ?
    By ahmedBanihammad in forum C Programming
    Replies: 6
    Last Post: 11-08-2010, 01:15 PM
  4. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  5. Replies: 24
    Last Post: 11-11-2008, 01:39 PM