# Array matrices Question

• (2 Pages)
• 1
• 2

## 21 Replies - 2541 Views - Last Post: 02 December 2005 - 10:26 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=13472&amp;s=ac9ec4900939e75cb51fdaf8271d3a5d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gsnipe77

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 11-August 05

# Array matrices Question

Posted 28 November 2005 - 11:56 AM

I am just looking for help to get started with this one or what I would start out doing or what I will have to use. This is the question I must satisfy. I am not asking for code just what I should use to start with this if anyone can help that would be great..... Like I said I AM NOT LOOKING FOR THE ANSWERS OR CODE FOR THIS...I just want to know where I should start. I am confused and have a lot of trouble with arrays. This one out of the local book at the Library is causing me some trouble.

~~~~~~~~~~~~~~~~~Question~~~~~~~~~~~~~~~~~~~~~~~
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.

Is This A Good Question/Topic? 0

## Replies To: Array matrices Question

### #2 WolfCoder

• Isn't a volcano just an angry hill?

Reputation: 815
• Posts: 7,684
• Joined: 05-May 05

## Re: Array matrices Question

Posted 28 November 2005 - 12:04 PM

I can give you a good hint:

Use two FOR LOOPS (row,column).

Use either a single or two-dimensional array.

### #3 gsnipe77

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 11-August 05

## Re: Array matrices Question

Posted 28 November 2005 - 12:17 PM

So set up 1 for Loop to check to see if the row it is white and/or have numbers and then a second to check and see if the columns are shaded or have numbers. And I am thinking it would be best 2 use a 2-D array??

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Array matrices Question

Posted 28 November 2005 - 12:20 PM

A two d array would be more efficient...it allows you to use only one data structure...it can be done with single d arrays, though.

### #5 gsnipe77

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 11-August 05

## Re: Array matrices Question

Posted 28 November 2005 - 01:26 PM

Well This is what I have coded so far. I am just looking for Some tweaking with probably my for statements and the file I/O. Thanks,...
~~~~~~~~~~~~~~~~~CODE~~~~~~~~~~~~~~~~~~~~~~~~

#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];
int name;
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

inf >> s.fname;
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;
}

### #6 Mrafcho001

Reputation: 41
• Posts: 769
• Joined: 01-November 05

## Re: Array matrices Question

Posted 28 November 2005 - 01:36 PM

#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];
int name; //name is never used through the program
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

inf >> s.fname; //What is s? s is undefined..
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;
}

Maybe where it says File open error, you could ask the user if they want to create the file. That would speed things up if they dont have the files.
only problem is that s is undefined, maybe its just a silly typing error?
Look for my comment^ up in the code

Also may i suggest that you use either code tags or a color coder (one can be found in my signature (follow the links)). Makes the code easier to read

Thank you!

This post has been edited by Mrafcho001: 28 November 2005 - 01:41 PM

### #7 gsnipe77

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 11-August 05

## Re: Array matrices Question

Posted 28 November 2005 - 03:55 PM

See But I am still confused. If I use the For loops I don't see how they will help to create the matrix. I think that from the question would I not want to do a check to see if the squares have a zero or a non zero to decide whether they are shaded or not. I guess my ? is how do I get the input from a user and put it into an array so that it can be displayed and then i can check the users input against being a zero or a non zero.

### #8 Mrafcho001

Reputation: 41
• Posts: 769
• Joined: 01-November 05

## Re: Array matrices Question

Posted 28 November 2005 - 04:39 PM

You can still use the for loops to check if they are 0's 1's or w/e

for(int i = 0; i < 10; i++)
{
for(int t = 0; t < 10; t++)
{
if(nums[i][t] == 0)
//add one to the count of the 0s
if(nums[i][t] == 1)
//add one to the count of the 1s
//And so on
}
}

in the for loops check if its a 0, 1, 2, or w/e, and if it is one of those you add one to a variable that keeps count of the them.

### #9 gsnipe77

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 11-August 05

## Re: Array matrices Question

Posted 28 November 2005 - 05:11 PM

Mrafcho001, on 28 Nov, 2005 - 05:36 PM, said:

You can still use the for loops to check if they are 0's 1's or w/e

for(int i = 0; i < 10; i++)
{
for(int t = 0; t < 10; t++)
{
if(nums[i][t] == 0)
//add one to the count of the 0s
if(nums[i][t] == 1)
//add one to the count of the 1s
//And so on
}
}

in the for loops check if its a 0, 1, 2, or w/e, and if it is one of those you add one to a variable that keeps count of the them.

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.

That part above is where I am getting stuck now.

now with that ? how would I go about doing that. This is what i revamped as of right now with the getting and putting of the file. Just the creation and checking of the matrix. Also I know and have heard of a Search function that can be used would I want to stay away from that. I probably do considering i want to keep things simple.

#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 name, matrix;
int i, t, nums[10][10];

// This is where i want to prompt the user to enter data for a matrix. Is there code that can do that??

cout << " Please enter your first matrix " << endl;
cin >> //Not Sure what to put here to get the data from the user and put it into the matrix.

if(nums[t][i] != 0)
{
numwhite++;
}
if(nums[t][i] = 0)
{
}

cin >> name;
return 0;
}

• g+ + -o drink whiskey.cpp

Reputation: 248
• Posts: 13,507
• Joined: 12-July 02

## Re: Array matrices Question

Posted 28 November 2005 - 05:30 PM

This
``` if(nums[t][i] = 0)

```

is using an assignment operator...it should read
``` if(nums[t][i] == 0)

```

...the equality operator.

### #11 Mrafcho001

Reputation: 41
• Posts: 769
• Joined: 01-November 05

## Re: Array matrices Question

Posted 28 November 2005 - 05:35 PM

Amadeus, on 28 Nov, 2005 - 06:27 PM, said:

This
``` if(nums[t][i] = 0)

```

is using an assignment operator...it should read
``` if(nums[t][i] == 0)

```

...the equality operator.

Yes, or it will always return true. Causing unwanted side effects that can be hard to spot.

One idea of solving this problem is to put the the number fist like so:

```if(1 == variable)

```

if you happened to write only one =
```if(1 = variable) // Compiler error!

```

### #12 gsnipe77

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 11-August 05

## Re: Array matrices Question

Posted 28 November 2005 - 05:58 PM

Now how woudl I get data from user to store into my matrix. that is my overall goal i guess before I can test the users input against being a 1 or a 0 i have to get it. But How do i get enough numbers to store it into a matrix>?? Is there an operator or function to use for this./

### #13 Mrafcho001

Reputation: 41
• Posts: 769
• Joined: 01-November 05

## Re: Array matrices Question

Posted 28 November 2005 - 06:11 PM

The way I see this done is with 2 for loops:

for(int i = 0; i < 10; i++)
{
for(int t = 0; t < 10; t++)
{
cout << "Enter [" << i << "][" << t << "] :";
cin >> matrice[i][t];
}
}
//The Matrice should be filled by the end of the loops
//But the user would have to input 100 numbers! seems a bit too much?

Thats how i see it done, maybe there is a different way.

This post has been edited by Mrafcho001: 28 November 2005 - 06:12 PM

### #14 gsnipe77

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 11-August 05

## Re: Array matrices Question

Posted 28 November 2005 - 07:22 PM

I know that may seem like a lot but if you look up at my first post it has to account for the possibility that the user enters a 10 X 10 matrix. thanks a lot though that is what i was looking for. I will go work on it now and tweak it and hopefully return with a succesful project. I will post any more ?'s I have. but i am very greatful for the help. I just couldnt figure ths out for the life of me and i have 2 books in front of me.

### #15 gsnipe77

• New D.I.C Head

Reputation: 0
• Posts: 34
• Joined: 11-August 05

## Re: Array matrices Question

Posted 28 November 2005 - 07:36 PM

Ok, SO i ran my program slimmed it down to 3 X 3 just to see if it worked and i got all the data entered but when I went to output everything I got a fatal error and the C:/ Prompt Window Closed. Any suggestions. Here is my complete code as of now. Also I tried downloading that color thing from your site and it didn't work??

#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(int i = 0; i < 3; i++)
{
for(int t = 0; t < 3; t++)
{
cout << "Enter [" << i << "][" << t << "] :";
cin >> nums[i][t];
if(nums[i][t] != 0)
{
numwhite++;
}
if(nums[i][t] == 0)
{
}
}
}

cout << "Your Matrix is: " << endl;
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;
}