Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am reading The C Programming Language 2nd Edition. I am doing the 2.10 in a tutorial introduction. I have to write a program about arrays. It is supposed to count digits, white spaces and others. This is the program:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c - '0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d ", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
return 0;
}
According to the book, the output of the program by itself is
digits = 9 3 0 0 0 0 0 0 0 1, white space = 123, other = 345
I have 2 questions:
How will the program output by itself without me doing the CTRL+Z?
When I do it manually the output is not right. Please see if I made a mistake in the code.
The output I get is
digits =, white space = 0, other = 0
This:
printf("digits =");
for (i = 0; i < 0; ++i)
printf(" %d ", ndigit[i]);
has a broken middle part in the for loop's header; i < 0 will not be true (ever!) so the loop will not run.
for (i = 0; i < 0; ++i)
This is wrong.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have started working through "The C programming language, 2nd Ed." & already I'm running into problems. This code is from Chapter 1 on arrays,
#include <stdio.h>
int main(void){
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i){
ndigit[i] = 0;
}
while ((c = getchar()) != EOF){
if (c >= '0' && c <= '9')
++ndigit[c - '0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
}
printf("digits =");
for (i = 0; i < 10; ++i){
printf(" %d", ndigit[i]);
}
printf(", white space = %d, other = %d\n", nwhite, nother);
}
It compiles (gcc via Visual Studio Code, Win 10, x64) no errors or warnings. I input the following sequence via the keyboard,
12333 45666 789
and the output I get,
digits = 0^C
Now I was expecting,
digits = 0 1 1 3 1 1 3 1 1 1, white space = 2, other = 0
I then rewrote the for loop that is supposed to print the array to print out i
for (i = 0; i < 10; ++i){
printf(" %d", i);
}
Then got,
digits = 0^C
What's going on here? First off, why isnt the array being printed, secondly why is the final printf() statement not being executed?
EDIT:: Thanks to the comments, I didn't understand the EOF character properly & how the input stream works. If I exit with ctrl+z and then enter the program executes correctly!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am working though the C programming book. I have a question in the arrays section (1.6) that says "Let us write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline) and all other characters.
#include <stdio.h>
/* count digits, white space, others */
int main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-0];
else if (c == ' ' || c == '\n' || c == '\t')
nwhite++;
else
nother++;
printf("digits =");
for (i = 0; i < 10; i++);
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
This is the compiling code. If I type 23, enter, 23, enter then ctrl+d it returns "digits = 1044509184, white space = 2, other = 0". Why? What is digits doing?
if (c >= '0' && c <= '9')
++ndigit[c-0];
This block of code is problematic as it is accessing the array out of bounds (and will cause undefined behaviours).
It should be
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
Notice the difference between 0 and '0'. The latter piece of code works as it is guaranteed that number characters are encoded consecutively.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is from K&R, it's supposed to count digits, white space, etc. I compile fine but when I run I just get a blank screen and all I can do is type inputs. Is this not working because the C language is outdated?
#include <stdio.h>
main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
Well to put it simply. This loop will end once you type ctrl + d. If you want for it to end when you press enter, just modify it a bit: change
while ((c = getchar()) != EOF)
to
while ((c = getchar()) != '\n')
The program is written to get input until an end-of-file character. Since you're using DOS, enter Ctrl+Z to terminate input (you have to hit "enter" after the Ctrl+Z because keyboard input will be line-buffered).
On edit: Ctrl+Z is the end-of-file (EOF) character in the Windows/DOS world. In UNIX/Linux environments, it is (usually) Ctrl+D.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The task is to be sure that phrases are can be readed vise versa or not (without spaces ' ').
So I got the point and made some code. But when I made
printf("%s", str_1)
I found a lot of same russian letters.
For example my inputs are: '123' '321', but displayed rubbish
My str_1[i] is going to be like 123321 and then checked first and last and so on members.
After that I added in if condition if(str[i] != 'М') and now everything nice.
Please tell me what I did wrong and what is going on with "M".
I hope my question is clear.
#include "stdio.h"
#include "locale.h"
int main(){
char str[100];
char str_1[100];
int i, j, k, l;
j = 0;
l = 0;
fgets(str, 100, stdin); //we got array
//how many chars before '\0'
for(i = 0; i < 100; i++)
{
if(str[i] != ' ' && str[i] != '\n' && str[i] != 'М')
{
str_1[j] = str[i]; //made new array without spaces between words or whatever
j++;
}
}
j--;
for(k = 0; k < j; k++)
{
if(str_1[k] == str_1[j - 1 - k])
{
l++;
}
}
if( l == k)
{
printf(";) YES! good job!\n");
}
else
{
printf(";) NOT! I'm sorry!\n");
}
return 0;
}
As it seems, you skip over the end of the string, as you never check where it ends.
At least, this is my suspicion; I am not sure if it is the right reason.
You should check for str[i] == '\0' as well and then break the whole loop.
As I see, you even made this consideration in
//how many chars before '\0'
but didn't include it in code.
A
for(i = 0; i < 100 && str[i] != '\0'; i++)
should do the trick if you omit the j-- afterwards.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
well this program is supposed to convert a binary number to decimal. im new to c and im not very confident in coding C. tried and coded this program but doesnt run properly. and I have no idea what part is wrong here. need some help to complete this program.
thanks
binaryToDecimal(char str[]) {
for(int i = strlen(str)-i; i>=0; i--) {
if(str[i] == 48 || str[i] == 49) {
int tmp = pow(2, counter);
int sum= str[i]*tmp;
counter++;
answer += sum;
} else if(str[i]>49 || str[i]<48) {
printf("error");
}
}
printf("%d", &answer);
}
This phrase doesn't make sense:
int i = strlen(str)-i;
i hasn't been initialized yet, but you're using it in the expression!
I believe you meant:
int i = strlen(str)-1; // ONE ... not I
str[i] is a character, either '1' or '0' which as you have it is equal to 48 and 49 as integers. So you want to convert them to 1 and 0 them do the multiplication.
sum = (str[i] - 48) * tmp;
Assuming all variables have been initialised to sensible values and str holds the input. The following code should work, although it is not particularly efficient.
You can use '0' and '1' instead of more obscure 48 and 49.
for (i = strlen(str)-1, j = 0; i >= 0; --i, ++j) {
if (str[i] != '0' && str[i] != '1') {
printf("ERROR: unxepected input\n");
exit(EXIT_FAILURE);
}
if (str[i] == '1')
sum += pow(2, j);
}
when printing the result provide the value and not it's address, e.g.
printf("%d\n", sum); // (!) not &sum