LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2020, 12:41 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
Converting many 1D array to 2D array


Hi,
I am writing a program in which inside recursive function call another function with (array and its size as argument) calls multiple number of times before returning.

For simplicity I am not showing recursive function below instead called from main many times this function Copying1Dto2D(a, 4);.

Converting this array to 2D array using Copying1Dto2D() and accessing Accessing2D()as shown below.
It is working fine as expected.

I declared 2D array Out[5][10] and row_index as global. Which I felt is not that good. Is there is any better way of writing this code?



Code:
int Out[5][10];
int row_index;

void Copying1Dto2D(int A[], int size)
{
    int i;
    for(i=0;i<size;i++)
    {
        Out[row_index][i] =  A[i];
        printf("%d\t", A[i]);
    }
    Out[row_index][i] = NULL;
    row_index++;
    printf("\n");
}

void Accessing2D(void)
{
    int len_rows = sizeof(Out[0])/sizeof(Out[0][0]);
    int i=0,j=0;
    for(i=0;i< len_rows;i++)
    {
        for(j=0;Out[i][j]!=NULL;j++)
        {
            printf("%d\t", Out[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int a[5] = {1,2,3,4};
    int b[6] = {7, 6, 3};
    int c[6] = {2};
    int d[5] = {4, 9};

    Copying1Dto2D(a, 4);
    Copying1Dto2D(b, 3);
    Copying1Dto2D(c, 1);
    Copying1Dto2D(d, 2);
    printf("----------------------------\n");
    Accessing2D();
    
    return 0;
}

Last edited by srinietrx; 05-26-2020 at 02:47 AM.
 
Old 05-26-2020, 01:23 AM   #2
shruggy
Member
 
Registered: Mar 2020
Posts: 994

Rep: Reputation: Disabled
At least, row_index could be declared a local static variable inside Copying1Dto2D if it's accessed only from there.
 
Old 05-26-2020, 07:59 AM   #3
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 305

Rep: Reputation: Disabled
You are using NULL inappropriately. Did your compiler not complain? NULL is designed to assign a pointer to a value that won't blow up the world should the pointer be accessed before initialized to some useful value. I don't know if it's a guarantee but this is probably working for you because it is 0 cast to a void*.
 
Old 06-05-2020, 05:14 PM   #4
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 395

Rep: Reputation: Disabled
A 1D array can be any array you wish, if you index it that way.

2D:
Code:
|0|1|2|3|4|
|0|1|2|3|4|
can be represented as 1D:
Code:
|0|1|2|3|4|0|1|2|3|4|
Only instead of
Code:
array[x][y]
it's
Code:
array[(x*ySize) + y]
(parentheses for clarity, they are not needed)
so, for example, to treat it like
Code:
array[1][3] = 9;
its
Code:
1*5
         | 
|0|1|2|3|4|0|1|2|3|4|
+ 3
         | - - v  
|0|1|2|3|4|0|1|9|3|4|
It's just a sequence of memory, you can map it like that (as long as you are careful and correct about it, with failsafes and all that) or any other way you wish.

P.S.:
You have to really stick to your chosen way of addressing/indexing. So it's best to write functions for that, so it's all consistent.

And not
Code:
array[(y*xSize) + x]
In one place
And
Code:
array[(x*ySize) + y]
in others, etc

Last edited by Geist; 06-05-2020 at 05:17 PM.
 
  


Reply

Tags
array, c program


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
One to Many or Many to Many? baldur2630 Programming 5 04-23-2013 12:09 PM
converting long 1-D array into 2-D array yahoosam Programming 1 12-03-2012 07:28 AM
PHP: Converting a PHP array into an HTML array koosha Programming 4 08-11-2009 08:47 AM
Multi Booting many, many, many, OS's mac_man25 Linux - General 10 10-26-2003 09:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration