Hi, everyone. This is my first post here. I'm not very good with C++ and I have to write a program using 2 dimensional arrays.

My program is a Menu that works with 2 two dimensional arrays. The program has to generate two random matrices and perform arthimetic procedures like addition and subtraction. My program generates the matrices perfectly, but I'm having a lot of trouble writing the function to add the two matrices. I know that in order to add to matrices you have to sum the elements in every row and column and store them into a new matrix, but I can't figure out how to write the actual code for it. Can anyone help? PLEASE! Here is the code I have written so far for the program. The addition part of the code is being written in the last function.

//February 7, 2005

#include <iostream>
#include <cstdlib>
#include <ctime> 
#include <iomanip>

using namespace std;

const int ROW = 3; //global constant for row
const int COL = 3; //global constant for column
const int LOW = 1; //global const for lower limit 
const int UP = 10; //global const for upper limit 

void RandomArray(int[][COL],int, int, int, int);  //Random array generator function prototype
void Menu();									 //Menu function prototype
void PrintArray(int[][COL],int,int);			//Print array function prototype
void AddArray(int[][COL],int[][COL],int,int);

int main()
{
	srand(time(NULL)); 	
	int matrix[ROW][COL]; //Array declaration 
	int NewMatrix[ROW][COL];
	int menuOption;
	
	
	

	RandomArray(matrix, ROW, COL, LOW, UP); //Function call
	
	do
	{
		Menu();
		cin >> menuOption;
		cout << endl;

		switch(menuOption)
		{
		case 1:
			RandomArray(matrix, ROW, COL, LOW, UP);
			PrintArray(matrix,ROW,COL);
			RandomArray(matrix, ROW, COL, LOW, UP);
			PrintArray(matrix,ROW,COL);
			AddArray(matrix,NewMatrix,ROW,COL);
			PrintArray(matrix,ROW,COL);
			break;
	
		case 7:
			PrintArray(matrix,ROW,COL);
			RandomArray(matrix, ROW, COL, LOW, UP);
			PrintArray(matrix,ROW,COL);
			break;
		default:
			cout << "Invalid Option." << endl;
			break;
		}
	}while (menuOption != 0);


	return 0;
}

void Menu()
{
	 cout << endl << "Menu Options: " << endl << endl
			<< "1. Add 2 matrices " << endl
         	<< "2. Subtract 2 matrices " << endl
         	<< "3. Multiply 2 matrices " << endl
         	<< "4. Multiply matrix by a constant " << endl 
         	<< "5. Transpose matrix " << endl
         	<< "6. Matrix trace " << endl
	   		<< "7. Print " << endl
         	<< "0. EXIT " << endl;
}

void RandomArray(int table[ ][COL], int row, int col, int lowerLimit, int upperLimit)
{ 

	for (int r = 0; r < ROW; r++) 
		for (int c = 0; c < COL; c++) 
			table[r][c] = rand()%(upperLimit - lowerLimit + 1) + lowerLimit;
}


void PrintArray(int table[][COL], int row, int col)
{
		for (row = 0; row < ROW; row++)
		{ 
		for (col = 0; col < COL; col++) 
			cout << setw(3) << table[row][col]; 
		cout << endl; 
		}
	cout << endl;
}


void AddArrays(int table[][COL], int theMatrix[][COL], int row, int col)
{ 
	for (int r=0; r < row; r++) 
		for (int c=0; c < col; c++) 
			theMatrix[r][c]= table[r][c]+ table[r][c]; 
}

By the way, should my AddArray function be a value-returning function?

Nevermind. I just figured it out.

thanks,,, its useful 4 me

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],d[10][10],i,j,r,c;
clrscr();
printf("enter the value for ROWS:");
scanf("%d",&r);
printf("enter the value of COLUMNS:");
scanf("%d",c);
for(i=0;i<r;i++)
{
      for(j=0;j<c;j++)
       {
        printf("enter the element:");
        scanf("%",&a[i][j]);
       }
}
for(i=0;i<r;i++)
{
      for(j=0;j<c;j++)
       {
        printf("\t %d",a[i][j]);
       }
       {
        printf("\n");
       }
}
for(i=0;i<r;i++)
{
      for(j=0;j<c;j++)
       {
        printf("enter the element:");
        scanf("%",&b[i][j]);
       }
}
for(i=0;i<r;i++)
{
      for(j=0;j<c;j++)
       {
        printf("\t %d",b[i][j]);
       }
       {
        printf("\n");
       }
}
printf("answer of a+b:\n");
for(i=0;i<r;i++)
{
      for(j=0;j<c;j++)
       {
        d[i][j]=a[i][j]+b[i][j];        
       }
}
for(i=0;i<r;i++)
{
      for(j=0;j<c;j++)
       {
        printf("\t %d",d[i][j]);
       }
       {
        printf("\n");
       }
}
getch();
}

this programe is done to get two nos. from the user and print the entered no. into rows and columns and acccordingly add both the matrix.....

commented: Not even in C++, non-standard, no code tags, no dice -1
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.