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.
std::vector?std::vector