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

Pages: 1-4041-8081-

K&R

Name: Anonymous 2008-06-14 19:20

K&R

Exercise 1-8. Write a program to count blanks, tabs, and newlines.

#include <stdio.h>

main()
{
    int blank, charac, tab, newline;
   
    blank = 0;
    tab = 0;
    newline = 0;
   
    printf("          | Blanks | Tabs | Newlines | \n");

    while((charac = getchar()) != EOF)
    {
        if(charac == ' ')
            {       
                ++blank;   
            }
        if(charac == '\t')
            {       
                ++tab;       
            }
        if(charac == '\n')
            {       
                ++newline;
                printf("| No. of: | %6d | %4d | %8d | \n", blank, tab, newline);
            }
    }
}


Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

#include <stdio.h>

main()
{
    int input_s, space, space_c;
   
    space = ' ';
    space_c = 0;
   
    while((input_s = getchar()) != EOF)
        {   
            if(input_s != space)
                {
                putchar(input_s);
                space_c = 0;
                }
            else if(input_s == space)
                {
                ++space_c;
                if(space_c == 1)
                    {
                    putchar(input_s);
                    }
                else if(space_c > 1)
                    {
                    --space_c;
                    printf("");
                    }
                }
        }
}


Using Cygwin --GCC 3.4.4, compile with following; -ansi -m32 -pedantic.

Comments on how to make it better, what is bad and what is good, please. (1.8 sucks I know.)

Name: Anonymous 2008-06-14 19:26

>>1
You just failed K&R.

A N D  I  F E E L  K I N D  O F  B A D  A B O U T  I T

Name: Anonymous 2008-06-14 19:40

>>2
You just failed unicode, god that's ugly.

Don't＀you＀feel＀kind＀of＀bad＀about＀it?

Name: Anonymous 2008-06-14 19:43

What the fuck, when I read K&R (I, a Haskellite), I had no problems with these exercises and wouldn't even think of them as being worth discussing.

Name: Anonymous 2008-06-14 19:50

>>4
Do you see anyone discussing >>1?

Name: Anonymous 2008-06-14 19:56

>>5
I see >>1 wanting to discuss it.

Name: Anonymous 2008-06-14 20:15

Your indentation and verboseness is disgusting.

printf("");

idiot

Name: Anonymous 2008-06-14 20:33

>>3
boxes
;___;

Name: Anonymous 2008-06-14 20:37

>>8
Congratulations, I didn't think it was even possible to fail Unicode that badly.

Name: Anonymous 2008-06-14 20:40

"You're not using C++, faggot. Never to behold the powers of string!"

Name: Anonymous 2008-06-14 20:44

>>9
I can't install the Japanese language pack at work since I don't have administrator access (any1 knoz fairX? i ned 0day h4x).

Name: Anonymous 2008-06-14 20:51

>>11
Don't make excuses, faggot, just accept that you're the Administrator's bitch. And you like it.

Name: Anonymous 2008-06-14 20:54

#include <stdio.h>
int main(int argc, char** argv) {
  int b=0,t=0,n=0,i=0;
  char c;
  if(argc<2) return 1;
  while((c=argv[1][i++]) != '\0') {
    switch(c) {
    case ' ':b++; break;
    case '\t':t++; break;
    case '\n':n++; break;
    }
  }
  printf("%d %d %d", b,t,n);
  return 0;
}

Name: Anonymous 2008-06-14 21:06

>>12
I don't think the Administrator works here anymore ;___;

Name: Anonymous 2008-06-14 21:35

#include <string>

Name: Anonymous 2008-06-14 21:54

#include <stdio.h>
int main(int argc, char** argv) {
  char c;
  while((c=getchar()) != EOF) {
    if(c==' ') {
      printf(" ");
      while((c=getchar())==' ');
    }
    printf("%c", c);
  }
}

Name: Anonymous 2008-06-14 21:54

Comments on how to make it better, what is bad and what is good, please. (1.8 sucks I know.)
main() is only valid in ANSI C89, ISO C90 and ISO C94.
Not returning a value from main is only valid in C99.
Which makes your program invalid for all published standards of the C language.
YOU HAVE FAILED.

Name: Anonymous 2008-06-14 22:16

I'd recommend working on your coding style.

- You have a lot of empty space that doesn't need to be there. Use a tabstop of 2 or 4, and don't put the opening { on a separate line.

- It's clearer and shorter to combine declarations and initializations, so you can write: int blank=0, charac=0; rather than int blank, charac;
blank = 0;
charac = 0;


- Use switch as an alternative to if/else if/else if/...

- Don't forget the parameters to main and the return value (cf. >>17)

- It's redundant to write if(foo) {...} else if(!foo) {...} when the condition 'foo' has no side effects. Just use if(foo) {...} else {...}.

Name: Anonymous 2008-06-14 22:40

>>18
you fail, foo is not a condition, it's an expression.
fucking faggot

Name: Anonymous 2008-06-14 22:57

>>19
It's both.

Name: Anonymous 2008-06-14 23:10

>>1,18,19
SPAWHBTC

Name: Anonymous 2008-06-14 23:37

>>20
no, it's not.
lrn2c

Name: Anonymous 2008-06-15 0:09

>>21
NYJMUA

Name: Anonymous 2008-06-15 2:47

ITT we solve KNR exercises 1-8,1-9 in our favorite languages.

#!/usr/bin/perl

$chars{$1}++ while <>=~/([ \t\n])/g;

print "\\",ord $_,": $chars{$_}\n"
    foreach keys %chars;


#!/usr/bin/perl

print map{s/ +/ /g;$_}<>;

Name: Anonymous 2008-06-15 4:20

>>23
No, You Just Make Up Acronyms?

Name: Anonymous 2008-06-15 4:42

>>24

this is why i hate perl, but understand why people love it

Name: Anonymous 2008-06-15 4:54

SINCE WHEN DOES /prog/ COME IN COLOR?

Name: Anonymous 2008-06-15 4:55

>>23,25
same person
>>27
Since mr fagbob decided to fag up /prog/ a little more

Name: Anonymous 2008-06-15 4:59

>>28
You are so wrong.

Name: Anonymous 2008-06-15 5:17

>>27
Color?

Name: Anonymous 2008-06-15 5:27

>>28
your wrong bitch

Name: Anonymous 2008-06-15 6:23

where can i download EMACS .deb file to install

Name: Anonymous 2008-06-15 6:35

printf("| No. of: | %6d | %4d | %8d | \n", blank, tab, newline);
Should be outside of your while loop.

Name: Anonymous 2008-06-15 7:21

>>33
But it reports progress for each line nicely.

Name: Anonymous 2008-06-15 7:26

>>1 here.

>>7
Tell me what to use then. Having read as far as to page p. 22 in the K&R, it doesn't mention another way of printing ``".

>>13
I haven't read about arrays and the switch(). But I assume that the switch is dependt on the char c, which tells what statement should be executed.
Besides that, isn't the switch/case only used for testing if the program gives the expected values, and isn't used for the final program?

>>int main(int argc, char** argv)
Please explain what ``int argc, char** argv" part do.

>>15
I haven't been introduced to the string header. But I assume it has functions for string manipulation. Also shouldn't the header be written like this: #include <string.h>?

>>18
>>- You have a lot of empty space that doesn't need to be >>there. Use a tabstop of 2 or 4, and don't put the opening { >>on a separate line.

Done. Not using notepad for editing. I'm now using vim and a tabstop of 2.(though I have emacs too)

>>- It's clearer and shorter to combine declarations and >>initializations, so you can write: int blank=0, charac=0; >>rather than int blank, charac;
>>blank = 0;
>>charac = 0;

Done.

>>- Use switch as an alternative to if/else if/else if/...

Refer to my question to >>13.

>>- Don't forget the parameters to main and the return value >>(cf. >>17)

Done. Although I havn't been introduced to what parameteres main() can hold. Also is possible for main() to have a null-statement or is it absolutely necessary to use return 0; and return nothing that way, in order for it to be valid ANSI C?

>>- It's redundant to write if(foo) {...} else if(!foo) {...} >>when the condition 'foo' has no side effects. Just use >>if(foo) {...} else {...}.

Allright. How about this?

Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.

#include <stdio.h>
main(){
  double lower = 0.0, upper = 100.0, step = 1.0, abs_zero = -273.15;
  double celsius = lower, fahr, kelvin, rankine;
  int esc;   
  printf(" ------------------------------------------- \n");
  printf("| Celsius |  Fahrenheit |  Kelvin | Rankine |\n");
  printf(" ------------------------------------------- \n");
  while (celsius <= upper){
    fahr = celsius * (9.0/5.0) + 32.0;
    kelvin = celsius + -abs_zero;
    rankine = (celsius + -abs_zero) * (9.0/5.0);
    printf("| %7.0f |%12.4f | %7.3f | %7.3f |\n", celsius, fahr, kelvin, rankine);
    printf(" ------------------------------------------- \n");
    celsius = celsius + step; /*or ++celsius;*/
  }
  printf("\nPress ENTER to terminate...");
  esc = (getchar() != EOF);
  printf("%",esc);
  return 0;
}


I extented the program to included Celsius to Kelvin & Celsius to Rankine conversion.

Name: Anonymous 2008-06-15 7:44

>>35
Switch only accepts constants as its cases, so it's supposed to be easier for compiler to generate faster code with switch.

using just main() is okay in C. It means you omit argc and argv, they're still there, but you don't use them; main() is not the same as main(void)

Name: Anonymous 2008-06-15 7:50

make it multibyte-char compatible

Name: Anonymous 2008-06-15 9:05

>>1 Ah now I see what >>7 is trying to say. There is not need to print if there is nothing to print, duh, fail.


#include <stdio.h>
main(){
  int input_s, space = ' ', space_c = 0;
  while((input_s = getchar()) != EOF){   
    if(input_s != space){
      putchar(input_s);
      space_c = 0;
      }
    else{
      ++space_c;
      if(space_c == 1){
    putchar(input_s);
      }
      else{
    --space_c;
      }
    }
  }
  return 0;
}

Name: Anonymous 2008-06-15 10:17

>>35
"
Don't think I didn't see your failed double single-quotes.

Name: Anonymous 2008-06-15 11:55

>>39
Don't think I didn't see your mother last night.

Name: Anonymous 2008-06-15 14:06

>>35
Besides that, isn't the switch/case only used for testing if the program gives the expected values, and isn't used for the final program?

You are confusing it with assert(), which is not present in standard C.

>>36
I think >>17 meant that main() is wrong and int main() is right, so:

>>1
Using Cygwin --GCC 3.4.4, compile with following; -ansi -m32 -pedantic.

Forgot your -Wall

Name: Anonymous 2008-06-15 14:53

1>> here.

41>>
Oh, allright, thanks. But is it possible for example to set main() to long main() or double main() or float main() or char main() or one of the other types?

Name: Anonymous 2008-06-15 15:26

>>42
What were you trying to do?

Name: Anonymous 2008-06-15 15:39

>42<
Ask your compiler

Name: Anonymous 2008-06-15 15:40

>>43

Nothing. I just want to know if it's possible.

Name: Anonymous 2008-06-15 16:25

>>38
FUCKING TYPE int main(void) YOU FUCKING MORON.

Name: Anonymous 2008-06-15 16:33

>>41
You are confusing it with assert(), which is not present in standard C.
THAT'S FUCKING BULLSIHT.
assert() is ANSI C89, ISO C90, ISO C99. You FAILED YOU COCKSUCKER.
(there is a subtle difference between C89/90's assert and C99's assert)
>>42
IT'S FUCKING >> NOT << YOU FUCKING MORON COCKSUCKER MOTHERFUCKER
NO main() CANNOT RETURN ANYTHING BUT int. MORON.
>>45
YOURE A MORON, NO ITS NOT

Name: Anonymous 2008-06-15 17:06

>>47
I lol'd but i shouldn't.

Name: Anonymous 2008-06-15 17:34

>>1 here.

>>46,47
Your RAGE made laugh. But I understand why you RAGE over my foolishness and obviously not-smart/shit programming. And I have no excuse for doing so. Beginners fail. Although I want to learn more programming and become better at it. Of course, not only C, but other programming languages as well.

I have to admit, I RAGE too. But I believe that RAGE only attracts misery, trolls and destruction. Ultimately the result is that you're TROLLED, UTTERLY FUCKED or ``HAX MY ANUS'd". Calm down and think about it, before you RAGE even more.

Good evening, sir.

Name: Anonymous 2008-06-15 17:51


#include <stdio.h>
//Been 5 years since I last wrote anything in c
main()
{
  int counters[128];
  int index = -1;

  memset(counters,0,100);
  while((index = getchar()) != EOF)
  {
    counters[index]++;
  }

  printf("blanks:%i tabs:%i newlines:%i\n", counters[32], counters[9], counters[10]);

  return 0;
}

Name: Anonymous 2008-06-15 18:29

>>50
Not too bad, but...

int counters[128];
Not 8-bit clean.

memset(counters,0,100);
Why 28 less? Also you forgot to multiply by sizeof(int).

printf("blanks:%i tabs:%i newlines:%i\n", counters[32], counters[9], counters[10]);
Hardcoding ASCII values considered harmful.

And of course the complete lack of error checking, but that applies to everything in this thread.

>>16 fails because char c isn't large enough to hold all character values and EOF.

>>35 printf("%",esc); wat. Also the two lines before it.

Name: Anonymous 2008-06-15 19:10

main =
   interact $ unwords . zipWith (++) ["blanks: ","tabs: ","newlines: "] . counts
   where counts s = map (\c -> show $ length $ filter (==c) s) [' ','\t','\n']

Name: Anonymous 2008-06-15 19:25

>>52
Too  bad  that  it's  slow  as  fuck.

Name: Anonymous 2008-06-15 19:31

>>53

2/10

Name: Anonymous 2008-06-15 20:10

Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
main = interact $ unwords . words

Name: Anonymous 2008-06-15 20:11

>>53
You do know that Haskell compilers can make certain assumptions that would be fatal if a C compiler also assumed the same things? C programs are unstable as fuck.

Name: >>55 2008-06-15 20:17

main = interact $ unlines . map (unwords . words) . lines

Name: Anonymous 2008-06-15 20:19

>>54,56
If such small Haskell programmes are so fast, then why isn't it so popular?

Name: Anonymous 2008-06-15 20:28

>>58
because of the smell.

Name: Anonymous 2008-06-15 20:28

>>58
Monads.

Name: Anonymous 2008-06-15 20:42

>>58
Uh, because that's a trivial program.

Name: Anonymous 2008-06-15 21:52

>>58
Because you have to be a mathematician to make sense of it.

Name: Anonymous 2008-06-15 22:03

>>62

5/10

Name: Anonymous 2008-06-15 22:41

>>49
LEARN TO TROLL YOU FUCKING PIECE OF SHIT YOU THINK I DON'T KNOW YOU DID THAT SHIT << ON PURPOSE? DO YOU REALLY THINK TROLLING IS THAT EASY? YOU FUCKING SCATCUMHOLE, YOU CORNY FAGGOTASSLICKER GODCOCKSHITFUCKMOTHERDAD GOD DAMMITFUCK.
NOW GO RIDE THE FUCKING TAJ MAHAL OF DICKS.
>>50
memset(counters,0,100);
YOU HAVE TO MULTIPLY THE NUMBER OF ELEMENTS WITH THE SIZE OF EACH ELEMENT OR INITIALIZE THE CORNYFAGARRAY TO {0} GOD DAMN FUCK
BOB SCATGET YOUR FUCKING GCODE HAS SO MANY ERRORS ILL POST MY OWN YOU FAGGOTASSCOCKSUCKERLICKERMOTHERFUCKE


#include <stdio.h>
#include <limits.h>
int main(void) {
  unsigned array[2 << CHAR_BIT];
  int c;
  while(((c = getchar()) != EOF) array[c]++;
  /* printf array['\n'] array[' '] array['\t'] */
  return 0;
}

TEHRE YOU GO GODFUCKDAMN STAY AWAY FROM C YOU FUCKING MORONSHITFUCK.

>>51
Not 8-bit clean.
GTFO YOU FUCKING NONPORTABLE FAGGOT. YOU ARE NOT CHAR_BIT CLEAN.
Why 28 less? Also you forgot to multiply by sizeof(int).
IT'S NOT 28 LESS. I HAVE NO FUCKING IDEA WHAT THE FUCK YOU'RE TALKING ABOUT AND YOU PROBABLY DO NOT AS WELL.
Hardcoding ASCII values considered harmful.
INDEED.
>>16 fails because char c isn't large enough to hold all character values and EOF.
WELL HERE'S A UFCKING SURPRISE TO YOU int IS NOT BIG ENOUGH FOR ALL unsigned char VALUES AND EOF EITHER. YOU NEED TO USE feof() AND ferror(). COCKLIPS

>>56
OH GOD DONT FUCKING START ME ON THIS LEARN A BIT ABOUT COMPILER THEORY AND OPTIMIZATION FIRST YOU DICKROCKER
>>60
MONADS ARE FUCKING TRIVIAL
>>62
WHAT SHUT UP
 SHUTT UPPPPPP SUHT YOUR WHORE MOUTH YOU FUCKING FUCK
>>63
GTFO WITH YOUR GOD DAMN RANKS NOBODY GIVES A SHIT
GOD DAMNGODF UCK SAGE

Name: Anonymous 2008-06-15 22:43

>>64
OH GOD MOTHERSHITFUCK THERES A MISTAKE GOD DAMN FUCK
unsigned array[2 << CHAR_BIT];
CHANGE THAT SHIT TO
unsigned array[1 << (CHAR_BIT-1)];
FUCKMOTHERS ALL OF THEM

Name: Anonymous 2008-06-15 22:48

>>63
You can't rate the truth.

Name: Anonymous 2008-06-15 22:49

>>47,64,65
Shut up Fullforce.

Name: Anonymous 2008-06-15 22:54

>>38
Your indentation is still horrible.

This is superior:
#include <stdio.h>
main(){
  int input_s, space = ' ', space_c = 0;
  while((input_s = getchar()) != EOF) {
    if(input_s != space) {
      putchar(input_s);
      space_c = 0;
    } else {
      ++space_c;
      if(space_c == 1)
       putchar(input_s);
      else
       --space_c;
    }
  }
  return 0;
}


I also notice the abundance of trailing whitespace. Use a instead of i to minimize having to remove it afterwards.

Name: Anonymous 2008-06-15 22:55

>>67
FUCK YOU FAGGOT IM NOT THAT FAGGOTFUCK I NEVER POST WITH TRIPCODES OR NAMES. ALWAYS SAGE MODE. ALWAYS RAGE MODE. FUCK YOU FUCKINGSHITFUCK

Name: Anonymous 2008-06-15 22:57

>>68
THAT DOESN'T WORK YOU MORON

Name: Anonymous 2008-06-15 23:12

>>62
What's that? You need math skills in order to understand the applications of information and computation theory? Terrible!

Name: Anonymous 2008-06-15 23:28

You want bad references?
You want horrible humor execution?
>>71 is PIG DISGUSTING.

Name: Anonymous 2008-06-15 23:56

>>71
Did I say "math skills", or did I say "mathematician"? Most languages don't involve so much "monad monad monad functor (arrows)". And all the nasty syntax keeps us Lispers off too.

Name: Anonymous 2008-06-16 0:00

>>51
And of course the complete lack of error checking, but that applies to everything in this thread.

There's no need to error check in this program. And getchar even returns EOF on error. Are you suggesting to error-check printf?

Name: Anonymous 2008-06-16 0:33

Are you suggesting to error-check printf?

yibbit.

Name: Anonymous 2008-06-16 1:06

>>1 here.

>>64,65,69
Actully, I'm not trolling.

You're the CAPS-LOCK cancer that is killing /prog/ and this thread.I recommend seeking a professional psychiatrist and/or anti-rage group psychotherapy.

Good day, sir.

>>68
int main() so it doesn't throw up a warning when compiling.

>>70
It does work.

Name: Anonymous 2008-06-16 1:20

>>76
no it doesn't you fucking faggot

Name: Anonymous 2008-06-16 3:42

>>77
Explain yourself

Name: Anonymous 2008-06-16 4:03

>>78
5.1.2.2.1

Name: Anonymous 2008-06-16 5:44

>>76
You have issues.

Name: Anonymous 2008-06-16 6:18

sage

Name: Anonymous 2008-06-16 10:08

>>62
Because you have to be a mathematician to make sense of it.
That's just what most Haskell tutorial writers want you to believe.

Name: Anonymous 2008-06-16 16:12

>>1 here.

>>78
When compiling >>68

$ gcc -ansi -pedantic [b]-Wall[/b] -m32
[filename].c:2: warning: return type defaults to `int´


By adding the int infront the main(), the warning doesn't show up. I just hate warnings.

Name: Anonymous 2008-06-16 16:26

>>82
Have you read your YAHT today?

Name: Anonymous 2008-06-16 16:31

>>83
You have issues.

Name: Anonymous 2008-06-16 17:30

>>84
I said "most", not "all".

Name: Anonymous 2008-06-16 20:59

>>83
By doing >>1-82,84- your post doesn't show up. I just hate pedants.

Name: Anonymous 2009-03-06 7:07


Blanks tabs and newlines!

Name: Anonymous 2010-12-20 23:39

Name: Sgt.Kabukiman﾿逓 2012-05-24 6:08

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Name: bampu pantsu 2012-05-29 3:51

bampu pantsu

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