hope you doing well.
so i have just started to do an assignment and the first thing i wanted to do was to create the two dynamic array. however, there is something wrong with the array i can't assign values to it. Here is the code:
void Room::memory(int **array){
int x,x2;
int count=0;
cout << "Array size? rows: columns: \n";
cin >> x >> x2;
array = new int*[x];
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
array[i][j]=count;
count++;
}
}
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
cout<< array[i][j]<< " | ";
}
cout << endl;
}
}
i always get the value 0 for my array. whether i use this line or not:
array[i][j]=count;
i tied to compare my code with someone else and it is the same steps but it doesn't work for me.
class Room{
private:
int **array;
public:
void memory(int **array);
};
Why do you do
for(int i=0; i<x;i++){
array[i]= new int[x2];
} twice?
at the same time, please change void memory(int **array); to void memory();
while you crested the 2nd 2D dynamic array,you did not initialize it...And just printed it... In the 2nd 2D array you also need to initialize before printing the all index values....
for(int i=0; i<x;i++){
array[i]= new int[x2];
}
//here should be the initialization
for(int i=0; i<x; i++){
for(int j=0; j<x2; j++){
cout<< array[i][j]<< " | ";
}
cout << endl;
}
Related
I'm trying to generate and solve an NxN odd-numbered magic square through dynamic memory allocation but whenever I run the code, it displays nothing on the terminal and the programme ends. I reckon it has something to do with the dynamic allocation of the 2D array, as when I make a normal NxN array with a constant size, the programme runs fine. I'd appreciate any help regarding this!
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void calcuateMagicSquare(int N)
{
int **Array;
Array=new int*[N];
for(int i=0; i<N; i++)
{
Array[i]=new int[N];
}
memset(Array, 0, sizeof(Array));
int nSquare=N*N;
int i=N/2;
int j=N-1;
for(int k=1; k<=nSquare;)
{
if(i==-1 && j==N)
{
j=N-2;
i=0;
}
else
{
if(j==N)
{
j=0;
}
if(i<0)
{
i=N-1;
}
}
if(Array[i][j])
{
j=j-2;
i++;
continue;
}
else
{
Array[i][j]=k++;
}
j++;
i--;
}
int SolutionMagicSquare=N*(N*N+1)/2;
cout << "Solution of the magic Square: " << SolutionMagicSquare << endl;
cout << "MAGIC SQUARE: \n" << endl;
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
cout << setw(4) << Array[i][j] << " ";
cout << endl;
}
}
}
int main()
{
int N;
cout << "Please enter the dimension of the magic square:" << endl;
cin >> N;
calcuateMagicSquare(N);
}
This isn't too bad.
int ** Array=new int*[N];
for(int i=0; i<N; i++)
{
Array[i]=new int[N];
}
memset(Array, 0, sizeof(Array));
The memset is causing your trouble, and it's wrong for two reasons. First, let's say you really wanted to do that. You don't, but let's say you did. How big is sizeof(Array). Array is an int **. On a 64-bit machine, that's 8 bytes. So conveniently, you only destroyed the first pointer.
What you really need to do is this:
int ** Array=new int*[N];
// Don't do this memset, but showing you what it should look like)
memset(Array, 0, sizeof(int *) * N);
for(int i=0; i<N; i++)
{
Array[i]=new int[N];
// But this one is safe
memset(Array[i], 0, sizeof(int) * N);
}
Your version was erasing the first pointer -- Array[0]. I put that first memset in there so you could see what it would look like if you needed it. It's the second one you need. You're clearing out the size of an int times the number of ints that you have.
I'm trying to figure out how to pass a 2 dimensional Matrix to a function
//function to print Matrix
void refl(int n, int board[][])
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << board[i][j] << " ";
}
cout << endl; //printing the matrix to the screen
}
}
int main()
{
int n;
string refl;
cin>>n;
int board[n][n]; //creates a n*n matrix or a 2d array.
for(int i=0; i<n; i++) //This loops on the rows.
{
for(int j=0; j<n; j++) //This loops on the columns
cin>>board[i][j];
refl(n , board);
}
return 0;
}
It says that "n" and "board" isn't declared in the function while they are .
Try to use C++ std::vector or good old C malloc+free.
C++
#include <string>
#include <iostream>
#include <vector>
using namespace std;
void reflxx(int n, vector<vector<int>>& board)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cout << board[i][j] << " ";
}
cout << endl; //printing the matrix to the screen
}
}
int main()
{
int n;
string refl;
cin>>n;
vector<vector<int>> board(n, vector<int>(n));
//creates a n*n matrix or a 2d array.
for(int i=0; i<n; i++) //This loops on the rows.
{
for(int j=0; j<n; j++) //This loops on the columns
cin>>board[i][j];
}
reflxx(n , board);
return 0;
}
See http://www.cplusplus.com/reference/vector/vector/resize/ for more example.
i am new to programing and i am trying to understand the two dimensional array. i wrote this code to just test my code to see if it working or not. unfortunately, i am getting a segmentation error. i know that means that something i wrote is unreadable for the compiler but i do not know what is it. because everything seems fine to me.
#include<iostream>
using namespace std;
int main(){
int col, row,i;
int **array;
cout << "How many rows?\n";
cin >> row;
cout << "How many colomns\n";
cin >> col;
cout << "!!!!!!!!!!!!";
array = new int*[row];
for (int i = 0;i<row;i++){
array[i] = new int[col];
}
cout << "!!!!!!!!!!!!";
for( i=0; i<row; i++){
int x=1;
array[0][i]= x;;
x++;
}
cout << "!!!!!!!!!!!!";
cout << array[row][col];
for(i=0; i<row; i++){
delete [] array[i];
delete [] array;
}
return 0;
}
the ERROR is:
"
How many rows?
3
How many colomns
3
Segmentation fault (core dumped)
"
array[0][i]= x; looks wrong. It should be:
array[i][0]= x;
First index is for row and second for col.
Later cout << array[row][col]; is also wring as row is out of range.
The way you delete the array is also wrong, it should be:
for(i=row - 1; i >= 0; --i){
delete [] array[i];
}
delete [] array;
cout << array[row][col];
Out of range in each of the two dimensions, as others have said.
for(i=0; i<row; i++){
delete [] array[i];
delete [] array;
}
You're deleting array's elements three times, once for each element. This is good. You're also deleting array itself three times. This is dangerous and wrong.
so it should be like this?
#include<iostream>
using namespace std;
int main(){
int col, row,i;
int **array;
cout << "How many rows?\n";
cin >> row;
cout << "How many colomns\n";
cin >> col;
array = new int*[row];
for (int i = 0;i<row;i++){
array[i] = new int[col];
}
cout << "!!!!!!!!!!!!";
for( i=0; i<row; i++){
int x=1;
array[i][0]= x;;
x++;
}
cout << "!!!!!!!!!!!!";
for(i=0; i<row; i++){
for(int j=0; j<col; j++){
array[i][j];
}
}
cout << array[row][col];
for(i=0; i<row; i++){
delete [] array[i];
delete [] array;
}
return 0;
}
This line is out of range cout << array[row][col];.If you want to print the last element then change this line to cout << array[row-1][col-1];
As others mentioned the below is code for deleting the allocated memory
for(i=0; i <row; i++){
delete [] array[i];
}
delete [] array;
Also in your code below x is always going to be 1 , x++ is noneffective.
for( i=0; i<row; i++){
int x=1;
array[0][i]= x;
x++;
}
if you want to increment x for each row then initialize x outside the loop
like this
int x=1;
for( i=0; i<row; i++){
array[0][i]= x;;
x++;
}
EDIT: I'm fairly new to c++. Began working with this language two weeks ago.
Sorry if this have been asked before, but i have searched everywhere on the web on how to sum individual rows in a 2D array and not found the answer that i was looking for.
i need to display the sum of each individual row in a[m][n], but for some reason this only works when my array is 2x2, but if it's 3x3 or bigger, then i get the following output in the terminal:
for intsance, a[3][3]=
{1,2,3}, //this is determined by the user
{1,2,3},
{1,2,3};
then i get the following output:
9179942 //address of some sort???
6 // actual sum. code is working here (yay :D)
469090925// again something i dont understand
This is what i have so far
#include <iostream>
using namespace std;
int main(){
int m,n;
cout<<"Enter number of rows for array"<<endl;
cin>>m;
if (m>10){
cout<<"More than 10 rows will be too big"<<endl;
return 1;
}
cout<<"Enter number of collumns for array"<<endl;
cin>>n;
if (n>10){
cout<<"More than 10 collumns will be too big"<<endl;
return 1;
}
int a[m][n];
for(int i=0; i<m;i++){
cout<<"Enter "<<m<<" values into row "<<i+1<<endl;
for(int j=0; j<n; j++){
cout<<"a ["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
}
}
cout<<"Array dimensions: "<<m<<"x"<<n<<'\n'<<"resulting array: "<<endl;
for(int i=0; i<m;i++){
for(int j=0; j<n; j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
avg[i]+=a[i][j];
}
}cout<<"\n\n";
for(int i=0; i<m; i++){
cout<<avg[i]<<endl;
}
return 0;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
You're not initializing these values so they're free to be whatever cruft is on the stack at the time. You need to initialize them.
int avg[m];
for (int i = 0; i < m; ++i) {
avg[i] = 0;
for (int j = 0; j < n; ++j) {
avg[i] += a[i][j];
}
}
int a[m][n]; is not allowed in Standard C++. The dimensions of C-style arrays must be known at compile-time. A program using this code could do literally anything.
You could replace this line with:
vector<vector<int>> a(m, vector<int>(n));
which seems like a mouthful at first, but you will find it makes your problem go away.
Another bonus of this approach is that you can then use range-based loops:
for(int x : avg)
cout << x << endl;
which reduces the chance of making an error by using the wrong letter in the loop condition.
i am trying to add two 2D arrays in C++ with my following code by i am getting output as this
333 333, but i want out as 2 rows
{
int a[2][3], b[2][3], i , j;
cout<<"First Matrix"<<endl;
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cin>>a[i] [j];
}
}
cout<<"Second Matrix"<<endl;
for(int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cin>>b[i][j];
}
}
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j];
}
cout<<" ";
}
cout<<endl;
_getch();
}
Last for loop is wrong. You have to move cout's.
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j];
cout<<" ";
}
cout<<endl;
}
Also your variables i and j are unused because you are declaring new ones in for loops with int i=0; and int j=0;.
How about changing the line that prints spaces to print a newline?
cout<<" ";
becomes
cout<<"\n";
You don't put any newlines in your code, so of course it won't print out on a new line. replace cout<<" "; with cout<<std::endl; and you should get each row on a new line.
Replace the last piece of code by this:
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
cout<<a[i] [j] + b[i] [j] << ' ';
}
cout<< "\n";
}
cout << "\n";