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

why is the return type of whitespace(...) void ?

why is the return type of whitespace(...) void ?

aren't recursion functions suppose to be void?? or am i mistaken?

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;
}

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).

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.