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

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.

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