-
March 7th, 2009, 01:08 AM
#1
Why does this matrix escape me?
Ok. I have been working on a math program for quite a while now, with limited success.
You may have seen my other project before, which involved a triangular Matrix. But that has changed.
This program utilizes a square matrix that adds to a square (if you have Excel or any other similar program, now's the time to break that out.)
Start at cell A,1. The value here is 1. Now pretend that outside the chamber of cells (A,-1) are 0. Add the column down, similar to the Fibonacci Sequance, but instead of adding the 2 preceding numbers, you add all of them. The values in colum A should be: 1,1,2,4,...
Now heres the tricky part.
Go to colum B. Now you must add ACROSS as well as down
(colum B: 1,2,5,12)
do the same for colum C (2,5,14,29)
and D (4, 12, 39, 110)
Heres what it looks like:
1 1 2 4
1 2 5 12
2 5 14 39
4 12 39 110
Now here is My current problem, My code does not turn that out,
It turns out a strange sequance (I will show the area that we did, a [4x4] matrix)
|1|1|2|4|
|0|1|3|11|
|1|3|9|31|
|2|7|23|81|
as you can see there is a problem, and i have not a clue as to what it does. Here is the code, and it is well comented. If could PLEASE PLEASE PLEASE PLEASE help that would make my life.
I have spent the past month looking at this with a dumb founded look on.
If you have questions please be specific, this is a kind of confusing concept for all involved (my matmatician dad didnt get the matrix) and it doesnt help when theres a generic "i dont get what it does, can you explain it better?" question.
THANKS IN ADVANCE!!!
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <fstream>
using namespace std;
//Variables
int row; //Used for placing rows in matrix
int col; //col number
int state_row;
int state_col; //used as another portion to the rule
int pre = 0;
int size_row;
int size_col;
int matrix[10][10];
int sub;
// Functions
void txtclr() //Text Clear, used to clear the out file before the next print
{
ofstream UM("Out.txt", ios::out); //Opens "Out.txt" for clean, overwriting
UM.flush();
UM.close();
}
void rule()
{
size_row = 10; //numbers of rows ( -- )
size_col = 10; //Number or colums ( | )
for( state_col = 0; state_col != size_col; state_col++ ) //collum switch
{
pre = 0; //return pre to 0
for( state_row = 0; state_row != size_row; state_row++ ) //row switch
{
matrix[0][0] = 1;
for(row = 0; row != state_row; row++) //Vert. addition (|)
{
pre = matrix[row-1][state_col] + pre;
}
for(col = 0; col != state_col; col++) //horiz. addition (-)
{
pre = matrix[state_row][col] + pre;
}
matrix[state_row][state_col] = pre;
}
}
}
void txtprnt() //Text Print, prints out the data from rule() to a file
{
rule(); //Runs the Rule
ofstream UM("Out.txt", ios::out | ios::app); //Dump arrays onto "Out.txt" at end of
for( row = 0; row != size_row; row++) //Used to advance to next row once the first row is completed
{
UM << "|"; //Visual Character
cout << "|"; //View Real time generation
for( col = 0; col != size_col; col++ ) //Matrix Colum Builder
{
UM << matrix[row][col] << "|"; //Print the number in the space [row]x[col], and a Visual Character
cout << matrix[row][col] << "|"; //View Real time generation
}
UM << '\n'; //once row completed, move down a line
cout << '\n'; //View Real time generation
}
UM.flush();
UM.close();
system("pause");
}
//Program
int main(int argc, char *argv[])
{
txtclr(); //Clear text
txtprnt(); //Print text [Generate numbers]
return 0;
}
-
March 7th, 2009, 06:06 AM
#2
Re: Why does this matrix escape me?
Try fixing the bugs in your code first, then lets worry about whether you get the rest correct. Here's the bug:
Code:
void rule()
{
...
for(row = 0; row != state_row; row++) //Vert. addition (|)
{
pre = matrix[row-1][state_col] + pre;
}
...
}
Your row index is less than zero on the first iteration.
Btw, why did you choose to write the C++ application to resemble a C program? You've got floating (free) functions, global variables, etc. The only thing that is C++ is the usage of cout and ofstream.
Last edited by dwhitney67; March 7th, 2009 at 06:10 AM.
-
March 7th, 2009, 08:33 PM
#3
Re: Why does this matrix escape me?
Ok i removed the -1 (now its: pre = matrix[row][state_col] + pre;)
but the reason i put that there was it seemed to be the only way for it to work :
|1|1|2|4|8|
|1|3|8|20|48|
|3|10|31|88|
|8|32|112|
Interesting note, 11,3,8, is part of the Fibonacci sequence (with 2, and 5 removed) i did a little experimenting, and found a similar pattern, but they where missing other numbers from it. just a interesting thought.
And its in C++ because its my default coding file in Dev C++
-
March 15th, 2009, 06:55 PM
#4
Re: Why does this matrix escape me?
-
March 15th, 2009, 07:47 PM
#5
Re: Why does this matrix escape me?
This post appears as if it is homework related, and thus you should not expect any direct coding help to solve your problem.
If you would express the requirements of the problem in concise words, perhaps the solution will present itself to you. Posting buggy code and very little, if any, requirements, is not going to get you very far. I have already attempted to help you with the indexing of your array, but it still seems that you have not yielded the results you seek. Thus this indicates that you are not fulfilling the requirements of the problem.
For grins I wrote an application that created a square-matrix, and performed the sums of the columns and rows, and placed the result at the appropriate location within the matrix. My results are correct because I followed my requirements. My requirements may differ from yours.
-
March 15th, 2009, 10:52 PM
#6
Re: Why does this matrix escape me?
Thanks for your help.
-
March 16th, 2009, 04:03 PM
#7
Re: Why does this matrix escape me?
its not homework related at all. My highschool doesnt have a computer programming course. This is more of me watching Pi and reading some other stuff.
In short im a bad programmer with a liking of math
what were your requirements
-
March 16th, 2009, 04:07 PM
#8
Re: Why does this matrix escape me?
this also sparked an interest:
http://www.bleepingcomputer.com/forums/topic185872.html
and if you are really curious here is an old forum about the same project (started by me)
http://www.bleepingcomputer.com/forums/topic194752.html
-
March 16th, 2009, 09:04 PM
#9
Re: Why does this matrix escape me?
Originally Posted by H aun
its not homework related at all. My highschool doesnt have a computer programming course. This is more of me watching Pi and reading some other stuff.
In short im a bad programmer with a liking of math
what were your requirements
My requirements were made up, based on your OP. I did not fully understand what your requirements were, so I invented my own. Here's my code:
Code:
#include <tr1/array>
#include <iostream>
#include <iomanip>
static const size_t COLS = 4;
static const size_t ROWS = COLS;
typedef std::tr1::array<int, COLS> Row;
typedef std::tr1::array<Row, ROWS> Matrix;
void initMatrix(Matrix& m);
Matrix rule(const Matrix& m);
void displayMatrix(std::ostream& os, Matrix& m);
int main()
{
Matrix matrix;
initMatrix(matrix);
Matrix results = rule(matrix);
std::cout << "Original Matrix:" << std::endl;
displayMatrix(std::cout, matrix);
std::cout << "\nResulting Matrix:" << std::endl;
displayMatrix(std::cout, results);
}
void initMatrix(Matrix& m)
{
for (size_t i = 0; i < ROWS; ++i)
{
for (size_t j = 0; j < COLS; ++j)
{
m[i][j] = (i + j + 1) * 5;
}
}
}
Matrix rule(const Matrix& m)
{
Matrix results;
for (size_t row = 0; row < ROWS; ++row)
{
for (size_t col = 0; col < COLS; ++col)
{
int sum = 0;
for (size_t r = row; r < ROWS; ++r)
{
sum += m[r][col];
}
for (size_t c = col; c < COLS; ++c)
{
sum += m[row][c];
}
results[row][col] = sum;
}
}
return results;
}
void displayMatrix(std::ostream& os, Matrix& m)
{
for (size_t row = 0; row < ROWS; ++row)
{
for (size_t col = 0; col < COLS; ++col)
{
os << std::setw(3) << m[row][col] << " ";
}
os << std::endl;
}
}
-
March 16th, 2009, 11:23 PM
#10
Re: Why does this matrix escape me?
could you possibly post an output?
My compiler doesn't like your code
-
March 17th, 2009, 04:41 AM
#11
Re: Why does this matrix escape me?
Code:
Original Matrix:
5 10 15 20
10 15 20 25
15 20 25 30
20 25 30 35
Resulting Matrix:
100 115 125 130
115 120 120 115
125 120 110 95
130 115 95 70
-
March 17th, 2009, 10:23 AM
#12
Re: Why does this matrix escape me?
DWhitney's code use std::tr1 additions. Most compilers dont have these yet, but the latest version of microsoft visual c++ does. The link is in my sig. There are also equivalents of std::tr1 for the most part in the BOOST library, again link is in my sig, but that would require changing his code to take out the tr1 stuff and supplement it with boosts equivalents.
Do yourself a favour and either grab visual c or boost or even both!
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
March 18th, 2009, 12:25 AM
#13
Re: Why does this matrix escape me?
I FINALLY FIGURED IT OUT!!!!!!!!!!!!!
Funny things happen at 11, like good ideas
Code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <fstream>
using namespace std;
//Variables
int row; //Used for placing rows in matrix (output_
int col; //col number
int state_row; //used to state the current place the matrix its adding to
int state_col;
int loop_row; //used it addition
int loop_col;
int pre = 0;
int size_row = 10; //numbers of rows ( -- )
int size_col = 10; //Number or colums ( | )
int matrix[10][10];
int sub;
// Functions
void txtclr() //Text Clear, used to clear the out file before the next print
{
ofstream UM("Out.txt", ios::out); //Opens "Out.txt" for clean, overwriting
UM.flush();
UM.close();
}
void rule()
{
for(state_col = 0; state_col != size_col; state_col++)
{
for(state_row = 0; state_row != size_row; state_row++)
{
matrix[0][0] = 1;
pre = 0;
for(loop_row = 0; loop_row != state_row; loop_row++)
{
pre = matrix[loop_row][state_col] + pre;
}
for(loop_col = 0; loop_col != state_col; loop_col++)
{
pre = matrix[state_row][loop_col] + pre;
}
matrix[state_row][state_col] = pre;
}
}
}
void txtprnt() //Text Print, prints out the data from rule() to a file
{
rule(); //Runs the Rule
ofstream UM("Out.txt", ios::out | ios::app); //Dump arrays onto "Out.txt" at end of
for( row = 0; row != size_row; row++) //Used to advance to next row once the first row is completed
{
UM << "|"; //Visual Character
cout << "|"; //View Real time generation
for( col = 0; col != size_col; col++ ) //Matrix Colum Builder
{
UM << matrix[row][col] << "|"; //Print the number in the space [row]x[col], and a Visual Character
cout << matrix[row][col] << "|"; //View Real time generation
}
UM << '\n'; //once row completed, move down a line
cout << '\n'; //View Real time generation
}
UM.flush();
UM.close();
system("pause");
}
//Program
int main(int argc, char *argv[])
{
txtclr(); //Clear text
txtprnt(); //Print text [Generate numbers]
return 0;
}
|1|1|2|4|8|16|32|64|128|256|
|1|2|5|12|28|64|144|320|704|1536|
|2|5|14|37|94|232|560|1328|3104|7168|
|4|12|37|106|289|760|1944|4864|11952|28928|
|8|28|94|289|838|2329|6266|16428|42168|106336|
|16|64|232|760|2329|6802|19149|52356|139764|365696|
|32|144|560|1944|6266|19149|56190|159645|441750|1195600|
|64|320|1328|4864|16428|52356|159645|470010|1344585|3755120|
|128|704|3104|11952|42168|139764|441750|1344585|3968310|11413105|
|256|1536|7168|28928|106336|365696|1195600|3755120|11413105|33747490|
Thanks!
Next step, consolidate!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|