6 Replies - 11883 Views - Last Post: 23 October 2009 - 04:46 PM Rate Topic: -----

#1 jinroh00   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 06-April 08

adding 2 arrays with user input

Post icon  Posted 18 April 2008 - 05:35 PM

Hello I'm having troubling getting started with my program for my last assignment for class. My professor is asking for a program that declares two integer square matrices of size 3X3. The program then prompts the user to input the values of the elements in both matrices. After that the program will add the two matrices and display the resultant matrix in a matrix form (row by row). here is my code its not much at all because im stumped in starting it. I have just defined the basic defining the 2 matrices and the addition of both of the matrices, im confused on how to get the code going. thanks for the help.

#include <iostream>

using namespace std;

int main()
{
	float matrixSum;
	float matrix1[3][3];
	float matrix2[3][3];
	matrixSum = ((matrix1[3][3]) + (matrix2[3][3]));



Is This A Good Question/Topic? 1

Replies To: adding 2 arrays with user input

#2 Guest_Whizzy*


Reputation:

Re: adding 2 arrays with user input

Posted 18 April 2008 - 09:08 PM

View Postjinroh00, on 18 Apr, 2008 - 05:35 PM, said:

Hello I'm having troubling getting started with my program for my last assignment for class. thanks for the help.


#include <iostream>
using namespace std;
int main()
{
	float matrixSum;
	float matrix1[3][3];
	float matrix2[3][3];
	matrixSum = ((matrix1[3][3]) + (matrix2[3][3]));




I used to write some programs way back in the day (80's) in BASIC... C++ is way different, and I'm barely into it so, I hope my advice is good.
From what I have read, the only thing you need to get started my friend, is a plan. You have requirements, write each one down, Each requirement, is a different function. The next step here as you say would be to get the users input of the values of the elements in both matrices. Start a routine for that, next...
add the two matrices, start a routine for that. Then, display the resultant matrix in a matrix form (row by row). Start a routine for that.

It's like going to the store shopping for food. Sometimes you need to bounce around between the isle a few times before you have everything you need for your soup. Writing a program (atleast it used to be) can require bouncing around between your routines. As you start writing, a lot of times, it comes together... sometimes it doesn't.

Just make sure to leave yourself some notes along the way like:

// Ok... Still need a variable to complete the link here (or whatever)

Try starting your other routines, and see how far you get.


I would start with something like this, and change my functions around as I needed.
#include <iostream>
using namespace std;

// routine to create matrix
void create(){
float matrixSum;
	float matrix1[3][3];
	float matrix2[3][3];
	matrixSum = ((matrix1[3][3]) + (matrix2[3][3]));
}
// routine to get users input
void userin(){
}
// routine to add the two matrices
void addmatrix(){
}
//routine to display matrix
void displaymatrix(){
}

int main()
{
create();
userin();
addmatrix();
displaymatrix();

system ("pause");
}

This post has been edited by Whizzy: 18 April 2008 - 09:13 PM

Was This Post Helpful? 0

#3 GravityGuy   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-January 08

Re: adding 2 arrays with user input

Posted 18 April 2008 - 11:04 PM

Whizzy's advice about a plan and writing the program in modules or blocks is pretty good for a start. Without getting in over your head real fast I'll ignore his other comments about bouncing around and sometimes not coming together. There are some pretty good methods for putting a plan together and knowing it's going to work or not well before you've spent days writing code to find out later that it won't work. The important thing to remember is to break the program up into manageable pieces where each piece is not too complicated. If it is then break it up into further pieces.

I would add the following comment and you're going to need it if you follow the above plan. All of Whizzy's functions are not returning anything since he's using void as the return type. Therefore, you can't really create the arrays in a function without using global variables. Since this program isn't very big, you can afford to create the variables at the beginning and pass them as function parameters.

If you haven't taken functions yet and we don't really know what you've covered so far, just follow the same plan and do all the coding right in the main(). You still break up the program into blocks of code and use comments to explain what each section is doing.

I have to comment on the line matrixsum = matrix1[3][3] + matrix2[3][3]. I hope you understand that this will not work to add the two matrices together with one line of code. You will have to use a double for loop and add the individual cells of the array one at a time.
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,233
  • Joined: 14-September 07

Re: adding 2 arrays with user input

Posted 19 April 2008 - 02:13 AM

/*
 * KYA
 * Adding Matrices
 * 4-19-08
 * Possible Solution
 */

#include <iostream>
using namespace std;

//function prototypes
void DisplayExampleMatrix();

int main() {

	int addedMatrix [3][3];
	int matrixOne [3][3];
	int matrixTwo [3][3];
	cout << "You will be entering the values of two matrices to add together in the following format:\n";
	DisplayExampleMatrix();
	cout << "Please enter the values for the first matrix: ";

	//User input first matrix
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cin >> matrixOne [i][j];
		}//end inner for
	}//end outer for

	/*Display for debugging purposes
	cout << "The first matrix:\n";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << matrixOne [i][j] << "\t";
			if (j==2)
				cout << "\n";
		}//end inner
	}//end outer
	cout << "\n";
	*/

	//User input for second matrix
	cout << "Please enter the values for the second matrix: ";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cin >> matrixTwo [i][j];
		}//end inner for
	}//end outer for

	/*Display for debugging purposes
	cout << "The second matrix:\n";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << matrixTwo [i][j] << "\t";
			if (j==2)
				cout << "\n";
		}//end inner
	}//end outer
	cout << "\n";
	*/

	//Add them
	for(int i = 0; i < 3; i++) {
		for(int j = 0; j < 3; j++){
			addedMatrix [i][j] = matrixOne[i][j] + matrixTwo [i][j];
		}//end inner for
	}//end outer for


	//Final Diaplay
	cout << "User inputted matrices with resulting addition:\n";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << matrixOne [i][j] << "\t";
			if (j==2)
				cout << "\n";
		}//end inner
	}//end outer
	cout << "\n+\n\n";

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << matrixTwo [i][j] << "\t";
			if (j==2)
				cout << "\n";
		}//end inner
	}//end outer
	cout << "\n=\n\n";

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << addedMatrix [i][j] << "\t";
			if (j==2)
				cout << "\n";
		}//end inner
	}//end outer
	cout << "\n\n";

	return 0;
}//end main

void DisplayExampleMatrix() {
	cout << "0\t0\t0\n";
	cout << "1\t3\t1\n";
	cout << "2\t3\t0\n";
}//end example display



There are several ways to clean this up. You can call functions, etc... Enjoy.
Was This Post Helpful? 0
  • +
  • -

#5 jinroh00   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 06-April 08

Re: adding 2 arrays with user input

Posted 19 April 2008 - 06:17 AM

Thanks for the advice in working with this assignment. I did learn to use functions in my class but still trying to wrap everything in my head. I will try working on assignment with the advice that everyone has provided. Thanks I will get back if I still need more help working my way through this.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Whizzy*


Reputation:

Re: adding 2 arrays with user input

Posted 19 April 2008 - 07:38 AM

View PostGravityGuy, on 18 Apr, 2008 - 11:04 PM, said:

Whizzy's advice about a plan and writing the program in modules or blocks is pretty good for a start.

Thanks

View PostGravityGuy, on 18 Apr, 2008 - 11:04 PM, said:

The important thing to remember is to break the program up into manageable pieces where each piece is not too complicated. If it is then break it up into further pieces.

I would add the following comment and you're going to need it if you follow the above plan. All of Whizzy's functions are not returning anything since he's using void as the return type.


That's basically what I was trying to get across, however you stated it much better. I made all of the functions as void, as a starting point, nothing more. I stated he needed to change them as he went. You did make some excellent points.
Was This Post Helpful? 0

#7 alwayslrng   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-October 09

Re: adding 2 arrays with user input

Posted 23 October 2009 - 04:46 PM

I have a similiar problem/solution. I need to adjust my final output statement to be:
xxx yyy zzz
xxx + yyy = zzz
xxx yyy zzz

all in one line and not:
a[i][j]
+
b[i][j]
=
c[i][j]

Could someone help with the output statement please?

#include <iostream>
#define N 3
using namespace std;

void addMatrix(const double a[][N], const double b[][N], double c[][N]);

int main () 
{

	double a[N][N];
	double b[N][N];
	double c[N][N];
	double input;
	
	
	cout << "Enter matrix 1: ";
	
	for (int i=0; i<N; i++)
	{
		for(int j=0; j<N; j++)
		{
			cin >> input;
			a[i][j]= input;	
		}
		
	}
	
	cout << "Enter matrix 2: ";
	for (int i=0; i<N; i++)
	{
		for(int j=0; j<N; j++)
		{
			cin >> input;
			b[i][j]= input;	
		}
		
	}
	
	cout << "The sum of the matricies is: " << endl;
	
	addMatrix(a, b, c);
	
	
	for (int i=0; i<N; i++)
	{
		for(int j=0; j<N; j++)
		{
			cout << c[i][j] << " ";
		}
		cout  << endl;			
	}
				
	
	return 0;
}

void addMatrix(const double a[][N], const double b[][N], double c[][N])
{
	
	for (int i=0; i<N; i++)
	{
		for(int j=0; j<N; j++)
		{
			c[i][j] = a[i][j] + b[i][j];
			
			
		}
		cout << endl;			
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1