Matrix Values change after function is executed
up vote
0
down vote
favorite
I am using C++ to create an algorithm to amend matrix entries - however I have come across some strange behaviour.
The algorithm itself should provide two matrices, one of which is inputted as a variable and should be amended in the algorithm, and the other will be returned.
All was fine until I noticed that the matrix that was inputted has entry values inside the function that differ from outside the function (after the function had been executed).
I will not write out the whole code (because it is rather long and I don't think is relevant), however, I will show snippet, hopefully elucidating the issue.
double** function(double** A)
{
/*Some coding that amends entries in A...*/
for (int i = 0; i <= Dimension - 1; i++)
{
for (int j = 0; j <= Dimension -1; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << "n";
}
return B; /*nothing else after this line*/
}
This is the function (above) used which currently outputs the correct values of matrix A. However, when ouputting the values of A in the main() function, i.e.
int main()
{
/*some code*/
B = function(A)
for (int i = 0; i <= Dimension - 1; i++)
{
for (int j = 0; j <= Dimension -1; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << "n";
}
}
The values seem to differ from the ones displayed inside the function. The values are not random, they seem to be fixed values every time I compile the code, so I don't believe its a memory issue.
I would appreciate some guidance on this issue, if possible - I don't think is related to the coding of my algorithm, hence why I have not included it - If you think otherwise, please let me know. Thank you.
c++ arrays pointers matrix multidimensional-array
add a comment |
up vote
0
down vote
favorite
I am using C++ to create an algorithm to amend matrix entries - however I have come across some strange behaviour.
The algorithm itself should provide two matrices, one of which is inputted as a variable and should be amended in the algorithm, and the other will be returned.
All was fine until I noticed that the matrix that was inputted has entry values inside the function that differ from outside the function (after the function had been executed).
I will not write out the whole code (because it is rather long and I don't think is relevant), however, I will show snippet, hopefully elucidating the issue.
double** function(double** A)
{
/*Some coding that amends entries in A...*/
for (int i = 0; i <= Dimension - 1; i++)
{
for (int j = 0; j <= Dimension -1; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << "n";
}
return B; /*nothing else after this line*/
}
This is the function (above) used which currently outputs the correct values of matrix A. However, when ouputting the values of A in the main() function, i.e.
int main()
{
/*some code*/
B = function(A)
for (int i = 0; i <= Dimension - 1; i++)
{
for (int j = 0; j <= Dimension -1; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << "n";
}
}
The values seem to differ from the ones displayed inside the function. The values are not random, they seem to be fixed values every time I compile the code, so I don't believe its a memory issue.
I would appreciate some guidance on this issue, if possible - I don't think is related to the coding of my algorithm, hence why I have not included it - If you think otherwise, please let me know. Thank you.
c++ arrays pointers matrix multidimensional-array
3
All you've supplied is code that outputs values from the matrix. Nothing about how it's declared, initialized, or modified. Half the variables you're using don't exist in the code you've provided. Minimal is good, but something that reproduces the problem does need to be provided.
– zzxyz
Nov 20 at 0:26
3
Unrelated:for (int i = 0; i <= Dimension - 1; i++)
Makes people look at you funny even if the compiler doesn't and quietly modifies the output to eliminate the waste. Preferfor (int i = 0; i < Dimension; i++)
.
– user4581301
Nov 20 at 0:35
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using C++ to create an algorithm to amend matrix entries - however I have come across some strange behaviour.
The algorithm itself should provide two matrices, one of which is inputted as a variable and should be amended in the algorithm, and the other will be returned.
All was fine until I noticed that the matrix that was inputted has entry values inside the function that differ from outside the function (after the function had been executed).
I will not write out the whole code (because it is rather long and I don't think is relevant), however, I will show snippet, hopefully elucidating the issue.
double** function(double** A)
{
/*Some coding that amends entries in A...*/
for (int i = 0; i <= Dimension - 1; i++)
{
for (int j = 0; j <= Dimension -1; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << "n";
}
return B; /*nothing else after this line*/
}
This is the function (above) used which currently outputs the correct values of matrix A. However, when ouputting the values of A in the main() function, i.e.
int main()
{
/*some code*/
B = function(A)
for (int i = 0; i <= Dimension - 1; i++)
{
for (int j = 0; j <= Dimension -1; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << "n";
}
}
The values seem to differ from the ones displayed inside the function. The values are not random, they seem to be fixed values every time I compile the code, so I don't believe its a memory issue.
I would appreciate some guidance on this issue, if possible - I don't think is related to the coding of my algorithm, hence why I have not included it - If you think otherwise, please let me know. Thank you.
c++ arrays pointers matrix multidimensional-array
I am using C++ to create an algorithm to amend matrix entries - however I have come across some strange behaviour.
The algorithm itself should provide two matrices, one of which is inputted as a variable and should be amended in the algorithm, and the other will be returned.
All was fine until I noticed that the matrix that was inputted has entry values inside the function that differ from outside the function (after the function had been executed).
I will not write out the whole code (because it is rather long and I don't think is relevant), however, I will show snippet, hopefully elucidating the issue.
double** function(double** A)
{
/*Some coding that amends entries in A...*/
for (int i = 0; i <= Dimension - 1; i++)
{
for (int j = 0; j <= Dimension -1; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << "n";
}
return B; /*nothing else after this line*/
}
This is the function (above) used which currently outputs the correct values of matrix A. However, when ouputting the values of A in the main() function, i.e.
int main()
{
/*some code*/
B = function(A)
for (int i = 0; i <= Dimension - 1; i++)
{
for (int j = 0; j <= Dimension -1; j++)
{
std::cout << A[i][j] << " ";
}
std::cout << "n";
}
}
The values seem to differ from the ones displayed inside the function. The values are not random, they seem to be fixed values every time I compile the code, so I don't believe its a memory issue.
I would appreciate some guidance on this issue, if possible - I don't think is related to the coding of my algorithm, hence why I have not included it - If you think otherwise, please let me know. Thank you.
c++ arrays pointers matrix multidimensional-array
c++ arrays pointers matrix multidimensional-array
asked Nov 20 at 0:21
Naji
1
1
3
All you've supplied is code that outputs values from the matrix. Nothing about how it's declared, initialized, or modified. Half the variables you're using don't exist in the code you've provided. Minimal is good, but something that reproduces the problem does need to be provided.
– zzxyz
Nov 20 at 0:26
3
Unrelated:for (int i = 0; i <= Dimension - 1; i++)
Makes people look at you funny even if the compiler doesn't and quietly modifies the output to eliminate the waste. Preferfor (int i = 0; i < Dimension; i++)
.
– user4581301
Nov 20 at 0:35
add a comment |
3
All you've supplied is code that outputs values from the matrix. Nothing about how it's declared, initialized, or modified. Half the variables you're using don't exist in the code you've provided. Minimal is good, but something that reproduces the problem does need to be provided.
– zzxyz
Nov 20 at 0:26
3
Unrelated:for (int i = 0; i <= Dimension - 1; i++)
Makes people look at you funny even if the compiler doesn't and quietly modifies the output to eliminate the waste. Preferfor (int i = 0; i < Dimension; i++)
.
– user4581301
Nov 20 at 0:35
3
3
All you've supplied is code that outputs values from the matrix. Nothing about how it's declared, initialized, or modified. Half the variables you're using don't exist in the code you've provided. Minimal is good, but something that reproduces the problem does need to be provided.
– zzxyz
Nov 20 at 0:26
All you've supplied is code that outputs values from the matrix. Nothing about how it's declared, initialized, or modified. Half the variables you're using don't exist in the code you've provided. Minimal is good, but something that reproduces the problem does need to be provided.
– zzxyz
Nov 20 at 0:26
3
3
Unrelated:
for (int i = 0; i <= Dimension - 1; i++)
Makes people look at you funny even if the compiler doesn't and quietly modifies the output to eliminate the waste. Prefer for (int i = 0; i < Dimension; i++)
.– user4581301
Nov 20 at 0:35
Unrelated:
for (int i = 0; i <= Dimension - 1; i++)
Makes people look at you funny even if the compiler doesn't and quietly modifies the output to eliminate the waste. Prefer for (int i = 0; i < Dimension; i++)
.– user4581301
Nov 20 at 0:35
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384509%2fmatrix-values-change-after-function-is-executed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384509%2fmatrix-values-change-after-function-is-executed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
All you've supplied is code that outputs values from the matrix. Nothing about how it's declared, initialized, or modified. Half the variables you're using don't exist in the code you've provided. Minimal is good, but something that reproduces the problem does need to be provided.
– zzxyz
Nov 20 at 0:26
3
Unrelated:
for (int i = 0; i <= Dimension - 1; i++)
Makes people look at you funny even if the compiler doesn't and quietly modifies the output to eliminate the waste. Preferfor (int i = 0; i < Dimension; i++)
.– user4581301
Nov 20 at 0:35