1

Any desired array entered by the user , code look for every column , if any element in any column is equal to number y.Then the code should add a new column of zeros in front of it.

Code

#include <pch.h>
#include <iostream>

using namespace std;

int main()
{
int y, rows, columns;
std::cout << "Enter the number of rows: ";
std::cin >> rows;
std::cout << "Enter the number of columns: ";
std::cin >> columns;
std::cout << "Enter a number Y: ";
std::cin >> y;

//-----------------------Generating 2-D array---------------------------------------------------------
int **array = new int*[2 * rows];
for (int i = 0; i < rows; i++)
    array[i] = new int[columns];
//------------------------Generating bool--------------------------------------------------------------
bool *arrx = new bool[columns];
//-----------------------Input Array Elements---------------------------------------------------------
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < columns; i++)
    for (int j = 0; j < rows; j++)
        std::cin >> array[i][j];
//--------------------Loop for the array output--------------------------------------------------------
for (int i = 0; i < columns; i++) {
    for (int j = 0; j < rows; j++) {
        std::cout << array[i][j] << " ";
    }
    std::cout << "\n";
}
//-------------------Loop for finding columns with even numbers----------------------------------------
for (int i = 0; i < columns; i++) {
    arrx[i] = false;
    for (int j = 0; j < rows; j++) {
        if (array[j][i] == y) {
            arrx[i] = true;
        }
    }
}
std::cout << "\n";
//--------------------Loop for addition of new columns infront of even numbers--------------------------
for (int i = 0; i < columns; i++) {
    for (int j = 0; j < rows; j++) {
        std::cout << array[i][j] << " ";
    }
    std::cout << "\n";
    if (arrx[i]) {
        for (int i = 0; i < rows; i++) {
            std::cout << 0 << " ";
        }
        std::cout << "\n";
    }
}

return 0;
}

Here this code adds only rows to the array while I need to add columns . I have tried changing array[i][j] to array[j][i] but in vain.

3
  • any particular reason why you do not use std::vector ? Commented Nov 20, 2018 at 13:19
  • Because this is the last manual task , of adding and removing elements in our course . The next topic we are going to start is std::vector Commented Nov 20, 2018 at 13:37
  • 1
    please add such requirements to the question. Homework assignments often come with rather strange constraints, eg in real life no sane c++ coder would use manually allocated arrays for that task Commented Nov 20, 2018 at 13:40

1 Answer 1

2

You need to replace

for (int i = 0; i < columns; i++) {
    for (int j = 0; j < rows; j++) {
        std::cout << array[i][j] << " ";
    }
    std::cout << "\n";
    if (arrx[i]) {
        for (int i = 0; i < rows; i++) {
            std::cout << 0 << " ";
        }
        std::cout << "\n";
    }
}

with

for (int i = 0; i < rows; i++) {
    for (int j = 0; j < columns; j++) {
        std::cout << array[i][j] << " ";
        if (arrx[j]) {
           std::cout << 0 << " ";
         }
    }
}

This prints a zero after every element whose column value is marked. What you were trying to do is print to standard output column by column which is not how it works.

Also I urge you to consider using a std::vector instead of plain pointers to avoid errors like here where you have forgotten to deallocate your memory.

Sign up to request clarification or add additional context in comments.

1 Comment

And should I change all the arrx[i] to arrx[j] ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.