Loop through indexes in C
I would like to look for values in an array that equal to 0 up to a certain index, but the function sets all my previous values to 0; instead of checking for 0s.
a is the specified index, and Mem is an array of size 100.
for(int i=0;i<a;i++){
if (Mem[i]=0){
printf("Error!n");
}
}
I'm trying to check for 0s in an array up to a certain index.
If there are any, I need to output an error.
c arrays
|
show 9 more comments
I would like to look for values in an array that equal to 0 up to a certain index, but the function sets all my previous values to 0; instead of checking for 0s.
a is the specified index, and Mem is an array of size 100.
for(int i=0;i<a;i++){
if (Mem[i]=0){
printf("Error!n");
}
}
I'm trying to check for 0s in an array up to a certain index.
If there are any, I need to output an error.
c arrays
What makes you doubt the code? Any unwanted behaviour observed?
– Yunnosch
Nov 20 '18 at 19:24
Yeah, the function sets all my previous values to0; instead of checking for0s
– user10158754
Nov 20 '18 at 19:27
Do you wantMem[a]included in the check?
– Yunnosch
Nov 20 '18 at 19:27
1
And do you see any part of your code which does something likeMem[i]=0? (Caught me at reading lazily by the way ;-)
– Yunnosch
Nov 20 '18 at 19:28
1
For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.
– Yunnosch
Nov 20 '18 at 19:41
|
show 9 more comments
I would like to look for values in an array that equal to 0 up to a certain index, but the function sets all my previous values to 0; instead of checking for 0s.
a is the specified index, and Mem is an array of size 100.
for(int i=0;i<a;i++){
if (Mem[i]=0){
printf("Error!n");
}
}
I'm trying to check for 0s in an array up to a certain index.
If there are any, I need to output an error.
c arrays
I would like to look for values in an array that equal to 0 up to a certain index, but the function sets all my previous values to 0; instead of checking for 0s.
a is the specified index, and Mem is an array of size 100.
for(int i=0;i<a;i++){
if (Mem[i]=0){
printf("Error!n");
}
}
I'm trying to check for 0s in an array up to a certain index.
If there are any, I need to output an error.
c arrays
c arrays
edited Nov 20 '18 at 19:40
Yunnosch
11.3k52033
11.3k52033
asked Nov 20 '18 at 19:22
user10158754
What makes you doubt the code? Any unwanted behaviour observed?
– Yunnosch
Nov 20 '18 at 19:24
Yeah, the function sets all my previous values to0; instead of checking for0s
– user10158754
Nov 20 '18 at 19:27
Do you wantMem[a]included in the check?
– Yunnosch
Nov 20 '18 at 19:27
1
And do you see any part of your code which does something likeMem[i]=0? (Caught me at reading lazily by the way ;-)
– Yunnosch
Nov 20 '18 at 19:28
1
For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.
– Yunnosch
Nov 20 '18 at 19:41
|
show 9 more comments
What makes you doubt the code? Any unwanted behaviour observed?
– Yunnosch
Nov 20 '18 at 19:24
Yeah, the function sets all my previous values to0; instead of checking for0s
– user10158754
Nov 20 '18 at 19:27
Do you wantMem[a]included in the check?
– Yunnosch
Nov 20 '18 at 19:27
1
And do you see any part of your code which does something likeMem[i]=0? (Caught me at reading lazily by the way ;-)
– Yunnosch
Nov 20 '18 at 19:28
1
For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.
– Yunnosch
Nov 20 '18 at 19:41
What makes you doubt the code? Any unwanted behaviour observed?
– Yunnosch
Nov 20 '18 at 19:24
What makes you doubt the code? Any unwanted behaviour observed?
– Yunnosch
Nov 20 '18 at 19:24
Yeah, the function sets all my previous values to
0; instead of checking for 0s– user10158754
Nov 20 '18 at 19:27
Yeah, the function sets all my previous values to
0; instead of checking for 0s– user10158754
Nov 20 '18 at 19:27
Do you want
Mem[a] included in the check?– Yunnosch
Nov 20 '18 at 19:27
Do you want
Mem[a] included in the check?– Yunnosch
Nov 20 '18 at 19:27
1
1
And do you see any part of your code which does something like
Mem[i]=0 ? (Caught me at reading lazily by the way ;-)– Yunnosch
Nov 20 '18 at 19:28
And do you see any part of your code which does something like
Mem[i]=0 ? (Caught me at reading lazily by the way ;-)– Yunnosch
Nov 20 '18 at 19:28
1
1
For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.
– Yunnosch
Nov 20 '18 at 19:41
For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.
– Yunnosch
Nov 20 '18 at 19:41
|
show 9 more comments
2 Answers
2
active
oldest
votes
if (Mem[i]=0)
is assigning 0 to each Mem[i].
You are effectively writing 0 to every element of the array.
You need to use the == comparison operator
i.e if (Mem[i] == 0)
Other than that, you have the right idea.
thank you, that sorted it!
– user10158754
Nov 20 '18 at 19:31
Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.
– Yunnosch
Nov 20 '18 at 19:37
add a comment |
Try below code:
for(int i=0;i<a;i++){
if (Mem[i]==0){
printf("Error!n");
}
}
The = operator is for assigning a value to a variable.
But == is a comparison operator for logical expressions.
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%2f53400116%2floop-through-indexes-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
if (Mem[i]=0)
is assigning 0 to each Mem[i].
You are effectively writing 0 to every element of the array.
You need to use the == comparison operator
i.e if (Mem[i] == 0)
Other than that, you have the right idea.
thank you, that sorted it!
– user10158754
Nov 20 '18 at 19:31
Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.
– Yunnosch
Nov 20 '18 at 19:37
add a comment |
if (Mem[i]=0)
is assigning 0 to each Mem[i].
You are effectively writing 0 to every element of the array.
You need to use the == comparison operator
i.e if (Mem[i] == 0)
Other than that, you have the right idea.
thank you, that sorted it!
– user10158754
Nov 20 '18 at 19:31
Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.
– Yunnosch
Nov 20 '18 at 19:37
add a comment |
if (Mem[i]=0)
is assigning 0 to each Mem[i].
You are effectively writing 0 to every element of the array.
You need to use the == comparison operator
i.e if (Mem[i] == 0)
Other than that, you have the right idea.
if (Mem[i]=0)
is assigning 0 to each Mem[i].
You are effectively writing 0 to every element of the array.
You need to use the == comparison operator
i.e if (Mem[i] == 0)
Other than that, you have the right idea.
edited Nov 20 '18 at 19:34
Yunnosch
11.3k52033
11.3k52033
answered Nov 20 '18 at 19:29
64bitFrog64bitFrog
362
362
thank you, that sorted it!
– user10158754
Nov 20 '18 at 19:31
Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.
– Yunnosch
Nov 20 '18 at 19:37
add a comment |
thank you, that sorted it!
– user10158754
Nov 20 '18 at 19:31
Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.
– Yunnosch
Nov 20 '18 at 19:37
thank you, that sorted it!
– user10158754
Nov 20 '18 at 19:31
thank you, that sorted it!
– user10158754
Nov 20 '18 at 19:31
Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.
– Yunnosch
Nov 20 '18 at 19:37
Quite nice work for what seems to be your first answer. Check out formatting options for future answers and have fun.
– Yunnosch
Nov 20 '18 at 19:37
add a comment |
Try below code:
for(int i=0;i<a;i++){
if (Mem[i]==0){
printf("Error!n");
}
}
The = operator is for assigning a value to a variable.
But == is a comparison operator for logical expressions.
add a comment |
Try below code:
for(int i=0;i<a;i++){
if (Mem[i]==0){
printf("Error!n");
}
}
The = operator is for assigning a value to a variable.
But == is a comparison operator for logical expressions.
add a comment |
Try below code:
for(int i=0;i<a;i++){
if (Mem[i]==0){
printf("Error!n");
}
}
The = operator is for assigning a value to a variable.
But == is a comparison operator for logical expressions.
Try below code:
for(int i=0;i<a;i++){
if (Mem[i]==0){
printf("Error!n");
}
}
The = operator is for assigning a value to a variable.
But == is a comparison operator for logical expressions.
edited Nov 20 '18 at 19:33
Yunnosch
11.3k52033
11.3k52033
answered Nov 20 '18 at 19:31
Mohammadreza PanahiMohammadreza Panahi
2,66521433
2,66521433
add a comment |
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%2f53400116%2floop-through-indexes-in-c%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
What makes you doubt the code? Any unwanted behaviour observed?
– Yunnosch
Nov 20 '18 at 19:24
Yeah, the function sets all my previous values to
0; instead of checking for0s– user10158754
Nov 20 '18 at 19:27
Do you want
Mem[a]included in the check?– Yunnosch
Nov 20 '18 at 19:27
1
And do you see any part of your code which does something like
Mem[i]=0? (Caught me at reading lazily by the way ;-)– Yunnosch
Nov 20 '18 at 19:28
1
For better future questions, please study the concept of making a Minimal, Complete, and Verifiable example.
– Yunnosch
Nov 20 '18 at 19:41