954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Addition with Two Dimensional Arrays

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?

dss1984
Newbie Poster
2 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

Nevermind. I just figured it out.

dss1984
Newbie Poster
2 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

thanks,,, its useful 4 me

CloudyBoy
Newbie Poster
1 post since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

#include
#include
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

ameya potnis
Newbie Poster
1 post since Feb 2011
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You