1. Study (write, compile, and run/test) the following two programs.

++++++++++++++++++++++++++++++++++++++++++++++++++

// Here goes brief description of the program – program 1 // Author:
// Date:
// CSCI3100A-Spring2018-LA1

++++++++++++++++++++++++++++++++++++++++++++++++++

#include

int main() {

int c;

c = getchar(); while (c != EOF) { putchar(c); c = getchar();

}

return 0; }

//++++++++++++++++++++++++++++++++++++++++++++++++++
// Here goes brief description of the program – program 2 // Author:
// Date:
// CSCI3100A-Spring2018-LA1

//++++++++++++++++++++++++++++++++++++++++++++++++++

#include

int main() {

int c;

while ((c = getchar()) != EOF){ putchar(c);

}

return 0;

}

• What do the programs do?

• Explain the differences between these two programs

• Compare the following two statements and describe its differences: c = getchar() != EOF; and c = (getchar() != EOF);

2. Study (write, compile, and run/test) the following two programs.

//++++++++++++++++++++++++++++++++++++++++++++++++++
// Here goes brief description of the program – program 3 // Author:
// Date:
// CSCI3100A-Spring2018-LA1

//++++++++++++++++++++++++++++++++++++++++++++++++++

#include

int main() {

long nc;

nc = 0; while (getchar() != EOF) ++nc; printf (“%ldn”, nc);

return 0; }

//++++++++++++++++++++++++++++++++++++++++++++++++++
// Here goes brief description of the program – program 4

// Author:
// Date:
// CSCI3100A-Spring2018-LA1

//++++++++++++++++++++++++++++++++++++++++++++++++++

#include

int main() {

double nc;

for (nc = 0; getchar() != EOF; ++nc) ; printf (“%.0fn”, nc);

return 0; }

• What do the programs do?
• Explain the differences between these two programs
• In program 4, why the body of the for loop is empty? Is this an error? Explain.

3. Study (write, compile, and run/test) the following two programs.

//++++++++++++++++++++++++++++++++++++++++++++++++++
// Here goes brief description of the program – program 5

// Author:

// Date:
// CSCI3100A-Spring2018-LA1

//++++++++++++++++++++++++++++++++++++++++++++++++++

#include

int main() {

int c, nl;

nl = 0; while ((c = getchar()) != EOF) if (c == ’n’) ++nl; printf (“%dn”, nl);

return 0; }

1 //++++++++++++++++++++++++++++++++++++++++++++++++++

2 //

3 //

4 //

5 //

6 //++++++++++++++++++++++++++++++++++++++++++++++++++

7 #include

Here goes brief description of the program – program 6 Author:
Date:
CSCI3100A-Spring2018-LA1

8

9 #define IN 1

10 #define OUT 0

11

12 int main()

13 {

14 int c, nl, nw, nc, state;

15

16 state = OUT;

17 nl=nw=nc=0;

18 while 19
20
21

22 23 24 25 26 27

28 }

29 printf (“%d %d

30 return 0;

31 }

• What do the programs do?

• In program 6, what is the variable state used for?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *