/***** The prompt i have is : use a class with 3 member functions named InputMatrix, Calculate, and OutMatrix. The program will prompt the user to (e)enter the matrix data, (c) to calculate the matrix multiplication, (d) to display the input and output matrices, and (q) to quit. no exit() or abort() commands are to be used. no global variables allowed.

I have created the Menu for users to enter either e, c, d or q...I have also created the correct functions for input, output and calculation. i am having trouble with how to put all the functions in the menu so the program works like it says in the prompt. thank you for the help.

//header file

#ifndef matrix_H
#define matrix_H


#include <iostream>
using namespace std;
const short SIZE = 3;
void CalculateMatrix(double [SIZE][SIZE] , double [SIZE][SIZE], double [SIZE][SIZE]);
void InputMatrix(double localmatrix [SIZE][SIZE]);
void OutMatrix(double [SIZE][SIZE]);
void initializematrix(double [SIZE][SIZE] );

void initializematrix(double localmatrix[SIZE][SIZE] )
{
    int p,q;
    for(p=0; p<SIZE; p++)
    {
        for (q=0; q<SIZE; q++)
        {
            localmatrix [p][q] = 0;
        }
    }
}

#endif

// cpp file

#include "matrix.h"

    int main ()
    {
    //  int choice;  changed it to char choice
    //  char choice;
        char choice;

        for (;;)
        {
            menu(choice);
            if(choice == 'e' || choice == 'E')
            {
                double Matrix1 [SIZE][SIZE] , Matrix2 [SIZE][SIZE] , Matrix3 [SIZE][SIZE] ;
                cout << " \nplese enter first 3x3 matrix " <<endl;              // ask user to enter first matrix
                InputMatrix(Matrix1);                                           // get the input from user for first matrix
                cout << " \nYou have entered the following Matrix " << endl;
                OutMatrix(Matrix1);                                             // output the matrix they have entered

                cout << " \nplese enter second 3x3 matrix " <<endl;             // ask user to enter second matrix
                InputMatrix(Matrix2);                                           // get the input from user for second matrix
                cout << "\nYou have entered the following Matrix2 " << endl;
                OutMatrix(Matrix2);

            }
            else if (choice == 'c' || choice == 'C')
            {
                CalculateMatrix();

            }
            else if (choice == 'd' || choice == 'D')
            {
                OutMatrix();

            }
            else if (choice == 'q' || choice == 'Q')
                cout << "Thank you for using the program." << end;
            break;
            else
            {
                cout << "Please enter a valid character: "; << endl;
            }
        }

    }
    void menu( char & choice ) // menu for the user to choose m, e or q
    {
        cout << endl;
        cout << "        Welcmoe to the matrix conversion program." << endl;
        cout << " Press E to enter the matrix data. " << endl;
        cout << " Press C to calculate the matrix multiplication. " << endl;
        cout << " Press D to display the input and output matrices. " << endl;
        cout << " Press Q to quit the program. "
        cin.get(choice); 
        cin.ignore();
    }

void InputMatrix(double localmatrix [SIZE][SIZE])
{
    int p,q;
    for(p=0; p<SIZE; p++)
    {
        for (q=0; q<SIZE; q++)
        {
        cin >> localmatrix [p][q];
        }
    }
}

void OutMatrix(double localmatrix [SIZE][SIZE])
{
    int p,q;
    for( p=0; p<SIZE; p++)
    {
        cout << endl;
        for ( q=0; q<SIZE; q++)
        {
            cout << localmatrix [p][q] << "\t";
        }
    }
}

void CalculateMatrix(double multmat1[SIZE][SIZE] , double multmat2[SIZE][SIZE] , double multmat3 [SIZE][SIZE])
{
    int p,q,r;
    for(p=0; p<SIZE; p++)
    {
        for (q=0; q<SIZE; q++)
        {
            for (r=0; r<SIZE; r++)
            {
                multmat3 [p][q] =+ multmat3 [p][q] + (multmat1 [p][r] * multmat2 [r][q]);

            }
        }
    }

}

Recommended Answers

All 3 Replies

line # 36 in cpp file, i forgot to put the brackets to open and close the function and corrected it now, still having problems

Sorry guys but i found couple small errors and corrected them so reposting the code here again.

//header file

#ifndef matrix_H
#define matrix_H


#include <iostream>
using namespace std;
const short SIZE = 3;
void menu( char & choice ); 
void CalculateMatrix(double [SIZE][SIZE] , double [SIZE][SIZE], double [SIZE][SIZE]);
void InputMatrix(double localmatrix [SIZE][SIZE]);
void OutMatrix(double [SIZE][SIZE]);
void initializematrix(double [SIZE][SIZE] );

void initializematrix(double localmatrix[SIZE][SIZE] )
{
    int p,q;
    for(p=0; p<SIZE; p++)
    {
        for (q=0; q<SIZE; q++)
        {
            localmatrix [p][q] = 0;
        }
    }
}

#endif

// cpp file

# include "matrix.h"

    int main ()
    {
    //  int choice;  changed it to char choice
    //  char choice;
        char choice;

        for (;;)
        {
            menu(choice);
            if(choice == 'e' || choice == 'E')
            {
                double Matrix1 [SIZE][SIZE] , Matrix2 [SIZE][SIZE] , Matrix3 [SIZE][SIZE] ;
                cout << " \nplese enter first 3x3 matrix " <<endl;              // ask user to enter first matrix
                InputMatrix(Matrix1);                                           // get the input from user for first matrix
                cout << " \nYou have entered the following Matrix " << endl;
                OutMatrix(Matrix1);                                             // output the matrix they have entered

                cout << " \nplese enter second 3x3 matrix " <<endl;             // ask user to enter second matrix
                InputMatrix(Matrix2);                                           // get the input from user for second matrix
                cout << "\nYou have entered the following Matrix2 " << endl;
                OutMatrix(Matrix2);

            }
            else if (choice == 'c' || choice == 'C')
            {
                CalculateMatrix();

            }
            else if (choice == 'd' || choice == 'D')
            {
                OutMatrix();

            }
            else if (choice == 'q' || choice == 'Q')
            {
                cout << "Thank you for using the program." << end;
            break;
            }
            else
            {
                cout << "Please enter a valid character: " << endl;
            }
        }

    }
    void menu( char & choice ) // menu for the user to choose m, e or q
    {
        cout << endl;
        cout << "        Welcmoe to the matrix conversion program." << endl;
        cout << " Press E to enter the matrix data. " << endl;
        cout << " Press C to calculate the matrix multiplication. " << endl;
        cout << " Press D to display the input and output matrices. " << endl;
        cout << " Press Q to quit the program. "
        cin.get(choice); 
        cin.ignore();
    }

void InputMatrix(double localmatrix [SIZE][SIZE])
{
    int p,q;
    for(p=0; p<SIZE; p++)
    {
        for (q=0; q<SIZE; q++)
        {
        cin >> localmatrix [p][q];
        }
    }
}

void OutMatrix(double localmatrix [SIZE][SIZE])
{
    int p,q;
    for( p=0; p<SIZE; p++)
    {
        cout << endl;
        for ( q=0; q<SIZE; q++)
        {
            cout << localmatrix [p][q] << "\t";
        }
    }
}

void CalculateMatrix(double multmat1[SIZE][SIZE] , double multmat2[SIZE][SIZE] , double multmat3 [SIZE][SIZE])
{
    int p,q,r;
    for(p=0; p<SIZE; p++)
    {
        for (q=0; q<SIZE; q++)
        {
            for (r=0; r<SIZE; r++)
            {
                multmat3 [p][q] =+ multmat3 [p][q] + (multmat1 [p][r] * multmat2 [r][q]);

            }
        }
    }

}

You should probably start by reading the instructions: "use a class with 3 member functions named InputMatrix, Calculate, and OutMatrix. ... no global variables allowed."

You have some problems with your code, but they will be solved if you follow those highlighted instructions. This tutorial might help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.