C 2D Arrays removing certain predetermined rows by shifting the ones below it
This is my first post here.
An assignment of my online course in C asked me to remove each row in a real(non dynamically allocated, pointers are not used) whose average sum is greater than the average sum of the whole matrix. The rows should be "removed" by shifting the ones below it up by one position.
I have set up a matrix with the following code:
int matrix[100][100]
Now, my idea was to create a regular 1D array which stores the indexes of the rows to-be-removed.
This is how I did it:
k = 0;
for (i = 0; i < no_of_rows; i++) {
average_sum_of_row = 0;
for (j = 0; j < no_of_columns; j++) {
average_sum_of_row += matrix[i][j];
}
average_sum_of_row = average_sum_of_row / no_of_columns;
if (average_sum_of_row > average_sum_of_matrix) {
indexes_of_rows_to_remove[k] = i;
k++;
l++;
}
}
Which works just fine! I get an array whose elements are the indexes of the rows which need to be removed. However, while implementing my code into the following:
m = 0;
for (i = 0; i < V; i++) {
if (indexes_of_rows_to_remove[m] == i) {
for (k = i; k < no_of_rows - 1; k++) {
for (j = 0; j < no_of_columns; j++) {
matrix[k][j] = matrix[k + 1][j];
}
}
i--;
no_of_rows--;
}
m++;
}
It does not work. What I used is my existing code of removing a row by shifting the ones below it up and decreasing the number of rows by one, but this simply doesn't work and I don't know why.
I tried using a separate integer(m) to go through all elements of the array of indexes, but for some reason it does not work.
Thanks all!
c arrays matrix 2d
add a comment |
This is my first post here.
An assignment of my online course in C asked me to remove each row in a real(non dynamically allocated, pointers are not used) whose average sum is greater than the average sum of the whole matrix. The rows should be "removed" by shifting the ones below it up by one position.
I have set up a matrix with the following code:
int matrix[100][100]
Now, my idea was to create a regular 1D array which stores the indexes of the rows to-be-removed.
This is how I did it:
k = 0;
for (i = 0; i < no_of_rows; i++) {
average_sum_of_row = 0;
for (j = 0; j < no_of_columns; j++) {
average_sum_of_row += matrix[i][j];
}
average_sum_of_row = average_sum_of_row / no_of_columns;
if (average_sum_of_row > average_sum_of_matrix) {
indexes_of_rows_to_remove[k] = i;
k++;
l++;
}
}
Which works just fine! I get an array whose elements are the indexes of the rows which need to be removed. However, while implementing my code into the following:
m = 0;
for (i = 0; i < V; i++) {
if (indexes_of_rows_to_remove[m] == i) {
for (k = i; k < no_of_rows - 1; k++) {
for (j = 0; j < no_of_columns; j++) {
matrix[k][j] = matrix[k + 1][j];
}
}
i--;
no_of_rows--;
}
m++;
}
It does not work. What I used is my existing code of removing a row by shifting the ones below it up and decreasing the number of rows by one, but this simply doesn't work and I don't know why.
I tried using a separate integer(m) to go through all elements of the array of indexes, but for some reason it does not work.
Thanks all!
c arrays matrix 2d
1
Your deletion algorithm won't work if you store the rows to be deleted. Ie: When you delete the row from the matrix your previously stored indexes will become invalid.
– kiran Biradar
Nov 13 '18 at 13:59
add a comment |
This is my first post here.
An assignment of my online course in C asked me to remove each row in a real(non dynamically allocated, pointers are not used) whose average sum is greater than the average sum of the whole matrix. The rows should be "removed" by shifting the ones below it up by one position.
I have set up a matrix with the following code:
int matrix[100][100]
Now, my idea was to create a regular 1D array which stores the indexes of the rows to-be-removed.
This is how I did it:
k = 0;
for (i = 0; i < no_of_rows; i++) {
average_sum_of_row = 0;
for (j = 0; j < no_of_columns; j++) {
average_sum_of_row += matrix[i][j];
}
average_sum_of_row = average_sum_of_row / no_of_columns;
if (average_sum_of_row > average_sum_of_matrix) {
indexes_of_rows_to_remove[k] = i;
k++;
l++;
}
}
Which works just fine! I get an array whose elements are the indexes of the rows which need to be removed. However, while implementing my code into the following:
m = 0;
for (i = 0; i < V; i++) {
if (indexes_of_rows_to_remove[m] == i) {
for (k = i; k < no_of_rows - 1; k++) {
for (j = 0; j < no_of_columns; j++) {
matrix[k][j] = matrix[k + 1][j];
}
}
i--;
no_of_rows--;
}
m++;
}
It does not work. What I used is my existing code of removing a row by shifting the ones below it up and decreasing the number of rows by one, but this simply doesn't work and I don't know why.
I tried using a separate integer(m) to go through all elements of the array of indexes, but for some reason it does not work.
Thanks all!
c arrays matrix 2d
This is my first post here.
An assignment of my online course in C asked me to remove each row in a real(non dynamically allocated, pointers are not used) whose average sum is greater than the average sum of the whole matrix. The rows should be "removed" by shifting the ones below it up by one position.
I have set up a matrix with the following code:
int matrix[100][100]
Now, my idea was to create a regular 1D array which stores the indexes of the rows to-be-removed.
This is how I did it:
k = 0;
for (i = 0; i < no_of_rows; i++) {
average_sum_of_row = 0;
for (j = 0; j < no_of_columns; j++) {
average_sum_of_row += matrix[i][j];
}
average_sum_of_row = average_sum_of_row / no_of_columns;
if (average_sum_of_row > average_sum_of_matrix) {
indexes_of_rows_to_remove[k] = i;
k++;
l++;
}
}
Which works just fine! I get an array whose elements are the indexes of the rows which need to be removed. However, while implementing my code into the following:
m = 0;
for (i = 0; i < V; i++) {
if (indexes_of_rows_to_remove[m] == i) {
for (k = i; k < no_of_rows - 1; k++) {
for (j = 0; j < no_of_columns; j++) {
matrix[k][j] = matrix[k + 1][j];
}
}
i--;
no_of_rows--;
}
m++;
}
It does not work. What I used is my existing code of removing a row by shifting the ones below it up and decreasing the number of rows by one, but this simply doesn't work and I don't know why.
I tried using a separate integer(m) to go through all elements of the array of indexes, but for some reason it does not work.
Thanks all!
c arrays matrix 2d
c arrays matrix 2d
edited Nov 13 '18 at 13:49
perreal
71.8k9110138
71.8k9110138
asked Nov 13 '18 at 13:46
THE_CRANIUMTHE_CRANIUM
111
111
1
Your deletion algorithm won't work if you store the rows to be deleted. Ie: When you delete the row from the matrix your previously stored indexes will become invalid.
– kiran Biradar
Nov 13 '18 at 13:59
add a comment |
1
Your deletion algorithm won't work if you store the rows to be deleted. Ie: When you delete the row from the matrix your previously stored indexes will become invalid.
– kiran Biradar
Nov 13 '18 at 13:59
1
1
Your deletion algorithm won't work if you store the rows to be deleted. Ie: When you delete the row from the matrix your previously stored indexes will become invalid.
– kiran Biradar
Nov 13 '18 at 13:59
Your deletion algorithm won't work if you store the rows to be deleted. Ie: When you delete the row from the matrix your previously stored indexes will become invalid.
– kiran Biradar
Nov 13 '18 at 13:59
add a comment |
1 Answer
1
active
oldest
votes
You can use this algorithm, which skips the rows to be deleted:
k = 0
For i in number of rows:
If i not to be deleted:
matrix[k] = matrix[i] # copy the whole row here
k++
The algorithm you are trying to implement is complicated and very inefficient.
I am sure I am missing something here, but what do you mean by "skipping" the rows that need to be deleted? As I understand it, I should create only one loop which if the row "i" doesn't need to be deleted copies the row "i" to row "k"? Did I get it?
– THE_CRANIUM
Nov 13 '18 at 14:24
Yes exactly. I meant if i needs to be deleted don't copy (skip) but it's not clear I agree.
– perreal
Nov 13 '18 at 14:34
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%2f53282431%2fc-2d-arrays-removing-certain-predetermined-rows-by-shifting-the-ones-below-it%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
You can use this algorithm, which skips the rows to be deleted:
k = 0
For i in number of rows:
If i not to be deleted:
matrix[k] = matrix[i] # copy the whole row here
k++
The algorithm you are trying to implement is complicated and very inefficient.
I am sure I am missing something here, but what do you mean by "skipping" the rows that need to be deleted? As I understand it, I should create only one loop which if the row "i" doesn't need to be deleted copies the row "i" to row "k"? Did I get it?
– THE_CRANIUM
Nov 13 '18 at 14:24
Yes exactly. I meant if i needs to be deleted don't copy (skip) but it's not clear I agree.
– perreal
Nov 13 '18 at 14:34
add a comment |
You can use this algorithm, which skips the rows to be deleted:
k = 0
For i in number of rows:
If i not to be deleted:
matrix[k] = matrix[i] # copy the whole row here
k++
The algorithm you are trying to implement is complicated and very inefficient.
I am sure I am missing something here, but what do you mean by "skipping" the rows that need to be deleted? As I understand it, I should create only one loop which if the row "i" doesn't need to be deleted copies the row "i" to row "k"? Did I get it?
– THE_CRANIUM
Nov 13 '18 at 14:24
Yes exactly. I meant if i needs to be deleted don't copy (skip) but it's not clear I agree.
– perreal
Nov 13 '18 at 14:34
add a comment |
You can use this algorithm, which skips the rows to be deleted:
k = 0
For i in number of rows:
If i not to be deleted:
matrix[k] = matrix[i] # copy the whole row here
k++
The algorithm you are trying to implement is complicated and very inefficient.
You can use this algorithm, which skips the rows to be deleted:
k = 0
For i in number of rows:
If i not to be deleted:
matrix[k] = matrix[i] # copy the whole row here
k++
The algorithm you are trying to implement is complicated and very inefficient.
edited Nov 13 '18 at 14:10
answered Nov 13 '18 at 13:54
perrealperreal
71.8k9110138
71.8k9110138
I am sure I am missing something here, but what do you mean by "skipping" the rows that need to be deleted? As I understand it, I should create only one loop which if the row "i" doesn't need to be deleted copies the row "i" to row "k"? Did I get it?
– THE_CRANIUM
Nov 13 '18 at 14:24
Yes exactly. I meant if i needs to be deleted don't copy (skip) but it's not clear I agree.
– perreal
Nov 13 '18 at 14:34
add a comment |
I am sure I am missing something here, but what do you mean by "skipping" the rows that need to be deleted? As I understand it, I should create only one loop which if the row "i" doesn't need to be deleted copies the row "i" to row "k"? Did I get it?
– THE_CRANIUM
Nov 13 '18 at 14:24
Yes exactly. I meant if i needs to be deleted don't copy (skip) but it's not clear I agree.
– perreal
Nov 13 '18 at 14:34
I am sure I am missing something here, but what do you mean by "skipping" the rows that need to be deleted? As I understand it, I should create only one loop which if the row "i" doesn't need to be deleted copies the row "i" to row "k"? Did I get it?
– THE_CRANIUM
Nov 13 '18 at 14:24
I am sure I am missing something here, but what do you mean by "skipping" the rows that need to be deleted? As I understand it, I should create only one loop which if the row "i" doesn't need to be deleted copies the row "i" to row "k"? Did I get it?
– THE_CRANIUM
Nov 13 '18 at 14:24
Yes exactly. I meant if i needs to be deleted don't copy (skip) but it's not clear I agree.
– perreal
Nov 13 '18 at 14:34
Yes exactly. I meant if i needs to be deleted don't copy (skip) but it's not clear I agree.
– perreal
Nov 13 '18 at 14:34
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%2f53282431%2fc-2d-arrays-removing-certain-predetermined-rows-by-shifting-the-ones-below-it%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
1
Your deletion algorithm won't work if you store the rows to be deleted. Ie: When you delete the row from the matrix your previously stored indexes will become invalid.
– kiran Biradar
Nov 13 '18 at 13:59