Adding columns to a 2-D array











up vote
1
down vote

favorite












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.










share|improve this question






















  • any particular reason why you do not use std::vector ?
    – user463035818
    Nov 20 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
    – J.Doe
    Nov 20 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
    – user463035818
    Nov 20 at 13:40















up vote
1
down vote

favorite












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.










share|improve this question






















  • any particular reason why you do not use std::vector ?
    – user463035818
    Nov 20 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
    – J.Doe
    Nov 20 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
    – user463035818
    Nov 20 at 13:40













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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.










share|improve this question













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.







c++ arrays visual-c++ multidimensional-array jagged-arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 13:17









J.Doe

173




173












  • any particular reason why you do not use std::vector ?
    – user463035818
    Nov 20 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
    – J.Doe
    Nov 20 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
    – user463035818
    Nov 20 at 13:40


















  • any particular reason why you do not use std::vector ?
    – user463035818
    Nov 20 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
    – J.Doe
    Nov 20 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
    – user463035818
    Nov 20 at 13:40
















any particular reason why you do not use std::vector ?
– user463035818
Nov 20 at 13:19




any particular reason why you do not use std::vector ?
– user463035818
Nov 20 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
– J.Doe
Nov 20 at 13:37




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
– J.Doe
Nov 20 at 13:37




1




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
– user463035818
Nov 20 at 13:40




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
– user463035818
Nov 20 at 13:40












1 Answer
1






active

oldest

votes

















up vote
2
down vote













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.






share|improve this answer























  • And should I change all the arrx[i] to arrx[j] ?
    – J.Doe
    Nov 20 at 13:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393872%2fadding-columns-to-a-2-d-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













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.






share|improve this answer























  • And should I change all the arrx[i] to arrx[j] ?
    – J.Doe
    Nov 20 at 13:48















up vote
2
down vote













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.






share|improve this answer























  • And should I change all the arrx[i] to arrx[j] ?
    – J.Doe
    Nov 20 at 13:48













up vote
2
down vote










up vote
2
down vote









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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 13:34

























answered Nov 20 at 13:29









Hristijan Gjorshevski

115117




115117












  • And should I change all the arrx[i] to arrx[j] ?
    – J.Doe
    Nov 20 at 13:48


















  • And should I change all the arrx[i] to arrx[j] ?
    – J.Doe
    Nov 20 at 13:48
















And should I change all the arrx[i] to arrx[j] ?
– J.Doe
Nov 20 at 13:48




And should I change all the arrx[i] to arrx[j] ?
– J.Doe
Nov 20 at 13:48


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393872%2fadding-columns-to-a-2-d-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'