Home > OS >  How to store values from a normal array to a 2D array in C ?
How to store values from a normal array to a 2D array in C ?

Time:04-19

I want to store the Values to the CustomValues array. How can I do this? Any code and explanation would be great.

int main() {
    int CustomValues[4][3];

    int Values[3] = {234, 98, 0};
    int Values1[3] = {21, 34, 5};
    int Values2[3] = { 12, 6, 765 };
    int Values3[3] = { 54, 67, 76 };
}

The CustomValues array should look like:

{{234, 98, 0}, { 21, 34, 5 }, { 12, 6, 765 }, { 54, 67, 76 }}

CodePudding user response:

There's a few different ways you can do this. Since we already know your constraints, I've taken liberties to not do this dynamically.

The first is memcpy, which is in the <cstring> header:

memcpy(CustomValues[0], Values, sizeof(Values));
memcpy(CustomValues[1], Values1, sizeof(Values1));
memcpy(CustomValues[2], Values2, sizeof(Values2));
memcpy(CustomValues[3], Values3, sizeof(Values3));

Another is to loop through the array and store the values individually:

for (int i = 0; i < sizeof(CustomValues)/sizeof(CustomValues[0]); i  ) {
    for (int j = 0; j < sizeof(CustomValues[0])/sizeof(CustomValues[0][0]); j  ) {
        if (i == 0) {
            CustomValues[i][j] = Values[j];
        }
        else if (i == 1) {
            CustomValues[i][j] = Values1[j];
        }
        else if (i == 2) {
            CustomValues[i][j] = Values2[j];
        }
        else if (i == 3) {
            CustomValues[i][j] = Values3[j];
        }
    }
}

There is probably a better way to handle the logic for selecting which Values array you want, but that was just a quick solution to demonstrate.

EDIT: Example of 2D Vector usage

This example doesn't contain the logic for actually controlling the number of elements in a vector, but you can simply do that by following the for loop logic. Basically, you just need something to check the size of your vector with size(), and then move to a different one.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<int>> CustomValues; //2D vector

    //1D vectors
    vector<int> Values;
    vector<int> Values1;

    Values.push_back(234); //to insert a value individually
    Values.insert(Values.end(), {98, 0}); //to append multiple values

    Values1.insert(Values1.end(), {21, 34, 5});

    //inserting the 1D arrays to the 2D arrays
    CustomValues.push_back(Values);
    CustomValues.push_back(Values1);

    //example of getting # of elements
    int countOfInnerVectors = 0;
    for (int i = 0; i < CustomValues.size(); i  )
        countOfInnerVectors  ;

    cout << "The number of 1D vectors in CustomValues is: " << countOfInnerVectors;
}

An example of checking for the correct amount of vectors would be:

//check if we have less than 10 inner vectors
int maxCustomValuesSize = 10;
if (CustomValues2.size() < maxCustomValuesSize)

In this example, you would have something like int index = 0, and when that if is no longer satisfied, you could do index or some other logic to start inserts at your new index, like CustomValues2[index].push_back(Values);.

You can follow the same logic for the inner vectors as well, you would just be changing to a new 1D array instead of changing to a new "row" like the outer vector does.

  • Related