Newsgroup: comp.lang.c++
Subject: 2D Array
From: axcytz@...
Date: Fri, 28 Jun 2013 10:59:46 -0700 (PDT)
Hello all,
I have the following code to have a 2D array, and my aim is to have a matrix whose (1) column size is variable(i.e. 1st row has size 3, 2nd has 2, 3rd has 5, and so on);(2) row size is dynamically changed (I want to be able to add new rows in the future.) Unfortunately, I can't achieve these goals. Thanks for your helps in advance!
#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main()
{
int rowSize = 3, columnSize = 5, z=0, counter = 0;
int **array;
array = new int* [rowSize];
for (int i = 0; i < rowSize; i++)
array[i] = new int [columnSize]; // rowSize* columnSize matrix is created
int* addedRow;
addedRow = new int [rowSize]; //to add new rows
for (int z = 0; z < counter; z++ ){ //z is the index of new row
array[rowSize+z] = new int [size[z]];} //to add new row
counter++; // count the added rows
cout << "added row's number of elements:\n";
cin >> addedRow[z];
for (int i = 0; i < addedRow[z]; i++ ){
array[z][i] = 1; //all elements are set to 1
}
for(int i=0; i<rowSize; i++)
{
for(int j=0; j<n; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < rowSize; i++){
delete [] array[i];
}
delete [] array;
}
Subject: 2D Array
From: axcytz@...
Date: Fri, 28 Jun 2013 10:59:46 -0700 (PDT)
Hello all,
I have the following code to have a 2D array, and my aim is to have a matrix whose (1) column size is variable(i.e. 1st row has size 3, 2nd has 2, 3rd has 5, and so on);(2) row size is dynamically changed (I want to be able to add new rows in the future.) Unfortunately, I can't achieve these goals. Thanks for your helps in advance!
#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main()
{
int rowSize = 3, columnSize = 5, z=0, counter = 0;
int **array;
array = new int* [rowSize];
for (int i = 0; i < rowSize; i++)
array[i] = new int [columnSize]; // rowSize* columnSize matrix is created
int* addedRow;
addedRow = new int [rowSize]; //to add new rows
for (int z = 0; z < counter; z++ ){ //z is the index of new row
array[rowSize+z] = new int [size[z]];} //to add new row
counter++; // count the added rows
cout << "added row's number of elements:\n";
cin >> addedRow[z];
for (int i = 0; i < addedRow[z]; i++ ){
array[z][i] = 1; //all elements are set to 1
}
for(int i=0; i<rowSize; i++)
{
for(int j=0; j<n; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < rowSize; i++){
delete [] array[i];
}
delete [] array;
}
via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?30817-2D-Array&goto=newpost
View all the progranning help forums at:
http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums
No comments:
Post a Comment