Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C programming

Name: Anonymous 2010-01-24 12:37

#include <stdio.h>
/* count digits, white space, others */
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);
}


Why is it, on line 12, ++ndigit[c-'0']; is used instead of just ++ndigit[c];?

Name: Anonymous 2010-01-24 12:40

Because ASCII values 0-9 are NUL, SOH, STX, ETX, EOT, ENQ, ACK, BEL, BS and HT.

Name: Anonymous 2010-01-24 12:41

cool indentation
The ASCII code for 0 is 48 I believe and all the other numbers are after that.
So if c is say '8' that expression would be 56 - 48 which is 8, the position you want.

Name: Anonymous 2010-01-24 12:41

Because if c is a character, the '0' character represents the number 48, '1' = 49, etc. Since the array is only size 10, accessing index 48 would be an out of bounds reference. By subtracting '0', you reduce the range to 0-9, which are the fight indices.

Name: Anonymous 2010-01-24 12:47

Alright. Thanks muchly.

Name: Anonymous 2010-01-24 13:14

WE HELPED HIM!!!

Name: Anonymous 2010-01-24 13:28

>>6
Yes, and we should all be ashamed of it.

Name: Anonymous 2010-01-24 16:35

wchar_t

Name: Anonymous 2010-01-25 4:38

>>8
WIDE CHARACTER MY ANUS

Name: Anonymous 2010-01-25 11:22

>>9
FUCK OFF SPAMMER

Name: Anonymous 2010-01-25 11:33

Use SPACE
USE EMPTY LINES
Use BRACKETS!
BRACKETS ARE COOL!
NOT USING THEM MAKES YOU UNCOOL!

MAKE IT INT Main(int argc, char argv[]) AND RETURN 0 AT THET END STATNG THAT YOUR PROGRAM WAS A SUCCESS!

NOT DOING SO WILL RESULT IN PISS-POOR PROGRAMMING!

IF YOU ARE A COMPUTER GEEK YOU WOULD WANT TO BE POPULAR AT LEAST WITH THE GEEKS RIGHT?

THE FIRST FOR LOOP IS UNNECESSARY USE
int ndigit[10] = {0};

c IS NOT AN INT USE CHAR!!!

NO NEWLINE NEED INSIDE OF AN PRINTF!
IF YOU NEED ONE CLOSE PRINTF AND START WWITH ANOTHER PRINTF WHERE YOU LEFT OFF!!!

AND MOST IMPORTANTLY DO YOUR HOMEWORK YOURSELF!!!!

Name: Anonymous 2010-01-25 12:29

>>10
SPAM MY ANUS

Name: Anonymous 2010-01-25 12:30

>>11
c IS NOT AN INT USE CHAR!!!
lrn2c
IHBT

Name: Anonymous 2010-11-27 21:43


Don't change these.
Name: Email:
Entire Thread Thread List