1,105,534 Community Members

Addition with Two Dimensional Arrays

Member Avatar
dss1984
Newbie Poster
2 posts since Feb 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
dss1984
Newbie Poster
2 posts since Feb 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

Nevermind. I just figured it out.

Member Avatar
CloudyBoy
Newbie Poster
1 post since Mar 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks,,, its useful 4 me

Member Avatar
ameya potnis
Newbie Poster
1 post since Feb 2011
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 
#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.....

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article