Really confused with voids

Discussion in 'C++' started by TBsparky, Apr 6, 2011.

  1. TBsparky

    TBsparky New Member

    Joined:
    Feb 13, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Uni
    Location:
    In a house
    Hey everyone, I'm trying to get my variables to work within voids, as the variables hold 2D matrices, but i cannot get it to work. What do I need to do to get them to pass across? here's my code,:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    #include <math.h>
    
    using namespace std;
    void subtract_data();
    void add_data();
    void multiply_data();
    void data_output();
    int selectioninput();
    void logout();
    
    
    void data_output(){
    cout<<"---------------------------------"<<endl;
    cout<<"   The output of Matrices Data:  "<<endl;
    cout<<"---------------------------------"<<endl;
    int numbers[3][4];
    int col, row;
    
    for (row=1; !(row>3); row++)
    {
    
    for (col=1; !(col>4); col++)
    { cout<<numbers[row][col]<<" ";
    }
    
    cout<<"End"<<endl;
    }
    }
    
    void add_data(){
    
    cout<<"---------------------------------"<<endl;
    cout<<"     Adding Data of Matrices:"<<endl;
    cout<<"---------------------------------"<<endl;
    
    }
    
    void subtract_data(){
    cout<<"---------------------------------"<<endl;
    cout<<"   Subtracting Data of Matrices:"<<endl;
    cout<<"---------------------------------"<<endl;
    
    }
    
    void multiply_data(){
    cout<<"---------------------------------"<<endl;
    cout<<"   Multiplying Data of Matrices:"<<endl;
    cout<<"---------------------------------"<<endl;
    
    }
    
    void logout()
    {
    cout<<"---------------------------------"<<endl;
    cout<<"      Exiting Program:"<<endl;
    cout<<"---------------------------------"<<endl;
    cout<<"--PROGRAM TERMINATED---"<<endl;
    exit (0);
    
    }
    
    int main()
    {
        cout<<"============================="<<endl;
        cout<<"||  Matrices Calculations   ||"<<endl;
        cout<<"============================="<<endl;
    
        cout<<"----------------------------"<<endl;
        cout<<"| Created by Rhys Thomas    |"<<endl;
        cout<<"| Student ID: 20524455      |"<<endl;
        cout<<"|       25/03/2011          |"<<endl;
        cout<<"----------------------------"<<endl;
    int numbers[3][4];
    int row, col;
    int option;
    cout<<"---------------------------------"<<endl;
    cout<<"Inputting Data into Matrices:"<<endl;
    cout<<"---------------------------------"<<endl;
    
    for (row=1; !(row>3); row++)
    { cout<<"Data for Row Number "<<row<<endl;
    for (col=1; !(col>4); col++)
    { cout<<"Please enter a number for this Matrice ";
    cin>>numbers[row][col];
    }
    }
    while (true){
    
            option = selectioninput();
    
            if (option ==1)
            {
    
                subtract_data();
            }    
    
            else if (option ==2)
            {
    
                add_data();
            }
    
            else if (option ==3)
            {
    
            multiply_data();
            }
    
            else if (option ==4)
            {
            data_output();
            }
            else if (option ==5){
                logout ();
            }
    
        }
    }
    
    
    int selectioninput(){
        int option= 0;
        cout<<"===================YOUR CHOICES==========================="<<endl;
        cout<<"Press 1 for Subtraction               Press 2 for Addition"<<endl;
        cout<<"press 3 for Multiplication            Press 4 to Display"<<endl;
        cout<<"===============Press 5 for Quit=========================="<<endl;
        cin>>option;
        if (option >0 && option <6){
            return option;
        }
    
        else {
            cout<<"--Incorrect Value--"<<endl<<endl;
            return selectioninput();
        }
    
    }
    
     
  2. TBsparky

    TBsparky New Member

    Joined:
    Feb 13, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Uni
    Location:
    In a house
    Sorry, here's an update of my code, the actual variables are in "INT MAIN"

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string.h>
    #include <math.h>
    
    using namespace std;
    void subtract_data();
    void add_data();
    void multiply_data();
    void data_output();
    int selectioninput();
    void logout();
    
    
    void data_output(){
    cout<<"---------------------------------"<<endl;
    cout<<"   The output of Matrices Data:  "<<endl;
    cout<<"---------------------------------"<<endl;
    
    for (row=1; !(row>3); row++)
    {
    for (col=1; !(col>4); col++)
    { cout<<numbers[row][col]<<" ";
    }
    cout<<"End"<<endl;
    }
    }
    
    void add_data(){
    cout<<"---------------------------------"<<endl;
    cout<<"     Adding Data of Matrices:"<<endl;
    cout<<"---------------------------------"<<endl;
    
    }
    
    void subtract_data(){
    cout<<"---------------------------------"<<endl;
    cout<<"   Subtracting Data of Matrices:"<<endl;
    cout<<"---------------------------------"<<endl;
    
    }
    
    void multiply_data(){
    cout<<"---------------------------------"<<endl;
    cout<<"   Multiplying Data of Matrices:"<<endl;
    cout<<"---------------------------------"<<endl;
    
    }
    
    void logout()
    {
    cout<<"---------------------------------"<<endl;
    cout<<"      Exiting Program:"<<endl;
    cout<<"---------------------------------"<<endl;
    cout<<"--PROGRAM TERMINATED---"<<endl;
    exit (0);
    
    }
    
    int main()
    {
    int numbers[3][4];
    int row, col;
    int option;
    
        cout<<"============================="<<endl;
        cout<<"||  Matrices Calculations   ||"<<endl;
        cout<<"============================="<<endl;
    
        cout<<"----------------------------"<<endl;
        cout<<"| Created by Rhys Thomas    |"<<endl;
        cout<<"| Student ID: 20524455      |"<<endl;
        cout<<"|       25/03/2011          |"<<endl;
        cout<<"----------------------------"<<endl;
    
    cout<<"---------------------------------"<<endl;
    cout<<"Inputting Data into Matrices:"<<endl;
    cout<<"---------------------------------"<<endl;
    
    for (row=1; !(row>3); row++)
    { cout<<"Data for Row Number "<<row<<endl;
    for (col=1; !(col>4); col++)
    { cout<<"Please enter a number for this Matrice ";
    cin>>numbers[row][col];
    }
    }
    while (true){
    
            option = selectioninput();
    
            if (option ==1)
            {
    
                subtract_data();
            }    
    
            else if (option ==2)
            {
    
                add_data();
            }
    
            else if (option ==3)
            {
    
            multiply_data();
            }
    
            else if (option ==4)
            {
            data_output();
            }
            else if (option ==5){
                logout ();
            }
    
        }
    }
    
    
    int selectioninput(){
        int option= 0;
        cout<<"===================YOUR CHOICES==========================="<<endl;
        cout<<"Press 1 for Subtraction               Press 2 for Addition"<<endl;
        cout<<"press 3 for Multiplication            Press 4 to Display"<<endl;
        cout<<"===============Press 5 for Quit=========================="<<endl;
        cin>>option;
        if (option >0 && option <6){
            return option;
        }
    
        else {
            cout<<"--Incorrect Value--"<<endl<<endl;
            return selectioninput();
        }
    
    }
    
     
  3. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    http://www.crazylearner.in
    hii there first of all there is some problem in your code which i want to point out...

    if you carefully look to your matrix you will find that your rows and columns will go out of bound

    the range of your rows is from [0,2] but in the loop it will go [1,3].may be you forget that array starts from 0 not from 1 here in c++

    similarly in your columns alloted range is [0,3] but your loop will go [1,4].so first correct this.

    i am writing correct version of loops .
    Code:
    for (row=0;!(row>2); row++)
    { 
    cout<<"Data for Row Number "<<row<<endl;
    
    for (col=0; !(col>3); col++)
    { 
    cout<<"Please enter a number for this Matrice ";
    cin>>numbers[row][col];
    }
    }
    
    will reply your doubt in next post.
     
  4. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    http://www.crazylearner.in
    now your actual problem

    to pass 2d matrix do like this
    Code:
    void func(int t[][4]){
    int row,col;
    for (row=0;!(row>2); row++)
    {
    for (col=0; !(col>3); col++)
    {
    cout<<numbers[row][col];
    }
    cout<<"\n";
    }
    }
    
    int main(){
    int m[3][4];
    // initialize your matrix with data here
    func(m);
    }
    
    hope this helps...
     
    Last edited: Apr 6, 2011
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice