Unable to produce wanted output, loops and arrays in C [closed]
Why on lord's green earth does my code not work? Was expecting an output of 5 prompts but instead the program gets stuck after the first or second scanf.
#include<stdio.h>
int main()
{
int n[5], i, plus = 0, minus = 0;
printf("Enter 5 positive/negative numbers: ");
for (i = 1; i <= 5; i++)
{
scanf("%d", n[i]);
if (n[i] > 0)
{
plus++;
}
else if (n[i] < 0)
{
minus++;
}
}
printf("Total positive numbers: %d", plus);
printf("Total negative numbers: %d", minus);
}
Thanks for any help.
c arrays loops
closed as off-topic by Lundin, gsamaras, Almo, Leushenko, qrdl Nov 12 at 16:11
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Lundin, gsamaras, Almo, Leushenko, qrdl
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 2 more comments
Why on lord's green earth does my code not work? Was expecting an output of 5 prompts but instead the program gets stuck after the first or second scanf.
#include<stdio.h>
int main()
{
int n[5], i, plus = 0, minus = 0;
printf("Enter 5 positive/negative numbers: ");
for (i = 1; i <= 5; i++)
{
scanf("%d", n[i]);
if (n[i] > 0)
{
plus++;
}
else if (n[i] < 0)
{
minus++;
}
}
printf("Total positive numbers: %d", plus);
printf("Total negative numbers: %d", minus);
}
Thanks for any help.
c arrays loops
closed as off-topic by Lundin, gsamaras, Almo, Leushenko, qrdl Nov 12 at 16:11
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Lundin, gsamaras, Almo, Leushenko, qrdl
If this question can be reworded to fit the rules in the help center, please edit the question.
7
You seem to have forgotten that array indexes are zero based. I.e. an array of five elements have indexes from0to4(inclusive).
– Some programmer dude
Nov 12 at 14:32
7
Andscanf("%d", n[i])-->scanf("%d", &n[i])
– kiran Biradar
Nov 12 at 14:32
2
Both the problem I pointed out, and the problem you're asking about, could be solved by reading just about any beginners book or tutorial.
– Some programmer dude
Nov 12 at 14:33
@Someprogrammerdude No need to be this harsh, most questions on this site could be solved by reading a book too. Anyway regarding the question, in C parameters by default are passed by value. This means that if we have a function like thisvoid f(int a)then each time it's called the function will receive just a copy ofa, not the real variable. Say, we haveint a = 5;statement inmain(). If we callf(a);and that functions somehow changes its value likea = 10;or anything similar to that, after thef(a);finishes the oneain the main will still hold the value of5...
– Larry Teischwilly
Nov 12 at 21:01
...To solve that problem, there's another way of passing parameters in C, which is by reference. To do that, just pass a pointer to the function,void f(int *a);. Now what we're actually doing is, we're passing the address or the location of the variablea. So now instead of passing a copy of the value, we're just telling the compiler where exactly to find that variable. And conversely to the previous method, if we change the value ofain the newf(&a);function,awill remain like that indefinitely (or until we change it ourselves ormain()ends)...
– Larry Teischwilly
Nov 12 at 21:06
|
show 2 more comments
Why on lord's green earth does my code not work? Was expecting an output of 5 prompts but instead the program gets stuck after the first or second scanf.
#include<stdio.h>
int main()
{
int n[5], i, plus = 0, minus = 0;
printf("Enter 5 positive/negative numbers: ");
for (i = 1; i <= 5; i++)
{
scanf("%d", n[i]);
if (n[i] > 0)
{
plus++;
}
else if (n[i] < 0)
{
minus++;
}
}
printf("Total positive numbers: %d", plus);
printf("Total negative numbers: %d", minus);
}
Thanks for any help.
c arrays loops
Why on lord's green earth does my code not work? Was expecting an output of 5 prompts but instead the program gets stuck after the first or second scanf.
#include<stdio.h>
int main()
{
int n[5], i, plus = 0, minus = 0;
printf("Enter 5 positive/negative numbers: ");
for (i = 1; i <= 5; i++)
{
scanf("%d", n[i]);
if (n[i] > 0)
{
plus++;
}
else if (n[i] < 0)
{
minus++;
}
}
printf("Total positive numbers: %d", plus);
printf("Total negative numbers: %d", minus);
}
Thanks for any help.
c arrays loops
c arrays loops
edited Nov 12 at 14:32
Jon Grant
10k23153
10k23153
asked Nov 12 at 14:30
mhwk1999
34
34
closed as off-topic by Lundin, gsamaras, Almo, Leushenko, qrdl Nov 12 at 16:11
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Lundin, gsamaras, Almo, Leushenko, qrdl
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Lundin, gsamaras, Almo, Leushenko, qrdl Nov 12 at 16:11
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Lundin, gsamaras, Almo, Leushenko, qrdl
If this question can be reworded to fit the rules in the help center, please edit the question.
7
You seem to have forgotten that array indexes are zero based. I.e. an array of five elements have indexes from0to4(inclusive).
– Some programmer dude
Nov 12 at 14:32
7
Andscanf("%d", n[i])-->scanf("%d", &n[i])
– kiran Biradar
Nov 12 at 14:32
2
Both the problem I pointed out, and the problem you're asking about, could be solved by reading just about any beginners book or tutorial.
– Some programmer dude
Nov 12 at 14:33
@Someprogrammerdude No need to be this harsh, most questions on this site could be solved by reading a book too. Anyway regarding the question, in C parameters by default are passed by value. This means that if we have a function like thisvoid f(int a)then each time it's called the function will receive just a copy ofa, not the real variable. Say, we haveint a = 5;statement inmain(). If we callf(a);and that functions somehow changes its value likea = 10;or anything similar to that, after thef(a);finishes the oneain the main will still hold the value of5...
– Larry Teischwilly
Nov 12 at 21:01
...To solve that problem, there's another way of passing parameters in C, which is by reference. To do that, just pass a pointer to the function,void f(int *a);. Now what we're actually doing is, we're passing the address or the location of the variablea. So now instead of passing a copy of the value, we're just telling the compiler where exactly to find that variable. And conversely to the previous method, if we change the value ofain the newf(&a);function,awill remain like that indefinitely (or until we change it ourselves ormain()ends)...
– Larry Teischwilly
Nov 12 at 21:06
|
show 2 more comments
7
You seem to have forgotten that array indexes are zero based. I.e. an array of five elements have indexes from0to4(inclusive).
– Some programmer dude
Nov 12 at 14:32
7
Andscanf("%d", n[i])-->scanf("%d", &n[i])
– kiran Biradar
Nov 12 at 14:32
2
Both the problem I pointed out, and the problem you're asking about, could be solved by reading just about any beginners book or tutorial.
– Some programmer dude
Nov 12 at 14:33
@Someprogrammerdude No need to be this harsh, most questions on this site could be solved by reading a book too. Anyway regarding the question, in C parameters by default are passed by value. This means that if we have a function like thisvoid f(int a)then each time it's called the function will receive just a copy ofa, not the real variable. Say, we haveint a = 5;statement inmain(). If we callf(a);and that functions somehow changes its value likea = 10;or anything similar to that, after thef(a);finishes the oneain the main will still hold the value of5...
– Larry Teischwilly
Nov 12 at 21:01
...To solve that problem, there's another way of passing parameters in C, which is by reference. To do that, just pass a pointer to the function,void f(int *a);. Now what we're actually doing is, we're passing the address or the location of the variablea. So now instead of passing a copy of the value, we're just telling the compiler where exactly to find that variable. And conversely to the previous method, if we change the value ofain the newf(&a);function,awill remain like that indefinitely (or until we change it ourselves ormain()ends)...
– Larry Teischwilly
Nov 12 at 21:06
7
7
You seem to have forgotten that array indexes are zero based. I.e. an array of five elements have indexes from
0 to 4 (inclusive).– Some programmer dude
Nov 12 at 14:32
You seem to have forgotten that array indexes are zero based. I.e. an array of five elements have indexes from
0 to 4 (inclusive).– Some programmer dude
Nov 12 at 14:32
7
7
And
scanf("%d", n[i]) --> scanf("%d", &n[i])– kiran Biradar
Nov 12 at 14:32
And
scanf("%d", n[i]) --> scanf("%d", &n[i])– kiran Biradar
Nov 12 at 14:32
2
2
Both the problem I pointed out, and the problem you're asking about, could be solved by reading just about any beginners book or tutorial.
– Some programmer dude
Nov 12 at 14:33
Both the problem I pointed out, and the problem you're asking about, could be solved by reading just about any beginners book or tutorial.
– Some programmer dude
Nov 12 at 14:33
@Someprogrammerdude No need to be this harsh, most questions on this site could be solved by reading a book too. Anyway regarding the question, in C parameters by default are passed by value. This means that if we have a function like this
void f(int a) then each time it's called the function will receive just a copy of a, not the real variable. Say, we have int a = 5; statement in main(). If we call f(a); and that functions somehow changes its value like a = 10; or anything similar to that, after the f(a); finishes the one a in the main will still hold the value of 5...– Larry Teischwilly
Nov 12 at 21:01
@Someprogrammerdude No need to be this harsh, most questions on this site could be solved by reading a book too. Anyway regarding the question, in C parameters by default are passed by value. This means that if we have a function like this
void f(int a) then each time it's called the function will receive just a copy of a, not the real variable. Say, we have int a = 5; statement in main(). If we call f(a); and that functions somehow changes its value like a = 10; or anything similar to that, after the f(a); finishes the one a in the main will still hold the value of 5...– Larry Teischwilly
Nov 12 at 21:01
...To solve that problem, there's another way of passing parameters in C, which is by reference. To do that, just pass a pointer to the function,
void f(int *a);. Now what we're actually doing is, we're passing the address or the location of the variable a. So now instead of passing a copy of the value, we're just telling the compiler where exactly to find that variable. And conversely to the previous method, if we change the value of a in the new f(&a); function, a will remain like that indefinitely (or until we change it ourselves or main() ends)...– Larry Teischwilly
Nov 12 at 21:06
...To solve that problem, there's another way of passing parameters in C, which is by reference. To do that, just pass a pointer to the function,
void f(int *a);. Now what we're actually doing is, we're passing the address or the location of the variable a. So now instead of passing a copy of the value, we're just telling the compiler where exactly to find that variable. And conversely to the previous method, if we change the value of a in the new f(&a); function, a will remain like that indefinitely (or until we change it ourselves or main() ends)...– Larry Teischwilly
Nov 12 at 21:06
|
show 2 more comments
1 Answer
1
active
oldest
votes
I am sorry but scanf does not work that way. To write the value the user types in at the prompt, as n[i], scanf needs an address (in memory).
C is in this regard intentionally rudimentary, more so than a language like Python or JavaScript.
The way you wrote your code, you end up passing garbage (whatever integer value is contained at the address n + i) as an argument to scanf and expect the machine to replace that garbage with the value. And it cannot do that.
An apt analogy would be if I asked you to take whatever you find by the door and put it on a shelf, but instead of telling you which shelf to put it on, I'd give you the object that is currently on the shelf. You'd be right to ask me "why did you give me this, and which shelf do I put that what I find by the door on?".
When you specify a variable as an argument to a procedure, what is passed is the value contained in the space allocated for the variable, commonly called the "variable value", of course.
You need to give scanf the address of the space to write to. The address of n[i] is obtained using the unary operator &, e.g. &n[i] or n + i.
4
C is a portable assembler: this statement is not correct at all.
– Jabberwocky
Nov 12 at 14:39
The statement was intended to drive the point home -- one needs to understand memory addressing with C. Perhaps it is ill fit to state in general, what with optimizations a modern compiler puts into the translation and C99 and all. Anyway, if that's the only thing wrong with my answer, I can live with that :)
– amn
Nov 12 at 14:49
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am sorry but scanf does not work that way. To write the value the user types in at the prompt, as n[i], scanf needs an address (in memory).
C is in this regard intentionally rudimentary, more so than a language like Python or JavaScript.
The way you wrote your code, you end up passing garbage (whatever integer value is contained at the address n + i) as an argument to scanf and expect the machine to replace that garbage with the value. And it cannot do that.
An apt analogy would be if I asked you to take whatever you find by the door and put it on a shelf, but instead of telling you which shelf to put it on, I'd give you the object that is currently on the shelf. You'd be right to ask me "why did you give me this, and which shelf do I put that what I find by the door on?".
When you specify a variable as an argument to a procedure, what is passed is the value contained in the space allocated for the variable, commonly called the "variable value", of course.
You need to give scanf the address of the space to write to. The address of n[i] is obtained using the unary operator &, e.g. &n[i] or n + i.
4
C is a portable assembler: this statement is not correct at all.
– Jabberwocky
Nov 12 at 14:39
The statement was intended to drive the point home -- one needs to understand memory addressing with C. Perhaps it is ill fit to state in general, what with optimizations a modern compiler puts into the translation and C99 and all. Anyway, if that's the only thing wrong with my answer, I can live with that :)
– amn
Nov 12 at 14:49
add a comment |
I am sorry but scanf does not work that way. To write the value the user types in at the prompt, as n[i], scanf needs an address (in memory).
C is in this regard intentionally rudimentary, more so than a language like Python or JavaScript.
The way you wrote your code, you end up passing garbage (whatever integer value is contained at the address n + i) as an argument to scanf and expect the machine to replace that garbage with the value. And it cannot do that.
An apt analogy would be if I asked you to take whatever you find by the door and put it on a shelf, but instead of telling you which shelf to put it on, I'd give you the object that is currently on the shelf. You'd be right to ask me "why did you give me this, and which shelf do I put that what I find by the door on?".
When you specify a variable as an argument to a procedure, what is passed is the value contained in the space allocated for the variable, commonly called the "variable value", of course.
You need to give scanf the address of the space to write to. The address of n[i] is obtained using the unary operator &, e.g. &n[i] or n + i.
4
C is a portable assembler: this statement is not correct at all.
– Jabberwocky
Nov 12 at 14:39
The statement was intended to drive the point home -- one needs to understand memory addressing with C. Perhaps it is ill fit to state in general, what with optimizations a modern compiler puts into the translation and C99 and all. Anyway, if that's the only thing wrong with my answer, I can live with that :)
– amn
Nov 12 at 14:49
add a comment |
I am sorry but scanf does not work that way. To write the value the user types in at the prompt, as n[i], scanf needs an address (in memory).
C is in this regard intentionally rudimentary, more so than a language like Python or JavaScript.
The way you wrote your code, you end up passing garbage (whatever integer value is contained at the address n + i) as an argument to scanf and expect the machine to replace that garbage with the value. And it cannot do that.
An apt analogy would be if I asked you to take whatever you find by the door and put it on a shelf, but instead of telling you which shelf to put it on, I'd give you the object that is currently on the shelf. You'd be right to ask me "why did you give me this, and which shelf do I put that what I find by the door on?".
When you specify a variable as an argument to a procedure, what is passed is the value contained in the space allocated for the variable, commonly called the "variable value", of course.
You need to give scanf the address of the space to write to. The address of n[i] is obtained using the unary operator &, e.g. &n[i] or n + i.
I am sorry but scanf does not work that way. To write the value the user types in at the prompt, as n[i], scanf needs an address (in memory).
C is in this regard intentionally rudimentary, more so than a language like Python or JavaScript.
The way you wrote your code, you end up passing garbage (whatever integer value is contained at the address n + i) as an argument to scanf and expect the machine to replace that garbage with the value. And it cannot do that.
An apt analogy would be if I asked you to take whatever you find by the door and put it on a shelf, but instead of telling you which shelf to put it on, I'd give you the object that is currently on the shelf. You'd be right to ask me "why did you give me this, and which shelf do I put that what I find by the door on?".
When you specify a variable as an argument to a procedure, what is passed is the value contained in the space allocated for the variable, commonly called the "variable value", of course.
You need to give scanf the address of the space to write to. The address of n[i] is obtained using the unary operator &, e.g. &n[i] or n + i.
edited Nov 12 at 16:14
qrdl
27.2k114576
27.2k114576
answered Nov 12 at 14:36
amn
3,91753062
3,91753062
4
C is a portable assembler: this statement is not correct at all.
– Jabberwocky
Nov 12 at 14:39
The statement was intended to drive the point home -- one needs to understand memory addressing with C. Perhaps it is ill fit to state in general, what with optimizations a modern compiler puts into the translation and C99 and all. Anyway, if that's the only thing wrong with my answer, I can live with that :)
– amn
Nov 12 at 14:49
add a comment |
4
C is a portable assembler: this statement is not correct at all.
– Jabberwocky
Nov 12 at 14:39
The statement was intended to drive the point home -- one needs to understand memory addressing with C. Perhaps it is ill fit to state in general, what with optimizations a modern compiler puts into the translation and C99 and all. Anyway, if that's the only thing wrong with my answer, I can live with that :)
– amn
Nov 12 at 14:49
4
4
C is a portable assembler: this statement is not correct at all.
– Jabberwocky
Nov 12 at 14:39
C is a portable assembler: this statement is not correct at all.
– Jabberwocky
Nov 12 at 14:39
The statement was intended to drive the point home -- one needs to understand memory addressing with C. Perhaps it is ill fit to state in general, what with optimizations a modern compiler puts into the translation and C99 and all. Anyway, if that's the only thing wrong with my answer, I can live with that :)
– amn
Nov 12 at 14:49
The statement was intended to drive the point home -- one needs to understand memory addressing with C. Perhaps it is ill fit to state in general, what with optimizations a modern compiler puts into the translation and C99 and all. Anyway, if that's the only thing wrong with my answer, I can live with that :)
– amn
Nov 12 at 14:49
add a comment |
7
You seem to have forgotten that array indexes are zero based. I.e. an array of five elements have indexes from
0to4(inclusive).– Some programmer dude
Nov 12 at 14:32
7
And
scanf("%d", n[i])-->scanf("%d", &n[i])– kiran Biradar
Nov 12 at 14:32
2
Both the problem I pointed out, and the problem you're asking about, could be solved by reading just about any beginners book or tutorial.
– Some programmer dude
Nov 12 at 14:33
@Someprogrammerdude No need to be this harsh, most questions on this site could be solved by reading a book too. Anyway regarding the question, in C parameters by default are passed by value. This means that if we have a function like this
void f(int a)then each time it's called the function will receive just a copy ofa, not the real variable. Say, we haveint a = 5;statement inmain(). If we callf(a);and that functions somehow changes its value likea = 10;or anything similar to that, after thef(a);finishes the oneain the main will still hold the value of5...– Larry Teischwilly
Nov 12 at 21:01
...To solve that problem, there's another way of passing parameters in C, which is by reference. To do that, just pass a pointer to the function,
void f(int *a);. Now what we're actually doing is, we're passing the address or the location of the variablea. So now instead of passing a copy of the value, we're just telling the compiler where exactly to find that variable. And conversely to the previous method, if we change the value ofain the newf(&a);function,awill remain like that indefinitely (or until we change it ourselves ormain()ends)...– Larry Teischwilly
Nov 12 at 21:06