Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,454 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,128 people online right now. Registration is fast and FREE... Join Now!




matrices and addition

 
Reply to this topicStart new topic

matrices and addition, i can't get the output file to add right with my addMatrix functio

jajuje2000
14 Feb, 2008 - 05:52 PM
Post #1

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 36

Hi i am new to C++ and my professor just threw us a curve ball code to do. i have to Develop a C++ program to model the mathematical notion of a matrix of integers wich im only using addition. He also asks that we use functions that should validate the matrix dimensions before performing the operations, as the dimensions of the matrices should be compatible for that operation. I have to using pointers for the 2D array, use pointer notation to access the value of any element in the 2D array. My program should not use the notation of [] but only use * operator for dereferencing.

well im only trying to get the dang addition code to work wothout pointers and im even getting trouble with that. i get the data and display it right on the compiler, but when the output is sent to my outfile.txt file it gives me the wrong numbers, i know my problem is the addMatrix funtion, here is my coding so far, pleas help.

CODE

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

using namespace std;

using std::ifstream;
using std::ofstream;
using std::endl;

double AddMatrix(double x[][3], double y[][3]);


int main()
{
    const int nrows = 3;
    const int ncols = 3;
    double instreamAInfo[nrows][ncols];
    double instreamBInfo[nrows][ncols];

    int row, col;
    char ch;

    ifstream instreamA;
    ifstream instreamB;
    ofstream outstream;
    

instreamA.open("infileA.txt");
    
for (row=0; row<nrows; row++)
      for (col=0; col<ncols; col++)
         instreamA >> instreamAInfo[row][col]; //stores info in instreamAInfo
  for(int r=0; r<3; r++)
   {
    for(int c=0; c<3; c++)
    {
     cout << instreamAInfo[r][c] << ' ';
    }
    cout <<  endl;
   }
cout << endl;

instreamB.open("infileB.txt");
    
for (row=0; row<nrows; row++)
      for (col=0; col<ncols; col++)
         instreamB >> instreamBInfo[row][col]; //sotres info in instreamBInfo
   for(int r=0; r<3; r++)
   {
    for(int c=0; c<3; c++)
    {
     cout << instreamBInfo[r][c] << ' ';
    }
    cout << endl;
   }
   outstream.open("outfile.txt");


    cout << "Enter A for addition or Q to quit: " << endl;

    cin >> ch;
    while(ch != 'Q')
  {
   switch(ch)
   {
   case 'A':
    outstream << AddMatrix << endl;
    break;
   default:
    cout << "Invalid operation choice." << endl;
    cout << "Enter A for addition or Q to quit: " <<endl;
   }//end switch
  cin >> ch;
    }
  
    return 0;

}

double AddMatrix (double x[][3], double y [][3])
    {
        double matsum[3][3];
        int row, col;
        const int nrows = 3;
        const int ncols = 3;
        for (row = 0; row < 3; row++)
            for (col=0; col < 3; col++)
            {
                matsum[nrows][ncols] = (x[row][col] + y[row][col]);
            }
return matsum[3][3];
    }

User is offlineProfile CardPM
+Quote Post

jajuje2000
RE: Matrices And Addition
14 Feb, 2008 - 08:28 PM
Post #2

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 36

please somebody help, im stumped.
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Matrices And Addition
14 Feb, 2008 - 08:54 PM
Post #3

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,582



Thanked: 12 times
Dream Kudos: 325
My Contributions
You seem to be on the right track, but you need to use pointers.

I haven't done a lot with C++, but I know in C you can't return arrays (only pointers).

Here is some matrix stuff I did in C for my game engine, since you've basically got the gist of it. The part you'll want is down at line 160. The only big differences between your code and mine are that I used a structure for a matrix (m = # rows, n = # columns, v = the matrix), I did some error checking, and I used pointers.

This post has been edited by Tom9729: 14 Feb, 2008 - 08:55 PM
User is online!Profile CardPM
+Quote Post

jajuje2000
RE: Matrices And Addition
14 Feb, 2008 - 09:23 PM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2008
Posts: 36

coo man thanks very much, much appreciated. i understand my error a little more now.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 01:16PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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