SYM2RR
SYM2RR

Reputation: 31

Dynamic Memory Allocation & Matrix Calculation

Here's a piece of code I just wrote. All the variables are declared. The purpose for this is initialize the values of the matrix such that the matrix element (i row, j column) contains the value (i-j). The result desired to be similar to the following output:

a = 
     0.0   -1.0   -2.0   -3.0
     1.0    0.0   -1.0   -2.0
     2.0    1.0    0.0   -1.0
     3.0    2.0    1.0    0.0

However, the code down below won't work for me and I can't find why. Also I'm not sure if I'm allowed to allocate new memory locations like the if statements down there.

    double data[1];             // Initialize the array 

    if (type == 'd')            // Set the array to the correct size (not sure if it's correct)
    {
        double* data = new double [arraysize];
    }
    else if (type == 'f')
    {
        float* data = new float [arraysize];
    }
    else if (type == 'i')
    {
        int* data = new int [arraysize];
    }
    for (int i = 0; i <= arraysize; i++)                                                                        // Fill the array with desired data
    {
        int numofrow = (i / columns) + 1;
        int numofcol = (i % columns) + 1;
        data[i] = numofrow - numofcol;
    }
    cout << "a = " << endl;             // Start to output the result
    for (int i = 0; i < rows; i++)      // Nest for loop to output one row by another
    {
        for (int j = 0; j < columns; j++)
        {
            cout << "   " << showpoint << setprecision(1) << setw(8) << right << data[columns*i+j];
        }
        cout << "\n";
    }

Upvotes: 0

Views: 134

Answers (2)

xtofl
xtofl

Reputation: 41509

C++ does not let you change the type of a variable; i.e. data. It's a double[1] from the beginning, and so it will be. What you do in your if (type == ...) body is declare a new variable, visible only in the body of this conditional, that hides the original data.

If you want to do the same thing for multiple data types, you should use a function template (oh, and use std::vector instead of raw pointers - you don't need to remember deleting, and you get a lot for free):

template<typename T> void doTheMatrix(const int rows, const int columns) {
    const auto size = rows * columns;
    std::vector<T> data(size);
    for (int i = 0; i != size; ++i) {
         int numofrow = (i / columns) + 1;
         int numofcol = (i % columns) + 1;
         data[i] = numofrow - numofcol;
    }
    ... the output code
}

And call this function from your type switch:

switch (type) {
   case 'd': doTheMatrix<double>(10, 10); break;
   case 'f': doTheMatrix<float>(10, 10); break;
   case 'n': doTheMatrix<int>(10, 10); break;

}

Upvotes: 4

Jabberwocky
Jabberwocky

Reputation: 50776

It's all wrong.

This piece of code doesn't make sense:

double data[1];  // outer data variable

if (type == 'd')    
{
    double* data = new double [arraysize];  // inner data variable
}

The variable data goes out of scope at the end of the if bloc. All this will do is create a memory leak, nothing else. The outer data variable won't be changed at all if the if block is exected, because it's two different variables.

Upvotes: 1

Related Questions