Recurrsion grid
My program is suppose to create a two dimensional array that is read from a file. And use a recursive function to find how many white areas ('w'). My program runs with no errors but i cant get any response from my recursive function.
Cpp file:
/*
This program accepts a file with a grid 6 by 6 and uses a recursive function to find the white spaces ('w') in the grid.
The recursive function does not work though, but from diffferent test i know that it does read in the file.
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void whitespaces(char[8][8], int, int);
int main()
{
char array[8][8], letter;
int count =0, squareCount=0;
for (int i=0; i<8; i++)
{
for(int j=0; i<8; i++)
{
array[i][j]=='n';
}
}
for (int i =0; i<8; i++)
array[0][i]='b';
for (int i = 0; i<8; i++)
array[i][0]='b';
for (int i=0; i<8; i++)
array [7][i] ='b';
for (int i=0; i<8; i++)
array[i][7]='b';
ifstream testfile;
string myfile;
cout<<"This program counts the number of white spaces in a 6X6 grid";
cout<<endl;
cout << "Enter file name that contains the grid: ";
cin >> myfile; //read in file name
testfile.open(myfile.c_str()); //open file
if(!testfile.is_open())
{
cout<< "Error! This file cannot be found; "<<endl;
exit(1);
}
while(!testfile.eof())
{
for (int i=2; i<7; i++)
{
for(int j=1; i<7; i++)
{
if(array[i][j]=='n')
{
testfile>>letter;
array[i][j]=letter;
}
}
}
}
whitespaces(array, count, squareCount);
return 0;
}
void whitespaces(char array[8][8],int count, int squareCount)
{
cout<<count;
while(squareCount<64)
{
for (int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
if (array[i][j]=='w')
{
whitespaces(array, count+1, squareCount+1);
}
}
}
}
cout<<count;
}
data file
bwbwbw
wwwwww
wwwbbb
wbwbwb
bwwbww
wbbwbb
bbbwww
bwbwwb
Related Article: Help: Recursion/Arrays
is a C++ discussion thread by theoryforlife that has 1 reply, was last updated 2 years ago and has been tagged with the keywords: arrays, recursion.
rena0514
Junior Poster in Training
57 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
why is the return type of whitespace(...) void ?
griswolf
Veteran Poster
1,176 posts since Apr 2010
Reputation Points: 344
Solved Threads: 262
Skill Endorsements: 1
why is the return type of whitespace(...) void ?
aren't recursion functions suppose to be void?? or am i mistaken?
rena0514
Junior Poster in Training
57 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
This is new code and my recursion still isn't working.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int whitespaces(char [8][8], int);
int main()
{
ifstream inFile ("data.txt");
char grid[8][8];
int count = 0;
while (!inFile.eof())
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
inFile>>grid[i][j];
}
}
}
for (int i = 0; i < 8; i++)
{
cout<<endl;
for (int j = 0; j < 8; j++)
{
cout<<grid[i][j];
}
}
cout<<endl;
inFile.close();
int total;
total=whitespaces(grid, count);
cout<<total;
return 0;
}
int whitespaces(char letter[8][8],int count)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if(letter[i][j]=='b')
return (letter[i][j], count);
else
return (letter[i][j], count+1);
}
}
return count;
}
rena0514
Junior Poster in Training
57 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
The essence of recursion is that you do the big job by doing "something" plus a smaller job. If the smaller job is small enough, you stop "diving down" and just do the work. For this problem, I might do the work by counting the 'w' slots in the last row of the array and adding that to the sum of the 'w' slots in the first N-1 rows. For that, you want to pass an array and a number of rows to the whitespace() function. It counts the last row, and adds that count to the return value from calling itself with the same array and a smaller count of rows, then returns the sum of the two. The function also needs to check that the number of rows is greater than zero and just return 0 if the number is zero (or just don't call further down if the number is one).
griswolf
Veteran Poster
1,176 posts since Apr 2010
Reputation Points: 344
Solved Threads: 262
Skill Endorsements: 1
The essence of recursion is that you do the big job by doing "something" plus a smaller job. If the smaller job is small enough, you stop "diving down" and just do the work. For this problem, I might do the work by counting the 'w' slots in the last row of the array and adding that to the sum of the 'w' slots in the first N-1 rows. For that, you want to pass an array and a number of rows to the whitespace() function. It counts the last row, and adds that count to the return value from calling itself with the same array and a smaller count of rows, then returns the sum of the two. The function also needs to check that the number of rows is greater than zero and just return 0 if the number is zero (or just don't call further down if the number is one).
ok thanks i think i know what you are saying...about to make another attempt
rena0514
Junior Poster in Training
57 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0