Okay, so I was given this problem for my assignment, and I was wondering if I have completed the tasks the question asks. I would also like to know how I can improve this code as well. Please nothing too fancy as I have just learned arrays.

Question:

Write a method, linearize, that takes a 2-dimensional array as parameter and returns a 1-
dimensional array with all the elements of the two-dimensional array. For example, given the
following array:
10, 20, 30
40, 50, 0
60, 0, 0
The method will return a 1-dimensional array with the following elements:
10, 20, 30, 40, 50, 60

My code:

#include <iostream>//include statement(s)
#include <iomanip>

using namespace std;//using namespace statement(s)

void linearize(int arry[][3], int oneDimArray[]);//void function header linearize to convert 2-d array to 1-d array

int main()
{
    int arry[3][3] = {{10, 20, 30}, {40, 50, 0}, {60, 0, 0}};//variable declaration(s), to initialize the array
    int oneDimArray[25] = {0};

    cout << " Your 2-d array before linearized: " << endl;

    for (int row = 0; row < 3; row++)//to create the row(s)
    {
        cout << endl;

        for (int col = 0; col < 3; col++)//to create the column(s)
            cout << setw(3) << arry[row][col] << "\t";//print out 2-d array before
    }

    cout << endl;
    cout << endl;

    cout << " Your 2-d array after linearized: " << endl;

    linearize(arry, oneDimArray);//function call for 1-d array

    cout << endl;
    cout << endl;

    system ("PAUSE");//black box to appear
    return 0;//return statment(s)
}

void linearize(int arry[][3], int oneDimArray[])//void unction call to create 1-d array from 2-d array
{
    int counter = 0;

    for (int row = 0; row < 3; row++)//creates rows
    {
        for (int col = 0; col < 3; col++)//creates columns
        {
            if (arry[row][col] != 0)//set the condition for the 1-d array to NOT print 0's
            {
                counter++;//to create an index for 1-d array
                oneDimArray[counter] = arry[row][col];//sets 1-d array to values of 2-d array
                cout << setw(3) << oneDimArray[counter];//prints out 1-d array
            }
        }
    }

    return;
}

Thank you for feed back! It's much appreciated! :)

Recommended Answers

All 2 Replies

The line counter++ should come after this line: cout << setw(3) << oneDimArray[counter]; else one array space is wasted.
Plus you have inserted the values inside the code, which is bad.
Your task is to linearize any 2-D array and not just the one given in example.
So you should take rows and columns and the array as input from the user, or atleast pass row and columns as arguments to the function even if you just test it for one case.
Remember this forever: Avoid hard coding values into your code.
Secodly, it should return the 1-D array, we can do this by using dynamic allocation(but i am not sure if you know that yet), so instead you can take a global array and return its pointer(though its really of no use).

With arrays I am in favor of having the caller suppling the 1d array to the function. That way the caller knows they are responsible for the array. It's a shame that dont just start of with using the STL and after they are comfortable with it teach them how it works under the hood.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.