Array matrices QuestionStumped on the Start
21 Replies - 2079 Views - Last Post: 02 December 2005 - 10:26 AM
#1
Array matrices Question
Posted 28 November 2005 - 11:56 AM
~~~~~~~~~~~~~~~~~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.
Replies To: Array matrices Question
#2
Re: Array matrices Question
Posted 28 November 2005 - 12:04 PM
Use two FOR LOOPS (row,column).
Use either a single or two-dimensional array.
#3
Re: Array matrices Question
Posted 28 November 2005 - 12:17 PM
#4
Re: Array matrices Question
Posted 28 November 2005 - 12:20 PM
#5
Re: Array matrices Question
Posted 28 November 2005 - 01:26 PM
~~~~~~~~~~~~~~~~~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
Re: Array matrices Question
Posted 28 November 2005 - 01:36 PM
#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
Re: Array matrices Question
Posted 28 November 2005 - 03:55 PM
#8
Re: Array matrices Question
Posted 28 November 2005 - 04:39 PM
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
Re: Array matrices Question
Posted 28 November 2005 - 05:11 PM
Mrafcho001, on 28 Nov, 2005 - 05:36 PM, said:
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)
{
numshaded++;
}
cin >> name;
return 0;
}
#10
Re: Array matrices Question
Posted 28 November 2005 - 05:30 PM
if(nums[t][i] = 0)
is using an assignment operator...it should read
if(nums[t][i] == 0)
...the equality operator.
#11
Re: Array matrices Question
Posted 28 November 2005 - 05:35 PM
Amadeus, on 28 Nov, 2005 - 06:27 PM, said:
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
Re: Array matrices Question
Posted 28 November 2005 - 05:58 PM
#13
Re: Array matrices Question
Posted 28 November 2005 - 06:11 PM
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
Re: Array matrices Question
Posted 28 November 2005 - 07:22 PM
#15
Re: Array matrices Question
Posted 28 November 2005 - 07:36 PM
#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)
{
numshaded++;
}
}
}
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;
}
|
|

New Topic/Question
Reply



MultiQuote






|