Home > database >  How to put int arrays into int 2d matrix in C ?
How to put int arrays into int 2d matrix in C ?

Time:02-21

I wrote this(memory error) when I try to put int arrays into int matrix:

#include <iostream>

using std::cout; using std::endl;

int* intToIntArray(int input, int length) {
    int output[20];
    for (int i = 0; i < length; i  ) {
        output[i] = input % 10;
        input /= 10;
        cout << output[i];
    }
    cout << endl;
    cout << "=====" << endl;
    return output;
}

int main() {
    const int arraySize = 5;
    int a[arraySize] = { 111110, 1111000, 11100000, 110000000, 1000000000 };
    int NumSize[arraySize] = { 6,7,8,9,10 };
    int* arr1D;
    int** arr2D = new int*;

    for (int counter = 0; counter < arraySize; counter  ) {
        cout << a[counter] << endl;
        arr1D = intToIntArray(a[counter], NumSize[counter]);
        arr2D[counter] = arr1D;
    }


    for (int i = 0; i < arraySize; i  ) {
        for (int j = 0; j < NumSize[i]; j  ) {
            cout << arr2D[i][j];
        }
        cout << endl;
    }
}

I know how to put the data from int arrays to a int matrix, but very very inefficient:

#include <iostream>

using std::cout; using std::endl;

int* intToIntArray(int input, int length) {
    int output[20];
    for (int i = length-1; i >= 0; i--) {
        output[i] = input % 10;
        input /= 10;
        cout << output[i];
    }
    cout << endl;
    cout << "=====" << endl;
    return output;
}

int main() {
    const int arraySize = 5;
    int a[arraySize] = { 111110, 1111000, 11100000, 110000000, 1000000000 };
    int NumSize[arraySize] = { 6,7,8,9,10 };
    
    int arr2D[20][20];
    
    for (int i = 0; i < arraySize; i  ) {
        for (int j = 0; j < NumSize[i]; j  ) {
            arr2D[i][j] = intToIntArray(a[i], NumSize[i])[j];
        }
    }

    for (int i = 0; i < arraySize; i  ) {
        for (int j = 0; j < NumSize[i]; j  ) {
            cout << arr2D[i][j];
        }
        cout << endl;
    }
}

How do I do it efficiently? I don't want to use any vectors because I want to code it to my GPU later on. Is there an efficient way to insert the int arrays directly into int matrix?

EDIT: (I kind of did what I wanted to do, I just made it static...)

#include <iostream>

using std::cout; using std::endl;

int* intToIntArray(int input, int length) {
    static int output[20];
    for (int i = 0; i < length; i  ) {
        output[i] = input % 10;
        input /= 10;
    }
    return output;
}

int main() {
    const int arraySize = 5;
    int a[arraySize] = { 111110, 1111000, 11100000, 110000000, 1000000000 };
    int NumSize[arraySize] = { 6,7,8,9,10 };
    int arr2D[20][20];

    for (int counter = 0; counter < arraySize; counter  ) {
        int *arr1D = intToIntArray(a[counter], NumSize[counter]);

        for (int i = 0; i < NumSize[counter]; i  ) {
            arr2D[counter][i] = arr1D[NumSize[counter]-1-i];
        }
    }

    for (int i = 0; i < arraySize; i  ) {
        for (int j = 0; j < NumSize[i]; j  ) {
            cout << arr2D[i][j];
        }
        cout << endl;
    }
}

CodePudding user response:

#include <iostream>
using std::cout; using std::cin;
void intToIntArray(int input, int length, int* &output) {
    output = new int[length];
    for(int i = length - 1; i >= 0; --i) {
        output[i] = input % 10;
        input /= 10;
    }
}
int main() {
    const int arraySize = 5;
    int a[] = { 111110, 1111000, 11100000, 110000000, 1000000000 };
    int NumSize[] = { 6,7,8,9,10 };
    int** arr2D = new int*[arraySize];
    for(int i = 0; i < arraySize;   i) {
        intToIntArray(a[i], NumSize[i], arr2D[i]);
    }
}
  • Related