Problem with demension array [Matrix] exercise
We have a square matrix A = [aij], where a number of lines and rows more than 3. Goal is: to find in matrix A position of table D [3;3], where the sum of the elements is the highest. Give this position by specifying the leftmost element's indices.
At the moment, what have i done: already wrote a code, which create a dimension array, and that all :C
int main() {
int n;
size_t height, weight;
cout << "Input height and weight of your matrix:" << endl;
cin >> dlina >> weight;
int **a = new int*[height];
for (int i = 0; i < height; i++)
a[i] = new int[weight];
for (int i = 0; i < height; i++)
for (int j = 0; j < weight; j++) {
cout << "Enter your matrix element: " << endl;
cin >> a[i][j];
}
for (int i = 0; i < height; i++){ //i=0
for (int j = 0; j < weight; j++) {//j=7
cout << a[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < height; i++)
delete a[i];
delete a;
cin >> n;
return 0;
}
c++ matrix
add a comment |
We have a square matrix A = [aij], where a number of lines and rows more than 3. Goal is: to find in matrix A position of table D [3;3], where the sum of the elements is the highest. Give this position by specifying the leftmost element's indices.
At the moment, what have i done: already wrote a code, which create a dimension array, and that all :C
int main() {
int n;
size_t height, weight;
cout << "Input height and weight of your matrix:" << endl;
cin >> dlina >> weight;
int **a = new int*[height];
for (int i = 0; i < height; i++)
a[i] = new int[weight];
for (int i = 0; i < height; i++)
for (int j = 0; j < weight; j++) {
cout << "Enter your matrix element: " << endl;
cin >> a[i][j];
}
for (int i = 0; i < height; i++){ //i=0
for (int j = 0; j < weight; j++) {//j=7
cout << a[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < height; i++)
delete a[i];
delete a;
cin >> n;
return 0;
}
c++ matrix
You haven't shown what you have tried to solve the problem.
– R Sahu
Jan 19 at 21:09
cin >> dlina >> weight
must becin >> height >> weight;
, and better to replace all the int indexes bysize_t
– bruno
Jan 19 at 21:10
add a comment |
We have a square matrix A = [aij], where a number of lines and rows more than 3. Goal is: to find in matrix A position of table D [3;3], where the sum of the elements is the highest. Give this position by specifying the leftmost element's indices.
At the moment, what have i done: already wrote a code, which create a dimension array, and that all :C
int main() {
int n;
size_t height, weight;
cout << "Input height and weight of your matrix:" << endl;
cin >> dlina >> weight;
int **a = new int*[height];
for (int i = 0; i < height; i++)
a[i] = new int[weight];
for (int i = 0; i < height; i++)
for (int j = 0; j < weight; j++) {
cout << "Enter your matrix element: " << endl;
cin >> a[i][j];
}
for (int i = 0; i < height; i++){ //i=0
for (int j = 0; j < weight; j++) {//j=7
cout << a[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < height; i++)
delete a[i];
delete a;
cin >> n;
return 0;
}
c++ matrix
We have a square matrix A = [aij], where a number of lines and rows more than 3. Goal is: to find in matrix A position of table D [3;3], where the sum of the elements is the highest. Give this position by specifying the leftmost element's indices.
At the moment, what have i done: already wrote a code, which create a dimension array, and that all :C
int main() {
int n;
size_t height, weight;
cout << "Input height and weight of your matrix:" << endl;
cin >> dlina >> weight;
int **a = new int*[height];
for (int i = 0; i < height; i++)
a[i] = new int[weight];
for (int i = 0; i < height; i++)
for (int j = 0; j < weight; j++) {
cout << "Enter your matrix element: " << endl;
cin >> a[i][j];
}
for (int i = 0; i < height; i++){ //i=0
for (int j = 0; j < weight; j++) {//j=7
cout << a[i][j] << " ";
}
cout << endl;
}
for (int i = 0; i < height; i++)
delete a[i];
delete a;
cin >> n;
return 0;
}
c++ matrix
c++ matrix
asked Jan 19 at 21:02
Michael BazyshynMichael Bazyshyn
1
1
You haven't shown what you have tried to solve the problem.
– R Sahu
Jan 19 at 21:09
cin >> dlina >> weight
must becin >> height >> weight;
, and better to replace all the int indexes bysize_t
– bruno
Jan 19 at 21:10
add a comment |
You haven't shown what you have tried to solve the problem.
– R Sahu
Jan 19 at 21:09
cin >> dlina >> weight
must becin >> height >> weight;
, and better to replace all the int indexes bysize_t
– bruno
Jan 19 at 21:10
You haven't shown what you have tried to solve the problem.
– R Sahu
Jan 19 at 21:09
You haven't shown what you have tried to solve the problem.
– R Sahu
Jan 19 at 21:09
cin >> dlina >> weight
must be cin >> height >> weight;
, and better to replace all the int indexes by size_t
– bruno
Jan 19 at 21:10
cin >> dlina >> weight
must be cin >> height >> weight;
, and better to replace all the int indexes by size_t
– bruno
Jan 19 at 21:10
add a comment |
1 Answer
1
active
oldest
votes
A trivial solution doing minimal changes from your code including my previous remark :
#include <iostream>
using namespace std;
const int N = 3;
int sum(int * a, size_t i, size_t j)
{
int n = 0;
for (size_t ii = i; ii != i + N; ++ii)
for (size_t jj = j; jj != j + N; ++jj)
n += a[ii][jj];
return n;
}
int main() {
size_t height, width;
cout << "Input height and width of your matrix:" << endl;
cin >> height >> width;
if ((height < N) || (width < N))
return 0;
int **a = new int*[height];
for (size_t i = 0; i < height; i++)
a[i] = new int[width];
for (size_t i = 0; i < height; i++) {
for (size_t j = 0; j < width; j++) {
cerr << "Enter your matrix element: " << i << ' ' << j << ":";
cin >> a[i][j];
}
}
int max = sum(a, 0, 0);
size_t maxi = 0, maxj = 0;
for (size_t i = 1; i <= (height - N); i++){
for (size_t j = 0; j <= (width - N); j++) {
int s = sum(a, i, j);
if (s > max) {
max = s;
maxi = i;
maxj = j;
}
}
}
cout << maxi << ' ' << maxj << " : " << max << endl;
for (size_t i = 0; i < height; i++)
delete a[i];
delete a;
return 0;
}
Example of execution :
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
Note : this trivial solution can be optimized to not redo all the sum of cells each time the NxN matrices moves, I let you doing ...
Execution under valgrind :
==13767== Memcheck, a memory error detector
==13767== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13767== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13767== Command: ./a.out
==13767==
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
==13767==
==13767== HEAP SUMMARY:
==13767== in use at exit: 0 bytes in 0 blocks
==13767== total heap usage: 9 allocs, 9 frees, 22,372 bytes allocated
==13767==
==13767== All heap blocks were freed -- no leaks are possible
==13767==
==13767== For counts of detected and suppressed errors, rerun with: -v
==13767== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
Great code! It fits me perfectly! Thank you!
– Michael Bazyshyn
Jan 21 at 15:55
Nice. What about to mark my answer as accepted ?
– bruno
Jan 21 at 16:43
add a comment |
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',
autoActivateHeartbeat: false,
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%2f54271362%2fproblem-with-demension-array-matrix-exercise%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A trivial solution doing minimal changes from your code including my previous remark :
#include <iostream>
using namespace std;
const int N = 3;
int sum(int * a, size_t i, size_t j)
{
int n = 0;
for (size_t ii = i; ii != i + N; ++ii)
for (size_t jj = j; jj != j + N; ++jj)
n += a[ii][jj];
return n;
}
int main() {
size_t height, width;
cout << "Input height and width of your matrix:" << endl;
cin >> height >> width;
if ((height < N) || (width < N))
return 0;
int **a = new int*[height];
for (size_t i = 0; i < height; i++)
a[i] = new int[width];
for (size_t i = 0; i < height; i++) {
for (size_t j = 0; j < width; j++) {
cerr << "Enter your matrix element: " << i << ' ' << j << ":";
cin >> a[i][j];
}
}
int max = sum(a, 0, 0);
size_t maxi = 0, maxj = 0;
for (size_t i = 1; i <= (height - N); i++){
for (size_t j = 0; j <= (width - N); j++) {
int s = sum(a, i, j);
if (s > max) {
max = s;
maxi = i;
maxj = j;
}
}
}
cout << maxi << ' ' << maxj << " : " << max << endl;
for (size_t i = 0; i < height; i++)
delete a[i];
delete a;
return 0;
}
Example of execution :
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
Note : this trivial solution can be optimized to not redo all the sum of cells each time the NxN matrices moves, I let you doing ...
Execution under valgrind :
==13767== Memcheck, a memory error detector
==13767== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13767== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13767== Command: ./a.out
==13767==
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
==13767==
==13767== HEAP SUMMARY:
==13767== in use at exit: 0 bytes in 0 blocks
==13767== total heap usage: 9 allocs, 9 frees, 22,372 bytes allocated
==13767==
==13767== All heap blocks were freed -- no leaks are possible
==13767==
==13767== For counts of detected and suppressed errors, rerun with: -v
==13767== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
Great code! It fits me perfectly! Thank you!
– Michael Bazyshyn
Jan 21 at 15:55
Nice. What about to mark my answer as accepted ?
– bruno
Jan 21 at 16:43
add a comment |
A trivial solution doing minimal changes from your code including my previous remark :
#include <iostream>
using namespace std;
const int N = 3;
int sum(int * a, size_t i, size_t j)
{
int n = 0;
for (size_t ii = i; ii != i + N; ++ii)
for (size_t jj = j; jj != j + N; ++jj)
n += a[ii][jj];
return n;
}
int main() {
size_t height, width;
cout << "Input height and width of your matrix:" << endl;
cin >> height >> width;
if ((height < N) || (width < N))
return 0;
int **a = new int*[height];
for (size_t i = 0; i < height; i++)
a[i] = new int[width];
for (size_t i = 0; i < height; i++) {
for (size_t j = 0; j < width; j++) {
cerr << "Enter your matrix element: " << i << ' ' << j << ":";
cin >> a[i][j];
}
}
int max = sum(a, 0, 0);
size_t maxi = 0, maxj = 0;
for (size_t i = 1; i <= (height - N); i++){
for (size_t j = 0; j <= (width - N); j++) {
int s = sum(a, i, j);
if (s > max) {
max = s;
maxi = i;
maxj = j;
}
}
}
cout << maxi << ' ' << maxj << " : " << max << endl;
for (size_t i = 0; i < height; i++)
delete a[i];
delete a;
return 0;
}
Example of execution :
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
Note : this trivial solution can be optimized to not redo all the sum of cells each time the NxN matrices moves, I let you doing ...
Execution under valgrind :
==13767== Memcheck, a memory error detector
==13767== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13767== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13767== Command: ./a.out
==13767==
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
==13767==
==13767== HEAP SUMMARY:
==13767== in use at exit: 0 bytes in 0 blocks
==13767== total heap usage: 9 allocs, 9 frees, 22,372 bytes allocated
==13767==
==13767== All heap blocks were freed -- no leaks are possible
==13767==
==13767== For counts of detected and suppressed errors, rerun with: -v
==13767== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
Great code! It fits me perfectly! Thank you!
– Michael Bazyshyn
Jan 21 at 15:55
Nice. What about to mark my answer as accepted ?
– bruno
Jan 21 at 16:43
add a comment |
A trivial solution doing minimal changes from your code including my previous remark :
#include <iostream>
using namespace std;
const int N = 3;
int sum(int * a, size_t i, size_t j)
{
int n = 0;
for (size_t ii = i; ii != i + N; ++ii)
for (size_t jj = j; jj != j + N; ++jj)
n += a[ii][jj];
return n;
}
int main() {
size_t height, width;
cout << "Input height and width of your matrix:" << endl;
cin >> height >> width;
if ((height < N) || (width < N))
return 0;
int **a = new int*[height];
for (size_t i = 0; i < height; i++)
a[i] = new int[width];
for (size_t i = 0; i < height; i++) {
for (size_t j = 0; j < width; j++) {
cerr << "Enter your matrix element: " << i << ' ' << j << ":";
cin >> a[i][j];
}
}
int max = sum(a, 0, 0);
size_t maxi = 0, maxj = 0;
for (size_t i = 1; i <= (height - N); i++){
for (size_t j = 0; j <= (width - N); j++) {
int s = sum(a, i, j);
if (s > max) {
max = s;
maxi = i;
maxj = j;
}
}
}
cout << maxi << ' ' << maxj << " : " << max << endl;
for (size_t i = 0; i < height; i++)
delete a[i];
delete a;
return 0;
}
Example of execution :
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
Note : this trivial solution can be optimized to not redo all the sum of cells each time the NxN matrices moves, I let you doing ...
Execution under valgrind :
==13767== Memcheck, a memory error detector
==13767== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13767== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13767== Command: ./a.out
==13767==
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
==13767==
==13767== HEAP SUMMARY:
==13767== in use at exit: 0 bytes in 0 blocks
==13767== total heap usage: 9 allocs, 9 frees, 22,372 bytes allocated
==13767==
==13767== All heap blocks were freed -- no leaks are possible
==13767==
==13767== For counts of detected and suppressed errors, rerun with: -v
==13767== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
A trivial solution doing minimal changes from your code including my previous remark :
#include <iostream>
using namespace std;
const int N = 3;
int sum(int * a, size_t i, size_t j)
{
int n = 0;
for (size_t ii = i; ii != i + N; ++ii)
for (size_t jj = j; jj != j + N; ++jj)
n += a[ii][jj];
return n;
}
int main() {
size_t height, width;
cout << "Input height and width of your matrix:" << endl;
cin >> height >> width;
if ((height < N) || (width < N))
return 0;
int **a = new int*[height];
for (size_t i = 0; i < height; i++)
a[i] = new int[width];
for (size_t i = 0; i < height; i++) {
for (size_t j = 0; j < width; j++) {
cerr << "Enter your matrix element: " << i << ' ' << j << ":";
cin >> a[i][j];
}
}
int max = sum(a, 0, 0);
size_t maxi = 0, maxj = 0;
for (size_t i = 1; i <= (height - N); i++){
for (size_t j = 0; j <= (width - N); j++) {
int s = sum(a, i, j);
if (s > max) {
max = s;
maxi = i;
maxj = j;
}
}
}
cout << maxi << ' ' << maxj << " : " << max << endl;
for (size_t i = 0; i < height; i++)
delete a[i];
delete a;
return 0;
}
Example of execution :
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
Note : this trivial solution can be optimized to not redo all the sum of cells each time the NxN matrices moves, I let you doing ...
Execution under valgrind :
==13767== Memcheck, a memory error detector
==13767== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13767== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13767== Command: ./a.out
==13767==
Input height and width of your matrix:
5 4
Enter your matrix element: 0 0:0
Enter your matrix element: 0 1:1
Enter your matrix element: 0 2:2
Enter your matrix element: 0 3:3
Enter your matrix element: 1 0:10
Enter your matrix element: 1 1:11
Enter your matrix element: 1 2:12
Enter your matrix element: 1 3:13
Enter your matrix element: 2 0:20
Enter your matrix element: 2 1:21
Enter your matrix element: 2 2:22
Enter your matrix element: 2 3:23
Enter your matrix element: 3 0:30
Enter your matrix element: 3 1:31
Enter your matrix element: 3 2:32
Enter your matrix element: 3 3:33
Enter your matrix element: 4 0:40
Enter your matrix element: 4 1:41
Enter your matrix element: 4 2:42
Enter your matrix element: 4 3:43
2 1 : 288
==13767==
==13767== HEAP SUMMARY:
==13767== in use at exit: 0 bytes in 0 blocks
==13767== total heap usage: 9 allocs, 9 frees, 22,372 bytes allocated
==13767==
==13767== All heap blocks were freed -- no leaks are possible
==13767==
==13767== For counts of detected and suppressed errors, rerun with: -v
==13767== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
edited Jan 19 at 21:54
answered Jan 19 at 21:46
brunobruno
5,79711021
5,79711021
Great code! It fits me perfectly! Thank you!
– Michael Bazyshyn
Jan 21 at 15:55
Nice. What about to mark my answer as accepted ?
– bruno
Jan 21 at 16:43
add a comment |
Great code! It fits me perfectly! Thank you!
– Michael Bazyshyn
Jan 21 at 15:55
Nice. What about to mark my answer as accepted ?
– bruno
Jan 21 at 16:43
Great code! It fits me perfectly! Thank you!
– Michael Bazyshyn
Jan 21 at 15:55
Great code! It fits me perfectly! Thank you!
– Michael Bazyshyn
Jan 21 at 15:55
Nice. What about to mark my answer as accepted ?
– bruno
Jan 21 at 16:43
Nice. What about to mark my answer as accepted ?
– bruno
Jan 21 at 16:43
add a comment |
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.
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%2f54271362%2fproblem-with-demension-array-matrix-exercise%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
You haven't shown what you have tried to solve the problem.
– R Sahu
Jan 19 at 21:09
cin >> dlina >> weight
must becin >> height >> weight;
, and better to replace all the int indexes bysize_t
– bruno
Jan 19 at 21:10