|
-
April 6th, 2011, 08:42 AM
#1
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);
}
-
April 6th, 2011, 08:48 AM
#2
Re: Matrices problems with addition and a variable.
 Originally Posted by TBsparky
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
-
April 6th, 2011, 08:49 AM
#3
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++)
-
April 6th, 2011, 08:51 AM
#4
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);
}
-
April 6th, 2011, 08:56 AM
#5
Re: Matrices problems with addition and a variable.
 Originally Posted by Skizmo
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.
-
April 6th, 2011, 09:10 AM
#6
Re: Matrices problems with addition and a variable.
 Originally Posted by TBsparky
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|