r/programminghelp • u/vaslui-berceni • Mar 02 '21
C++ A simple fill() algorithm.
Hello. I am a newbie programmer and I need help. This algorithm is supposed to give a number of the 0s next to "a[l][c]". My teacher taught me this and I added the a[l][c]=1 because without that it called the function indefinitly and basically gave no answer. It still does not work. Can somebody help me? (suma means sum)
#include <iostream>
int suma, a[20][20], n, l, c;
using namespace std;
void fill()
{
suma++;
cout << "Suma has increased" << endl;
l--;
if (l > 0 && a[l][c] == 0)
{
a[l][c] = 1;
fill();
cout << "a[" << l << "][" << c<<"] has become 1" << endl;
}
l = l + 2;
if (l < n - 1 && a[l][c] == 0)
{
a[l][c] = 1;
fill();
cout << "a["<<l<<"]["<<c<<"] has become 1"<<endl;
}
l--;
c--;
if (c > 0 && a[l][c] == 0)
{
a[l][c] = 1;
fill();
cout << "a[" << l << "][" << c<<"] has become 1" << endl;
}
c = c + 2;
if (c < n + 1 && a[l][c] == 0)
{
a[l][c] = 1;
fill();
cout << "a[" << l << "][" << c<<"] has become 1" << endl;
}
c--;
}
int main()
{
int i, j;
cin >> n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
cout << "a[" << i << "][" << j << "]= ";
cin >> a[i][j];
}
cout << endl;
}
cin >> l >> c;
suma = 0;
if (a[l][c] == 0)
fill();
cout << suma;
}
2
u/[deleted] Mar 02 '21
I believe /u/jedwardsol found your issue, and please, the next time you post, follow the rules. Specifically Rule #2 in this case.