Matrix with two diamensions on the run:My program runs but it crashes!

Jun 23, 2016 at 5:16pm
I am trying to run through the matrix so i can access with an recursive way like this: array[0,columns/2] and array[0,columns/2 +1] to array[1,columns/4], array[0,columns/4+1], array[1,3/4*columns] and array[1,3/4*columns+1] and so on. n and m are given on the run and n are the lines and columns=pow(2,n+2). My program runs but i can't get results because it crashes or sometimes it prints hex. Here is my code(please ignore my comments because threre in greek):

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;

main()
{
cout << "This is the game that is called: Remove the prelast nucleotide of the second sequence and win!";//Åêôýðùóç åéóáãùãéêïý ìçíýìáôïò ðñïò ôïí ÷ñÞóôç.
cout << endl;
int n;//Ïñéóìüò ìåãßóôïõ ðñþôçò áëëçëïõ÷ßáò.
//int max_step;//Ïñéóìüò ïñßïõ ìÝãéóôïõ âÞìáôïò.
int step = 2;//Ïñéóìüò êáé áñ÷éêïðïßçóç âÞìáôïò.
bool checking1;
do //Âñü÷ïò åðáíÜëçøçò üðïõ ï ÷ñÞóôçò ïñßæåé ôï ìÝãéóôï ôçò ðñþôçò áëëçëïõ÷ßáò ìå Ýëåã÷ï åãêõñüôçôáò.
{
cout << "Please select the maximum of the first sequence (n): ";
checking1 = true;
if (!(cin >> n)||(n > 8)||(n <= 0))
{
cout << endl;
cout << "The maximum of the sequence (n) must be an integer (not char) between 8 and 1!";
cin.clear();
cin.ignore(100000, '\n');
checking1 = false;
cout << endl;
}



}while (checking1 == false);


int m; //Ïñéóìüò ìåãßóôïõ äåýôåñçò áëëçëïõ÷ßáò.


bool checking2;
do //Âñü÷ïò åðáíÜëçøçò üðïõ ï ÷ñÞóôçò ïñßæåé ôï ìÝãéóôï ôçò äåýôåñçò áëëçëïõ÷ßáò ìå Ýëåã÷ï åãêõñüôçôáò.
{
cout << "Please select the maximum of the second sequence (m): ";
checking2 = true;
if (!(cin >> m)||(m >= n)||(m <= 0))
{
cout << endl;
cout << "The maximum of the sequence (m) must be an integer (not char) between "<< n-1<< " and 1!";
cin.clear();
cin.ignore(100000, '\n');
checking2 = false;
cout << endl;
}



}while (checking2 == false);




double columns;

//columns = (pow((double)2,(double)n)+0.5*pow((double)2,(double)n));
columns=pow(2,(double)n+2);
cout << "columns= "<<columns;

cout << endl;
cout << endl;
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;//ÊáèõóôÝñçóç ôïõ ðñïóãñÜììáôïò ãéá íá äåé ï ÷ñÞóôçò ôï áðïôÝëåóìá.
cin.get();


/*try
{

int** array;
array = new int*[n];
for (int i = 0;i<n;i++)
array[i] = new int[(int)columns];

}
catch (exception& ex)
{

cout << "Exception: "<<ex.what();
}*/

//int array[n][(int)columns];

double **array;
array=(double **) malloc(n*sizeof(double *));

for(int i=0;i<n;i++)
array[i]=(double *) malloc(columns*sizeof(double));

try
{

for (int i=0; i<n; i++)
{
int j;
for (j=0; j<columns; j++)
array[i][j]=-1;
//std::cout<<std:: dec << array[i][j];
}

}
catch (exception& ex)
{

cout << "Exception: "<<ex.what();
}
cout << endl;
cout<< endl;
for (int i=0; i<n; i++)
{
int j;
for (j=0; j<columns; j++)

std::cout<<std:: dec << array[i][j];
}




cout << endl;

array[0][(int)columns/2]=n;
array[0][(int)columns/2+1]=m;
cout << endl;
cout << array[0][(int)columns/2]<<endl;
cout << array[0][(int)columns/2+1]<<endl;

for (int i=1; i<=n; i++)
for (int j=2; j<=pow(2,i); j=2*j )
{

cout << columns;
cout << endl;
cout << i-1 <<","<< (int)(columns/j-columns/(j*2));
cout << endl;
cout << endl;
cout << endl;

cout << (int) columns/j;

cout << endl;
cout << endl;
cout << endl;
cout << array[i-1,(int)columns/j];

cout << endl;
cout << endl;
cout << endl;

if (array[i-1,(int)(columns/j)]>0)
{

array[i,(int)(columns/j-columns/(j*2))]=array[i-1,(int)(columns/j)]-1;




}
if (array[i-1,((int)(columns/j+1))]>0)
{

array[i,(int)(columns/j-columns/2*j+1)]=array[i-1,(int)(columns/j+1)]-2;
}
if (array[i-1,(int)(columns/j)]>0)
{
array[i,(int)(columns/j-columns/2*j)]=array[i-1,(int)(columns/j)]-2;
}
if (array[i-1,(int)(columns/j*2+1)]>0)
{
array[i,(int)(columns/j+columns/2*j+1)]=array[i-1,(int)(columns/j+1)]-1;
}
//std::cout<<std::dec << array[i,(int)(columns/pow(2,i))-(int)(columns/pow(2,i+1))];
}



for (int i=0; i<n; i++)
{
if (i==0)
{
cout << "Begining: ";
}
else if (i % 2 ==0)
{
cout << "Player2: ";
}
else if (i % 2 == 1)
{
cout << "Player1: ";
}
for (int j=1; j<columns; j++)
{
if (array[i,j]<0)
{
cout << " ";
}
else
{
//std::cout<<std::dec << array[i,j]<<",";
std::cout<<std::dec << array[i,j];
//std::cout<<std::dec << array[i,(int)(columns/j-columns/j*2)];
//std::cout<<std::dec << array[i,(int)(columns/j-columns/j*2+1)];
//std::cout<<std::dec << array[i,(int)(columns/j+columns/j*2)];
//std::cout<<std::dec << array[i,(int)(columns/j+columns/2*j+1)];
}




}
cout << endl;
}


cout << endl;
cout << endl;
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;//ÊáèõóôÝñçóç ôïõ ðñïóãñÜììáôïò ãéá íá äåé ï ÷ñÞóôçò ôï áðïôÝëåóìá.
cin.get();


}
Jun 24, 2016 at 6:44am
closed account (48bpfSEw)
please format your code with the brackets (first button on the right).

hex-output: seem to be the address not the value.

use the cppchecker http://cppcheck.sourceforge.net/ to test your code beyond syntax!
Jun 24, 2016 at 7:01am
closed account (48bpfSEw)

access to the array with comma is syntaxly correct, but semanticaly wrong!!!

 
cout << array[i - 1, (int)columns / j];


this code means:

1. do "i-1" - irrelevant code!
2. do "(int) columns/j
use 2. term to access to array : array[(int) columns/j] and this delivers the address to the columns of the (int) columns/j 'th row of your array!



use two breakets array [] [] for the two dimensional array!

 
cout << array [i-1] [(int)columns/j];



I registered that this semanticaly error is not found with cppcheck!

Last edited on Jun 24, 2016 at 7:08am
Jun 24, 2016 at 8:21am
You were right about the brackets! I changed all the ',' with brackets, but it still crashes.
Jun 24, 2016 at 9:23am
closed account (48bpfSEw)
print more debug-informations out!


here is a logical error I think:

j = 2, if columns > 2 then j-columns is < 0
now look at your code:

array[i][ (int)(columns / j - columns / (j * 2))

it would access to a col-index < 0 !
Jun 24, 2016 at 9:34am
closed account (48bpfSEw)
therefor it is so important to write code contract driven.
that means:

don't access directly to the array!
write a function which checks the correct indizes
and throw an exception if failed.

f.ex.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

double GetValue (double** array, int iRowMax, int iColMax, int iRow, int iCol) {

  // ASSERTS
  if (iRow < 0 || iRow > iRowMax)
    throw -1;

  if (iCol < 0 || iCol > iColMax)
    throw -2;

  if (array == NULL)
    throw -3;


   return array[iRow][iCol];
}



1
2
3
4
5
6
7
8
9
10

main () {

  try {
    double v = GetValue(array, ROWS, COLS, 0,0);
  }
  except(...) {
     cout << "ASSERT: assert occured in GetValue!" << endl;
    }
 }


Jun 24, 2016 at 12:29pm
I tried that but i am afraid i 've done something wrong. It compiles, but i get this error on runtime: terminate called after throwing an instance of 'int'

Here is my new code:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;



double GetValue (double** array, int iRowMax, int iColMax, int iRow, int iCol) {

// ASSERTS
if (iRow < 0 || iRow > iRowMax)
throw -1;

if (iCol < 0 || iCol > iColMax)
throw -2;

if (array == NULL)
throw -3;

if (array[iRow][iCol]<0)
throw -4;

return array[iRow][iCol];
}

main()
{
cout << "This is the game that is called: Remove the prelast nucleotide of the second sequence and win!";
cout << endl;
int n;
//int max_step;
int step = 2;
bool checking1;
do
{
cout << "Please select the maximum of the first sequence (n): ";
checking1 = true;
if (!(cin >> n)||(n > 8)||(n <= 0))
{
cout << endl;
cout << "The maximum of the sequence (n) must be an integer (not char) between 8 and 1!";
cin.clear();
cin.ignore(100000, '\n');
checking1 = false;
cout << endl;
}



}while (checking1 == false);


int m;


bool checking2;
do
{
cout << "Please select the maximum of the second sequence (m): ";
checking2 = true;
if (!(cin >> m)||(m >= n)||(m <= 0))
{
cout << endl;
cout << "The maximum of the sequence (m) must be an integer (not char) between "<< n-1<< " and 1!";
cin.clear();
cin.ignore(100000, '\n');
checking2 = false;
cout << endl;
}



}while (checking2 == false);




double columns;

//columns = (pow((double)2,(double)n)+0.5*pow((double)2,(double)n));
columns=pow(2,(double)(n+3.0));
cout << "columns= "<<columns;

cout << endl;
cout << endl;
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();


/*try
{

int** array;
array = new int*[n];
for (int i = 0;i<n;i++)
array[i] = new int[(int)columns];

}
catch (exception& ex)
{

cout << "Exception: "<<ex.what();
}*/

//int array[n][(int)columns];

double **array;
array=(double **) malloc((n+2)*sizeof(double *));

for(int i=0;i<n+2;i++)
array[i]=(double *) malloc(columns*sizeof(double));

try
{

for (int i=0; i<n+2; i++)
{
int j;
for (j=0; j<columns; j++)
array[i][j]=-1;
//std::cout<<std:: dec << array[i][j];
}

}
catch (exception& ex)
{

cout << "Exception: "<<ex.what();
}
cout << endl;
cout<< endl;
for (int i=0; i<n+2; i++)
{
int j;
for (j=0; j<columns; j++)

std::cout<<std:: dec << array[i][j];
}




cout << endl;

array[1][(int)(columns/2-1)]=n;
array[1][(int)columns/2]=m;
array[1][(int)(columns/2+1)]=n;
array[1][(int)(columns/2+2)]=m;
cout << endl;
cout << array[1][(int)(columns/2-1)]<<endl;
cout << array[1][(int)columns/2]<<endl;
cout << array[1][(int)(columns/2+1)]<<endl;
cout << array[1][(int)(columns/2+2)]<<endl;
for (int i=0; i<n; i++)
for (int j=2; j<=pow(2,i); j=2*j )
{
cout << i << " iterration!";
cout << endl;
cout << endl;
cout << "j= "<< pow(2,i);
cout<< endl;
cout << "columns/j= " << (int)(columns/j);
cout << columns;
cout << endl;
//cout << i-1 <<","<< (int)(columns/j-columns/(j*2));
cout << endl;
cout << endl;
cout << endl;

//cout << (int) columns/j;

cout << endl;
cout << endl;
cout << endl;
//cout << array[i-1][(int)columns/j];

cout << endl;
cout << endl;
cout << endl;
//cout << array[i][(int)(columns/j-columns/j*2)];
cout << endl;
//if (array[i][(int)(columns/j-1)]>0)
//{

try {
double v1 = GetValue(array, n+2, (int)columns, i,(int) (columns/2*j)-1);
double v2 = GetValue(array, n+2,(int)columns, i+1,(int)(columns/j-columns/(j*2)-1));
v2=v1-1;
}
catch(exception ex)
{
cout << "ASSERT: assert occured in GetValue!" << endl;
}

//array[i+1][(int)(columns/j-columns/(j*2)-1)]=array[i][(int)(columns/j-1)]-1;

cout << endl;
cout << "array["<<i+1<<"]["<<(int)(columns/j-columns/(j*2)-1)<<"]=" << array[i+1][(int)(columns/j-columns/(j*2)-1)];
cout << endl;

//}
//if (array[i][((int)(columns/j))]>1)
//{


try {
double v3 = GetValue(array, n+2, (int)columns, i,(int) (columns/j));
double v4 = GetValue(array, n+2,(int)columns, i+1,(int)(columns/j-columns/(j*2)));
v4=v3-2;
}
catch(exception ex)
{
cout << "ASSERT: assert occured in GetValue!" << endl;
}




// array[i+1][(int)(columns/j-columns/(2*j))]=array[i][(int)(columns/j)]-2;

cout << endl;
cout << "array["<<i+1<<"]["<<(int)(columns/j-columns/(j*2))<<"]= " << array[i+1][(int)(columns/j-columns/(2*j))];
cout << endl;

//}
//if (array[i][(int)(columns/j+1)]>1)
//{
try {
double v5 = GetValue(array, n+2, (int)columns, i,(int) (columns/j)+1);
double v6 = GetValue(array, n+2,(int)columns, i+1,(int)(columns/j+columns/(j*2)+1));
v6=v5-2;
}
catch(exception ex)
{
cout << "ASSERT: assert occured in GetValue!" << endl;
}





// array[i+1][(int)(columns/j+columns/(2*j)+1)]=array[i][(int)(columns/j)+1]-2;
cout << endl;
cout << "array["<<i+1<<"]["<<(int)(columns/j+columns/(2*j)+1)<<"]= " << array[i+1][(int)(columns/j+columns/(2*j)+1)];
cout << endl;
//}
//if (array[i][(int)(columns/(j*2)+2)]>0)
//{

try {
double v7 = GetValue(array, n+2, (int)columns, i,(int) (columns/j)+2);
double v8 = GetValue(array, n+2,(int)columns, i+1,(int)(columns/j+columns/(j*2)+2));
v8=v7-1;
}
catch(exception ex)
{
cout << "ASSERT: assert occured in GetValue!" << endl;
}








// array[i+1][(int)(columns/j+columns/(2*j)+2)]=array[i][(int)(columns/j+2)]-1;
cout << endl;
cout << "array["<<i+1<<"]["<<(int)(columns/j+columns/(2*j)+2)<<"]= "<< array[i+1][(int)(columns/j+columns/(2*j)+2)];
cout << endl;
//}
//std::cout<<std::dec << array[i,(int)(columns/pow(2,i))-(int)(columns/pow(2,i+1))];
}



/*for (int i=0; i<n; i++)
{
if (i==1)
{
cout << "Begining: ";
}
else if (i % 2 ==1)
{
cout << "Player2: ";
}
else if (i % 2 == 2)
{
cout << "Player1: ";
}
for (int j=2; j<=pow(2,i); j=2*j)
{
if (array[i+1][(int)(columns/j-columns/(j*2))-1]<0||array[i+1][(int)(columns/j-columns/j*2)]<0||array[i+1][(int)(columns/j+columns/(j*2)+1)]<0||array[i+1][(int)(columns/j+columns/(2*j)+2)]<0)
{
cout << " ";
}
else
{
//std::cout<<std::dec << array[i,j]<<",";
//std::cout << array[i,j];
std::cout<<std::dec << array[i+1][(int)(columns/j-columns/(j*2))-1];
std::cout<<std::dec << array[i+1][(int)(columns/j-columns/j*2)];
std::cout<<std::dec << array[i+1][(int)(columns/j+columns/(j*2)+1)];
std::cout<<std::dec << array[i+1][(int)(columns/j+columns/(2*j)+2)];
}




}
cout << endl;
}*/

for (int i=1; i<n+1; i++)
{
if (i==1)
{
cout << "Begining: ";
}
else if (i % 2 ==1)
{
cout << "Player2: ";
}
else if (i % 2 == 0)
{
cout << "Player1: ";
}
for (int j=0; j<columns; j++)
{
if (array[i][j]<0)
{
cout << " ";
}
else
{
//std::cout<<std::dec << array[i,j]<<",";
std::cout<<std::dec << array[i,j];
//std::cout<<std::dec << array[i][(int)(columns/j-columns/(j*2))-1];
//std::cout<<std::dec << array[i][(int)(columns/j-columns/j*2)];
//std::cout<<std::dec << array[i][(int)(columns/j+columns/(j*2)+1)];
//std::cout<<std::dec << array[i][(int)(columns/j+columns/(2*j)+2)];
}




}
cout << endl;
}

cout << endl;
cout << endl;
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();


}
Jun 24, 2016 at 12:49pm
closed account (48bpfSEw)
I run your code on http://cpp.sh/ with no error.

This is the log:

This is the game that is called: Remove the prelast nucleotide of the second sequence and win!
Please select the maximum of the first sequence (n): 3
Please select the maximum of the second sequence (m): 2
columns= 64

Press ENTER to continue...



-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1

3
2
3
2
1 iterration!

j= 2
columns/j= 3264



Topic archived. No new replies allowed.