Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ Matrices Question


  • Please log in to reply

#1
goldfish04

goldfish04

    Member

  • Member
  • PipPip
  • 11 posts
I know posting your HW ?'s in forums is kinda forbidden in some ways but I have a Project to do and I am unsure where to start. I am not asking for the code for the entire program though that would be nice. I am just looking to get help on where to start with this. It has to do with Arrays and Matrices and I am a little shaky with arrays so i figured I would post and see if anyone was willing to help. This is what we have to do
If you have any ideas on starting and stuff please post back or email me at [email protected]. Any help would be appreciated. :tazz: Thanks IN advance

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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
  • 0

Advertisements


#2
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Hey goldfish,

I think you're going to have more luck if you get started on this on your own and then ask specific questions when you get stuck. As for how to get started, one way would be to think about what kind of objects you will need and what functionality they will need to provide. For example, maybe you need a matrix object with the abitlity to initialize itself from a file and count the white blocks. Then you might want to think about the order in which you will use your objects and their functionality.

Start with that and post again if you run into problems.

-Ricci
  • 0

#3
goldfish04

goldfish04

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
This is what I have so far. Any Tweaking would be nice. Is this what I wanna do for the ? proposed in my first post?? or am I way off track??

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
ifstream inf;//input file stream
ofstream opf;//output file stream
fstream iof;//input file stream and output append file

int i, t, nums[10][10];
char file_name [100];// Array to hold the file name

for (int i = 0; i < 40; i++)//clears storage
file_name[i] = '\0';

cout << "enter input file name:";
cin >> file_name; //file_in.txt
inf.open(file_name);//connects external file name
//with internal handle
if(!inf) {cout << "file open error";
return 0;//exit main here
}
cout << "enter your output file name: ";
cin >> file_name; //myfile.txt
if (!opf) {cout << "file open error output";
return 0;//exit main here
}
opf.open(file_name);//connects external file name
//with internal handle

while(!inf.eof())//test for end of file on input
{
cout << nums[t][i];
}
inf.close();//close file
opf.close();//close file

cout << "enter file name of your created output file for appending";
cin >> file_name; //myfile.txt
//reopen your output file created above for appending
opf.open(file_name,ios::app);
if(!opf) {return 0;}//exit main here
//append to end of file
opf.close();

cout << "enter file name of your created output file for input and appending";
cin >> file_name; //myfile.txt
iof.open(file_name,ios::in|ios::app);
if(!iof) {return 0;}//exit main here
return 0;//all done ready to exit

for(t=0; t < 10; ++t)
{
for(i=0; i < 10; ++i)
{
nums[t][i] = (t+0);
cout << nums[t][i] << ' ';
}
cout << '\n';
}
return 0;
}

Edited by goldfish04, 28 November 2005 - 02:50 PM.

  • 0

#4
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
That's certainly a good start. You seem to be well on the right track for reading the input file. You might want to look closely at your code where you loop through and do the reading:

while(!inf.eof())//test for end of file on input
{
	cout << nums[t][i];
}
There's going to be a couple problems here. I don't think you have the correct stream or operator (<<), and I don't see where t or i ever change while you're in this loop.

I also really think this problem is complicated enough to require some division of labor. For example, I'd create a function called something like ReadMatrix that will take care of all of your input for you. Then you can have one called CountBlocks or something to do all your computing. This is not a trivial problem, and I think you'll find that using one function for it will cause that function to get unmanagable quickly.

The last thing I'd mention for now would be that I'm not sure why you need so much file io. From what you said about the requirements in your original post, it seems like the only file you need is the input file -- no need for an output file or an intermediate file. Your matrices are limited to 10x10 in size. That's plenty small for just using memory instead of disk.

-Ricci
  • 0

#5
goldfish04

goldfish04

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Well I got all the code done for the matrix portion. Now I just have to implement the input file streams. COuld you give me an example of what you meant on the code below. Not all of it just a snippet or someting so i have something to follow. Thanks.

~~~~~~~~Code~~~~~~~~~~

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

int numwhite = 0 ;//Calcs the whitesquares
int numshaded = 0;//Calcs the Shaded squares
int numsquares = 0;//What I want to check to see how many squares of that number
int i, t, nums[10][10];

for(i = 0; i < 3; i++)
{
for(t = 0; t < 3; t++)
{
cout << "Enter [" << i << "][" << t << "] :";
cin >> nums[i][t];
if(nums[i][t] != 0)
{
numwhite++;
}
if(nums[i][t] == 0)
{
numshaded++;
}
}
}
cout << "Your Matrix is: " << endl;
for(i = 0; i < 3; i++)
{
for(t = 0; t < 3; t++)
{
cout << nums[i][t] << endl;
}
}
cout << "The number of white squares was " << numwhite << endl;
cout << "The number of shaded squares was " << numshaded << endl;

return 0;
}

Edited by goldfish04, 28 November 2005 - 09:43 PM.

  • 0

#6
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
I'm not sure what you wanted the example about, but I'm going to assume you were talking about the division of labor. I would do something like this:

void GetInputMatrix(/*Include whatever parameters are necessary*/);
void CountBlocks(/*Include whatever parameters are necessary, and possibly change the return type*/);
void DisplayResults(/*Include whatever parameters are necessary*/);

int main()
{
	// Call GetInputMatrix
	// Call CountBlocks
	// Call DisplayResults

	return 0;
}

void GetInputMatrix(/*Include whatever parameters are necessary*/)
{
	// Your implementation
}

void CountBlocks(/*Include whatever parameters are necessary, and possibly change the return type*/)
{
	// Your implementation
}

void DisplayResults(/*Include whatever parameters are necessary*/)
{
	// Your implementation
}

On another note, I'm confused why you have changed from reading the matrix from an input file to prompting the user for it. The requirements you originally posted said to read it from a file. If you are going to prompt the user for it, I think you'll need to prompt them for a 10x10 matrix instead of the 3x3 matrix you're doing now. You have the following code (indentations added):

for(i = 0; i < 3; i++)
{
	for(t = 0; t < 3; t++)
	{
		cout << "Enter [" << i << "][" << t << "] :";
		cin >> nums[i][t];
		if(nums[i][t] != 0)
		{
			numwhite++;
		}
		if(nums[i][t] == 0)
		{
			numshaded++;
		}
	}
}

You should loop in both of these loops while i and t are less than 10.

-Ricci
  • 0

#7
goldfish04

goldfish04

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
No, Sorry for the Confusion. I have it set at 3 becuase it is smaller and easier to work with. Once I get it fully working I will switch it to 10. My ? now is how do I access the text file through the program. Was that what i was using before..??
  • 0

#8
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Yes, before you were trying to reading everything from a text file. There were a couple problems with how you were doing it (see me second post). You were also opening a few other files, which I don't think will be necessary. Just use the input file.

-Ricci
  • 0

#9
goldfish04

goldfish04

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Thanks, But while I am reading the data from the text file. Now my loop will be messed up technically wont it? I will probably take it out?? Also, How will I check to see what squares have a number and which have a zero. If I am bringing it in from a text file??
  • 0

#10
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Yes, you will need to change your loop a little, although reading from a file is not supposed to be any different than reading from the console. Just don't prompt the file for input. :tazz:

How will I check to see what squares have a number and which have a zero. If I am bringing it in from a text file??

You can check your squares as you read them if you want, just like you do when you input from the console. This isn't going to solve your problem though. You are not trying to count how many squares have a number, you are trying to count how many groups of squares have a number. That is a much more complicated problem. You probably don't want to try to solve it while you are reading the files. You can always loop through the matrix later, since you are storing it in your arrays in memory.

-Ricci
  • 0

Advertisements


#11
goldfish04

goldfish04

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

int name;
int numwhite = 0 ;//Calcs the whitesquares
int numshaded = 0;//Calcs the Shaded squares
int numsquares = 0;//What I want to check to see how many squares of that number
int i, t, nums[10][10];
char file_name[20]
ifstream inf;//input file stream


for(i = 0; i < 3; i++)
{
for(t = 0; t < 3; t++)
{
cout << "Please enter the file you would like to create your matrix from: ";
cin >> file_name; //file_in.txt
inf.open(file_name);//connects external file name
//with internal handle
if(!inf) {cout << "file open error";
return 0;//exit main here
}
if(nums[i][t] != 0)
{
numwhite++;
}
if(nums[i][t] == 0)
{
numshaded++;
}
}
}
cout << "Your Matrix is: " << endl;
for(i = 0; i < 3; i++)
{
for(t = 0; t < 3; t++)
{
cout << nums[i][t] << endl;
}
}
cout << "The number of white squares was " << numwhite << endl;
cout << "The number of shaded squares was " << numshaded << endl;

cin >> name;
return 0;
}
  • 0

#12
goldfish04

goldfish04

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
Is taht what I wanna do with teh file stream. I still gotta fix the counters but that should work for the filestream correct.>?
  • 0

#13
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts

Is taht what I wanna do with teh file stream. I still gotta fix the counters but that should work for the filestream correct.>?

The following code that you have in your main function will correctly open the input file:
cout << "Please enter the file you would like to create your matrix from: ";
cin >> file_name; //file_in.txt
inf.open(file_name);//connects external file name
//with internal handle 
if(!inf) {cout << "file open error";
return 0;//exit main here
}
The problem is that you have placed it inside the loop you should be using to read each element in the file. That means that the user will be prompted to open the file every time an element should be read. You should think about putting that section of code before the loop. Then, inside the loop you actually need to read from the file, just like you would from the console. When you were reading from the console, you did:
cin >> nums[i][t];

Now that you want to read from a file, you should do:
inf >> nums[i][t];

-Ricci
  • 0

#14
goldfish04

goldfish04

    Member

  • Topic Starter
  • Member
  • PipPip
  • 11 posts
ok, Now I am guessing for the calculating the numsquares or how many squares in each block I will have to do a check against the row and column to see if it is a 1 or a 0 and if its a 1 and the next row or column is a 1 or a 0 then i have to increment blocks by however many they're are. Does that sound right? Or what would you suggest>?
  • 0

#15
ricci

ricci

    Member

  • Member
  • PipPip
  • 64 posts
Something like that is about right. You will want to do this in a new loop, not the same one you use to input the matrix. You are also going to have to keep track of the squares you've already counted to make sure you don't count them twice.

-Ricci
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP