You are on page 1of 93

Introduction to Computers

2017 9

x63131, sychu@mail.ncku.edu.tw
x63118, yusin@mail.ncku.edu.tw


C
C

0. Google.
1. Paul Deitel and Harvey Deitel, C How To Program, 7ed, Pearson
Education (2012)
2. Al Kelley and Ira Pohl, A Book on C, Benjamin Cummings (1990).
3. Steven R. Lerman, Problem Solving and Computation for Scientists
and Engineers, Prentice Hall (1993).
4. Brian W. Kernighan and Dennis M. Ritchie, The C Programming
Language, 2ed, Prentice Hall (1988).
5. Ravindra K. Ahuja, Thomas L. Magnanti, and James B. Orlin,
Network Flows: Theory, algorithms, and Applications, Prentice Hall
(1993).
6. Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest,
Introduction to Algorithms, MIT Press (1990).
7. Ellis Horowitz and Sartaj Sahni, Fundamentals of Computer
Algorithms, Computer Science Press (1978).
8. Graham Seed, An Introduction to Object-Oriented Programming in
C++, Springer (2001).

- 2 -
25%



1. Visual Studio
2.

3. 5:00 24
8 24
4.
(http://moodle.ncku.edu.tw/)

5.
6.
email

7.

1.

2.

- 3 -

1 9

1 9/18 9/22 1.1 1.2 HW 1 out
2 9/25 9/29 1.3, 1.4 HW 1 due(9/29)
3 10/2 10/6 2.1, 2.2 HW 2 out
10/4
4 10/9 10/13 2.3, 2.4 HW 2 due(10/13)
10/10
5 10/16 10/20 2.5, 2.6 HW 3 out
6 10/23 10/27 2.7 HW 3 due(10/27)
7 10/30 11/3 2.8 HW 4 out
8 11/6 11/10 2.9, 2.10 HW 4 due(11/10)
9 11/13 11/17 11/14, 18:00

10 18

10 11/20 11/24 3.1, 3.2 HW 5 out
11 11/27 12/1 3.3, 3.4 HW 5 due(12/1)
12 12/4 12/8 3.5 HW 6 out
13 12/11 12/15 3.6, 3.7 HW 6 due(12/15)
14 12/18 12/22 3.7, 3.8 HW7 out
15 12/25 12/29 3.8 HW 7 due(12/29)
16 1/1 1/5 3.9, 3.10 HW 8 out
17 1/8 1/12 3.10 HW 8 due(1/12)
18 1/15 1/19 1/14, 15:00

- 4 -

.................................................................................................................................................. 2

.......................................................................................................................................................... 5

1 ...................................................................................................................................................... 7

1.1 ............................................................................................................................................... 7
1.2 ....................................................................................................................................... 8
1.3 ................................................................................................................................... 8
1.4 ..................................................................................................................... 15
2 C ......................................................................................................................................... 16

2.1 ......................................................................................................................... 16
2.2 ..................................................................................................................... 17
2.3 ..................................................................................................................................... 21
2.4 ......................................................................................................................... 22
2.5 ..................................................................................................................................... 23
2.6 ..................................................................................................................................... 26
2.7 ............................................................................................................................................. 28
2.8 ................................................................................................................................. 33
2.9 C ...................................................................................................................... 36
2.10 ........................................................................................................................... 40
2.11 ........................................................................................................................................... 40
3 C ................................................................................................................................. 46

3.1 ..................................................................................................................................... 46
3.2 ......................................................................................................................... 47
3.3 IF ... ELSE IF ...................................................................................................... 49
3.4 SWITCH............................................................................................................. 51
3.5 ................................................................................................................. 53
3.6 ......................................................................................................................... 55
3.7 STRUCTURES................................................................................................................................. 57
3.8 ................................................................................................................................. 64
3.9 ............................................................................................................................................. 72
3.10 ............................................................................................................................... 7778
3.11 ....................................................................................................................................... 7980
A VISUAL STUDIO ............................................................................................................ 87

- 5 -
A.1 .................................................................................................................................... 87
A.2 VISUAL STUDIO .......................................................................................................... 87

- 6 -
1
1.1

1972 Bell Laboratories Dennis Ritchie C


BCPL B BCPL Martin Richards 1967
Ken Thompson 1970 AT&T
BCPL B Ritchie
(UNIX)

1.

2.

3.



4.

5.

- 7 -
Bell Laboratories Bjarne Stroustrup C
C++ 1994 ISO C++ 1997
C++

/C++

1.2

(system software)(application
software)
(operating system)
C

(supercomputers)
(mainframe
computers)
(minicomputers)

(microcomputers)
(personal digital assistant, PDA)

1.3
(information)

0 1 0 1

(decimal number system)


(base 10 number system)
(digit)
365

36510 (3102 ) (6 101 ) (5 100 )

- 8 -
(positional value) 10

(positional notation)(Sign-value notation)


I 1V 5XLCDM 10
50100500 1000 0123456
789 9 10 1

(binary number system) 2 (base) 0
1 11012

11012 (1 23 ) (1 22 ) (0 21 ) (1 20 ) 13

4 00002 11112 24 16
(bit binary digit ) n
2 n

8 (byte)

8 bits= 1 byte
28 256
(Internet Protocol Address, IP Address) 4
32 IP Address

16 (2 bytes) 65,536
- 9 -
8 (1 byte char )
-128 +127 16 (2 bytes short int )
-32,768 +32,767

(drivers)

(octal)(hexadecimal)
01234567 8
16 16 ABCD
EF B.1
B.1

0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4
5 0101 5 5
6 0110 6 6
7 0111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
16 10000 20 10

C
0
k=0230x
0X( 0 x )
j=0x6BE printf()
"%x""%X""%o"
"%O"

- 10 -
#include <stdio.h>
int main()
{
int i,j,k;
i=1726;
j=0x6BE;
k=023;

printf("i=0x%x\n",i);
printf("j=%d\n",j);
printf("j=o%o\n",j);
printf("k=%d\n",k);
printf("k=o%o\n",k);
printf("k=0x%x\n",k);
return(0);
}

i=0x6be
j=1726
j=o3276
k=19
k=o23
k=0x13


(octal) B.1
0002 1112 0 7

011010111110
3

011 010 111 110


B.1 3276
32768

011 010 111 110

3 2 7 6

32768 (3 83 ) (2 82 ) (7 81 ) (6 80 ) 172610

- 11 -
1726

4
(hexadecimal)

0110 1011 1110

6 B E

6BE16 (6 162 ) (11161 ) (14 160 ) 172610

1726

4
3

#include <stdio.h>
int main()
{
int i=10;
printf("%p\n",&i);
printf("%d\n",&i);
return(0);
}

0013FF60
1310560
32 i 8
4 32 32
232 4.295 109 4GB CPU 64
264 16TB

(two,s complement representation)

- 12 -
#include <stdio.h>
int main()
{
int i=5,j=-5;
printf("0X: %08X\n",i);
printf("0X: %08X\n",j);
return(0);
}

0X: 00000005
0X: FFFFFFFB
0
1
1. 1 0 0 1

2. 1

(one,s complement)
5 8

0 0 0 0 0 1 0 1
-5
1 5

1 1 1 1 1 0 1 0
2 1
1 1 1 1 1 0 1 1
CPU
1. 1
2.

00010100 1 3 1
0 1 1 0 11101100 2
10 20 2 0

CPU
CPU

- 13 -
9

11110111 -9

n 2
n1

2 1 32 (4 bytes)
n1

-2,147,483,648 +2,147,483,647
(0verflow) ( 0.5 0.83 ) (
6.24 1022 )( 1.5 1018 )(floating point)

1 0 1.1 0 1
(decimat point)
(radix point)

(2)

- 14 -

1946 ENIAC
0 9

30 000 999 1000


628 30

30 230 1G 109

1.4

http://moodle.ncku.edu.tw/()Moodle

- 15 -
2 C
2.1
C
C

int main()
{
int i;
i = 5;
return(0);
}
C (free-format language)

int main(){int i;i = 5;return(0);}

/**/
int main()
{
int i; /* An integer */
i = 5; /* Set the value of this integer to 5 */
/* */
return (0);
}

int main()
{
int i; /* An integer */
i = /* Set the value of this integer to 5 */ 5;
return (0);
}

- 16 -

//
C++ C C++

int main()
{
int i; // This is a comment.
i = 5; // This is another comment.
return (0);
}

C function C
FORTRAN BASIC
main
main
main C

C C

Compiler

C Visual Studio
PC Turbo C++Borland C++Visual C++DJGPP
UNIX ccgcc C C++

2.2
C
C
int i i

- 17 -
int main() int main()
{ {
int i; int i = 10;
i = 10; return (0);
return (0); }
}

ijk k -10

int main()
{
int i, j, k = -10;

i = 5;
j = 6;
return (0);
}
C

int C
C
short int
long int
char

Bool ((Boolean value)) true false

bool x;
true false
x = true;
x = false;

char long int


(character)
ASCII (American Standard Code for Information Exchange)

- 18 -

\ 0 1 2 3 4 5 6 7 8 9
Back
0 \0 Bell tab
space
1 \n
2 esc
3 ! # $ % &
4 ( ) * + , - . / 0 1
5 2 3 4 5 6 7 8 9 : ;
6 < = > ? @ A B C D E
7 F G H I J K L M N O
8 P Q R S T U V W X Y
9 Z [ \ ] ^ _ ` a b c
10 d e f g h i j k l m
11 n o p q r s t u v w
12 x y z { | } ~ delete
C
C
intshort intlong int char

1. char
2. short int int long int
3. int long int

windows (byte)
char int float double
1 4 4 8
short -- 2 -- --
long -- 4 -- 8
C
C limits.h

limits.h
C
limits.h

unsigned unsigned char


unsigned intunsigned short int unsigned long int
short int -32768(-215) +32767(215-1) unsigned

- 19 -
short int 0 65535 (216-1)

7/4 14/7 0-7/4


-1

C
3.1415910-3
C float double
long double

int main()
{
int i; /* An integer */
float y;
double x;

i = 5;
y = 10.0;
x = y + i;
return (0);
}

i y floaty
i i float
y float float
double x
i y i

int main()
{
int i = 10, j = 4;
double x = 2.5, result1, result2;

result1 = i/j*x;
result2 = x*i/j;
return (0);
}

- 20 -
result1 result2
result1
10/4 2 2 2.5
2 double 5.0 result1 result2
2.5*10 25.0 25.0/4 6.25

C
type casting
result1 = (double) i/j*x;
i double 6.25

result2 = (double)(i/j)*x;

2.3

(standard output stream)

#include <iostream>
using namespace std;

int main()
{
int i; /* An integer */
double y;
double x;

i = 5;
y = 10.0;
x = y + i;
cout << "x = " << x << endl;
return (0);
}

#include <iostream>
cout endl
\n
using namespace std;
std namespace()Namespace

- 21 -
()
iostream cout cin
namespace std namespace
namespace std

x = 15

2.4
C

my_name
class_123
No_Justice
BuyMeOne

7_at_a_time /* */
my-brother /* - */
good or bad? /* */
C n
C n 6
31

C XYZxyz xYz

C 32 C
for
For int Int

- 22 -
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

2.5
(operator)
(operand)+-

+
-

=
i = 5;
=
i=5 5=i
i 5
C i=5
5=i 5 l-value

i = 10
i

l-value
10
l-value
r-value

+
a+b
a b i=5=

- 23 -

i = a + b;
+
a b
=+
i=
;

x = y * z + a;
k = (3 + a) * b;
=
c b

a
l-value
a = b = c;
x = y = z + 3;

a = (b = c);
x = (y = z + 3);
5
+
-
*
/
%

%
a b a%b 0
a b

C
5 */%
+-*/%+-

- 24 -
i=3+4*5+6*7*8 4*5=20 6*7=42
42*8=336 3+20=23 23+336= 359
C

C

{} () [] -> .
! + - ~ ++ -- * & (type) sizeof *+
-
* / % *
+ - +-
<< >>
< <= > >=
== !=
&
^
|
&&
||
?:
= += -= *= /= %= &= ^= |= <<= >>=
,

C
+=-=*=/=%=+=

i += 5;
i 5 i
i = i + 5;
i j 40
i = 10;
j = (i += 30);

j = i += 30;
-=*=/=%=-*
/%+=

- 25 -
C

2.6

C (bit)
(bitwise operator) 6 (
)


~ (complement)
<<
>>
& (AND)
^ XOR
| (OR)

<<>>~
2

<<>> cin cout

&^|

a b a&b a^b a|b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 0 1
&

#include <stdio.h>
int main()
{
int a=65,b=98;
- 26 -
printf("0X%08X\n",a);
printf("0X%08X\n",b);
printf("0X%08X\n",a&b);
return(0);
}

0X00000041
0X00000062
0X00000040

(byte)
AND (&)

a 65 41 0100 0001
b 98 62 0110 0010
a&b 64 40 0100 0000
XOR (^)

a 65 41 0100 0001
b 98 62 0110 0010
a^b 35 23 0010 0011
OR (|)

a 65 41 0100 0001
b 98 62 0110 0010
a|b 99 63 0110 0011


a 65 41 0100 0001
~a -66 BE 1011 1110

X N

X >> N;
X N 0

X << N;
X N 0
2

- 27 -
2


a 181 B5 1011 0101
a>>3 22 16 0001 0110



a 181 0B5 0000 1011 0101
a<<2 724 2D4 0010 1101 0100

( AND
&)pattern of bits
(driver)

2.7
C
C
main()
main()

main()
(call)
C
A B A (calling
function) B (called function)
C

(arguments)
(returned value)

int

- 28 -
sum
int

C
// Returns the sum of its arguments
int sum(int x, int y)
{
int z;
z = x + y;
return (z);
}

{ }
return

return return return

void

return return
return (z) z
2

main() 0 main()
0 0

c = sum(a, b)
sum( )

#include <iostream>
using namespace std;

int sum(int x, int y);


int main()
{
int a, b, c;
a = 5;
b = 10;
- 29 -
c = sum(a, b); // Function call
cout << "c = " << c << endl;
return(0);
}
// Returns the sum of its parameters
int sum(int x, int y)
{
int z;
z = x + y;
return (z);
}

c = 15

int sum(int, int);

C
#include <iostream>
sum()
main() main() 6
a b 5 10
sum() c main()
sum() c c 15
c 0

sum( ) main( )
sum( ) sum( )
main( ) sum( )
main( ) sum( )
sum( ) sum( )

#include <iostream>
using namespace std;

// Returns the sum of its parameters


int sum(int x, int y)
{
int z;
z = x + y;
return (z);
}

- 30 -
int main()
{
int a, b, c;
a = 5;
b = 10;
c = sum(a, b);
cout << "c = " << c << endl;
return(0);
}

sum()
c = sum(a, 20);

c = sum(a, a+b);

c = sum(a, sum(a, b));


a+b
sum() sum()
a b a
c
sum()
sum(a, b);

sum(a, 3.5);
sum()
squares() double sum()

#include <iostream>
using namespace std;

int sum(int x, int y);


double squares(double x);
int main()
{
int c;
c = sum(10, squares(100.0));
cout << "The result is " << c;
return(0);
}

- 31 -
// Returns the sum of its parameters
int sum(int x, int y)
{
int z;
z = x + y;
return (z);
}
// Returns the square of its parameter
double squares(double x)
{
return(x*x);

c = sum(10, (int)squares(100.0));
int double

void f(); /* function prototype */


void f() /* function definition */
{
/* this function has no statements */
}

main() f1() f2() f3()


{ { { {
... f2(); ... ...
f1(); ... f3(); ...
... f4(); ... }
} ... }
}
f4()
{
...
...
}

- 32 -
2.8
C (preprocessor)

##define

#define SIZE 20
SIZE
20
x = SIZE;

x = 20;
printf()
#include <stdio.h>
#define SIZE 20
int main()
{
int x = 0;

x += SIZE;
printf("x = %d\n", x);
return(0);
}

SIZE
SIZE
BIG_SIZE = 30;

#include <stdio.h>
#define GREETING "Hello\n"
int main()
{
printf(GREETING);
}
GREETING
printf("Hello\n");

- 33 -
""#define
;
#define GREETING "Hello\n"; /* Wrong!! */

printf("Hello\n";); /* Wrong again. */

#define SUM i+j


#define SUM i+j

i = SUM * 3;

i = i + j * 3;

i = (i+j) * 3;

#define SUM (i+j)

#define F(X,Y) X + Y

i = F(a,b);

i = a + b;

i = F(a,b) * F(c,d);

i = a + b * c + d;

#define F(X,Y) (X + Y)

i = (a + b) * (c + d);

- 34 -

#define F(X,Y) (X * Y)

i = F(a+b, c-d);

i = (a + b * c - d);

#define F(X,Y) ((X) * (Y))

i = F(a+b, c-d);

i = ((a + b) * (c - d));

#define F(A,B,C)((-(B)+sqrt((B)*(B)-4.0*(A)*(C)))/2.0/(A))
F(A,B,C) Ax2+Bx+C=0

y = F(3.0, 5.0, 2.0);

y=((-(5.0)+sqrt((5.0)*(5.0)-4.0*(3.0)*(2.0)))/2.0/(3.0));
sqrt( )

#include

#include <stdio.h>
stdio.h
stdio.h include

#include
<stdio.h>
\
#include \
<stdio.h>

- 35 -

printf("This is a good \
course.\n");

printf("This is a good course.\n");

2.9 C

1.
2. C (Standard C Library) C
C


3. C C



(header file) .h
iostream.h

#include
#include <iostream>
iostream.h
#include <stdio.h>

printf()

C C
printf() C
(Standard output)

printf("control string", value1, value2,...,valueN);

- 36 -
"control string"
value1, value2, ..., valueN
printf("%d", a);
a
#include <stdio.h>

int main()
{
int a = 10;
printf("%d", a);
return(0);
}

10
printf( ) "%d" a

printf("%d %f", a, b);


%d %f int double a b
"control string"
printf("Hello\n");
Hello\n

#include <stdio.h>

int main()
{
int b = 5;
printf("I have %d books.\n", b);
return(0);
}

I have 5 books.

#include <stdio.h>
int main()
{
int a = 10, b=20;
printf("%d + %d = %d", a, b, a+b);
return(0);

- 37 -
}

10 + 20 = 30
printf
%d %i %s
%f %lf %e %E
%g %G %f %c
%e

#include <stdio.h>
int main()
{
double x = 3.141592, y = 0.00003141592;

printf("f: %f %f\n", x, y);


printf("g: %g %g\n", x, y);
printf("G: %G %G\n", x, y);
printf("e: %e %e\n", x, y);
printf("E: %E %E\n", x, y);
return(0);
}

f: 3.141592 0.000031
g: 3.14159 3.14159e-005
G: 3.14159 3.14159E-005
e: 3.141592e+000 3.141592e-005
E: 3.141592E+000 3.141592E-005

#include
printf() stdio.h

"The sum is 15."


#include <stdio.h>
int sum(int x, int y);
int main()
{
int a, b, c;
a = 5;
b = 10;
c = sum(a, b);
printf("The sum is %d.\n", c);
return(0);
}
- 38 -
// Returns the sum of its parameters
int sum(int x, int y)
{
int z;
z = x + y;
return (z);
}

exit(int)
#include <stdlib.h>#include <string.h>

double sqrt(double) double sin(double)double


cos(double)double tan(double) double asin(double)
double acos(double)double atan(double) double
exp(double) double log(double) double
pow(double, double) double fabs(double)
math.h
#include <math.h>

#include <time.h> clock(void) CPU


-1 clock()/CLK_TCK
CLK_TCK 1000 CPU

<>

"
"
#include "my_header.h"
#include "c:\...\..\my_other_header.h"

C
#include

- 39 -
2.10
C

1.


2.


3.

4. 50
5.





6.

7.



8.

2.11
1.

(1)
int main()
{
int i;

- 40 -
i = 5
j = 10;
return(0);
}
(2)
int main()
(
Double x;

x=20.0;
return(0);
)
(3)
int main
{
double one_Airplane,
2_Airplanes;

one_airplane = 1000.0;
2_Airplanes = 2000.0;
return(0);
}
(4)
#include <stdio.h>
int sum(double, double)
{
return(x+y);
}
int main()
{
Double a, b, c;
a = 5.1;
b = 10.2;
c = sum(a, b);
printf("c=%f\n");
return(0);
}
2.
(1)
#include <stdio.h>
int main()
{
int j;

j = 100;
printf("j: %d\n", j);
return(0);
}
(2)
- 41 -
#include <stdio.h>
#include <math.h>
#define F_1(X,Y) (X<<Y)-X*((int)pow(2.0,Y)+1)+16
int main()
{
int i,j;
char ch1;

i=65;
j=F_1(i,1);
printf("j=0x%08X\n",j);
ch1=(int)fabs((double)j);
printf("ch1=%c\n",ch1);
return(0);
}
3. ultraedit,
PE2, vi, emacs, joe, pico C



#include <stdio.h>
int main()
{
printf("Hello! This is my first C program!\n");
}
4.



10203040 1525
3545 100 25.00
120 30.00

(1) main()
compute()
(2) compute()
dump()

(3) dump()

main() dump()
compute()

- 42 -
5. dump() compute()

main() dump()


5 8 6 3
5 8 2 9
2 6 4 1
3 6 2 3
3 6 5 8
4 8 1 2
3 6 3 2
7 4 2 1
3 7 2 9
3 9 2 1
6 6 7 1
3 6 5 1
3 7 4 2
3 7 4 3
9 8 3 7
3 7 5 1
4 4 6 9
6 6 8 2
3 9 1 2
3 7 6 1
3 6 9 2

hw2.c
#include <stdio.h>
#include <stdlib.h>
void compute(int, int, int, int);
void dump(int, int, int, int, int, double);

int main()
{
int pear, /* Price for pears */
wax_apple, /* Price for wax apples */
daikon, /* Price for daikons */
persimmon; /* Price for persimmons */
FILE *f; /* Pointer to the input file */

/* --------------------------------------- */
/* Open the data file and check if success */
/* --------------------------------------- */
if ((f=fopen("data.txt", "r"))==NULL)
{
printf("Cannot open data file\n");
exit(0);
}

/* ----------------------------------------- */
- 43 -
/* Process the grades, one student at a time */
/* ----------------------------------------- */
while (fscanf(f, "%d %d %d %d", &pear,
&wax_apple,&persimmon, &daikon)==4)
{
compute(pear, wax_apple, persimmon, daikon);
}
return(0);
}
/* -----------------------------------------------
*/
/* Output the computing result. */
/* -----------------------------------------------
*/
void dump(int pear, /* Price for pears */
int wax_apple, /* Price for wax apples */
int persimmon, /* Price for persimmons */
int daikon, /* Price for daikons */
int sum, /* Sum of all grades */
double average /* Sum of squares of grades */
)
{
printf("%6d %6d %6d %6d %6d %6.2f\n",
pear, wax_apple, persimmon, daikon, sum,
average);
}
/* -----------------------------------------------
*/
6. ax2 + bx + c = 0
main( ) void roots(double a, double b, double c)
main() roots( ) roots()
roots()
main() cin
double x;
cout << Enter a number: ;
cin >> x; // Read a number from the keyboard and
// save in x
#include <math.h> sqrt(double x)
x
double x, y;
x = 100.0;
y = sqrt(x);
7. 1 n main( )
void summation(int n) summation( )

The sum of 1 .. n is X.
n X n = 5

- 44 -
The sum of 1 .. 5 is 15.

- 45 -
3 C
3.1
C
scope

local variable
local
variable

global variable

main() i

i main() main()
f1() i main()

main()f1()
f2() f3()
i
#include <stdio.h>
void f1(int x);
void f2(int y);
void f3();
int i; /* A global variable */

int main()
{
int x; /* A variable local to main() */

i = 1000; /* This variable is global */


x = 10; /* x is local to main() */
f1(20);
f2(30);
f3();
printf("In main, x = %d, i = %d\n", x, i);
return(0);
}
void f1(int x)

- 46 -
{
x = 2; /* x is local to f1() */
i *= 2; /* i is global */
printf("In f1, x = %d, i = %d\n", x, i);
}
void f2(int y)
{
int x = 2000; /* x is local to f2() */
y = x + y + 100; /* y is also local to f2() */
i += 1000; /* i is global */
printf("In f2, x = %d, i = %d, y = %d\n", x, i, y);
}
void f3()
{
int i = 10; /* i is local to f3() */
printf("In f3, i = %d\n", i);
}

In f1, x = 2, i = 2000
In f2, x = 2000, i = 3000, y = 2130
In f3, i = 10
In main, x = 10, i = 3000
Global variables local variables

3.2
C
==
true false
!= true
false><>=<=
C 0
false 0 true

&&||!
i false(!i) true

- 47 -

true false
a a 0 false a = 2; a = 0;
true
a == b a b a = 3; a = 3;
true false b = 3; b = 5;
a != b a b a = 3; a = 3;
true false b = 5; b = 3;
a < b a b a = 3; a = 12;
true false b = 15; b = 6;
a < b && a < c a b a a = 5; a = 5;
c true b = 7; b = 7;
false c = 9; c = 3;
a < b || b < c a b b a = 5; a = 12;
c b = 7; b = 10;
true false c = 3; c = 10;
a < b || b < c || a b b a = 5; a = 12;
c < d c c b = 7; b = 10;
d true c = 3; c = 10;
d = 6; d = 8;
false
!(a < b) (a < b) true a = 10; a = 5;
false true b = 5; b = 10;

a<b || b<c || c<d


a<b || b<c && c<d
a<b || b<c || c<d a<b true
true
b<c c<d
if
int x, y;
/* . . . */
if (x/y < 3 && y > 0) printf(y is positive and x is less than
3 times of y\n);
y 0 y 0 0

if (y > 0 && x/y < 3) printf(y is positive and x is less than


3 times of y\n);
y 0 y>0 if( )
x/y 0

- 48 -
3.3 if ... else if
C

if...else if if if

true(
0 ) if(...)
false( 0 )
statement_1
if (expression)
statement_1;
statement_2;

if

false

if (expression)
{
statement_1;
statement_2;
statement_3;
/* other statements */
}
statement_4;
if else if

if (expression)
statement_1;
else
statement_2;
statement_3;
experssion statement_1
statement_2 statement_3

main() RealRoot()
RealRoot()
- 49 -

#include <stdio.h>
bool RealRoot(double a, double b, double c);
int main()
{
double p, q, r;
p = 2.0;
q = -5.0;
r = 3.0;
printf("%.2fx*x %+.2fx %+.2f = 0 ", p, q, r);
if(RealRoot(p, q, r))
printf("has real root\n");
else printf("has no real root\n");
return(0);

}
bool RealRoot(double a, double b, double c)
{
double x;
bool t;
x = b * b - 4.0 * a * c;
t = x >= 0.0;
if (t) return(true);
else return (false);
}

printf()%+.2f + printf()
.2f printf() 2

2.00x*x -5.00x +3.00 = 0 has real root


RealRoot()
bool RealRoot(double a, double b, double c)
{
double x;

x = b * b - 4.0 * a * c;
if (x >= 0.0) return(true);
else return (false);
}

bool RealRoot(double a, double b, double c)


{
if (b * b - 4.0 * a * c >= 0.0) return(true);
else return(false);
}

- 50 -

bool RealRoot(double a, double b, double c)
{
return(b * b - 4.0 * a * c >= 0.0);
}

1
2 3
else if experssion_1
statement_1 expression_2 statement_2
statement_3 statement_4 if
C else

if (expression_1)
statement_1;
else if (expression_2)
statement_2;
else
statement_3;
statement_4;
C ?: 3
condition ? X : Y
condition X Y
return (x>=0.0 ? true : false);
2
if (x >= 0.0) return(true);
else return (false);

3.4 switch
switch
switch case default break
switch switch
expression constant_i

switch (expression) {
case constant_0: statements_0;
case constant_1: statements_1;

- 51 -
case constant_2: statements_2;
case constant_3: statements_3;
/* . . . */
case constant_N: statements_N;
default: statements_4;
}
expression
constant_0, constant_1, ..., constant_N
switch break expression
if...else if...else
default
switch
choice 1 case 1
4,5,6,8 9 break switch 16
choice 2 8 9 16
choice 0 11 12 f3( )
14 printf( ) choice 1,2,0 14
switch 16
Example: a choice list processor
[0] int choice;
[1] /* other statements */
[2] switch (choice) {
[3] case 1:
[4] printf("Please wait ... \n");
[5] f1();
[6] f2();
[7] case 2:
[8] printf("This function is not ready yet.\n");
[9] break;
[10] case 0:
[11] printf("Thank you for using this program\n");
[12] f3( );
[13] default:
[14] printf("Invalid choice\n");
[15] }
[16] /* other statements */

case

switch default default


switch
default case
default

- 52 -
3.5
C forwhile
do...while

for

for (expression_1;expression_2;expression_3)
statement;
for

for (expression_1;expression_2;expression_3)
{
statement_1;
statement_2;
statement_3;
/* other statements */
}
expression_1 expression_2
expression_2 statement expression_3
expression_2 expression_2
expression_2
statement statement

for( ; ; )
{ T F

for

i 0
1 10
#include <stdio.h>
int main()
{
int i, j;

- 53 -
for(i=0; i < 10; i += 1)
{
j = i + 10;
printf("j = %d\n", j);
}
printf("Final: i = %d, j = %d\n", i, j);
return(0);

for(i=0; i < 10; i += 1)


printf("j = %d\n", j = i + 10);
i j 0 i 1
10
for(i=0, j=0; i < 10; i += 1, j = i + 10)
printf("j = %d\n", j);
i 10 j=i+10 j=i+10

for(i=j=0; j = i + 10, i < 10; i += 1)


printf("j = %d\n", j);

for expression_2

while statement
expression statement
statement for
while (expression)
statement;
while
while (expression)
{
statement_1;
statement_2;
statement_3;
/* other statements */
}
while 4
i = 10;
while ((i -= 2) != 0) printf("i = %d\n", i);
while
i = 11;
- 54 -
while ((i -= 2) != 0) printf("i = %d\n", i);
while

i = 11;
while ((i -= 2) != 0)
{
printf("i = %d\n", i);
i += 1;
}
do...while statement
expression statement
expression statement statement

do
statement;
while (expression);
do while
do
{
statement_1;
statement_2;
statement_3;
/* other statements */
}
while (expression);
i
1
i = 0;
do
{
printf("i = %d\n", i);
i += 1;
}
while (i < 10);
for
do...while

3.6

i+j

- 55 -
+ ij +
i+j
=+
a = i + j
+=+
=i+j
a=
=

b = k + (a = i + j);

1 C
4 i 5
i i
i
i++ 5 6
++i 6 6
i-- 5 4
--i 4 4

i i++
i ++i i 1
i 1i----ii--
i --i i 1

int i, j;
for(i=0; i < 10; i++)
{
j = i + 10;
printf("j = %d\n", j);
}

int i, j;
for(i=0; i++ < 10;)
{
j = i + 10;
printf("j = %d\n", j);
}

int i, j;
for(i=0; ++i < 10;)
{
j = i + 10;

- 56 -
printf("j = %d\n", j);
}
power2( ) y 2y
#include <stdio.h> //
double power2(int y); // 2 y
int main( )
{
printf("%.0f\n", power2(6));
return(0);
}

/* y 2 y */
/* y < 0 0 */
double power2(int y)
{
double x = 2.0; //

if (y > 0)
{
while (--y != 0) x *= 2; // 2 y
return(x); //
}
else if (y == 0) return(1); // y == 0 1
else return(0); // y < 0 0
}

3.7 Structures
C
structure

structure

structure double
structure structure

struct sGrades
struct
5

- 57 -

struct sGrades {
int id; /* ID of the student */
int math, /* Math grade of the student */
english; /* English grade */
double average; /* Grade average */
int rank; /* rank in class */
};
student
5

struct sGrades student;


student struct
sGrades 5
struct sGrades student
id
math
english
average
rank
struct sGrades

double weighted_sum;

student.average = (student.math + student.english)/2.0;


weighted_sum = student.math * 0.6 + student.english * 0.4;
struct
averager()
averager( ) struct sGrades
5 structure

Structure
/* This is a program to demonstrate the use of structures */
/* Declaration of structure */
struct sGrades {
int id; /* ID of the student */
int math, /* Math grade of the student */
english; /* English grade */

- 58 -
double average; /* Grade average */
int rank; /* rank in class */
};

/* Declaration of function */
double averager(struct sGrades x);
/*-----------------------------------------------------*/
/* The C code */
/*-----------------------------------------------------*/
int main()
{
struct sGrades mary, joe;
/* Variables for 2 students */

/* Set the initial value of grades */


mary.math = 90;
mary.english = 80;
joe.math = 85;
joe.english = 87;

/* Compute the average grade for both students */


mary.average = averager(mary);
joe.average = averager(joe);
return(0);
}
/*-----------------------------------------------------*/
/*
Compute and return the average grades.
*/
double averager(struct sGrades x)
{
return((x.math + x.english)/2.0);
}
structure
structure
structure
structure structure
structure
structure structure
structure

C 4
struct sGrade structure f( )
C
structure

- 59 -
void f();
int main()
{
f();
return(0);
}
void f()
{
struct sGrades {
int id; /* ID of the student */
int math, /* Math grade of the student */
english; /* English grade */
double average; /* Grade average */
int rank; /* rank in class */
} mary, joe;
struct sGrades steve, jack;

mary.id = 1234;
steve.id = 4321;
jack.english = joe.english = 80;
}
structure
mary joe
mary joe

#include <iostream>
using namespace std;
void f();
int main()
{
f();
return(0);
}
void f()
{
struct {
int id; /* ID of the student */
int math, /* Math grade of the student */
english; /* English grade */
double average; /* Grade average */
int rank; /* rank in class */
} mary, joe;

// The followings are not allowed.


// struct grades steve, jack;

mary.id = 1234;
joe.english = 80;
cout << mary.id << endl;
- 60 -
}
typedef
sGrades
struct sGrades {
int id; /* ID of the student */
int math, /* Math grade of the student */
english; /* English grade */
double average; /* Grade average */
int rank; /* rank in class */
};
typedef struct sGrades gT;

int main()
{
gT mary, joe;

mary.id = 1234;
joe.id = 4321;
return(0);
}
structure structrure

structure
2
2 3 struct sGrades
averager()
struct sGrades {
int id; /* ID of the student */
int math, /* Math grade of the student */
english, /* English grade */
physics; /* Physics grade */
double average; /* Grade average */
int rank; /* rank in class */
};
double averager(struct sGrades x)
{
return((x.math + x.english + x.physics)/3.0);
}
structure main() mary joe
averager() 2
2
double averager(int x, int y);

int main()
{
- 61 -
int mary_math, mary_english, joe_math, joe_english;
double mary_average, joe_average;
/* . . . */
mary_average = averager(mary_math, mary_english);
joe_average = averager(joe_math, joe_english);
return(0);
}
2 3 main() average()

double averager(int x, int y, int z);

int main()
{
int mary_math, mary_english, mary_physics,
joe_math, joe_english, joe_physics;
double mary_average, joe_average;
/* . . . */
mary_average = averager(mary_math, mary_english,
mary_physics);
joe_average = averager(joe_math, joe_english,
joe_physics);
return(0);}

structure structure struct sData


grade struct sGrades
struct sData {
double height; /* */
struct sGrades grade;
};
structure
struct sData judy;

judy.height = 175.31; /* judy.height is a double */


judy.grade.math = 92; /* judy.grade is a struct sGrades,
judy.grade.math is an integer */
= structure

struct sGrades {
int id; /* ID of the student */
int math, /* Math grade of the student */
english; /* English grade */
double average; /* Grade average */
int rank; /* rank in class */
};
- 62 -
struct sData {
double height; /* */
struct sGrades grade;
};

void f(void);

int main()
{
f();
return(0);
}

void f()
{
struct sGrades gr1, gr2;
struct sData stu;

gr2.id = 1234;
gr2.math = gr2.english = 91;
stu.grade = gr1 = gr2;
}
structure =

structure
#include <stdio.h>
struct sChickenRabbit {
int heads, // The total number of heads.
legs; // The total number of legs.
int chicken, // The number of chickens.
rabbit; // The number of rabbits.
};

// Solver for the chicken-and-rabbit problem.


struct sChickenRabbit CR_Solver(struct sChickenRabbit cr);
// Output a set of problem-solution.
void Output(struct sChickenRabbit cr);

int main()
{
struct sChickenRabbit chickenRabbit;
chickenRabbit.heads = 10;
chickenRabbit.legs = 28;

// Solve
chickenRabbit = CR_Solver(chickenRabbit);

- 63 -
// Output the solution
Output(chickenRabbit);
return(0);

}
// Solver for the chicken-and-rabbit problem.
struct sChickenRabbit CR_Solver(struct sChickenRabbit cr)
{
// A simple error check
if (cr.legs%2 == 1 || cr.legs<0 || cr.heads<0)
cr.rabbit = cr.chicken = -1;
else
{
cr.rabbit = (cr.legs-2*cr.heads)/2;
cr.chicken = cr.heads-cr.rabbit;
}
return(cr);
}
// Output a set of problem-solution.
void Output(struct sChickenRabbit cr)
{
printf("%d heads, %d legs\n", cr.heads, cr.legs);
if (cr.rabbit<0 || cr.chicken<0)
printf("Solution does not exist\n");
else printf("%d chickens, %d rabbits\n",
cr.chicken, cr.rabbit);

3.8

50

C (array)

int a[10];
a 10
int 10
4 bytes

- 64 -
40 byte

a
a[i] a i a[2]=10 10 a
2 x=a[5] a 5 x
C 0 int a[10]
a[0] a[9] C
[] a[n] a
n
[]

0 9
a sum 10

#include <stdio.h>
int main()
{
int a[10];
int i, sum;
double average;

for (i=0; i<10; i++)


a[i] = i;

for (i=sum=0;i<10;i++)
sum += a[i];
average = sum/10.0;
printf("The average is %f\n", average);
return(0);
}

#include <stdio.h>
int main()
{
int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9;
int sum;
double average;
- 65 -
a0 = 0; a1 = 1; a2 = 2; a3 = 3; a4 = 4; a5 = 5;
a6 = 6; a7 = 7; a8 = 8; a9 = 9;
sum = a0+a1+a2+a3+a4+a5+a6+a7+a8+a9;
average = sum/10.0;
printf("The average is %f\n", average);
return(0);
}

10 a[10]

C (string)

ASCII code
ABC ASCII 656667

20 1
byte

char s[20];
C
C ASCII code
aa
97 0 aabc

#include <stdio.h>
int main( )
{
char x = 'a';
char y;

y = x + 3;
printf("y is [%c]\n", y);
return(0);
}

y is [d]

- 66 -
#include <stdio.h>
int main( )
{
char x = 'a';

while (x <= 'z')


printf("[%c] ", x++);
printf("\n");
return(0);
}

[a] [b] [c] [d] [e] [f] [g] [h] [i] [j] [k]
[l] [m] [n] [o] [p] [q] [r] [s] [t] [u] [v]
[w] [x] [y] [z]
%c printf( )
%d printf( ) ASCII code
#include <stdio.h>
int main( )
{
char x = 'a'-1;

while (++x <= 'z')


printf("[%c = %d] ", x,x);
printf("\n");
}

[a = 97] [b = 98] [c = 99] [d = 100] [e = 101] [f = 102]


[g = 103] [h = 104] [i = 105] [j = 106] [k = 107] [l = 108]
[m = 109] [n = 110] [o = 111] [p = 112] [q = 113] [r = 114]
[s = 115] [t = 116] [u = 117] [v = 118] [w = 119] [x = 120]
[y = 121] [z = 122]
0 \0 0 0
00 ASCII code
48"average" s
ASCII code

a v e r a g e \0

97 118 101 114 97 103 101 0

C
string.h
#include <string.h>
- 67 -
a b n


strcpy() strcpy(a,b) b a
strncpy() strncpy(a,b,n) b a n
0 n
a b
n \0
b n n
b a 0
strcat() strcat(a,b) b a a

strncat() strncat(a,b,n) strcat() n 0

strcmp() strcmp(a,b) a b 0

strncmp() strncmp(a,b,n) strcmp() n
strlen() strlen(a) a 0

strcpy(a,b) a b
b a 0
b 0
0
a a
0 0
n n
0
0
a v e r a g e \0

#include <stdio.h>
#include <string.h>
int main()
{
char a[20], b[30];
int i, len, count;

strcpy(a, "abc");
strcpy(b, "ijk");
- 68 -
printf("a is %s\n", a);
strcat(a, "def");
strcat(a,b);
printf("a is now %s\n", a);
strncat(a, "cba", 2);
len = strlen(a);
for(i=count=0; i < len; i++)
if (a[i] == 'b') count++;
printf("length of a is %d, there are %d 'b' in a.\n",
len, count);
return(0);
}

a is abc
a is now abcdefijk
length of a is 11, there are 2 'b' in a.

#include <stdio.h>
#include <string.h>
int main()
{
char a[20], c;
int i, len;

strcpy(a, "abcdefg");
for(i=0, len=strlen(a); i < len/2; i++)
{
c = a[i];
a[i] = a[len-i-1];
a[len-i-1] = c;
}
printf("a = [%s]\n", a);
return(0);
}
structure
struct sGrades structure
f( ) 100 sg[] struct
sGrades struct sGrades

struct sGrades {
int id; /* ID of the student */
int math, /* Math grade of the student */
english, /* English grade */
physics; /* Physics grade */
double average; /* Grade average */
- 69 -
int rank; /* rank in class */
};
void f()
{
struct sGrades sg[100];

sg[6].id = 1234;
/* . . . . . . */
}
structure
#include <stdio.h>
struct sData {
int x, y[10];
};
int main ()
{
struct sData p;
int i;
p.x = 10;
for(i=0;i<10;i++) p.y[i] = i;
for(i=0;i<10;i++) printf(" %d", p.x*p.y[i]);
return(0);
}

0 10 20 30 40 50 60 70 80 90

#include <iostream>
using namespace std;
int main ()
{
double a2[10][5], a3[5][6][7], a4[10][11][12][13];
int i, j, k, m;
for(i=0;i<10;i++)
for(j=0;j<5;j++)
a2[i][j]=i+j;
for(i=0;i<5;i++)
for(j=0;j<6;j++)
for(k=0;k<7;k++)
a3[i][j][k]=i+j+k;
for(i=0;i<10;i++)
for(j=0;j<11;j++)
for(k=0;k<12;k++)
for(m=0;m<13;m++)
a4[i][j][k][m]=i+j+k+m;
cout << "a2[2][3]=" << a2[2][3] << endl;
cout << "a3[2][3][4]=" << a3[2][3][4] << endl;
cout << "a4[2][3][4][5]=" << a4[2][3][4][5] << endl;

- 70 -
return (0);
}

a2[2][3]=5
a3[2][3][4]=9
a4[2][3][4][5]=14
main() s
ary F() F()
F() main()
F()
2 main()

#include <iostream>
#include <string.h>
using namespace std;
void F(char c[], int a[]);
int main ()
{
int ary[10];
char s[15];
int i, x;
for(i=0;i<10;i++)
ary[i] = i*2;
strcpy(s,"hello");
F(s, ary);
for(x=i=0;i<10;i++)
x += ary[i];
cout << "In main(): x=" << x << endl;
return (0);
}
void F(char c[], int a[])
{
int i, x;
cout << c << endl;
for(x=i=0;i<10;i++)
x += a[i];
cout << "In F(): x=" << x << endl;
for(i=0;i<10;i++)
a[i] *= 2;
}

hello
In F(): x=90
In main(): x=180

- 71 -

#include <iostream>
using namespace std;
void F(int a[][5]);
int main ()
{
int ary[10][5];
int i, x;
F(ary);
cout << "ary[2][3]=" << ary[2][3] << endl;
return (0);
}
void F(int a[][5])
{
int i, j;
for(i=0;i<10;i++)
for(j=0;j<5;j++)
a[i][j] = i+j;
}

ary[2][3]=5

3.9
C file
stdio.h

fopen() fopen("filename", "r");
fclose() fclose(f);
fprintf() fprintf(f, "%d\n", i);
fscanf() fscanf(f, "%f", &x);

fopen() 2

fopen()FILE *
*
NULL FILE typedef NULL
define stdio.h
include fopen()

- 72 -
f=fopen(filename, r) f

fclose()fprintf() fscanf() f


"r"
"w"

"a"
"r+"
"w+"
"a+"

binary file 'b'


"rb""w+b"
#include <stdio.h>
void my_function();
int main()
{
my_function();
return(0);
}
void my_function()
{
FILE *f;
int i = 100;

f = fopen("my_file.txt", "w");
if (f == NULL)
printf("Cannot open file\n");
else
{
fprintf(f, "%d\n", i);
fclose(f);
}
}

#include <stdio.h>
void my_function();
int main()
{
my_function();

- 73 -
return(0);
}

void my_function()
{
FILE *f;
int i = 100;

if ((f = fopen("my_file.txt", "w")) == NULL)


printf("Cannot open file\n");
else
{
fprintf(f, "%d\n", i);
fclose(f);
}
}
3 stdoutstdin stderr
FILE *

my_file.txt 10
%d\n%d
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
int i;

if ((f=fopen("my_file.txt","a"))==NULL)
{
printf("Failed to open file\n");
exit(0);
}
for(i=0;i<10;i++)
fprintf(f, "%d\n", i);
fclose(f);
return(0);
}
fscanf()
EOF (
-1 stdio.h ) fopen()
fscanf()
printf() fprintf()
printf() fprintf()float double

- 74 -
%f %lf fscanf()float %f double
%lf
fscanf() fprintf()
int fscanf (FILE * stream, const char * format, ... );
int fprintf (FILE * stream, const char * format, ... );
. . . fprintf()
printf() float
double printf() float
double double fscanf()

fscanf() 1
i 1 x
int i, x;
i=fscanf(f, "%d", &x);
fscanf()
int i, x=99, y=99, z=99;
i=fscanf(f, "%d %d %d", &x, &y, &z);
printf(%d: %d %d %d\n, i, x, y, z);
i 3210 EOF

1 2 3

3: 1 2 3

1 2

2: 1 2 99

A 1 2

0: 99 99 99

1 2

- 75 -
fscanf()
i=fscanf(f, "%d %d %d", &x, &y, &z);
printf(%d: %d %d %d\n, i, x, y, z);

2: 1 2 99
-1: 1 2 99
fscanf() 2 fscanf()

1 2 3

i=fscanf(f, "%d, %d %d", &x, &y, &z);

1: 1 99 99
fscanf(),

1, 2 3
my_file.txt
my_even.txt my_odd.txt fscanf( )
my_file.txt
fscanf( ) EOF define stdio.h
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f_in, *f_even, *f_odd;
int i;

/*---------------------------------------*/
/* Open the 3 files and check if success */
/*---------------------------------------*/
if ((f_in=fopen("my_file.txt","r"))==NULL)
{
printf("Failed to open my_file\n");
exit(0);
}
if ((f_even=fopen("my_even.txt","w"))==NULL)
{
printf("Failed to open my_even\n");
exit(0);
}
- 76 -
if ((f_odd=fopen("my_odd.txt","w"))==NULL)
{
printf("Failed to open my_odd\n");
exit(0);
}
while(fscanf(f_in, "%d", &i) == 1)
{
if (i % 2 == 1) /* check if i is odd */
fprintf(f_odd, "%d\n", i);
else
fprintf(f_even, "%d\n", i);
}

/* close the 3 files */


fclose(f_in);
fclose(f_even);
fclose(f_odd);
return(0);
}
my_file.txt

#include <stdio.h>
int main()
{
FILE *f;
int i;

if ((f=fopen("my_file.txt", "w"))==NULL)
{
fprintf(stderr, "Failed to open file\n");
f = stdout;
}
for (i=0;i<10;i++) fprintf(f,"%d\n", i);
if (f != stdout) fclose(f);
return(0);
}
fclose()

3.10
C
recursive function call

- 77 -

#include <stdio.h>
void f(int i);
int main()
{
f(3);
return(0);
}
void f(int i)
{
if (i == 0) return;
printf("1: i = %d\n", i);
f(i-1);
printf("2: i = %d\n", i);
}

1: i = 3
1: i = 2
1: i = 1
2: i = 1
2: i = 2
2: i = 3
main( ) f( ) 3 f( )
3 210

main() f() i = 3
f(3); 1 if (i == 0) return;
return(0); printf(1: i = %d\n, i);
f(i-1);
2
printf(2: i = %d\n, i);

7 8
f() i = 2 3 f() i = 1
if (i == 0) return; if (i == 0) return;
printf(1: i = %d\n, i); printf(1: i = %d\n, i);
f(i-1); f(i-1);
printf(2: i = %d\n, i); printf(2: i = %d\n, i);
6
4
f() i = 0 5
if (i == 0) return;
printf(1: i = %d\n, i);
f(i-1);
printf(2: i = %d\n, i);

- 78 -
return
if (i == 0)

n n
f() local variables

#include <stdio.h>
int factor(int x);
int main( )
{
int n = 10;

printf("%d! = %d\n", n, factor(n));


return(0);
}
/* This function computes the factorization of x */
int factor(int x)
{
if (x == 1) return(1);
return (x * factor(x-1));
}

10! = 3628800

3.11
1.
(1)
#include <stdio.h>
void f()
{
i = 100;
while (i--) printf(i=%d\n,i);
}

(2)
int f(int a);
int f(int a)
{
if (a=1) {return(1);}
else return(0);
}
int main()
- 79 -
{
a=f(2);
return(0);
}

(3)
#include <stdio.h>
void f(int a);
int main()
{
int i;
f(i);
return(0);
}
void f(int a)
{
if(a)printf("Hello\n");
return(0);
}

(4)
#include <stdio.h>
struct sPoint {
double x;
double y;
};
double f(struct sPoint p);
int main()
{
struct sPoint a;
a.x=a.y=2.0;
f(a);
return(0);
}
double f(struct sPoint p)
{
double x;
x = p.x * p.y;
}

(5)
#include <stdio.h>
int main()
{
FILE *p;
int i;
do
p=fopen("abc.txt","r");
fscanf(p,"%d",&i);
while(i);
fclose(p);
- 80 -
return(0);
}

(6)
int main()
{
if(f(3,5))printf("f=%f\n",f(3,5));
return(0);
}
int f(int x, int y)
{
int i;
if(x>y)
return(i=x-y);
else
return(i=y-x);
}

(7)
#include <stdio.h>
int main()
{
printf("input a integer\n");
scanf("%d",i);
switch(i%3)
{
case 0:
printf("%d",i%3);
exit(0);
default:
printf(" Defaulf Case \n");
}
return(0);
}

(8)
#include <stdio.h>
void short(int,int,int)
int main()
{
int dis_1=3,dis_2=3,dis_3=5;
int length;
length=short(dis_1,dis_2,dis_3);
return(0);
}
void short(int x,int y,int z)
{
if((y-x)>(z-x))printf("%d\n",z-x);
else if((y-x)!=(z-x))printf("%d\n",y-x);
else printf("equal distance\n");

- 81 -
2.
(1)
#include <stdio.h>
void f(int i);
int main()
{
f(5);
return(0);
}
void f(int i)
{
switch (i) {
case 3: printf("3 chosen\n");
case 5: printf("5 chosen\n");
case 1: printf("1 chosen\n");
}
printf("Done\n");
}

(2)
#include <stdio.h>
int main()
{
int i = 5;

while (i--)
printf("i: %d\n", i);
}

(3)
#include <stdio.h>
int main()
{
int i;

for (i=0; i<5; i++)


printf("i: %d\n", i++);
}

(4)
#include <stdio.h>
int main()
{
int i=3,j=7;
while(i<6)
if(++i>4 && ++j<10)
printf("i=%d,j=%d\n",i,j);
return(0);
}

(5)

- 82 -
#include <stdio.h>
int What(int x);
int main()
{
int i=6;
printf("What(%d)=%d\n",i,What(i));
return(0);
}
int What(int x)
{
if (x<3)return(1);
return(What(x-1)+What(x-2));
}

(6)
#include <stdio.h>
int main()
{
int i=3;
for(printf("A %d\n",i+1);
i--;
printf("G %d\n",i-1));
return(0);
}

(7)
#include <stdio.h>
int f(int x);
int main()
{
int x=2;
x=f(x);
printf("%d\n",x);
return(0);
}
int f(int x)
{
x=10;
return(x+2);
}

(8)
#include <stdio.h>
int f(int s, int t);
int main()
{
int i=2,j=2;
while(!(i>10)&&f(i,j)){i+=1;j+=3;}
printf("i=%d\n",i);
printf("j=%d\n",j);
return(0);
}

int f(int s, int t)


{
- 83 -
if ((s+t)%-10) return(1);
return(0);
}

(9)
#include <stdio.h>
int main()
{
int i,j;
double x,y=6.5;
do{
y-=0.5;
for(i=0;i<2;)
{ x=i+y; i+=1;}
printf("%3.1f ",x);
}while(j=y-4.5);
printf("\n");
printf("%3.1f\n",y);
return(0);
}

(10)
#include <stdio.h>
int f(int a);
int main()
{
int a=0,i,j;
for(i=0,j=5; i<10 && (j-=1); i+=1)
a=a+i-j;
printf("%d %d %d\n",i,j,a);
switch(f(a))
{
case 1: printf("%d\n", a^2);
break;
case 2: printf("%d\n", a);
default: printf("0\n");
}
return(0);
}
int f(int a)
{
if (a>0) return(1);
if (a<0) return(2);
return(3);
}

3. c a b c a b
a b a b
gcd()
gcd()
a b 0 0 main()
gcd()main() gcd ()gcd
()
- 84 -
0main()
global variable

printf("Please enter two integers:\n");
scanf("%d %d", &a, &b);

cout << "Please enter two integers:\n";
cin >> a >> b;
4. 3x+1 x1, x2, xi+1 = (3xi+1)/(2y)
y 2y 3xi+1 xi=3 y=1
x1 1
x1=3,x2=5,x3=1x1=5, x2=1 x1<100

5. x
(1) x 7 1
(2) x 11 6
(3) x 13 5
(4) 0 <= x < 1000
6. dataA.txt dataB.txt 5
dataA.txt
dataB.txt dataA.txt
dataB.txt
dataC.txt

1 2

25 20 5
31 27 1

dataA.txt dataB.txt

dataC.txt

7. Fibonacci numbers a1, a2, , an
a1= a2=1ai= ai-1+ ai-2 for i>2
10 1,1,2,3,5,8,13,21,34,55
recursive call n

int fib(int n)
- 85 -
{
return(fib(n-1)+fib(n-2));
}
8. n recursive call

9. void roots(double a, double b, double c)

(1) ax2 + bx + c = 0 No real root exists.

(2) ax2 + bx + c = 0 1 The only real root is z.


z The only real root is 35.731.

(3) ax2 + bx + c = 0 2 The real roots are y and z.


y z The real roots are -49.512 and 51.218.

- 86 -
A Visual Studio
A.1

A.2 Visual Studio


Visual Studio C Visual Studio

Visual C++
Win32

- 87 -
1

- 88 -
1

C++

- 89 -

3 4

Homework.cpp

#include <iostream>
using namespace std;

int main()
{
int month, // Month of my birthday
day; // Day of my birthday
// Set the dates
month = 1;
- 90 -
day = 2;
// Output the message
cout << "My birthday is " << month << '/' << day << endl;
return(0);
}

(d:\temp\C_homework\MyFirstC)
Debug MyFirstC.exe

- 91 -


d: D
cd ..
cd abc abc
dir
dir *.exe .exe

abc abc.exe

MyFirstC Enter

- 92 -
- 93 -

You might also like