passing two dimensional array to function [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a problem with passing my created table to fillMatrix function.
Another thing is how to refer to this table in my function. I really appreciate every kind of help. I didn't get any console problem. Program is freezing.
int **createTable(int n)
{
int **table = new int*[n];
for(int i = 0; i<n; i++)
{
table[i] = new int[n];
}
return table;
}
void fillMatrix(int n, int **tab)
{
for(int x = 0; x<=n; x++)
{
for(int y= 0; y<=n; y++)
{
tab[x][y] = 0;
}
}
}
int main()
{
int n;
cout <<"Add size of table";
cin >> n;
int **table = createTable(n);
srand(time(NULL));
fillMatrix(n, table);
return 0;
}

I see only one problem in your code:
for(int x = 1; x <= n; x++)
{
for(int y = 1; y <= n; y++)
{
tab[x][y] = 0;
}
}
Arrays in C++ are zero-based, but you tried to assign tab[n][n] which causes undefined behavior.
Also, you should delete your array once it is no longer needed.
As another solution I suggest you use std::vector instead of dynamically allocated arrays.

it is what they told you about. Your for statement should be like this
for( int i = 0 ; i < n ; i++ )
instead of
for( int i = 0 ; i <= n ; i++ )
The compiler want warn you if you accidentally go beyond the size of an array thats why you didnt have any warnings or errors.
Think about using a vector instead of an array as suggested above.

Related

SelectionSorting in c++ (Error : EXC_BAD_ACCESS) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I just recently learned to do selection sorting an array. I am also using X-Code on a mac.
I thought I did everything correctly, but I seem to keep getting this error message on the if statement :
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000).
What am I doing wrong?
using namespace std;
void selectionSorting(int array[], int n)
{
for(int i = 0; i < n-1; n++)
{
int min = i;
for(int j = i + 1; j < n; j++)
{
if(array[j] < array[min]) //Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000)
min = j;
}
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
int main()
{
int n = 10;
int array[]= {10,9,8,7,6,5,4,3,2,1};
selectionSorting(array, n);
for(int x=0; x < n; x++)
{
cout << array[x] << " ";
}
return 0;
}
You have a logical error at for(int i = 0; i < n-1; n++). It should be for(int i = 0; i < n-1; i++) (iterate through the elements of the array).
Also EXC_BAD_ACCESS suggests that your are trying to access a piece of memory that is no longer accessible or it doesn't go well with the intended use.
See that this occurs at if(array[j] < array[min]), which is obvious because j is going beyond the array length as you do n++.
As suggested in the comments try using a debugger.

Printing 2D arrays? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have encountered a problem with printing 2D arrays. Here is my code, any help will be greatly appreciated.
#include<iostream>
using namespace std;
int main()
{
int NumRow, NumColumn;
int anArray[2][2] = {{1,2},{3,4}};
for (int N_column = 1; N_column < NumColumn; N_column++)
{
for (int N_row = 1; N_row < NumRow; N_row++)
{
cout << anArray[N_row,N_column];
}
}
return 0;
}
3 problems:
Array indexes start at 0.
NumColumn, NumRow are uninitialized.
wrong syntax [y,j], use [i][j]
Try like this:
...
int NumRow = 2, NumColumn = 2;
int anArray[2][2] = {{1,2},{3,4}};
for (int N_column = 0; N_column < NumColumn; N_column++)
{
for (int N_row = 0; N_row < NumRow; N_row++)
{
cout << anArray[N_row][N_column];
}
}
...
You declare
int NumRow, NumColumn;
but you never assign a value to them. Use
int NumRow = 2, NumColumn = 2;
instead. Also, C-arrays start at 0, not at 1, so you must update your for-loops as well:
for (int N_column = 0; ...
for (int N_row = 0; ...
Last, change the output statement, as multidimensional arrays need to be reached in a different way:
cout << anArray[N_row][N_column];
There are few issues in your code:
1st: You declare NumRow, NumColumn but use them without initializing them before which cause to Undefined Behaviour.
Solution: Initialized them
NumRow = 2;
NumColumn = 2;
2nd: Array syntax in the following line-
cout << anArray[N_row,N_column];
it should be
cout << anArray[N_row][N_column];
3rd: C++ arrays are zero indexed, so you should start initializing the loop control variables like following:
for (int N_column = 0; N_column < NumColumn; N_column++)
{ ^^^
for (int N_row = 0; N_row < NumRow; N_row++)
{ ^^^^
//...

Need Help in Code Blocks [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This code executed successfully in Visual C++, but showed run time error in Code
Blocks returning "Process terminated with status -1073741819". Also when MAX is
defined as "#define MAX 4" it executed successfully. Can anyone please help?
Thanx!
#include <iostream>
#include <cstdlib>
#define MAX 32
using namespace std;
double **A, **B, **C;
void initialize(double** x) //code to initialize matrix
{
static int n = 0;
for(int i = 0; i < MAX; i++)
*(x+i) = (double*) new double[MAX];
srand(n);
double* ptr = *x;
for(int i = 0; i < MAX; i++)
for(int j = 0; j < MAX; j++)
*(ptr+(i*MAX)+j) = rand() % 100;
n++;
}
void print(double** x)
{
double* ptr = *x;
for(int i = 0; i < MAX; i++){
for(int j = 0; j < MAX; j++)
cout<<*(ptr+(i*MAX)+j)<<" ";
cout<<endl;
}
}
int main(){
A = (double**) new double[MAX];
B = (double**) new double[MAX];
C = (double**) new double[MAX];
initialize(A);
initialize(B);
initialize(C);
print(A);
cout<<endl;
print(B);
cout<<endl;
print(C);
cout<<endl;
system("pause");
return 0;
}
In place of "*(ptr+(i*MAX)+j)" to access x[i,j] use " * ( *(x+i)+j) " .
This will resolve the segmentation fault.
"*(ptr+(i*MAX)+j)" is same as ptr[i*MAX+j] which is an out-of-bounds access for some values of i and j.
your variables should be double *, not double ** (pointers to pointers).

c++ multiple finder [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
im trying to find multiples of an array using a loop
int array[11] = {1,2,3,4,5,6,7,8,9,10,11};
int size=11;
for(int i=0;i > size;i++)
{
if (i%2==!0)
cout << array[i];
}
why wont this work
//Declare num first.
//Correct if condition..
//correct condition checking in for loop
int array[11] = {1,2,3,4,5,6,7,8,9,10,11};
int index=2;
int size=11;
int num = 5;
for(int i=0;i < size;i++)
{
if (i%num==0)
cout << array[i];
}
I think you might want to change the (somewhat ... unusual):
if (i%2==!0)
into:
if (i % 2 == 0)
In addition, you loop termination condition is such that the loop will never execute. Try:
for (int i = 0; i < size; i++)

How to add a column to a two dimensional array [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have data in a two dimensional array with n rows and p columns.
For example:
vector<vector<int> > dynamicArray(ROWS, vector<int>(COLUMNS));
for(int i = 0;i < dynamicArray.size();++i){
for(int j = 0;j < dynamicArray[i].size();++j){
dynamicArray[i][j] = i*j;
}
}
Now, I want to add several columns to this array. I tried the following (Add a column of all 10s to the array), but if failed:
for(int i=0; i < dynamicArray.size(); i++){
dynamicArray[i].push_back(10);
}
Is there a way to do this?
Thanks!
I've ran your code, and I've successfully added a column. What do you mean by it failed?
Personally I would've flattened the 2 dimensional array into 1 using one single vector.
class DynamicMatrix
{
vector<int> array;
int rows;
int columns;
public:
DynamixMatrix(int r,int c):array(vector<int>(r*c)),rows(r),columns(c){};
int getValue(int x,int y) { return array[x+y*c];}
int setValue(int x,int y, int v) { array[x+y*c] = v;}
void AddRow()
{
rows++;
array.resize(rows*columns);
}
void AddColumn()
{
column++;
array.resize(rows*columns);
}
}