ok so here is what im doin.

Given a square matrix, write a program that determines the number of white(numbered) blocks and total number of squares in each of the white blocks. By definition, the outside boundaries of the matrix must be shaded(0). A block of white squares consists of all of the white squares whose side boundaries are next to another white square. White squares that touch only a diagonal point are not adjacent. Accommodate a maximum of 10 blocks and a maximum matrix of 10 x 10 .
In the diagram below, block 1 contains three squares, and block 4 contains nine squares. Note that block 3 contains only one square. It touches block 1 only on the diagonal.

0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 2 2 2 2 0
0 1 1 0 0 2 2 2 2 0
0 0 0 3 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 4 4 4 4 4 0 0
0 0 0 0 4 4 4 0 0 0
0 0 0 0 0 4 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

Obtain the square definition from a text file. The file should contain a square matrix composed of zeros for shaded squares and non-zeros for the white squares. The input for the diagram above is shown below.
.input:
0000000000
0010022220
0110022220
0003000000
0000000000
0004444400
0000444000
0000040000
0000000000
0000000000

the only think i have a question on how to do it is how will i print out how many squares there are in each block?

Recommended Answers

All 15 Replies

Post an attempt. I'd imagine it might look something like this, though.

#include <stdio.h>

void print(const int value[10][10])
{
   int r, c;
   for ( r = 0; r < 10; ++r )
   {
      for ( c = 0; c < 10; ++c )
      {
         printf("%d", value[r][c]);
      }
      putchar('\n');
   }
   putchar('\n');
}

damn it I wanted to delte this post... but since it wont do try some simple variation of dfs or bfs just counting the steps taken...

ok this is what i got so far and im gettin 3 errors

# include <iostream>
# include <fstream>
# include <string>

const int value = 10;

void openFile();
void print(const int value[10][10]);


using namespace std;

void main()
{
	
	
	openFile();
	print(const value[10][10]);


}

void openFile()
{

	ifstream inFile;

	string inFileName,
		        line1,
				line2,
				line3,
				line4,
				line5,
				line6,
				line7,
				line8,
				line9,
				line10;

	cout << "Enter the file name: " << endl;
	cin >> inFileName;
	inFile.open(inFileName.c_str());
	if (!inFile.is_open())
	{
		cerr << "Cannot open file: " << inFileName << endl;
	};

	inFile >> ws;
	getline(inFile, line1, '#');
	inFile >> line1;

	inFile >> ws;
	getline(inFile, line2, '#');
	inFile >> line2;

	inFile >> ws;
	getline(inFile, line3, '#');
	inFile >> line3;

	inFile >> ws;
	getline(inFile, line4, '#');
	inFile >> line4;

	inFile >> ws;
	getline(inFile, line5, '#');
	inFile >> line5;

	inFile >> ws;
	getline(inFile, line6, '#');
	inFile >> line6;

	inFile >> ws;
	getline(inFile, line7, '#');
	inFile >> line7;

	inFile >> ws;
	getline(inFile, line8, '#');
	inFile >> line8;

	inFile >> ws;
	getline(inFile, line9, '#');
	inFile >> line9;

	inFile >> ws;
	getline(inFile, line10, '#');
	inFile >> line10;

	inFile.close();
}


void print(const int value[10][10])
{
   int r, c;
   for ( r = 0; r < 10; ++r )
   {
      for ( c = 0; c < 10; ++c )
      {
         printf("%d", value[r][c]);
      }
      putchar('\n');
   }
   putchar('\n');
}

ERRORS:
syntax error : missing ')' before 'const'
'print' : function does not take 0 parameters
syntax error : ')'

all are goin to this line:
print(const value[10][10]);

i just wanted to try urs our to see what it would do then i could go from there

You'd call the function like this,

print(matrix);

if matrix were declared like this.

int matrix[10][10];

As in,

#include <stdio.h>

void print(const int value[10][10])
{
   int r, c;
   for ( r = 0; r < 10; ++r )
   {
      for ( c = 0; c < 10; ++c )
      {
         printf("%d", value[r][c]);
      }
      putchar('\n');
   }
   putchar('\n');
}

int main(void)
{
   int matrix[10][10] =
   {
      {0,0,0,0,0,0,0,0,0,0},
      {0,0,1,0,0,2,2,2,2,0},
      {0,1,1,0,0,2,2,2,2,0},
      {0,0,0,3,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0},
      {0,0,0,4,4,4,4,4,0,0},
      {0,0,0,0,4,4,4,0,0,0},
      {0,0,0,0,0,4,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0},
      {0,0,0,0,0,0,0,0,0,0},
   };
   print(matrix);
   return 0;
}

/* my output
0000000000
0010022220
0110022220
0003000000
0000000000
0004444400
0000444000
0000040000
0000000000
0000000000
*/

It may have been helpful to mention C or C++ to start. ;)

oops sorry i for got mention that lol c++.

but i got to read the matrix from an input file though it cant be hardcoded in there.

and that just reprints the matrix what i cant figure out is how to print out how many squares are in each block or how many of each number > 0 there is

but i got to read the matrix from an input file though it cant be hardcoded in there.

Don't let me stop you.

and that just reprints the matrix what i cant figure out is how to print out how many squares are in each block or how many of each number > 0 there is

I'd imagine that you'd want to write a function that receives the array much like the print function, but goes through and counts nonzero values -- perhaps returning the count as a result.

ok now the only problem im havin is i cant get the first line to display but everything else is fine.

# include <iostream>
# include <fstream>
# include <string>

const int value = 10;

void openFile();



using namespace std;

#define cls   system("cls")
#define pause system("pause")

void main()
{

	
	openFile();
	


}

void openFile()
{

	ifstream inFile;
	ofstream outFile;

	string inFileName,
		  outFileName,
		        line1,


				row1,
				row2,
				row3,
				row4,
				row5,
				row6,
				row7,
				row8,
				row9,
				row10;

	cout << "Enter the input file name: " << endl;
	cin >> inFileName;
	inFile.open(inFileName.c_str());
	if (!inFile.is_open())
	{
		cerr << "Cannot open file: " << inFileName << endl;
		pause;
		cls;
		exit(4);
	}
	cin.get();
	cout << "Press any key to continue: " << endl;
	cin.get();
	cls;


	cout << "Enter the output file name: " << endl;
	cin >> outFileName;
	outFile.open(outFileName.c_str());
	if(!outFile.is_open())
	{
		cout << "could not open: " << outFileName << endl;
		pause;
		cls;
		exit(5);
	}
	cin.get();
	cout << "Press any key to continue: " << endl;
	cin.get();
	cls;

	inFile >> inFileName;
	while(!inFile.eof())
	{
		inFile >> line1 >> row1;

		cout << line1 << "\n" << row1 << row2
		<< row3 << row4 << row5 << row6
		<< row7 << row8 << row9 << row10 << endl;
	}
	inFile.close();
	outFile.close();	
}

Could you please post the contents of your input file?

ok now the only problem im havin is i cant get the first line to display but everything else is fine.

# include <iostream>
# include <fstream>
# include <string>

const int value = 10;

void openFile();



using namespace std;

#define cls   system("cls")
#define pause system("pause")

void main()
{

	
	openFile();
	


}

void openFile()
{

	ifstream inFile;
	ofstream outFile;

	string inFileName,
		  outFileName,
		        line1,


				row1,
				row2,
				row3,
				row4,
				row5,
				row6,
				row7,
				row8,
				row9,
				row10;

	cout << "Enter the input file name: " << endl;
	cin >> inFileName;
	inFile.open(inFileName.c_str());
	if (!inFile.is_open())
	{
		cerr << "Cannot open file: " << inFileName << endl;
		pause;
		cls;
		exit(4);
	}
	cin.get();
	cout << "Press any key to continue: " << endl;
	cin.get();
	cls;


	cout << "Enter the output file name: " << endl;
	cin >> outFileName;
	outFile.open(outFileName.c_str());
	if(!outFile.is_open())
	{
		cout << "could not open: " << outFileName << endl;
		pause;
		cls;
		exit(5);
	}
	cin.get();
	cout << "Press any key to continue: " << endl;
	cin.get();
	cls;

	inFile >> inFileName;
	while(!inFile.eof())
	{
		inFile >> line1 >> row1;

		cout << line1 << "\n" << row1 << row2
		<< row3 << row4 << row5 << row6
		<< row7 << row8 << row9 << row10 << endl;
	}
	inFile.close();
	outFile.close();	
}

I cannot read the file that you've got on your computer. I can only guess from the 3 or so attempts at file input to divine what the contents of your file actually look like. In order for good help with the reading of your input file, again, could you post the actual contents of this INPUT (not the source) file?!

oh i thought u meant comments in the source, missread it

inputfile is a txt file with these numbers

0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 2 2 2 2 0
0 1 1 0 0 2 2 2 2 0
0 0 0 3 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 4 4 4 4 4 0 0
0 0 0 0 4 4 4 0 0 0
0 0 0 0 0 4 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

oh i thought u meant comments in the source, missread it

inputfile is a txt file with these numbers

0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 2 2 2 2 0
0 1 1 0 0 2 2 2 2 0
0 0 0 3 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 4 4 4 4 4 0 0
0 0 0 0 4 4 4 0 0 0
0 0 0 0 0 4 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

Thank you. It can be a bit confusing when you have attempts that look like this:

inFile >> ws;
	getline(inFile, line3, '#');
	inFile >> line3;

This would suggest to me that the numbers might be delimited by a # , such as this.

0#0#1#0#0#2#2#2#2#0

So...

Where do you ever put anything into row1, row2, etc.? Where do you write to the output file? Did you know that cin is whitespace delimited -- which means it won't read a line containing whitespace?

Don't use void main() -- use int main() . Indent nicely.

[edit]And this has no place past your first week of programming:

#define cls   system("cls")
#define pause system("pause")

Thank you. It can be a bit confusing when you have attempts that look like this:

inFile >> ws;
	getline(inFile, line3, '#');
	inFile >> line3;

This would suggest to me that the numbers might be delimited by a # , such as this.

0#0#1#0#0#2#2#2#2#0

So...

Where do you ever put anything into row1, row2, etc.? Where do you write to the output file? Did you know that cin is whitespace delimited -- which means it won't read a line containing whitespace?

yea i know all that and i changed it all to have a while loop read in the file.

see above a couple post for the new better code dont go by what i origionally had anymore.

go to post 10 to be exact for the new code

Right. That's the one I meant. You input into row1, but not into row2, row3, etc. -- yet you try to output from them to the screen and not to the output file. And why do you try to read a filename from the file if the file does not contain a filename? And before we get too far into it let me look for the post about not controlling a loop with eof :
http://www.daniweb.com/techtalkforums/post155265-18.html

[Oops. That link also gives heavy hints on how to do what you are trying to do.]

the reasoning for me putting the rows in there cause i though thats how it would read all them but i see now.

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.