Really confused with voids

TBsparky's Avatar
Newbie Member
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();
    }

}
0
TBsparky's Avatar
Newbie Member
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();
    }

}
0
teacher's Avatar, Join Date: Mar 2011
Contributor
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: cpp
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.
0
teacher's Avatar, Join Date: Mar 2011
Contributor
now your actual problem

to pass 2d matrix do like this
Code: cpp
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 by teacher; 6Apr2011 at 23:43..
shabbir like this