Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,167 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,881 people online right now. Registration is fast and FREE... Join Now!




Matrix Manipulation (linear algebra program)

 
Reply to this topicStart new topic

Matrix Manipulation (linear algebra program)

MAX KORN
18 Dec, 2007 - 08:10 AM
Post #1

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 12


My Contributions
Hello everyone, I have a project due tomorrow that I am very clueless on. It involves matrices and elementary row operations. I understand the matrices aspects, but I don't really know how to put it in code. If anyone can find, or has, some sample code for me to study so I can get a better grasp on this program, I'd really appreciate it. I can't seem to find to much on the internet, but I am still trying. Here's the project description:

Write a program which reads the coefficients of a matrix consisting of N rows and N+1 columns from a file on disk. This file has to have the following format :
1. row : just the number N
2. row : the N+1 coefficients of the first row of the matrix
3. row : the N+1 coefficients of the 2nd row of the matrix
.
.
(N+1). row : the N+1 coefficients of the N'th row of the matrix.

The coefficients of the matrix are of data type "double".

The name of the file is arbitrary but is at most 15 characters long.

Your program has to work for any number N between 2 and 20 and stop with an error message if the file contains a number outside this range.

After reading in all the data using a 2-D array your program is to do the following :

Multiplying each coefficient of the 2nd row and adding the results to the corresponding coefficient of the first row and making the resulting coefficients become the coefficients of the new second row replacing the old. Choose the multiplier such that the 1. coefficient in the new 2nd row is zero !! Repeat this for the 3rd etc. up to and including the N'th row.

The final answer, which your program prints to the screen, must be a matrix consisting of N rows and N+1 columns with all elements in the first column equal to zero except the top row.


Thank you for the help.

User is offlineProfile CardPM
+Quote Post

MAX KORN
RE: Matrix Manipulation (linear Algebra Program)
18 Dec, 2007 - 08:31 AM
Post #2

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 12


My Contributions
I've been reading more about how to use gauss jordan elimination in C++ and one of my main questions now is: how do I solve a matrix that can have varying rows and columns? It seems like the operations I could perform in code could only be specific for a certain, already known, matrix.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Matrix Manipulation (linear Algebra Program)
18 Dec, 2007 - 02:06 PM
Post #3

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Nope. It can be done on a matrix of arbitrary size. Numerical Recipes has a chapter on it complete with code. Because you're reading in the value of N from a file, you'll either have to use dynamically allocated arrays, or something like an STL container to hold all the values. And your various loops will need to run from 0 to N-1 or N (depending on whether you're running through rows or columns), instead of some value fixed at compile time.

Because you're likely going to be dealing with swapping rows, my recommendation would be to implement this using nested STL vectors:
CODE
vector<vector<double> > myMatrix(N, vector<double>(N+1,0.0))

This will create a nested (i.e. 2D) vector that can be accessed using normal row/column indices:
CODE
double value=myMatrix[row][col];//accesses value at (row, col)

and also allows you to swap two rows quite easily:
CODE
myMatrix[row1].swap(myMatrix[row2]);//swaps row1 with row2


If you've got an implementation in progress, feel free to post for feedback and assistance. Matrices are yummy (and a tip of the hat to you, Martyr2 wink2.gif ).
User is offlineProfile CardPM
+Quote Post

MAX KORN
RE: Matrix Manipulation (linear Algebra Program)
18 Dec, 2007 - 03:56 PM
Post #4

New D.I.C Head
*

Joined: 26 Nov, 2007
Posts: 12


My Contributions
Thanks dude man, I'll try working it out with that. Get back to you at some point with some progress.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:17AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month