i've been struggling with the code of this program, i've done most of it but it keeps giving me error messages, need some help with it, any insights would be really appreciated.
Here's my code for it:
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
#include<time.h>
using namespace std;
int size(bool flag)
{
int test;
if(flag)
{
do
{
test=(int)(double(rand())/double(RAND_MAX)*14.0+3.0);
}while(test%2==0);
return test;
}
else
return 7;
}
void fillandprint()
{
int n;
bool flg=true;
n=size(flg);
int a[19][19];
int raw,column,l;
l=n*n;
int index;
raw=0;
column=(n/2);
a[raw][column]=1;
index=1;
#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
#include<time.h>
usingnamespace std;
int size (bool flag){
int test;
if (flag){
do{
test = (int) (double (rand ()) / double (RAND_MAX) * 14.0 + 3.0);
}while (test % 2 == 0);
return test;
}elsereturn 7;
}
void fillandprint (){
int n;
bool flg = true;
n = size (flg);
int a[19][19];
int raw, column, l;
l = n * n;
int index;
raw = 0;
column = (n / 2);
a[raw][column] = 1;
index = 1;
int i = 0;
while (i < l - 1){
if (raw == 0){
a[raw][column] = (index + 1);
raw = n - 1;
column++;
index++;
i++;
}elseif (column == (n - 1)){
a[raw][column] = (index + 1);
raw++;
column = 0;
index++;
i++;
}elseif (a[raw][column] != 0){
raw++;
index = index - 1;
}else{
a[raw][column] = (index + 1);
raw--;
column++;
i++;
}
}
for (int r = 0; r < n; r++){
for (int k = 0; k < n; k++){
cout << a[r][k] << " ";
if (k == n - 1){
cout << endl;
}
}
}
return;
}
int main (){
fillandprint ();
return (0);
}
Next time use tags. I have better things to do than formatting code.
Line 24: int a[19][19]; This creates a 19x19 matrix, not a matrix whose last element is 19,19. Changing this line to int a[20][20]; doesn't cause the program to crash, bu I don't know if the results are correct.
Mate, i am new to this forum, let me first get used to it.
Anyway, changing the array to 20X20 size did not help stopping the crash. I am pretty sure that i am doing the right logic in the if-else statements. I guess my problem would be involving getting out of range or something.
I chose a 19X19 size since this is the biggest size i would get from the random generator for the size of the array (3-17).
I had to use a while statement for my loop(instead of a for loop) because i wasn't sure how many steps i would need for the i counter, since there are some elements that you can't fill ( the elements that are already filled! ).