CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2010
    Posts
    17

    Question Matrices problems with addition and a variable.

    Hey everyone, I've got to create a program that calculates matrices in a 2D array, and then add, subtract and display the results. However, i cannot get the addition to work, which i will then use for the subtraction, and i have a problem with "data_input" not being initialized.

    here's my code, i hope you can help.

    Code:
    #include <iostream>
    using namespace std;
    typedef struct Matrices_varibles{
    int numbers [3] [4];
    int numbers1 [3];
    int numbers2 [4];
    int row;
    int col;
    } Matrices_variables;
    
    void data_input(Matrices_variables datamatrices);
    void data_output(Matrices_variables datamatrices);
    int askselectioninput();
    void add_Matrices(Matrices_variables datamatrices);
    void subtract_Matrices(Matrices_variables datamatrices);
    void display_Matrices(Matrices_variables datamatrices);
    void logout();
    
    void main(){
    cout<<"========================================="<<endl;
    	cout<<"========================================="<<endl;
    	cout<<"||   Mutltiple Matrices Calculations    ||"<<endl;
    	cout<<"========================================="<<endl;
    	cout<<"========================================="<<endl;
    	Matrices_variables Data_input;
    	int option =0;
    	data_input(Data_input);
    	while (true){
    
    		option = askselectioninput();
    
    		if (option ==1)
    		{
    
    			subtract_Matrices(Data_input);
    		}	
    
    		else if (option ==2)
    		{
    
    			add_Matrices(Data_input);
    		}
    
    		else if (option ==3)
    		{
    
    		display_Matrices(Data_input);
    		}
    
    		else if (option ==4)
    		{
    		logout();
    		}
    	}
    }
    
    int askselectioninput(){
    	int option= 0;
    	cout<<"==============CHOICES====================="<<endl;
    	cout<<"||PRESS 1 to SUBTRACT      PRESS 2 to ADD||"<<endl;
    	cout<<"||       PRESS 3 to DISPLAY RESULTS      || "<<endl;
    	cout<<"=============PRESS 4 to QUIT==============="<<endl;
    	cin>>option;
    	if (option >0 && option <5){
    		return option;
    	}
    
    	else {
    		cout<<"--Incorrect Value--"<<endl<<endl;
    		return askselectioninput();
    	}
    }
    
    void data_input(Matrices_variables datamatrices)
    {
    for (datamatrices.row=1; !(datamatrices.row>3); datamatrices.row++)
    { cout<<"Data for row: "<<datamatrices.row<<endl;
    for (datamatrices.col=1; !(datamatrices.col>4); datamatrices.col++)
    { cout<<"Type a number: ";
    cin>>datamatrices.numbers[datamatrices.row][datamatrices.col];
    }
    }
    }
    
    
    void data_output(Matrices_variables datamatrices){
    cout<<"DISPLAY"<<endl;
    for (datamatrices.row=1; !(datamatrices.row>3); datamatrices.row++)
    {
    for (datamatrices.col=1; !(datamatrices.col>4); datamatrices.col++)
    { cout<<datamatrices.numbers[datamatrices.row][datamatrices.col]<<" ";
    }
    cout<<"End"<<endl;
    }
    }
    
    void add_Matrices(Matrices_variables datamatrices){
    
    }
    
    void subtract_Matrices(Matrices_variables datamatrices){
    cout<<"SUB"<<endl;
    }
    
    void display_Matrices(Matrices_variables datamatrices){
    cout<<"DISPLAYING RESULTS :D"<<endl;
    for (datamatrices.row=1; !(datamatrices.row>3); datamatrices.row++)
    {
    for (datamatrices.col=1; !(datamatrices.col>4); datamatrices.col++)
    { cout<<datamatrices.numbers[datamatrices.row][datamatrices.col]<<" ";
    }
    cout<<"End"<<endl;
    }
    }
    
    
    void logout(){
    cout<<"Hasta La Vista,"<<endl;
    cout<<"--PROGRAM TERMINATED--"<<endl;
    exit(0);
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Matrices problems with addition and a variable.

    Quote Originally Posted by TBsparky View Post
    Hey everyone, I've got to create a program that calculates matrices in a 2D array, and then add, subtract and display the results. However, i cannot get the addition to work
    That's because you haven't written anything in the addition function.

    Secondly, please indent your code properly. When you write a program, you are to write it so that it is easy to read with respect to indentation and formatting. All of your lines in the functions are flushed to the left, making it almost impossible to know where a for loop, function body, etc. begins and ends.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Matrices problems with addition and a variable.

    Code:
    for (datamatrices.row=1; !(datamatrices.row>3); datamatrices.row++)
    Arrays start at 0, not at 1.
    Code:
    for (datamatrices.row=0;  datamatrices.row < 3; datamatrices.row++)

  4. #4
    Join Date
    Jan 2010
    Posts
    17

    Re: Matrices problems with addition and a variable.

    I did have addition code in there, i didn't notice that it didn't copy along with it, I also cant find it now... but here's the code for the variable, if it makes it easier.

    Code:
    #include <iostream>
    using namespace std;
    typedef struct Matrices_varibles{
    int numbers [3] [4];
    int row;
    int col;
    } Matrices_variables;
    
    void data_input(Matrices_variables datamatrices);
    void data_output(Matrices_variables datamatrices);
    int askselectioninput();
    void add_Matrices(Matrices_variables datamatrices);
    void subtract_Matrices(Matrices_variables datamatrices);
    void display_Matrices(Matrices_variables datamatrices);
    void logout();
    
    void main()
    {
    cout<<"========================================="<<endl;
    	cout<<"========================================="<<endl;
    	cout<<"||   Mutltiple Matrices Calculations    ||"<<endl;
    	cout<<"========================================="<<endl;
    	cout<<"========================================="<<endl;
    	Matrices_variables Data_input;
    	int option =0;
    	data_input(Data_input);
    
    	while (true){
    
    		option = askselectioninput();
    
    		if (option ==1)
    		{
    
    			subtract_Matrices(Data_input);
    		}	
    
    		else if (option ==2)
    		{
    
    			add_Matrices(Data_input);
    		}
    
    		else if (option ==3)
    		{
    
    		display_Matrices(Data_input);
    		}
    
    		else if (option ==4)
    		{
    		logout();
    		}
    	}
    }
    
    int askselectioninput()
    {
    	int option= 0;
    	cout<<"==============CHOICES====================="<<endl;
    	cout<<"||PRESS 1 to SUBTRACT      PRESS 2 to ADD||"<<endl;
    	cout<<"||       PRESS 3 to DISPLAY RESULTS      || "<<endl;
    	cout<<"=============PRESS 4 to QUIT==============="<<endl;
    	cin>>option;
    
    	if (option >0 && option <5)
    	{
    		return option;
    	}
    
    	else {
    		cout<<"--Incorrect Value--"<<endl<<endl;
    		return askselectioninput();
    	}
    }
    
    void data_input(Matrices_variables datamatrices){
    
    for (datamatrices.row=0; !(datamatrices.row>3); datamatrices.row++)
    
    { cout<<"Data for row: "<<datamatrices.row<<endl;
    
    for (datamatrices.col=0; !(datamatrices.col>4); datamatrices.col++)
    { cout<<"Type a number: ";
    
    cin>>datamatrices.numbers[datamatrices.row][datamatrices.col];
    }
    }
    }
    
    
    void data_output(Matrices_variables datamatrices){
    cout<<"DISPLAY"<<endl;
    
    for (datamatrices.row=0; !(datamatrices.row>3); datamatrices.row++)
    
    {
    for (datamatrices.col=0; !(datamatrices.col>4); datamatrices.col++)
    
    { cout<<datamatrices.numbers[datamatrices.row][datamatrices.col]<<" ";
    }
    
    cout<<"End"<<endl;
    }
    }
    
    void add_Matrices(Matrices_variables datamatrices){
    
    }
    
    void subtract_Matrices(Matrices_variables datamatrices){
    cout<<"SUB"<<endl;
    
    }
    
    void display_Matrices(Matrices_variables datamatrices)
    {
    cout<<"DISPLAYING RESULTS :D"<<endl;
    for (datamatrices.row=0; !(datamatrices.row>3); datamatrices.row++)
    {
    
    for (datamatrices.col=0; !(datamatrices.col>4); datamatrices.col++)
    { cout<<datamatrices.numbers[datamatrices.row][datamatrices.col]<<" ";
    
    }
    cout<<"End"<<endl;
    }
    }
    
    
    void logout(){
    cout<<"Hasta La Vista,"<<endl;
    cout<<"--PROGRAM TERMINATED--"<<endl;
    exit(0);
    }

  5. #5
    Join Date
    Jan 2010
    Posts
    17

    Re: Matrices problems with addition and a variable.

    Quote Originally Posted by Skizmo View Post
    Code:
    for (datamatrices.row=1; !(datamatrices.row>3); datamatrices.row++)
    Arrays start at 0, not at 1.
    Code:
    for (datamatrices.row=0;  datamatrices.row < 3; datamatrices.row++)
    The code you pasted i have just tried, it makes row 2 do a loop.

    the code for this is what was posted by a tutor as a base point, it works.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Matrices problems with addition and a variable.

    Quote Originally Posted by TBsparky View Post
    I did have addition code in there,
    And you can chalk that up to the poor formatting I mentioned to you. You still haven't edited your code so that it is formatted correctly.
    Code:
    void add_Matrices(Matrices_variables datamatrices){
    
    }
    This is what I see when I look at your post. Blank.

    Secondly, you are passing Matrices_variables by value. This means you are creating a temporary copy within the function, working on that temporary copy within that function, and when that function returns, that temporary goes away -- all of that work you did on that temporary inside the function is now gone and was done for nothing. Please pass the variables by reference, not by value.
    Code:
    void add_Matrices(Matrices_variables& datamatrices){
    
    }
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 6th, 2011 at 09:20 AM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured