Adding Two-Dimensional arrays together
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Adding Two-Dimensional arrays together

  1. #1
    Join Date
    Jan 2010
    Posts
    17

    Exclamation Adding Two-Dimensional arrays together

    Hey everyone, I'm having a little problem with adding two arrays together, as part of a Matrices program. Ive got the code for adding the matrices, I really dont think its right at all.

    Here's the code

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    int askselectioninput();
    int main()
    {
    	cout<<"============================="<<endl;
    	cout<<"|| Calculation of Matrices ||"<<endl;
    	cout<<"============================="<<endl;
    int numbers[3][4];
    int row, col;
    int option = 0;
    int result =0;
    
    for (row=1; !(row>3); row++)/
    { cout<<"Data for row: "<<row<<endl;
    for (col=1; !(col>4); col++)
    { cout<<"Type a number: ";
    cin>>numbers[row][col];
    }
    cout<<"Matrices Inserted.."<<endl;
    }
    while (true){
    
    		option = askselectioninput();
    
    		if (option ==1)
    		{
    
    			cout<<"==================================="<<endl;
    			cout<<"||Addition of Current Matrices Data||"<<endl;
    			cout<<"==================================="<<endl;
    		for (row=1; !(row>3); row++)
    		{
    	for (col=1; !(col>4); col++)
    		   cout<<row + col<<" ";
       }
    	cout<<"Results Successfully Displayed, returning to Main-Menu"<<endl<<endl<<endl;
    		}
    
    		else if (option ==2)
    		{
    
    			cout<<"======================================"<<endl;
    			cout<<"||Subtraction of Current Matrices Data||"<<endl;
    			cout<<"======================================"<<endl;
    		for (row=1; !(row>3); row++)
    		{
    	for (col=1; !(col>4); col++)
    		   cout<<row - col<<" "<<endl;
       }
    		}
    
    		else if (option ==3)
    		{
    			cout<<"==================================="<<endl;
    			cout<<"||Displaying Current Matrices Data||"<<endl;
    			cout<<"==================================="<<endl;
    			cout<<"The current results are: "<<endl;
    
    	for (row=1; !(row>3); row++)
    		{
    	for (col=1; !(col>4); col++)
    		{ cout<<numbers[row][col]<<" ";
    		 }
    		cout<<" "<<endl;
    		 }
    	cout<<"Results Successfully Displayed, returning to Main-Menu"<<endl<<endl<<endl;
    	}
    
    		else if (option ==4)
    		{
    			cout<<"==================================="<<endl;
    			cout<<"||Multiplying Current Matrices Data||"<<endl;
    			cout<<"==================================="<<endl;
    		}
    		else if (option ==5){
    			cout<<"--Program...TERMINATED--"<<endl;
    			cout<<"--I'll BE BACK--"<<endl;
    			exit (1);
    		}
    
    	}
    }
    int askselectioninput(){
    	int option= 0;
    	cout<<"==============YOUR TRANSACTION CHOICES==================="<<endl;
    	cout<<"Press 1 for Addition              Press 2 for Subtraction"<<endl;
    	cout<<"press 3 for Display Balance    Press 4 for Mutliplication"<<endl;
    	cout<<"===============Press 5 for Quit=========================="<<endl;
    	cin>>option;
    	if (option >0 && option <6){
    		return option;
    	}
    
    	else {
    		cout<<"--Incorrect Value--"<<endl<<endl;
    		return askselectioninput();
    	}
    
    }
    P.S The rows and cols are meant to be 1 in this code
    Code:
    	for (row=1; !(row>3); row++)
    		{
    	for (col=1; !(col>4); col++)
       }

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,525

    Re: Adding Two-Dimensional arrays together

    Quote Originally Posted by TBsparky View Post
    Hey everyone, I'm having a little problem with adding two arrays together, as part of a Matrices program. Ive got the code for adding the matrices, I really dont think its right at all.
    1. What TWO arrays??? "numbers" and what else?
    2. In C/C++ array index is zero-based. You are accessing array out-of-boundaries.
    3. !(row>3) is customarily written as row <= 3;
    4. you are NOT doing any addition, subtraction, etc.
    5. Thank you for using code tags. However, there main porpose it to preserve proper indentation, which you do not have. Compare to this:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    int askselectioninput();
    int main()
    {
    	cout<<"============================="<<endl;
    	cout<<"|| Calculation of Matrices ||"<<endl;
    	cout<<"============================="<<endl;
    	//int numbers[3][4];
    	int row, col;
    	int option = 0;
    	int result =0;
    	int numbers[3][4];
    
    	for (row=1; !(row>3); row++)
    	{ 
    		cout<<"Data for row: "<<row<<endl;
    		for (col=1; !(col>4); col++)
    		{ 
    			cout<<"Type a number: ";
    			cin>>numbers[row][col];
    		}
    		cout<<"Matrices Inserted.."<<endl;
    	}
    	while (true){
    
    		option = askselectioninput();
    
    		if (option ==1)
    		{
    
    			cout<<"==================================="<<endl;
    			cout<<"||Addition of Current Matrices Data||"<<endl;
    			cout<<"==================================="<<endl;
    			for (row=1; !(row>3); row++)
    			{
    				for (col=1; !(col>4); col++)
    					cout<<row + col<<" ";
    			}
    			cout<<"Results Successfully Displayed, returning to Main-Menu"<<endl<<endl<<endl;
    		}
    
    		else if (option ==2)
    		{
    
    			cout<<"======================================"<<endl;
    			cout<<"||Subtraction of Current Matrices Data||"<<endl;
    			cout<<"======================================"<<endl;
    			for (row=1; !(row>3); row++)
    			{
    				for (col=1; !(col>4); col++)
    					cout<<row - col<<" "<<endl;
    			}
    		}
    
    		else if (option ==3)
    		{
    			cout<<"==================================="<<endl;
    			cout<<"||Displaying Current Matrices Data||"<<endl;
    			cout<<"==================================="<<endl;
    			cout<<"The current results are: "<<endl;
    
    			for (row=1; !(row>3); row++)
    			{
    				for (col=1; !(col>4); col++)
    				{ 
    					cout<<numbers[row][col]<<" ";
    				}
    				cout<<" "<<endl;
    			}
    			cout<<"Results Successfully Displayed, returning to Main-Menu"<<endl<<endl<<endl;
    		}
    
    		else if (option ==4)
    		{
    			cout<<"==================================="<<endl;
    			cout<<"||Multiplying Current Matrices Data||"<<endl;
    			cout<<"==================================="<<endl;
    		}
    		else if (option ==5)
    		{
    			cout<<"--Program...TERMINATED--"<<endl;
    			cout<<"--I'll BE BACK--"<<endl;
    			exit (1);
    		}
    
    	}
    }
    int askselectioninput(){
    	int option= 0;
    	cout<<"==============YOUR TRANSACTION CHOICES==================="<<endl;
    	cout<<"Press 1 for Addition              Press 2 for Subtraction"<<endl;
    	cout<<"press 3 for Display Balance    Press 4 for Mutliplication"<<endl;
    	cout<<"===============Press 5 for Quit=========================="<<endl;
    	cin>>option;
    	if (option >0 && option <6){
    		return option;
    	}
    
    	else {
    		cout<<"--Incorrect Value--"<<endl<<endl;
    		return askselectioninput();
    	}
    
    }
    //P.S The rows and cols are meant to be 1 in this code
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  3. #3
    Join Date
    Jan 2010
    Posts
    17

    Re: Adding Two-Dimensional arrays together

    1. What TWO arrays??? "numbers" and what else?
    2. In C/C++ array index is zero-based. You are accessing array out-of-boundaries.
    3. !(row>3) is customarily written as row <= 3;
    4. you are NOT doing any addition, subtraction, etc.
    5. Thank you for using code tags. However, there main porpose it to preserve proper indentation, which you do not have. Compare to this:
    1. the two arrays are numbers [3] [4] for the rows and columns to which i have to calculate together.

    2. I'm sorry but the for loop code, for the rows and cols, is what the lecturer had posted for us to use, When i set it to 0 is messes the whole program up.

    3. I dont quite get what your saying with this one

    4. The reason for THIS program is preform addition, subtraction and multiplication on two dimensional arrays for matrices

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Adding Two-Dimensional arrays together

    Quote Originally Posted by TBsparky View Post
    1. the two arrays are numbers [3] [4] for the rows and columns to which i have to calculate together.
    What? I don't understand what you're saying here.

    2. I'm sorry but the for loop code, for the rows and cols, is what the lecturer had posted for us to use, When i set it to 0 is messes the whole program up.
    Put simply, your row for loop will attempt to access numbers at numbers[1][*], numbers[2][*], and numbers[3][*] (where * doesn't matter). However, 3 is an invalid index for the first dimension of numbers-----valid indexes are 0, 1, and 2. Trying to access 3 is simply wrong, so you either misunderstood your lecturer, or he's teaching you incorrect information and you need to get out of that class immediately.

  5. #5
    Join Date
    Jan 2010
    Posts
    17

    Re: Adding Two-Dimensional arrays together

    Sorry let me explain. The two arrays being used are numbers [3] [4]
    which are initialized in the for loop when in the:

    "cin>>numbers[row][col]"

    So rows is array [3] and cols is array [4]

    I Hope it clears it up.

    Also the whole variables, for loop and int row,col
    is the code she provided as a base line to work from.

    Sorry but i don't quite understand, the code works, as far as i know, when entering data in the rows and cols, meaning it displays in the form of matrices. I'm not saying your wrong its just would i need to change it if it seems to work? Minus the addition code of course which i need to add.

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,525

    Re: Adding Two-Dimensional arrays together

    Quote Originally Posted by TBsparky View Post
    1. the two arrays are numbers [3] [4] for the rows and columns to which i have to calculate together.
    "numbers[3][4]" is ONE two-dimencional array (matrix).
    Check this out.
    Quote Originally Posted by TBsparky View Post
    2. I'm sorry but the for loop code, for the rows and cols, is what the lecturer had posted for us to use, When i set it to 0 is messes the whole program up.
    Well, for numbers[3][4], there is NO element numbers[3][4].
    Quote Originally Posted by TBsparky View Post
    3. I dont quite get what your saying with this one
    I am saying that instead of "if NOT (row GREATER THEN 3)" people usually say "if (row LESS_THAN_OR_EQUAL_TO 3)". Just a matter of taste, I guess.
    Quote Originally Posted by TBsparky View Post
    4. The reason for THIS program is preform addition, subtraction and multiplication on two dimensional arrays for matrices
    But your code:
    Code:
    cout<<row + col<<" ";
    doesn't add anything; it simply outputs current row index + current column index, which makes no sense at all.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  7. #7
    Join Date
    Jan 2010
    Posts
    17

    Re: Adding Two-Dimensional arrays together

    Code:
    cout<<row + col<<" ";
    doesn't add anything; it simply outputs current row index + current column index, which makes no sense at all.[/QUOTE]

    but when the program runs it asks for input, to put into the array and then it brings up a prompt, from the bottom of the program,

    Code:
    int askselectioninput(){
    	int option= 0;
    
    	cout<<"==============YOUR TRANSACTION CHOICES==================="<<endl;
    	cout<<"Press 1 for Addition              Press 2 for Subtraction"<<endl;
    	cout<<"press 3 for Display Balance    Press 4 for Mutliplication"<<endl;
    	cout<<"===============Press 5 for Quit=========================="<<endl;
    	cin>>option;
    	if (option >0 && option <6){
    		return option;
    	}
    
    	else {
    		cout<<"--Incorrect Value--"<<endl<<endl;
    		return askselectioninput();
    	
    }
    
    }
    This prompt is where it asks for the options to which i need to work out the addition code as i know the rest of my program worls.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Adding Two-Dimensional arrays together

    Numbers is not 2 different arrays though....it's a single 2D array with 3*4=12 elements. When you talk about "adding" there's usually 2 things to work with, not just 1. Hence the confusion.

    Sorry but i don't quite understand, the code works, as far as i know, when entering data in the rows and cols, meaning it displays in the form of matrices. I'm not saying your wrong its just would i need to change it if it seems to work? Minus the addition code of course which i need to add.
    C++ doesn't offer any guarantees that doing incorrect things will cause the program to crash or not build. However, if you make an out-of-bounds access, then you're either reading or writing memory which does not really belong to the variable you think you're accessing. This is a major problem since it can either crash your program or----much worse----result in subtle corruptions to other variables which you don't notice immediately. These latter types of bugs can be much more difficult to track down than a simple crash.

    I'll say again, if your professor doesn't know that C and C++ use array indexes from 0 to N-1 rather than 1 to N, then he doesn't know one of the most fundamental aspects of the language and has no business teaching it.

  9. #9
    Join Date
    Jan 2010
    Posts
    17

    Re: Adding Two-Dimensional arrays together

    I think I might have the program wrong.. It's a Matrices program that calculates ONE 2D Array of rows and columns together to display a final Matrices.

    So you think if i change the values to 1 it would solve one problem. Also, is my addition code right at all?

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Adding Two-Dimensional arrays together

    Perhaps you should show us the instructions for the assignment, I'm still not understanding the purpose.

  11. #11
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,525

    Re: Adding Two-Dimensional arrays together

    Quote Originally Posted by TBsparky View Post
    .. It's a Matrices program that calculates ONE 2D Array of rows and columns together to display a final Matrices.
    Sorry, but I can't understand what you are saying here.
    Anybody want to take over?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  12. #12
    Join Date
    Jan 2010
    Posts
    17

    Re: Adding Two-Dimensional arrays together

    Assignment 2, part 1
    Using one of the following development environments:
     Microsoft Visual Basic 2005/2008/2010
     Microsoft Visual C++ Console Application
     Netbeans C++ Console Application
    Task (50&#37; of the grade for this assignment – up to C8)
     Write a program to solve a pair of simultaneous equations for 2 unknowns: x and y. Where the coefficients of x and y may be represented as a 2x2 matrix.
     Where possible, efficient use should be made of appropriate programming and data structures (e.g. loops and arrays).
     If a solution is not possible, an appropriate message should be displayed to the user.
    Note:
     Part 2 of this assignment will be held in-class on 12th May and will also contribute the remaining 50% of the grade for this assignment.
     Part 2 of this assignment will be based on the identification of appropriate test data and expected results for the program submitted in Part 1
    VB – A possible interface:
    C++ – A possible interface:
    VB – Submission:
    C++ - Submission:
    Zipped project folder .cpp file
    Grading Criteria:
     Programs should be well-structured using an appropriate interface. However, no credit will be given for unnecessary use of colours, fonts etc. in VB projects.
     Programs should be well-structured using appropriate

    Here is the 2D Array for C++ VB will be posted at a later date.
    Code:
    #include <iostream>
    using namespace std;
    void main()
    { /* declare variables */
    int numbers[3][4];
    int row, col;
    /* inputs */
    for (row=1; !(row>3); row++)
    { cout<<"Data for row: "<<row<<endl;
    for (col=1; !(col>4); col++)
    { cout<<"Type a number: ";
    cin>>numbers[row][col];
    }
    cout<<endl;
    }
    /* outputs */
    for (row=1; !(row>3); row++)
    {
    for (col=1; !(col>4); col++)
    { cout<<numbers[row][col]<<" ";
    }
    cout<<"End"<<endl;
    }
    }
    she has also posted the 1D Array
    Code:
    1-D Arrays in C++
    #include <iostream>
    using namespace std;
    void main()
    { /* declare variables */
    int values[10];
    int i;
    /* inputs */
    for (i=0; i<10; i++)
    {cout<<"Type a number: ";
    cin>>values[i];
    }
    /* outputs */
    for (i=0; i<10; i++)
    cout<<values[i]<<endl;
    }

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Adding Two-Dimensional arrays together

    Write a program to solve a pair of simultaneous equations for 2 unknowns: x and y. Where the coefficients of x and y may be represented as a 2x2 matrix.
    Here's the relevant bit. Before you worry too much about how to write the code, have you figured out the exact sequence of steps you would need to do by hand to solve this problem?

  14. #14
    Join Date
    Jan 2010
    Posts
    17

    Re: Adding Two-Dimensional arrays together

    By the exact sequence of steps you mean how the program will input, calculate and display?

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Adding Two-Dimensional arrays together

    No, I mean, given that someone hands you a paper with the coefficients of 2 equations written on it, do you know how to solve for x and y. You can't write a program to do this unless you know how to do it by hand.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center