SlideShare una empresa de Scribd logo
1 de 406
Chapter 1 – Introduction to Computers and C++ Programming Outline 1.1 Introduction 1.2 What Is a Computer? 1.3 Computer Organization 1.4 Evolution of Operating Systems 1.5 Personal Computing, Distributed Computing and Client/Server  Computing 1.6 Machine Languages, Assembly Languages and High-level  Languages 1.7 The History of C 1.8 The C Standard Library 1.9 The Key Software Trend: Object Technology 1.10 C++ and C++ How to Program 1.11 Java and Java How to Program 1.12 Other High-level Languages 1.13 Structured Programming 1.14 The Basics of a typical C Program Development Environment
Chapter 1 – Introduction to Computers and C++ Programming Outline 1.15 Hardware Trends 1.16 History of the Internet 1.17 History of the World Wide Web 1.18 General Notes About C and this Book
1.1 Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.2  What is a Computer? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.3 Computer Organization ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.4 Evolution of Operating Systems ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.5 Personal Computing, Distributed Computing, and Client/Server Computing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.6 Machine Languages, Assembly Languages, and High-level Languages ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.6 Machine Languages, Assembly Languages, and High-level Languages ,[object Object],[object Object],[object Object],[object Object],[object Object]
1.7 History of C ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.8 The C Standard Library ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.9 The Key Software Trend: Object Technology ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.10 C++ and C++ How to Program ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.11 Java and Java How to Program ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.12 Other High-level Languages ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.13 Structured Programming ,[object Object],[object Object],[object Object],[object Object],[object Object]
1.14 Basics of a Typical C Program Development Environment ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Program is created in the editor and stored on disk.   Preprocessor program processes the code. Loader puts program in memory. CPUtakes each instruction and executes it, possibly storing new data values as the program executes. Compiler creates object code and stores it on disk. Linker links the object code with the libraries Loader Primary Memory Compiler Editor Preprocessor Linker   Primary Memory . . . . . . . . . . . . Disk Disk Disk CPU Disk Disk
1.15 Hardware Trends ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.16 History of the Internet ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.17 History of the World Wide Web ,[object Object],[object Object],[object Object],[object Object],[object Object]
1.18 General Notes About C and This Book ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Chapter 2 - Introduction to C Programming Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision Making: Equality and Relational Operators
2.1 Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object]
2.2 A Simple C Program: Printing a Line of Text ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Welcome to C!  1 /* Fig. 2.1: fig02_01.c 2   A first program in C */ 3 #include  <stdio.h> 4 5 int  main() 6 { 7   printf( &quot;Welcome to C!&quot; ); 8 9   return  0; 10 }
2.2 A Simple C Program: Printing a Line of Text ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
2.2 A Simple C Program: Printing a Line of Text ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
2.2 A Simple C Program: Printing a Line of Text ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1. Initialize variables 2. Input 2.1 Sum 3. Print Program Output Enter first integer 45 Enter second integer 72 Sum is 117   1 /* Fig. 2.5: fig02_05.c 2   Addition program */ 3 #include  <stdio.h> 4 5 int  main() 6 { 7   int  integer1, integer2, sum;  /* declaration */ 8 9   printf( &quot;Enter first integer&quot; );  /* prompt */ 10   scanf( &quot;%d&quot;, &integer1 );  /* read an integer */ 11   printf( &quot;Enter second integer&quot; );  /* prompt */ 12   scanf( &quot;%d&quot;, &integer2 );  /* read an integer */ 13   sum = integer1 + integer2;  /* assignment of sum */ 14   printf( &quot;Sum is %d&quot;, sum );  /* print sum */ 15 16   return  0;  /* indicate that program ended successfully */ 17 }
2.3 Another Simple C Program: Adding Two Integers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
2.3 Another Simple C Program: Adding Two Integers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
2.3 Another Simple C Program: Adding Two Integers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
2.4 Memory Concepts ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],integer1 45
2.5  Arithmetic ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
2.5  Arithmetic ,[object Object],[object Object]
2.6 Decision Making: Equality and Relational Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
2.6 Decision Making: Equality and Relational Operators
2.6 Decision Making: Equality and Relational Operators
1. Declare variables 2. Input 2.1  if  statements 3. Print 1 /* Fig. 2.13: fig02_13.c 2   Using if statements, relational   3   operators, and equality operators */ 4 #include  <stdio.h> 5 6 int  main() 7 { 8   int  num1, num2; 9   10   printf( &quot;Enter two integers, and I will tell you&quot; ); 11   printf( &quot;the relationships they satisfy: &quot; ); 12   scanf( &quot;%d%d&quot;, &num1, &num2  );  /* read two integers */ 13   14   if  ( num1 == num2 )  15   printf( &quot;%d is equal to %d&quot;, num1, num2 ); 16 17   if  ( num1 != num2 )  18   printf( &quot;%d is not equal to %d&quot;, num1, num2 ); 19 20   if  ( num1 < num2 )  21   printf( &quot;%d is less than %d&quot;, num1, num2 ); 22 23   if  ( num1 > num2 )  24   printf( &quot;%d is greater than %d&quot;, num1, num2 ); 25 26   if  ( num1 <= num2 )  27   printf( &quot;%d is less than or equal to %d&quot;,  28   num1, num2 );
3.1 Exit  main Program Output Enter two integers, and I will tell you  the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7   Enter two integers, and I will tell you  the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12   29 30   if  ( num1 >= num2 )  31   printf( &quot;%d is greater than or equal to %d&quot;,  32   num1, num2 ); 33 34   return  0;  /* indicate program ended successfully */ 35 }
Chapter 3 - Structured Program Development Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4 Control Structures 3.5 The If Selection Structure 3.6 The If/Else Selection Structure 3.7 The While Repetition Structure 3.8 Formulating Algorithms: Case Study 1 (Counter-Controlled  Repetition) 3.9 Formulating Algorithms with Top-down, Stepwise Refinement:  Case Study 2 (Sentinel-Controlled Repetition) 3.10 Formulating Algorithms with Top-down, Stepwise Refinement:  Case Study 3 (Nested Control Structures) 3.11 Assignment Operators 3.12 Increment and Decrement Operators
3 .1 Introduction ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .2 Algorithms ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .3 Pseudocode ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .4 Control Structures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .4 Control Structures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .5 The  if  Selection Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .5 The  if  Selection Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .5 The  if  Selection Structure ,[object Object],  A decision can be made on any expression.  zero -  false   nonzero -  true Example: 3 - 4  is   true true false grade >= 60 print “Passed”
3 .6 The  if / else  Selection Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object], 
3 .6 The  if / else  Selection Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .6 The  if / else  Selection Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],true false print “Failed” print “Passed” grade >= 60
3 .6 The  if / else  Selection Structure ,[object Object],[object Object]
3 .6 The  if / else  Selection Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .6 The  if / else  Selection Structure ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .7 The  while  Repetition Structure ,[object Object],[object Object],[object Object],[object Object],[object Object], 
3 .7 The  while  Repetition Structure ,[object Object],[object Object],[object Object],product <= 1000 product = 2 * product true false
3 .8 Formulating Algorithms (Counter-Controlled Repetition) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Initialize Variables 2.  Execute Loop 3.  Output results 1 /* Fig. 3.6: fig03_06.c 2   Class average program with  3   counter-controlled repetition */ 4 #include  <stdio.h> 5 6 int  main() 7 { 8   int  counter, grade, total, average; 9   10   /* initialization phase */ 11   total = 0; 12   counter = 1; 13   14   /* processing phase */ 15   while  ( counter <= 10 ) { 16   printf( &quot;Enter grade: &quot; ); 17   scanf( &quot;%d&quot;, &grade ); 18   total = total + grade; 19   counter = counter + 1;  20   } 21   22   /* termination phase */ 23   average = total / 10; 24   printf( &quot;Class average is %d&quot;, average ); 25 26   return  0;  /* indicate program ended successfully */ 27 }
Program Output Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81
3 .9 Formulating Algorithms with Top-Down, Stepwise Refinement ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .9 Formulating Algorithms with Top-Down, Stepwise Refinement ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .9 Formulating Algorithms with Top-Down, Stepwise Refinement ,[object Object],[object Object],[object Object],[object Object]
3 .9 Formulating Algorithms with Top-Down, Stepwise Refinement ,[object Object],[object Object]
1.  Initialize Variables 2.  Get user input 2.1  Perform Loop 1 /* Fig. 3.8: fig03_08.c 2   Class average program with   3   sentinel-controlled repetition */ 4 #include  <stdio.h> 5 6 int  main() 7 { 8   float  average;  /* new data type */ 9   int  counter, grade, total; 10   11   /* initialization phase */ 12   total = 0; 13   counter = 0; 14   15   /* processing phase */ 16   printf( &quot;Enter grade, -1 to end: &quot; ); 17   scanf( &quot;%d&quot;, &grade ); 18   19   while  ( grade != -1 ) { 20   total = total + grade; 21   counter = counter + 1;  22   printf( &quot;Enter grade, -1 to end: &quot; ); 23   scanf( &quot;%d&quot;, &grade ); 24   }
3.  Calculate Average 3.1  Print Results Program Output Enter grade, -1 to end: 75 Enter grade, -1 to end: 94 Enter grade, -1 to end: 97 Enter grade, -1 to end: 88 Enter grade, -1 to end: 70 Enter grade, -1 to end: 64 Enter grade, -1 to end: 83 Enter grade, -1 to end: 89 Enter grade, -1 to end: -1 Class average is 82.50   25 26   /* termination phase */ 27   if  ( counter != 0 ) { 28   average = (  float  ) total / counter; 29   printf( &quot;Class average is %.2f&quot;, average ); 30   } 31   else 32   printf( &quot;No grades were entered&quot; ); 33 34   return  0;  /* indicate program ended successfully */ 35 }
3.10  Nested control structures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3.10  Nested control structures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3.10  Nested control structures ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Initialize variables 2.  Input data and count passes/failures 3.  Print results   1 /* Fig. 3.10: fig03_10.c 2   Analysis of examination results */ 3 #include  <stdio.h> 4 5 int  main() 6 { 7   /* initializing variables in declarations */ 8   int  passes = 0, failures = 0, student = 1, result; 9 10   /* process 10 students; counter-controlled loop */ 11   while  ( student <= 10 ) { 12   printf( &quot;Enter result ( 1=pass,2=fail ): &quot; ); 13   scanf( &quot;%d&quot;, &result ); 14 15   if  ( result == 1 )  /* if/else nested in while */ 16   passes = passes + 1; 17   else 18   failures = failures + 1; 19 20   student = student + 1; 21   } 22 23   printf( &quot;Passed %d&quot;, passes ); 24   printf( &quot;Failed %d&quot;, failures ); 25 26   if  ( passes > 8 ) 27   printf( &quot;Raise tuition&quot; ); 28 29   return  0;  /* successful termination */ 30 }
Program Output Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Passed 6 Failed 4
3 .11 Assignment Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .12 Increment and Decrement Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
3 .12 Increment and Decrement Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Chapter 5 - Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions 5.5 Function Definitions 5.6 Function Prototypes 5.7 Header Files 5.8 Calling Functions: Call by Value and Call by Reference 5.9 Random Number Generation 5.10 Example: A Game of Chance 5.11 Storage Classes 5.12 Scope Rules 5.13 Recursion 5.14 Example Using Recursion: The Fibonacci Series 5.15 Recursion vs. Iteration
5.1 Introduction ,[object Object],[object Object],[object Object],[object Object]
5.2 Program Modules in C ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.3 Math Library Functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.4 Functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.5 Function Definitions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.5 Function Definitions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],Enter three integers: 22 85 17 Maximum is: 85   1 /* Fig. 5.4: fig05_04.c 2   Finding the maximum of three integers */ 3 #include  <stdio.h> 4 5 int  maximum(  int ,  int ,  int  );  /* function prototype */ 6 7 int  main() 8 { 9   int  a, b, c; 10 11   printf( &quot;Enter three integers: &quot; ); 12   scanf( &quot;%d%d%d&quot;, &a, &b, &c ); 13   printf( &quot;Maximum is: %d&quot;, maximum( a, b, c ) ); 14 15   return  0; 16 } 17 18 /* Function maximum definition */ 19 int  maximum(  int  x,  int  y,  int  z ) 20 { 21   int  max = x; 22 23   if  ( y > max ) 24   max = y; 25 26   if  ( z > max ) 27   max = z; 28 29   return  max; 30 }
5.6 Function Prototypes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.7 Header Files ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.8 Calling Functions: Call by Value and Call by Reference ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.9 Random Number Generation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.9 Random Number Generation ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Initialize seed 2.  Input value for seed 2.1  Use  srand  to change random sequence 2.2  Define Loop  3.  Generate and output random numbers   1 /* Fig. 5.9: fig05_09.c 2   Randomizing die-rolling program */ 3 #include  <stdlib.h> 4 #include  <stdio.h> 5 6 int  main() 7 { 8   int  i; 9   unsigned  seed; 10   11   printf( &quot;Enter seed: &quot; ); 12   scanf( &quot;%u&quot;, &seed ); 13   srand( seed ); 14   15   for  ( i = 1; i <= 10; i++ ) { 16   printf( &quot;%10d&quot;, 1 + ( rand() % 6 ) ); 17   18   if  ( i % 5 == 0 ) 19   printf( &quot;&quot; ); 20   } 21 22   return  0; 23 }
Program Output Enter seed: 867 2  4  6  1  6 1  1  3  6  2    Enter seed: 67 6  1  4  6  2 1  6  1  6  4   Enter seed: 67 6  1  4  6  2 1  6  1  6  4
5.10 Example: A Game of Chance ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  rollDice  prototype 1.1  Initialize variables 1.2  Seed  srand   2.  Define  switch  statement for win/loss/continue 2.1 Loop 1 /* Fig. 5.10: fig05_10.c 2   Craps */ 3 #include  <stdio.h> 4 #include  <stdlib.h> 5 #include  <time.h> 6 7 int  rollDice(  void  ); 8 9 int  main() 10 { 11   int  gameStatus, sum, myPoint; 12 13   srand( time( NULL ) ); 14   sum = rollDice();  /* first roll of the dice */ 15 16   switch  ( sum ) { 17   case  7:  case  11:  /* win on first roll */ 18   gameStatus = 1; 19   break ; 20   case  2:  case  3:  case  12:  /* lose on first roll */ 21   gameStatus = 2; 22   break ; 23   default :  /* remember point */ 24   gameStatus = 0; 25   myPoint = sum; 26   printf( &quot;Point is %d&quot;, myPoint ); 27   break ; 28   } 29 30   while  ( gameStatus == 0 ) {  /* keep rolling */ 31   sum = rollDice(); 32
2.2  Print win/loss Program Output Player rolled 6 + 5 = 11 Player wins 33   if  ( sum == myPoint )  /* win by making point */ 34   gameStatus = 1; 35   else 36   if  ( sum == 7 )  /* lose by rolling 7 */ 37   gameStatus = 2; 38   } 39 40   if  ( gameStatus == 1 ) 41   printf( &quot;Player wins&quot; ); 42   else 43   printf( &quot;Player loses&quot; ); 44 45   return  0; 46 } 47 48 int  rollDice(  void  ) 49 { 50   int  die1, die2, workSum; 51 52   die1 = 1 + ( rand() % 6 ); 53   die2 = 1 + ( rand() % 6 ); 54   workSum = die1 + die2; 55   printf( &quot;Player rolled %d + %d = %d&quot;, die1, die2, workSum ); 56   return  workSum; 57 }
Program Output Player rolled 6 + 6 = 12 Player loses   Player rolled 4 + 6 = 10 Point is 10 Player rolled 2 + 4 = 6 Player rolled 6 + 5 = 11 Player rolled 3 + 3 = 6 Player rolled 6 + 4 = 10 Player wins   Player rolled 1 + 3 = 4 Point is 4 Player rolled 1 + 4 = 5 Player rolled 5 + 4 = 9 Player rolled 4 + 6 = 10 Player rolled 6 + 3 = 9 Player rolled 1 + 2 = 3 Player rolled 5 + 2 = 7 Player loses
5.11 Storage Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.11 Storage Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.12 Scope Rules ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.12 Scope Rules ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Function prototypes 1.1  Initialize global variable 1.2  Initialize local variable 1.3  Initialize local variable in block 2.  Call functions 3.  Output results 1 /* Fig. 5.12: fig05_12.c 2   A scoping example */ 3 #include  <stdio.h> 4 5 void  a(  void  );  /* function prototype */ 6 void  b(  void  );  /* function prototype */ 7 void  c(  void  );  /* function prototype */ 8 9 int  x = 1;  /* global variable */ 10 11 int  main() 12 { 13   int  x = 5;  /* local variable to main */ 14 15   printf(&quot;local x in outer scope of main is %d&quot;, x ); 16 17   {  /* start new scope */ 18   int  x = 7; 19 20   printf( &quot;local x in inner scope of main is %d&quot;, x ); 21   }  /* end new scope */ 22 23   printf( &quot;local x in outer scope of main is %d&quot;, x ); 24 25   a();  /* a has automatic local x */ 26   b();  /* b has static local x */ 27   c();  /* c uses global x */ 28   a();  /* a reinitializes automatic local x */ 29   b();  /* static local x retains its previous value */ 30   c();  /* global x also retains its value */
3.1 Function definitions 31 32   printf( &quot;local x in main is %d&quot;, x ); 33   return  0; 34 } 35 36 void  a(  void  ) 37 { 38   int  x = 25;  /* initialized each time a is called */ 39 40   printf( &quot;local x in a is %d after entering a&quot;, x ); 41   ++x; 42   printf( &quot;local x in a is %d before exiting a&quot;, x ); 43 } 44 45 void  b(  void  ) 46 { 47   static   int  x = 50;  /* static initialization only */ 48   /* first time b is called */ 49   printf( &quot;local static x is %d on entering b&quot;, x ); 50   ++x; 51   printf( &quot;local static x is %d on exiting b&quot;, x ); 52 } 53 54 void  c(  void  ) 55 { 56   printf( &quot;global x is %d on entering c&quot;, x ); 57   x *= 10; 58   printf( &quot;global x is %d on exiting c&quot;, x ); 59 }
Program Output local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c global x is 10 on exiting c local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is 5
5.13 Recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.13 Recursion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
5.14 Example Using Recursion: The Fibonacci Series ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object], 
5.14 Example Using Recursion: The Fibonacci Series ,[object Object],  f( 3 ) f( 1 ) f( 2 ) f( 1 ) f( 0 ) return 1 return 1 return 0 return + + return
1.  Function prototype 1.1  Initialize variables 2.  Input an integer 2.1  Call function  fibonacci 2.2  Output results. 3.  Define  fibonacci  recursively   Program Output Enter an integer: 0 Fibonacci(0) = 0 Enter an integer: 1 Fibonacci(1) = 1 1 /* Fig. 5.15: fig05_15.c 2   Recursive fibonacci function */ 3 #include  <stdio.h> 4 5 long  fibonacci(  long  ); 6 7 int  main() 8 { 9   long  result, number; 10 11   printf( &quot;Enter an integer: &quot; ); 12   scanf( &quot;%ld&quot;, &number ); 13   result = fibonacci( number ); 14   printf( &quot;Fibonacci( %ld ) = %ld&quot;, number, result ); 15   return  0; 16 } 17 18 /* Recursive definition of function fibonacci */ 19 long  fibonacci(  long  n ) 20 { 21   if  ( n == 0 || n == 1 ) 22   return  n; 23   else 24   return  fibonacci( n - 1 ) + fibonacci( n - 2 ); 25 }
Program Output Enter an integer: 2 Fibonacci(2) = 1 Enter an integer: 3 Fibonacci(3) = 2 Enter an integer: 4 Fibonacci(4) = 3 Enter an integer: 5 Fibonacci(5) = 5 Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 10 Fibonacci(10) = 55 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = 832040 Enter an integer: 35 Fibonacci(35) = 9227465
5.15 Recursion vs. Iteration ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays 6.3 Declaring Arrays 6.4 Examples Using Arrays 6.5 Passing Arrays to Functions 6.6 Sorting Arrays 6.7 Case Study: Computing Mean, Median and Mode Using Arrays 6.8 Searching Arrays 6.9 Multiple-Subscripted Arrays
6.1 Introduction ,[object Object],[object Object],[object Object],[object Object]
6.2 Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Name of array (Note that all elements of this array have the same name,  c ) Position number of the element within array  c c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4]
6.2 Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object]
6.3 Declaring Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
6.4 Examples Using Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1. Initialize array 2.  Loop 3. Print 1 /* Fig. 6.8: fig06_08.c 2   Histogram printing program */ 3 #include  <stdio.h> 4 #define  SIZE 10 5 6 int  main() 7 { 8   int  n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; 9   int  i, j; 10 11   printf( &quot;%s%13s%17s&quot;, &quot;Element&quot;, &quot;Value&quot;, &quot;Histogram&quot; ); 12 13   for  ( i = 0; i <= SIZE - 1; i++ ) { 14   printf( &quot;%7d%13d  &quot;, i, n[ i ]) ; 15 16   for  ( j = 1; j <= n[ i ]; j++ )  /* print one bar */ 17   printf( &quot;%c&quot;, '*' ); 18 19   printf( &quot;&quot; ); 20   } 21 22   return  0; 23 }
Program Output Element  Value  Histogram 0  19  ******************* 1  3  *** 2  15  *************** 3  7  ******* 4  11  *********** 5  9  ********* 6  13  ************* 7  5  ***** 8  17  ***************** 9  1  *
6.4 Examples Using Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Initialize strings 2.  Print strings 2.1  Define loop 2.2  Print characters  individually 2.3  Input string 3.  Print string  Program Output Enter a string: Hello there string1 is: Hello string2 is: string literal string1 with spaces between characters is: H e l l o  1 /* Fig. 6.10: fig06_10.c 2   Treating character arrays as strings */ 3 #include  <stdio.h> 4 5 int  main() 6 { 7   char  string1[ 20 ], string2[] = &quot;string literal&quot;; 8   int  i; 9 10   printf(&quot; Enter a string: &quot;); 11   scanf( &quot;%s&quot;, string1 ); 12   printf( &quot;string1 is: %sstring2: is %s&quot; 13   &quot;string1 with spaces between characters is:&quot;, 14   string1, string2 ); 15 16   for  ( i = 0; string1[ i ] != ''; i++ ) 17   printf( &quot;%c &quot;, string1[ i ] ); 18 19   printf( &quot;&quot; ); 20   return  0; 21 }
6.5 Passing Arrays to Functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
6.5 Passing Arrays to Functions ,[object Object],[object Object],[object Object],[object Object],[object Object]
1. Function definitions 2. Pass array to a function 2.1 Pass array element to a function 3. Print 1 /* Fig. 6.13: fig06_13.c 2   Passing arrays and individual array elements to functions */ 3 #include  <stdio.h> 4 #define  SIZE 5 5 6 void  modifyArray(  int  [],  int  );  /* appears strange */ 7 void  modifyElement(  int  ); 8 9 int  main() 10 { 11   int  a[ SIZE ] = { 0, 1, 2, 3, 4 }, i;  12 13   printf( &quot;Effects of passing entire array call &quot; 14   &quot;by reference:The values of the &quot; 15   &quot;original array are:&quot; ); 16 17   for  ( i = 0; i <= SIZE - 1; i++ )  18   printf( &quot;%3d&quot;, a[ i ] ); 19 20   printf( &quot;&quot; ); 21   modifyArray( a, SIZE );  /* passed call by reference */ 22   printf( &quot;The values of the modified array are:&quot; ); 23 24   for  ( i = 0; i <= SIZE - 1; i++ ) 25   printf( &quot;%3d&quot;, a[ i ] ); 26 27   printf( &quot;Effects of passing array element call &quot; 28   &quot;by value:The value of a[3] is %d&quot;, a[ 3 ] ); 29   modifyElement( a[ 3 ] ); 30   printf( &quot;The value of a[ 3 ] is %d&quot;, a[ 3 ] ); 31   return  0; 32 } Entire arrays passed call-by-reference, and can be modified Array elements passed call-by-value, and cannot be modified
3.1 Function definitions Program Output Effects of passing entire array call by reference: The values of the original array are: 0  1  2  3  4 The values of the modified array are: 0  2  4  6  8 Effects of passing array element call by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6 33 34 void  modifyArray(  int  b[],  int  size ) 35 { 36   int  j; 37 38   for  ( j = 0; j <= size - 1; j++ ) 39   b[ j ] *= 2; 40 } 41 42 void  modifyElement(  int  e ) 43 { 44   printf( &quot;Value in modifyElement is %d&quot;, e *= 2 ); 45 }
6.6 Sorting Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
6.7 Case Study: Computing Mean, Median and Mode Using Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Function prototypes 1.1  Initialize array 2.  Call functions  mean ,  median , and  mode 1 /* Fig. 6.16: fig06_16.c 2   This program introduces the topic of survey data analysis.   3   It computes the mean, median, and  mode of the data */ 4 #include  <stdio.h> 5 #define  SIZE 99 6 7 void  mean(  const   int  [] ); 8 void  median(  int  [] ); 9 void  mode(  int  [],  const   int  [] ) ; 10 void  bubbleSort(  int  [] ); 11 void  printArray(  const   int  [] ); 12 13 int  main() 14 { 15   int  frequency[ 10 ] = { 0 }; 16   int  response[ SIZE ] =  17   { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 18   7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 19   6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 20   7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 21   6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 22   7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 23   5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 24   7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 25   7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 26   4, 5, 6, 1, 6, 5, 7, 8, 7 }; 27 28   mean( response ); 29   median( response ); 30   mode( frequency, response ); 31   return  0; 32 }
3.  Define function  mean 3.1  Define function  median 3.1.1 Sort Array 3.1.2 Print middle element 33 34 void  mean(  const   int  answer[] ) 35 { 36   int  j, total = 0; 37 38   printf( &quot;%s%s%s&quot;, &quot;********&quot;, &quot;  Mean&quot;, &quot;********&quot; ); 39 40   for  ( j = 0; j <= SIZE - 1; j++ ) 41   total += answer[  j  ]; 42 43   printf( &quot;The mean is the average value of the data&quot; 44   &quot;items. The mean is equal to the total of&quot; 45   &quot;all the data items divided by the number&quot; 46   &quot;of data items ( %d ). The mean value for&quot; 47   &quot;this run is: %d / %d = %.4f&quot;, 48   SIZE, total, SIZE, (  double   ) total / SIZE ); 49 } 50 51 void  median(  int  answer[] ) 52 { 53   printf( &quot;%s%s%s%s&quot;,  54   &quot;********&quot;, &quot; Median&quot;, &quot;********&quot;,  55   &quot;The unsorted array of responses is&quot; ); 56 57   printArray( answer ); 58   bubbleSort( answer ); 59   printf( &quot;The sorted array is&quot; ); 60   printArray( answer ); 61   printf( &quot;The median is element %d of&quot; 62   &quot;the sorted %d element array.&quot; 63   &quot;For this run the median is %d&quot;, 64   SIZE / 2, SIZE, answer[ SIZE / 2 ] );
3.2  Define function  mode 3.2.1 Increase  frequency[]  depending on  response[] 65 } 66 67 void  mode(  int  freq[],  const   int  answer[] ) 68 { 69   int  rating, j, h, largest = 0, modeValue = 0; 70 71   printf( &quot;%s%s%s&quot;,  72   &quot;********&quot;, &quot;  Mode&quot;, &quot;********&quot; ); 73 74   for  ( rating = 1; rating <= 9; rating++ ) 75   freq[ rating ] = 0; 76 77   for  ( j = 0; j <= SIZE - 1; j++ ) 78   ++freq[ answer[ j ] ]; 79 80   printf( &quot;%s%11s%19s%54s%54s&quot;, 81   &quot;Response&quot;, &quot;Frequency&quot;, &quot;Histogram&quot;, 82   &quot;1  1  2  2&quot;, &quot;5  0  5  0  5&quot; ); 83 84   for  ( rating = 1; rating <= 9; rating++ ) { 85   printf( &quot;%8d%11d  &quot;, rating, freq[ rating ] ); 86 87   if  ( freq[ rating ] > largest ) { 88   largest = freq[ rating ]; 89   modeValue = rating; 90   } 91 92   for  ( h = 1; h <= freq[ rating ]; h++ ) 93   printf( &quot;*&quot; ); 94 Notice how the subscript in  frequency[]   is the value of an element in   response[]   ( answer[] ) Print stars depending on value of  frequency[]
3.3 Define  bubbleSort 3.3 Define  printArray 95   printf( &quot;&quot; ); 96   } 97 98   printf( &quot;The mode is the most frequent value.&quot; 99   &quot;For this run the mode is %d which occurred&quot; 100   &quot; %d times.&quot;, modeValue, largest ); 101 } 102 103  void  bubbleSort(  int  a[] ) 104   { 105   int  pass, j, hold; 106 107   for  ( pass = 1; pass <= SIZE - 1; pass++ ) 108 109   for  ( j = 0; j <= SIZE - 2; j++ ) 110 111   if  ( a[ j ] > a[ j + 1 ] ) { 112   hold = a[ j ]; 113   a[ j ] = a[ j + 1 ]; 114   a[ j + 1 ] = hold; 115   } 116  } 117 118  void  printArray(  const   int  a[] ) 119   { 120   int  j; 121 122   for  ( j = 0; j <= SIZE - 1; j++ ) { 123 124   if  ( j % 20 == 0 ) 125   printf( &quot;&quot; ); Bubble sort: if elements out of order, swap them.
Program Output ******** Mean ******** The mean is the average value of the data items. The mean is equal to the total of all the data items divided by the number of data items (99). The mean value for this run is: 681 / 99 = 6.8788   ******** Median ******** The unsorted array of responses is 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8 6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9 6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3 5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8 7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7   The sorted array is 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9   The median is element 49 of the sorted 99 element array. For this run the median is 7 126 127   printf( &quot;%2d&quot;, a[ j ] ); 128   } 129  }
Program Output ******** Mode ******** Response  Frequency  Histogram   1  1  2  2 5  0  5  0  5   1  1  * 2  3  *** 3  4  **** 4  5  ***** 5  8  ******** 6  9  ********* 7  23  *********************** 8  27  *************************** 9  19  ******************* The mode is the most frequent value. For this run the mode is 8 which occurred 27 times.
6.8 Searching Arrays: Linear Search and Binary Search ,[object Object],[object Object],[object Object],[object Object],[object Object]
6.8 Searching Arrays: Linear Search and Binary Search ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],5
6.9 Multiple-Subscripted Arrays ,[object Object],[object Object],[object Object],Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3 a[   0   ][   0   ] a[   1   ][   0   ] a[   2   ][   0   ] a[   0   ][   1   ] a[   1   ][   1   ] a[   2   ][   1   ] a[   0   ][   2   ] a[   1   ][   2   ] a[   2   ][   2   ] a[   0   ][   3   ] a[   1   ][   3   ] a[   2   ][   3   ] Row subscript Array name Column subscript
6.9 Multiple-Subscripted Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],  1  2 3  4 1  0 3  4
1.  Initialize variables 1.1  Define functions to take double scripted arrays 1.2  Initialize  studentgrades[][] 2.  Call functions  minimum ,  maximum , and  average 1 /* Fig. 6.22: fig06_22.c 2   Double-subscripted array example */ 3 #include  <stdio.h> 4 #define  STUDENTS 3 5 #define  EXAMS 4 6 7 int  minimum(  const   int  [][ EXAMS ],  int ,  int  ); 8 int  maximum(  const   int  [][ EXAMS ],  int ,  int  ); 9 double  average(  const   int  [],  int  ); 10 void  printArray(  const   int  [][ EXAMS ],  int ,  int  ); 11 12 int  main() 13 { 14   int  student; 15   const   int  studentGrades[ STUDENTS ][ EXAMS ] =  16   { { 77, 68, 86, 73 }, 17   { 96, 87, 89, 78 }, 18   { 70, 90, 86, 81 } }; 19 20   printf( &quot;The array is:&quot; ); 21   printArray( studentGrades, STUDENTS, EXAMS ); 22   printf( &quot;Lowest grade: %dHighest grade: %d&quot;, 23   minimum( studentGrades, STUDENTS, EXAMS ), 24   maximum( studentGrades, STUDENTS, EXAMS ) ); 25 26   for  ( student = 0; student <= STUDENTS - 1; student++ ) 27   printf( &quot;The average grade for student %d is %.2f&quot;,  28   student,  29   average( studentGrades[ student ], EXAMS ) ); 30 31   return  0; 32 } Each row is a particular student, each column is the grades on the exam.
3.  Define functions 33 34 /* Find the minimum grade */ 35 int  minimum(  const   int  grades[][ EXAMS ],  36   int  pupils,  int  tests ) 37 { 38   int  i, j, lowGrade = 100; 39 40   for  ( i = 0; i <= pupils - 1; i++ ) 41   for  ( j = 0; j <= tests - 1; j++ ) 42   if  ( grades[ i ][ j ] < lowGrade ) 43   lowGrade = grades[ i ][ j ]; 44 45   return  lowGrade; 46 } 47 48 /* Find the maximum grade */ 49 int  maximum(  const   int  grades[][ EXAMS ],  50   int  pupils,  int  tests ) 51 { 52   int  i, j, highGrade = 0; 53 54   for  ( i = 0; i <= pupils - 1; i++ ) 55   for  ( j = 0; j <= tests - 1; j++ ) 56   if  ( grades[ i ][ j ] > highGrade ) 57   highGrade = grades[ i ][ j ]; 58 59   return  highGrade; 60 } 61 62 /* Determine the average grade for a particular exam */ 63 double  average(  const   int  setOfGrades[],  int  tests ) 64 {
3.  Define functions 65   int  i, total = 0; 66 67   for  ( i = 0; i <= tests - 1; i++ ) 68   total += setOfGrades[ i ]; 69 70   return  (  double  ) total / tests; 71 } 72 73 /* Print the array */ 74 void  printArray(  const   int  grades[][ EXAMS ],  75   int  pupils,  int  tests ) 76 { 77   int  i, j; 78 79   printf( &quot;  [0]  [1]  [2]  [3]&quot; ); 80 81   for  ( i = 0; i <= pupils - 1; i++ ) { 82   printf( &quot;studentGrades[%d] &quot;, i ); 83 84   for  ( j = 0; j <= tests - 1; j++ ) 85   printf( &quot;%-5d&quot;, grades[ i ][ j ] ); 86   } 87 }
Program Output The array is: [0]  [1]  [2]  [3] studentGrades[0] 77  68  86  73  studentGrades[1] 96  87  89  78  studentGrades[2] 70  90  86  81  Lowest grade: 68 Highest grade: 96 The average grade for student 0 is 76.00 The average grade for student 1 is 87.50 The average grade for student 2 is 81.75
Chapter 7 - Pointers Outline 7.1 Introduction 7.2 Pointer Variable Declarations and Initialization 7.3 Pointer Operators 7.4 Calling Functions by Reference 7.5 Using the  const  Qualifier with Pointers 7.6 Bubble Sort Using Call by Reference 7.7 Pointer Expressions and Pointer Arithmetic 7.8 The Relationship between Pointers and Arrays 7.9 Arrays of Pointers 7.10 Case Study: A Card Shuffling and Dealing Simulation 7.11 Pointers to Functions
7.1 Introduction ,[object Object],[object Object],[object Object],[object Object]
7.2 Pointer Variable Declarations and Initialization ,[object Object],[object Object],[object Object],[object Object],[object Object],  count 7 count 7 countPtr
7.2 Pointer Variable Declarations and Initialization ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
7.3 Pointer Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],yPtr y 5 yptr 500000 600000 y 600000 5 Address of  y  is value of  yptr
7.3 Pointer Operators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Declare variables 2  Initialize variables 3.  Print Program Output The address of a is 0012FF88 The value of aPtr is 0012FF88 The value of a is 7 The value of *aPtr is 7 Proving that * and & are complements of each other. &*aPtr = 0012FF88 *&aPtr = 0012FF88 1 /* Fig. 7.4: fig07_04.c 2   Using the & and * operators */ 3 #include  <stdio.h> 4 5 int  main() 6 { 7   int  a;  /* a is an integer */ 8   int  *aPtr;  /* aPtr is a pointer to an integer */ 9 10   a = 7; 11   aPtr = &a;  /* aPtr set to address of a */ 12 13   printf( &quot;The address of a is %p&quot;  14   &quot;The value of aPtr is %p&quot;, &a, aPtr ); 15 16   printf( &quot;The value of a is %d&quot;  17   &quot;The value of *aPtr is %d&quot;, a, *aPtr ); 18 19   printf( &quot;Showing that * and & are inverses of &quot; 20   &quot;each other.&*aPtr = %p&quot;  21   &quot;*&aPtr = %p&quot;, &*aPtr, *&aPtr ); 22 23   return  0; 24 } The address of  a  is the value of  aPtr.   The  *  operator returns an alias to what its operand points to.  aPtr  points to  a , so  *aPtr  returns  a . Notice how  *  and  &  are inverses
7.4 Calling Functions by Reference ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],The original value of number is 5 The new value of number is 125 Notice that the function prototype takes a pointer to an integer ( int * ). 1 /* Fig. 7.7: fig07_07.c 2   Cube a variable using call-by-reference   3   with a pointer argument */ 4 5 #include  <stdio.h> 6 7 void  cubeByReference(  int  * );  /* prototype */ 8 9 int  main() 10 { 11   int  number = 5; 12 13   printf( &quot;The original value of number is %d&quot;, number ); 14   cubeByReference( &number ); 15   printf( &quot;The new value of number is %d&quot;, number ); 16 17   return  0; 18 } 19 20 void  cubeByReference(  int  *nPtr ) 21 { 22   *nPtr = *nPtr * *nPtr * *nPtr;  /* cube number in main */ 23 } Notice how the address of  number  is given -  cubeByReference  expects a pointer (an address of a variable).  Inside  cubeByReference ,  *nPtr  is used ( *nPtr  is  number ).
7.5 Using the  const  Qualifier with Pointers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Declare variables 1.1  Declare  const  pointer to an  int 2.  Change  *ptr  (which is  x ) 2.1  Attempt to change  ptr 3. Output Program Output FIG07_13.c: Error E2024 FIG07_13.c 16: Cannot modify a const object in  function main *** 1 errors in Compile *** 1 /* Fig. 7.13: fig07_13.c 2   Attempting to modify a constant pointer to 3   non-constant data */ 4 5 #include  <stdio.h> 6 7 int  main() 8 { 9   int  x, y; 10 11   int  *  const  ptr = &x;  /* ptr is a constant pointer to an   12   integer. An integer can be modified 13   through ptr, but ptr always points  14   to the same memory location. */ 15   *ptr = 7; 16   ptr = &y; 17 18   return  0; 19 } Changing  *ptr  is allowed –  x  is not a constant. Changing  ptr  is an error –  ptr  is a constant pointer.
7.6 Bubble Sort Using Call-by-reference ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
7.6 Bubble Sort Using Call-by-reference ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
1.  Initialize array 1.1  Declare variables 2.  Print array 2.1  Call  bubbleSort 2.2  Print array 1 /* Fig. 7.15: fig07_15.c 2   This program puts values into an array, sorts the values into 3   ascending order, and prints the resulting array. */ 4 #include  <stdio.h> 5 #define  SIZE 10 6 void  bubbleSort(  int  *,  const   int  ); 7 8 int  main() 9 { 10   11   int  a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 12   int  i; 13 14   printf( &quot;Data items in original order&quot; ); 15 16   for  ( i = 0; i < SIZE; i++ ) 17   printf( &quot;%4d&quot;, a[ i ] ); 18 19   bubbleSort( a, SIZE );  /* sort the array */ 20   printf( &quot;Data items in ascending order&quot; ); 21 22   for  ( i = 0; i < SIZE; i++ ) 23   printf( &quot;%4d&quot;, a[ i ] );  24 25   printf( &quot;&quot; ); 26 27   return  0; 28 } 29 30 void  bubbleSort(  int  *array,  const   int  size ) 31 { 32   void  swap(  int  *,  int  * ); Bubblesort  gets passed the address of array elements (pointers).  The name of an array is a pointer.
3. Function definitions Program Output Data items in original order 2  6  4  8  10  12  89  68  45  37 Data items in ascending order 2  4  6  8  10  12  37  45  33   int  pass, j;  34   for  ( pass = 0; pass < size - 1; pass++ ) 35 36   for  ( j = 0; j < size - 1; j++ ) 37 38   if  ( array[ j ] > array[ j + 1 ] ) 39   swap( &array[ j ], &array[ j + 1 ] ); 40 } 41 42 void  swap(  int  *element1Ptr,  int  *element2Ptr ) 43 { 44   int  hold = *element1Ptr; 45   *element1Ptr = *element2Ptr; 46   *element2Ptr = hold; 47 }
7.7 Pointer Expressions and Pointer Arithmetic ,[object Object],[object Object],[object Object],[object Object],[object Object], 
7.7 Pointer Expressions and Pointer Arithmetic ,[object Object],[object Object],[object Object],[object Object],[object Object],  pointer variable  vPtr v[0] v[1] v[2] v[4] v[3] 3000 3004 3008 3012 3016 location
7.7 Pointer Expressions and Pointer Arithmetic ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
7.7 Pointer Expressions and Pointer Arithmetic ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
7.8 The Relationship Between Pointers and Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
7.8 The Relationship Between Pointers and Arrays ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
7.9 Arrays of Pointers ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],  suit[3] suit[2] suit[1] suit[0] ’ H’ ’ e’ ’ a’ ’ r’ ’ t’ ’ s’ ’ ’ ’ D’ ’ i’ ’ a’ ’ m’ ’ o’ ’ n’ ’ d’ ’ s’ ’ ’ ’ C’ ’ l’ ’ u’ ’ b’ ’ s’ ’ ’ ’ S’ ’ p’ ’ a’ ’ d’ ’ e’ ’ s’ ’ ’
7.10 Case Study: A Card Shuffling and Dealing Simulation ,[object Object],[object Object]
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt
Book ppt

Más contenido relacionado

La actualidad más candente

C programming language
C programming languageC programming language
C programming languageMaha lakshmi
 
Programming languages
Programming languagesProgramming languages
Programming languagesSimon Mui
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / TranslatorsProject Student
 
best notes in c language
best notes in c languagebest notes in c language
best notes in c languageIndia
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxEng Teong Cheah
 
History of Programming Language
History of Programming LanguageHistory of Programming Language
History of Programming Languagetahria123
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programmingTejaswiB4
 
Introduction to c programming language
Introduction to c programming languageIntroduction to c programming language
Introduction to c programming languagesanjay joshi
 
introduction to c language
 introduction to c language introduction to c language
introduction to c languageRai University
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program executionRumman Ansari
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of cHarish Kumawat
 

La actualidad más candente (20)

Lecture 1- History of C Programming
Lecture 1- History of C Programming Lecture 1- History of C Programming
Lecture 1- History of C Programming
 
C programming language
C programming languageC programming language
C programming language
 
C program
C programC program
C program
 
Programming in c notes
Programming in c notesProgramming in c notes
Programming in c notes
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / Translators
 
best notes in c language
best notes in c languagebest notes in c language
best notes in c language
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
Introduction to problem solving in C
Introduction to problem solving in CIntroduction to problem solving in C
Introduction to problem solving in C
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic Syntax
 
History of Programming Language
History of Programming LanguageHistory of Programming Language
History of Programming Language
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
Introduction to c programming language
Introduction to c programming languageIntroduction to c programming language
Introduction to c programming language
 
introduction to c language
 introduction to c language introduction to c language
introduction to c language
 
C basics
C   basicsC   basics
C basics
 
C fundamental
C fundamentalC fundamental
C fundamental
 
C LANGUAGE NOTES
C LANGUAGE NOTESC LANGUAGE NOTES
C LANGUAGE NOTES
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program execution
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of c
 

Destacado

Programming embedded system_ii_keil_8051(1)
Programming embedded system_ii_keil_8051(1)Programming embedded system_ii_keil_8051(1)
Programming embedded system_ii_keil_8051(1)Fendie Mimpi
 
C presentation book
C presentation bookC presentation book
C presentation bookkrunal1210
 
Automotive Circuit Boards
Automotive Circuit BoardsAutomotive Circuit Boards
Automotive Circuit BoardsArt Wood
 
RoHS Compliant Lead Free PCB Fabrication
RoHS Compliant Lead Free PCB FabricationRoHS Compliant Lead Free PCB Fabrication
RoHS Compliant Lead Free PCB FabricationArt Wood
 
Pcb Production and Prototype Manufacturing Capabilities for Saturn Electronic...
Pcb Production and Prototype Manufacturing Capabilities for Saturn Electronic...Pcb Production and Prototype Manufacturing Capabilities for Saturn Electronic...
Pcb Production and Prototype Manufacturing Capabilities for Saturn Electronic...Art Wood
 
training_presentation
training_presentationtraining_presentation
training_presentationAniket Pawar
 
Epoxy flux a low cost high reliability approach for pop assembly-imaps 2011
Epoxy flux  a low cost high reliability approach for pop assembly-imaps 2011Epoxy flux  a low cost high reliability approach for pop assembly-imaps 2011
Epoxy flux a low cost high reliability approach for pop assembly-imaps 2011nclee715
 
New Algorithms to Improve X-Ray Inspection
New Algorithms to Improve X-Ray InspectionNew Algorithms to Improve X-Ray Inspection
New Algorithms to Improve X-Ray InspectionBill Cardoso
 
A solder joint reliability model for the philips lumileds luxeon rebel led c...
A solder joint reliability model for the philips lumileds luxeon rebel led  c...A solder joint reliability model for the philips lumileds luxeon rebel led  c...
A solder joint reliability model for the philips lumileds luxeon rebel led c...Greg Caswell
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in cyazad dumasia
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Smt notes
Smt notesSmt notes
Smt notesabishus
 
Prerna sharma
Prerna sharmaPrerna sharma
Prerna sharmaRCET
 
LED, BGA, and QFN assembly and inspection case studies
LED, BGA, and QFN assembly and inspection case studiesLED, BGA, and QFN assembly and inspection case studies
LED, BGA, and QFN assembly and inspection case studiesBill Cardoso
 

Destacado (20)

Programming embedded system_ii_keil_8051(1)
Programming embedded system_ii_keil_8051(1)Programming embedded system_ii_keil_8051(1)
Programming embedded system_ii_keil_8051(1)
 
C presentation book
C presentation bookC presentation book
C presentation book
 
Automotive Circuit Boards
Automotive Circuit BoardsAutomotive Circuit Boards
Automotive Circuit Boards
 
RoHS Compliant Lead Free PCB Fabrication
RoHS Compliant Lead Free PCB FabricationRoHS Compliant Lead Free PCB Fabrication
RoHS Compliant Lead Free PCB Fabrication
 
SMT machine Training Manual for FUJI CP6 Series Level 3
SMT machine Training Manual for FUJI  CP6 Series Level 3SMT machine Training Manual for FUJI  CP6 Series Level 3
SMT machine Training Manual for FUJI CP6 Series Level 3
 
Pcb Production and Prototype Manufacturing Capabilities for Saturn Electronic...
Pcb Production and Prototype Manufacturing Capabilities for Saturn Electronic...Pcb Production and Prototype Manufacturing Capabilities for Saturn Electronic...
Pcb Production and Prototype Manufacturing Capabilities for Saturn Electronic...
 
Reflow oven
Reflow ovenReflow oven
Reflow oven
 
training_presentation
training_presentationtraining_presentation
training_presentation
 
Epoxy flux a low cost high reliability approach for pop assembly-imaps 2011
Epoxy flux  a low cost high reliability approach for pop assembly-imaps 2011Epoxy flux  a low cost high reliability approach for pop assembly-imaps 2011
Epoxy flux a low cost high reliability approach for pop assembly-imaps 2011
 
New Algorithms to Improve X-Ray Inspection
New Algorithms to Improve X-Ray InspectionNew Algorithms to Improve X-Ray Inspection
New Algorithms to Improve X-Ray Inspection
 
PIC_ARM_AVR
PIC_ARM_AVRPIC_ARM_AVR
PIC_ARM_AVR
 
A solder joint reliability model for the philips lumileds luxeon rebel led c...
A solder joint reliability model for the philips lumileds luxeon rebel led  c...A solder joint reliability model for the philips lumileds luxeon rebel led  c...
A solder joint reliability model for the philips lumileds luxeon rebel led c...
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Embedded system
Embedded systemEmbedded system
Embedded system
 
Smt notes
Smt notesSmt notes
Smt notes
 
Prerna sharma
Prerna sharmaPrerna sharma
Prerna sharma
 
LED, BGA, and QFN assembly and inspection case studies
LED, BGA, and QFN assembly and inspection case studiesLED, BGA, and QFN assembly and inspection case studies
LED, BGA, and QFN assembly and inspection case studies
 

Similar a Book ppt

Introduction
IntroductionIntroduction
IntroductionKamran
 
Diploma ii cfpc u-1 introduction to c language
Diploma ii  cfpc u-1 introduction to c languageDiploma ii  cfpc u-1 introduction to c language
Diploma ii cfpc u-1 introduction to c languageRai University
 
Bsc cs i pic u-1 introduction to c language
Bsc cs i pic u-1 introduction to c languageBsc cs i pic u-1 introduction to c language
Bsc cs i pic u-1 introduction to c languageRai University
 
Btech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageBtech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageRai University
 
Mca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageMca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageRai University
 
Lecture01
Lecture01Lecture01
Lecture01Xafran
 
Introduction to Computer
Introduction to ComputerIntroduction to Computer
Introduction to Computerzaheeriqbal41
 
Learn c programming language in 24 hours allfreebooks.tk
Learn c programming language in 24 hours   allfreebooks.tkLearn c programming language in 24 hours   allfreebooks.tk
Learn c programming language in 24 hours allfreebooks.tkragulasai
 
Chapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfChapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfKirubelWondwoson1
 
Summer training PPT Manasv Singharia.pptx
Summer training PPT Manasv Singharia.pptxSummer training PPT Manasv Singharia.pptx
Summer training PPT Manasv Singharia.pptxshokeenk14
 

Similar a Book ppt (20)

C 1
C 1C 1
C 1
 
Introduction
IntroductionIntroduction
Introduction
 
Savitch Ch 01
Savitch Ch 01Savitch Ch 01
Savitch Ch 01
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Savitch ch 01
Savitch ch 01Savitch ch 01
Savitch ch 01
 
Savitch ch 01
Savitch ch 01Savitch ch 01
Savitch ch 01
 
Diploma ii cfpc u-1 introduction to c language
Diploma ii  cfpc u-1 introduction to c languageDiploma ii  cfpc u-1 introduction to c language
Diploma ii cfpc u-1 introduction to c language
 
Bsc cs i pic u-1 introduction to c language
Bsc cs i pic u-1 introduction to c languageBsc cs i pic u-1 introduction to c language
Bsc cs i pic u-1 introduction to c language
 
Btech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c languageBtech i pic u-1 introduction to c language
Btech i pic u-1 introduction to c language
 
Mca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c languageMca i pic u-1 introduction to c language
Mca i pic u-1 introduction to c language
 
Lecture01
Lecture01Lecture01
Lecture01
 
Introduction to Computer
Introduction to ComputerIntroduction to Computer
Introduction to Computer
 
Learn c programming language in 24 hours allfreebooks.tk
Learn c programming language in 24 hours   allfreebooks.tkLearn c programming language in 24 hours   allfreebooks.tk
Learn c programming language in 24 hours allfreebooks.tk
 
Chapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfChapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdf
 
Introduction to c language
Introduction to c language Introduction to c language
Introduction to c language
 
Computer Programming - Lecture 1
Computer Programming - Lecture 1Computer Programming - Lecture 1
Computer Programming - Lecture 1
 
Summer training PPT Manasv Singharia.pptx
Summer training PPT Manasv Singharia.pptxSummer training PPT Manasv Singharia.pptx
Summer training PPT Manasv Singharia.pptx
 
Chap 1 c++
Chap 1 c++Chap 1 c++
Chap 1 c++
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Programming in C
Programming in CProgramming in C
Programming in C
 

Más de FALLEE31188 (20)

Lecture4
Lecture4Lecture4
Lecture4
 
Lecture2
Lecture2Lecture2
Lecture2
 
L16
L16L16
L16
 
L2
L2L2
L2
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Functions
FunctionsFunctions
Functions
 
Field name
Field nameField name
Field name
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Cis068 08
Cis068 08Cis068 08
Cis068 08
 
Chapter14
Chapter14Chapter14
Chapter14
 
Chapt03
Chapt03Chapt03
Chapt03
 
C++lecture9
C++lecture9C++lecture9
C++lecture9
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Brookshear 06
Brookshear 06Brookshear 06
Brookshear 06
 
Assignment 2
Assignment 2Assignment 2
Assignment 2
 
Assignment
AssignmentAssignment
Assignment
 

Último

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Último (20)

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 

Book ppt

  • 1. Chapter 1 – Introduction to Computers and C++ Programming Outline 1.1 Introduction 1.2 What Is a Computer? 1.3 Computer Organization 1.4 Evolution of Operating Systems 1.5 Personal Computing, Distributed Computing and Client/Server Computing 1.6 Machine Languages, Assembly Languages and High-level Languages 1.7 The History of C 1.8 The C Standard Library 1.9 The Key Software Trend: Object Technology 1.10 C++ and C++ How to Program 1.11 Java and Java How to Program 1.12 Other High-level Languages 1.13 Structured Programming 1.14 The Basics of a typical C Program Development Environment
  • 2. Chapter 1 – Introduction to Computers and C++ Programming Outline 1.15 Hardware Trends 1.16 History of the Internet 1.17 History of the World Wide Web 1.18 General Notes About C and this Book
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. Chapter 2 - Introduction to C Programming Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic in C 2.6 Decision Making: Equality and Relational Operators
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28. 1. Initialize variables 2. Input 2.1 Sum 3. Print Program Output Enter first integer 45 Enter second integer 72 Sum is 117 1 /* Fig. 2.5: fig02_05.c 2 Addition program */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int integer1, integer2, sum; /* declaration */ 8 9 printf( &quot;Enter first integer&quot; ); /* prompt */ 10 scanf( &quot;%d&quot;, &integer1 ); /* read an integer */ 11 printf( &quot;Enter second integer&quot; ); /* prompt */ 12 scanf( &quot;%d&quot;, &integer2 ); /* read an integer */ 13 sum = integer1 + integer2; /* assignment of sum */ 14 printf( &quot;Sum is %d&quot;, sum ); /* print sum */ 15 16 return 0; /* indicate that program ended successfully */ 17 }
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36. 2.6 Decision Making: Equality and Relational Operators
  • 37. 2.6 Decision Making: Equality and Relational Operators
  • 38. 1. Declare variables 2. Input 2.1 if statements 3. Print 1 /* Fig. 2.13: fig02_13.c 2 Using if statements, relational 3 operators, and equality operators */ 4 #include <stdio.h> 5 6 int main() 7 { 8 int num1, num2; 9 10 printf( &quot;Enter two integers, and I will tell you&quot; ); 11 printf( &quot;the relationships they satisfy: &quot; ); 12 scanf( &quot;%d%d&quot;, &num1, &num2 ); /* read two integers */ 13 14 if ( num1 == num2 ) 15 printf( &quot;%d is equal to %d&quot;, num1, num2 ); 16 17 if ( num1 != num2 ) 18 printf( &quot;%d is not equal to %d&quot;, num1, num2 ); 19 20 if ( num1 < num2 ) 21 printf( &quot;%d is less than %d&quot;, num1, num2 ); 22 23 if ( num1 > num2 ) 24 printf( &quot;%d is greater than %d&quot;, num1, num2 ); 25 26 if ( num1 <= num2 ) 27 printf( &quot;%d is less than or equal to %d&quot;, 28 num1, num2 );
  • 39. 3.1 Exit main Program Output Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 29 30 if ( num1 >= num2 ) 31 printf( &quot;%d is greater than or equal to %d&quot;, 32 num1, num2 ); 33 34 return 0; /* indicate program ended successfully */ 35 }
  • 40. Chapter 3 - Structured Program Development Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4 Control Structures 3.5 The If Selection Structure 3.6 The If/Else Selection Structure 3.7 The While Repetition Structure 3.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) 3.9 Formulating Algorithms with Top-down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) 3.10 Formulating Algorithms with Top-down, Stepwise Refinement: Case Study 3 (Nested Control Structures) 3.11 Assignment Operators 3.12 Increment and Decrement Operators
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58. 1. Initialize Variables 2. Execute Loop 3. Output results 1 /* Fig. 3.6: fig03_06.c 2 Class average program with 3 counter-controlled repetition */ 4 #include <stdio.h> 5 6 int main() 7 { 8 int counter, grade, total, average; 9 10 /* initialization phase */ 11 total = 0; 12 counter = 1; 13 14 /* processing phase */ 15 while ( counter <= 10 ) { 16 printf( &quot;Enter grade: &quot; ); 17 scanf( &quot;%d&quot;, &grade ); 18 total = total + grade; 19 counter = counter + 1; 20 } 21 22 /* termination phase */ 23 average = total / 10; 24 printf( &quot;Class average is %d&quot;, average ); 25 26 return 0; /* indicate program ended successfully */ 27 }
  • 59. Program Output Enter grade: 98 Enter grade: 76 Enter grade: 71 Enter grade: 87 Enter grade: 83 Enter grade: 90 Enter grade: 57 Enter grade: 79 Enter grade: 82 Enter grade: 94 Class average is 81
  • 60.
  • 61.
  • 62.
  • 63.
  • 64. 1. Initialize Variables 2. Get user input 2.1 Perform Loop 1 /* Fig. 3.8: fig03_08.c 2 Class average program with 3 sentinel-controlled repetition */ 4 #include <stdio.h> 5 6 int main() 7 { 8 float average; /* new data type */ 9 int counter, grade, total; 10 11 /* initialization phase */ 12 total = 0; 13 counter = 0; 14 15 /* processing phase */ 16 printf( &quot;Enter grade, -1 to end: &quot; ); 17 scanf( &quot;%d&quot;, &grade ); 18 19 while ( grade != -1 ) { 20 total = total + grade; 21 counter = counter + 1; 22 printf( &quot;Enter grade, -1 to end: &quot; ); 23 scanf( &quot;%d&quot;, &grade ); 24 }
  • 65. 3. Calculate Average 3.1 Print Results Program Output Enter grade, -1 to end: 75 Enter grade, -1 to end: 94 Enter grade, -1 to end: 97 Enter grade, -1 to end: 88 Enter grade, -1 to end: 70 Enter grade, -1 to end: 64 Enter grade, -1 to end: 83 Enter grade, -1 to end: 89 Enter grade, -1 to end: -1 Class average is 82.50 25 26 /* termination phase */ 27 if ( counter != 0 ) { 28 average = ( float ) total / counter; 29 printf( &quot;Class average is %.2f&quot;, average ); 30 } 31 else 32 printf( &quot;No grades were entered&quot; ); 33 34 return 0; /* indicate program ended successfully */ 35 }
  • 66.
  • 67.
  • 68.
  • 69. 1. Initialize variables 2. Input data and count passes/failures 3. Print results 1 /* Fig. 3.10: fig03_10.c 2 Analysis of examination results */ 3 #include <stdio.h> 4 5 int main() 6 { 7 /* initializing variables in declarations */ 8 int passes = 0, failures = 0, student = 1, result; 9 10 /* process 10 students; counter-controlled loop */ 11 while ( student <= 10 ) { 12 printf( &quot;Enter result ( 1=pass,2=fail ): &quot; ); 13 scanf( &quot;%d&quot;, &result ); 14 15 if ( result == 1 ) /* if/else nested in while */ 16 passes = passes + 1; 17 else 18 failures = failures + 1; 19 20 student = student + 1; 21 } 22 23 printf( &quot;Passed %d&quot;, passes ); 24 printf( &quot;Failed %d&quot;, failures ); 25 26 if ( passes > 8 ) 27 printf( &quot;Raise tuition&quot; ); 28 29 return 0; /* successful termination */ 30 }
  • 70. Program Output Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 1 Enter Result (1=pass,2=fail): 2 Passed 6 Failed 4
  • 71.
  • 72.
  • 73.
  • 74. Chapter 5 - Functions Outline 5.1 Introduction 5.2 Program Modules in C 5.3 Math Library Functions 5.4 Functions 5.5 Function Definitions 5.6 Function Prototypes 5.7 Header Files 5.8 Calling Functions: Call by Value and Call by Reference 5.9 Random Number Generation 5.10 Example: A Game of Chance 5.11 Storage Classes 5.12 Scope Rules 5.13 Recursion 5.14 Example Using Recursion: The Fibonacci Series 5.15 Recursion vs. Iteration
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87. 1. Initialize seed 2. Input value for seed 2.1 Use srand to change random sequence 2.2 Define Loop 3. Generate and output random numbers 1 /* Fig. 5.9: fig05_09.c 2 Randomizing die-rolling program */ 3 #include <stdlib.h> 4 #include <stdio.h> 5 6 int main() 7 { 8 int i; 9 unsigned seed; 10 11 printf( &quot;Enter seed: &quot; ); 12 scanf( &quot;%u&quot;, &seed ); 13 srand( seed ); 14 15 for ( i = 1; i <= 10; i++ ) { 16 printf( &quot;%10d&quot;, 1 + ( rand() % 6 ) ); 17 18 if ( i % 5 == 0 ) 19 printf( &quot;&quot; ); 20 } 21 22 return 0; 23 }
  • 88. Program Output Enter seed: 867 2 4 6 1 6 1 1 3 6 2   Enter seed: 67 6 1 4 6 2 1 6 1 6 4 Enter seed: 67 6 1 4 6 2 1 6 1 6 4
  • 89.
  • 90. 1. rollDice prototype 1.1 Initialize variables 1.2 Seed srand 2. Define switch statement for win/loss/continue 2.1 Loop 1 /* Fig. 5.10: fig05_10.c 2 Craps */ 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <time.h> 6 7 int rollDice( void ); 8 9 int main() 10 { 11 int gameStatus, sum, myPoint; 12 13 srand( time( NULL ) ); 14 sum = rollDice(); /* first roll of the dice */ 15 16 switch ( sum ) { 17 case 7: case 11: /* win on first roll */ 18 gameStatus = 1; 19 break ; 20 case 2: case 3: case 12: /* lose on first roll */ 21 gameStatus = 2; 22 break ; 23 default : /* remember point */ 24 gameStatus = 0; 25 myPoint = sum; 26 printf( &quot;Point is %d&quot;, myPoint ); 27 break ; 28 } 29 30 while ( gameStatus == 0 ) { /* keep rolling */ 31 sum = rollDice(); 32
  • 91. 2.2 Print win/loss Program Output Player rolled 6 + 5 = 11 Player wins 33 if ( sum == myPoint ) /* win by making point */ 34 gameStatus = 1; 35 else 36 if ( sum == 7 ) /* lose by rolling 7 */ 37 gameStatus = 2; 38 } 39 40 if ( gameStatus == 1 ) 41 printf( &quot;Player wins&quot; ); 42 else 43 printf( &quot;Player loses&quot; ); 44 45 return 0; 46 } 47 48 int rollDice( void ) 49 { 50 int die1, die2, workSum; 51 52 die1 = 1 + ( rand() % 6 ); 53 die2 = 1 + ( rand() % 6 ); 54 workSum = die1 + die2; 55 printf( &quot;Player rolled %d + %d = %d&quot;, die1, die2, workSum ); 56 return workSum; 57 }
  • 92. Program Output Player rolled 6 + 6 = 12 Player loses Player rolled 4 + 6 = 10 Point is 10 Player rolled 2 + 4 = 6 Player rolled 6 + 5 = 11 Player rolled 3 + 3 = 6 Player rolled 6 + 4 = 10 Player wins Player rolled 1 + 3 = 4 Point is 4 Player rolled 1 + 4 = 5 Player rolled 5 + 4 = 9 Player rolled 4 + 6 = 10 Player rolled 6 + 3 = 9 Player rolled 1 + 2 = 3 Player rolled 5 + 2 = 7 Player loses
  • 93.
  • 94.
  • 95.
  • 96.
  • 97. 1. Function prototypes 1.1 Initialize global variable 1.2 Initialize local variable 1.3 Initialize local variable in block 2. Call functions 3. Output results 1 /* Fig. 5.12: fig05_12.c 2 A scoping example */ 3 #include <stdio.h> 4 5 void a( void ); /* function prototype */ 6 void b( void ); /* function prototype */ 7 void c( void ); /* function prototype */ 8 9 int x = 1; /* global variable */ 10 11 int main() 12 { 13 int x = 5; /* local variable to main */ 14 15 printf(&quot;local x in outer scope of main is %d&quot;, x ); 16 17 { /* start new scope */ 18 int x = 7; 19 20 printf( &quot;local x in inner scope of main is %d&quot;, x ); 21 } /* end new scope */ 22 23 printf( &quot;local x in outer scope of main is %d&quot;, x ); 24 25 a(); /* a has automatic local x */ 26 b(); /* b has static local x */ 27 c(); /* c uses global x */ 28 a(); /* a reinitializes automatic local x */ 29 b(); /* static local x retains its previous value */ 30 c(); /* global x also retains its value */
  • 98. 3.1 Function definitions 31 32 printf( &quot;local x in main is %d&quot;, x ); 33 return 0; 34 } 35 36 void a( void ) 37 { 38 int x = 25; /* initialized each time a is called */ 39 40 printf( &quot;local x in a is %d after entering a&quot;, x ); 41 ++x; 42 printf( &quot;local x in a is %d before exiting a&quot;, x ); 43 } 44 45 void b( void ) 46 { 47 static int x = 50; /* static initialization only */ 48 /* first time b is called */ 49 printf( &quot;local static x is %d on entering b&quot;, x ); 50 ++x; 51 printf( &quot;local static x is %d on exiting b&quot;, x ); 52 } 53 54 void c( void ) 55 { 56 printf( &quot;global x is %d on entering c&quot;, x ); 57 x *= 10; 58 printf( &quot;global x is %d on exiting c&quot;, x ); 59 }
  • 99. Program Output local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c global x is 10 on exiting c local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is 5
  • 100.
  • 101.
  • 102.
  • 103.
  • 104. 1. Function prototype 1.1 Initialize variables 2. Input an integer 2.1 Call function fibonacci 2.2 Output results. 3. Define fibonacci recursively Program Output Enter an integer: 0 Fibonacci(0) = 0 Enter an integer: 1 Fibonacci(1) = 1 1 /* Fig. 5.15: fig05_15.c 2 Recursive fibonacci function */ 3 #include <stdio.h> 4 5 long fibonacci( long ); 6 7 int main() 8 { 9 long result, number; 10 11 printf( &quot;Enter an integer: &quot; ); 12 scanf( &quot;%ld&quot;, &number ); 13 result = fibonacci( number ); 14 printf( &quot;Fibonacci( %ld ) = %ld&quot;, number, result ); 15 return 0; 16 } 17 18 /* Recursive definition of function fibonacci */ 19 long fibonacci( long n ) 20 { 21 if ( n == 0 || n == 1 ) 22 return n; 23 else 24 return fibonacci( n - 1 ) + fibonacci( n - 2 ); 25 }
  • 105. Program Output Enter an integer: 2 Fibonacci(2) = 1 Enter an integer: 3 Fibonacci(3) = 2 Enter an integer: 4 Fibonacci(4) = 3 Enter an integer: 5 Fibonacci(5) = 5 Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 10 Fibonacci(10) = 55 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = 832040 Enter an integer: 35 Fibonacci(35) = 9227465
  • 106.
  • 107. Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays 6.3 Declaring Arrays 6.4 Examples Using Arrays 6.5 Passing Arrays to Functions 6.6 Sorting Arrays 6.7 Case Study: Computing Mean, Median and Mode Using Arrays 6.8 Searching Arrays 6.9 Multiple-Subscripted Arrays
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113. 1. Initialize array 2. Loop 3. Print 1 /* Fig. 6.8: fig06_08.c 2 Histogram printing program */ 3 #include <stdio.h> 4 #define SIZE 10 5 6 int main() 7 { 8 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; 9 int i, j; 10 11 printf( &quot;%s%13s%17s&quot;, &quot;Element&quot;, &quot;Value&quot;, &quot;Histogram&quot; ); 12 13 for ( i = 0; i <= SIZE - 1; i++ ) { 14 printf( &quot;%7d%13d &quot;, i, n[ i ]) ; 15 16 for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */ 17 printf( &quot;%c&quot;, '*' ); 18 19 printf( &quot;&quot; ); 20 } 21 22 return 0; 23 }
  • 114. Program Output Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 *
  • 115.
  • 116. 1. Initialize strings 2. Print strings 2.1 Define loop 2.2 Print characters individually 2.3 Input string 3. Print string Program Output Enter a string: Hello there string1 is: Hello string2 is: string literal string1 with spaces between characters is: H e l l o 1 /* Fig. 6.10: fig06_10.c 2 Treating character arrays as strings */ 3 #include <stdio.h> 4 5 int main() 6 { 7 char string1[ 20 ], string2[] = &quot;string literal&quot;; 8 int i; 9 10 printf(&quot; Enter a string: &quot;); 11 scanf( &quot;%s&quot;, string1 ); 12 printf( &quot;string1 is: %sstring2: is %s&quot; 13 &quot;string1 with spaces between characters is:&quot;, 14 string1, string2 ); 15 16 for ( i = 0; string1[ i ] != ''; i++ ) 17 printf( &quot;%c &quot;, string1[ i ] ); 18 19 printf( &quot;&quot; ); 20 return 0; 21 }
  • 117.
  • 118.
  • 119. 1. Function definitions 2. Pass array to a function 2.1 Pass array element to a function 3. Print 1 /* Fig. 6.13: fig06_13.c 2 Passing arrays and individual array elements to functions */ 3 #include <stdio.h> 4 #define SIZE 5 5 6 void modifyArray( int [], int ); /* appears strange */ 7 void modifyElement( int ); 8 9 int main() 10 { 11 int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i; 12 13 printf( &quot;Effects of passing entire array call &quot; 14 &quot;by reference:The values of the &quot; 15 &quot;original array are:&quot; ); 16 17 for ( i = 0; i <= SIZE - 1; i++ ) 18 printf( &quot;%3d&quot;, a[ i ] ); 19 20 printf( &quot;&quot; ); 21 modifyArray( a, SIZE ); /* passed call by reference */ 22 printf( &quot;The values of the modified array are:&quot; ); 23 24 for ( i = 0; i <= SIZE - 1; i++ ) 25 printf( &quot;%3d&quot;, a[ i ] ); 26 27 printf( &quot;Effects of passing array element call &quot; 28 &quot;by value:The value of a[3] is %d&quot;, a[ 3 ] ); 29 modifyElement( a[ 3 ] ); 30 printf( &quot;The value of a[ 3 ] is %d&quot;, a[ 3 ] ); 31 return 0; 32 } Entire arrays passed call-by-reference, and can be modified Array elements passed call-by-value, and cannot be modified
  • 120. 3.1 Function definitions Program Output Effects of passing entire array call by reference: The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8 Effects of passing array element call by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6 33 34 void modifyArray( int b[], int size ) 35 { 36 int j; 37 38 for ( j = 0; j <= size - 1; j++ ) 39 b[ j ] *= 2; 40 } 41 42 void modifyElement( int e ) 43 { 44 printf( &quot;Value in modifyElement is %d&quot;, e *= 2 ); 45 }
  • 121.
  • 122.
  • 123. 1. Function prototypes 1.1 Initialize array 2. Call functions mean , median , and mode 1 /* Fig. 6.16: fig06_16.c 2 This program introduces the topic of survey data analysis. 3 It computes the mean, median, and mode of the data */ 4 #include <stdio.h> 5 #define SIZE 99 6 7 void mean( const int [] ); 8 void median( int [] ); 9 void mode( int [], const int [] ) ; 10 void bubbleSort( int [] ); 11 void printArray( const int [] ); 12 13 int main() 14 { 15 int frequency[ 10 ] = { 0 }; 16 int response[ SIZE ] = 17 { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 18 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 19 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 20 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 21 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 22 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 23 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 24 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 25 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 26 4, 5, 6, 1, 6, 5, 7, 8, 7 }; 27 28 mean( response ); 29 median( response ); 30 mode( frequency, response ); 31 return 0; 32 }
  • 124. 3. Define function mean 3.1 Define function median 3.1.1 Sort Array 3.1.2 Print middle element 33 34 void mean( const int answer[] ) 35 { 36 int j, total = 0; 37 38 printf( &quot;%s%s%s&quot;, &quot;********&quot;, &quot; Mean&quot;, &quot;********&quot; ); 39 40 for ( j = 0; j <= SIZE - 1; j++ ) 41 total += answer[ j ]; 42 43 printf( &quot;The mean is the average value of the data&quot; 44 &quot;items. The mean is equal to the total of&quot; 45 &quot;all the data items divided by the number&quot; 46 &quot;of data items ( %d ). The mean value for&quot; 47 &quot;this run is: %d / %d = %.4f&quot;, 48 SIZE, total, SIZE, ( double ) total / SIZE ); 49 } 50 51 void median( int answer[] ) 52 { 53 printf( &quot;%s%s%s%s&quot;, 54 &quot;********&quot;, &quot; Median&quot;, &quot;********&quot;, 55 &quot;The unsorted array of responses is&quot; ); 56 57 printArray( answer ); 58 bubbleSort( answer ); 59 printf( &quot;The sorted array is&quot; ); 60 printArray( answer ); 61 printf( &quot;The median is element %d of&quot; 62 &quot;the sorted %d element array.&quot; 63 &quot;For this run the median is %d&quot;, 64 SIZE / 2, SIZE, answer[ SIZE / 2 ] );
  • 125. 3.2 Define function mode 3.2.1 Increase frequency[] depending on response[] 65 } 66 67 void mode( int freq[], const int answer[] ) 68 { 69 int rating, j, h, largest = 0, modeValue = 0; 70 71 printf( &quot;%s%s%s&quot;, 72 &quot;********&quot;, &quot; Mode&quot;, &quot;********&quot; ); 73 74 for ( rating = 1; rating <= 9; rating++ ) 75 freq[ rating ] = 0; 76 77 for ( j = 0; j <= SIZE - 1; j++ ) 78 ++freq[ answer[ j ] ]; 79 80 printf( &quot;%s%11s%19s%54s%54s&quot;, 81 &quot;Response&quot;, &quot;Frequency&quot;, &quot;Histogram&quot;, 82 &quot;1 1 2 2&quot;, &quot;5 0 5 0 5&quot; ); 83 84 for ( rating = 1; rating <= 9; rating++ ) { 85 printf( &quot;%8d%11d &quot;, rating, freq[ rating ] ); 86 87 if ( freq[ rating ] > largest ) { 88 largest = freq[ rating ]; 89 modeValue = rating; 90 } 91 92 for ( h = 1; h <= freq[ rating ]; h++ ) 93 printf( &quot;*&quot; ); 94 Notice how the subscript in frequency[] is the value of an element in response[] ( answer[] ) Print stars depending on value of frequency[]
  • 126. 3.3 Define bubbleSort 3.3 Define printArray 95 printf( &quot;&quot; ); 96 } 97 98 printf( &quot;The mode is the most frequent value.&quot; 99 &quot;For this run the mode is %d which occurred&quot; 100 &quot; %d times.&quot;, modeValue, largest ); 101 } 102 103 void bubbleSort( int a[] ) 104 { 105 int pass, j, hold; 106 107 for ( pass = 1; pass <= SIZE - 1; pass++ ) 108 109 for ( j = 0; j <= SIZE - 2; j++ ) 110 111 if ( a[ j ] > a[ j + 1 ] ) { 112 hold = a[ j ]; 113 a[ j ] = a[ j + 1 ]; 114 a[ j + 1 ] = hold; 115 } 116 } 117 118 void printArray( const int a[] ) 119 { 120 int j; 121 122 for ( j = 0; j <= SIZE - 1; j++ ) { 123 124 if ( j % 20 == 0 ) 125 printf( &quot;&quot; ); Bubble sort: if elements out of order, swap them.
  • 127. Program Output ******** Mean ******** The mean is the average value of the data items. The mean is equal to the total of all the data items divided by the number of data items (99). The mean value for this run is: 681 / 99 = 6.8788   ******** Median ******** The unsorted array of responses is 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8 6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9 6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3 5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8 7 4 4 2 5 3 8 7 5 6 4 5 6 1 6 5 7 8 7   The sorted array is 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9   The median is element 49 of the sorted 99 element array. For this run the median is 7 126 127 printf( &quot;%2d&quot;, a[ j ] ); 128 } 129 }
  • 128. Program Output ******** Mode ******** Response Frequency Histogram   1 1 2 2 5 0 5 0 5   1 1 * 2 3 *** 3 4 **** 4 5 ***** 5 8 ******** 6 9 ********* 7 23 *********************** 8 27 *************************** 9 19 ******************* The mode is the most frequent value. For this run the mode is 8 which occurred 27 times.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133. 1. Initialize variables 1.1 Define functions to take double scripted arrays 1.2 Initialize studentgrades[][] 2. Call functions minimum , maximum , and average 1 /* Fig. 6.22: fig06_22.c 2 Double-subscripted array example */ 3 #include <stdio.h> 4 #define STUDENTS 3 5 #define EXAMS 4 6 7 int minimum( const int [][ EXAMS ], int , int ); 8 int maximum( const int [][ EXAMS ], int , int ); 9 double average( const int [], int ); 10 void printArray( const int [][ EXAMS ], int , int ); 11 12 int main() 13 { 14 int student; 15 const int studentGrades[ STUDENTS ][ EXAMS ] = 16 { { 77, 68, 86, 73 }, 17 { 96, 87, 89, 78 }, 18 { 70, 90, 86, 81 } }; 19 20 printf( &quot;The array is:&quot; ); 21 printArray( studentGrades, STUDENTS, EXAMS ); 22 printf( &quot;Lowest grade: %dHighest grade: %d&quot;, 23 minimum( studentGrades, STUDENTS, EXAMS ), 24 maximum( studentGrades, STUDENTS, EXAMS ) ); 25 26 for ( student = 0; student <= STUDENTS - 1; student++ ) 27 printf( &quot;The average grade for student %d is %.2f&quot;, 28 student, 29 average( studentGrades[ student ], EXAMS ) ); 30 31 return 0; 32 } Each row is a particular student, each column is the grades on the exam.
  • 134. 3. Define functions 33 34 /* Find the minimum grade */ 35 int minimum( const int grades[][ EXAMS ], 36 int pupils, int tests ) 37 { 38 int i, j, lowGrade = 100; 39 40 for ( i = 0; i <= pupils - 1; i++ ) 41 for ( j = 0; j <= tests - 1; j++ ) 42 if ( grades[ i ][ j ] < lowGrade ) 43 lowGrade = grades[ i ][ j ]; 44 45 return lowGrade; 46 } 47 48 /* Find the maximum grade */ 49 int maximum( const int grades[][ EXAMS ], 50 int pupils, int tests ) 51 { 52 int i, j, highGrade = 0; 53 54 for ( i = 0; i <= pupils - 1; i++ ) 55 for ( j = 0; j <= tests - 1; j++ ) 56 if ( grades[ i ][ j ] > highGrade ) 57 highGrade = grades[ i ][ j ]; 58 59 return highGrade; 60 } 61 62 /* Determine the average grade for a particular exam */ 63 double average( const int setOfGrades[], int tests ) 64 {
  • 135. 3. Define functions 65 int i, total = 0; 66 67 for ( i = 0; i <= tests - 1; i++ ) 68 total += setOfGrades[ i ]; 69 70 return ( double ) total / tests; 71 } 72 73 /* Print the array */ 74 void printArray( const int grades[][ EXAMS ], 75 int pupils, int tests ) 76 { 77 int i, j; 78 79 printf( &quot; [0] [1] [2] [3]&quot; ); 80 81 for ( i = 0; i <= pupils - 1; i++ ) { 82 printf( &quot;studentGrades[%d] &quot;, i ); 83 84 for ( j = 0; j <= tests - 1; j++ ) 85 printf( &quot;%-5d&quot;, grades[ i ][ j ] ); 86 } 87 }
  • 136. Program Output The array is: [0] [1] [2] [3] studentGrades[0] 77 68 86 73 studentGrades[1] 96 87 89 78 studentGrades[2] 70 90 86 81 Lowest grade: 68 Highest grade: 96 The average grade for student 0 is 76.00 The average grade for student 1 is 87.50 The average grade for student 2 is 81.75
  • 137. Chapter 7 - Pointers Outline 7.1 Introduction 7.2 Pointer Variable Declarations and Initialization 7.3 Pointer Operators 7.4 Calling Functions by Reference 7.5 Using the const Qualifier with Pointers 7.6 Bubble Sort Using Call by Reference 7.7 Pointer Expressions and Pointer Arithmetic 7.8 The Relationship between Pointers and Arrays 7.9 Arrays of Pointers 7.10 Case Study: A Card Shuffling and Dealing Simulation 7.11 Pointers to Functions
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143. 1. Declare variables 2 Initialize variables 3. Print Program Output The address of a is 0012FF88 The value of aPtr is 0012FF88 The value of a is 7 The value of *aPtr is 7 Proving that * and & are complements of each other. &*aPtr = 0012FF88 *&aPtr = 0012FF88 1 /* Fig. 7.4: fig07_04.c 2 Using the & and * operators */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int a; /* a is an integer */ 8 int *aPtr; /* aPtr is a pointer to an integer */ 9 10 a = 7; 11 aPtr = &a; /* aPtr set to address of a */ 12 13 printf( &quot;The address of a is %p&quot; 14 &quot;The value of aPtr is %p&quot;, &a, aPtr ); 15 16 printf( &quot;The value of a is %d&quot; 17 &quot;The value of *aPtr is %d&quot;, a, *aPtr ); 18 19 printf( &quot;Showing that * and & are inverses of &quot; 20 &quot;each other.&*aPtr = %p&quot; 21 &quot;*&aPtr = %p&quot;, &*aPtr, *&aPtr ); 22 23 return 0; 24 } The address of a is the value of aPtr. The * operator returns an alias to what its operand points to. aPtr points to a , so *aPtr returns a . Notice how * and & are inverses
  • 144.
  • 145.
  • 146.
  • 147. 1. Declare variables 1.1 Declare const pointer to an int 2. Change *ptr (which is x ) 2.1 Attempt to change ptr 3. Output Program Output FIG07_13.c: Error E2024 FIG07_13.c 16: Cannot modify a const object in function main *** 1 errors in Compile *** 1 /* Fig. 7.13: fig07_13.c 2 Attempting to modify a constant pointer to 3 non-constant data */ 4 5 #include <stdio.h> 6 7 int main() 8 { 9 int x, y; 10 11 int * const ptr = &x; /* ptr is a constant pointer to an 12 integer. An integer can be modified 13 through ptr, but ptr always points 14 to the same memory location. */ 15 *ptr = 7; 16 ptr = &y; 17 18 return 0; 19 } Changing *ptr is allowed – x is not a constant. Changing ptr is an error – ptr is a constant pointer.
  • 148.
  • 149.
  • 150. 1. Initialize array 1.1 Declare variables 2. Print array 2.1 Call bubbleSort 2.2 Print array 1 /* Fig. 7.15: fig07_15.c 2 This program puts values into an array, sorts the values into 3 ascending order, and prints the resulting array. */ 4 #include <stdio.h> 5 #define SIZE 10 6 void bubbleSort( int *, const int ); 7 8 int main() 9 { 10 11 int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; 12 int i; 13 14 printf( &quot;Data items in original order&quot; ); 15 16 for ( i = 0; i < SIZE; i++ ) 17 printf( &quot;%4d&quot;, a[ i ] ); 18 19 bubbleSort( a, SIZE ); /* sort the array */ 20 printf( &quot;Data items in ascending order&quot; ); 21 22 for ( i = 0; i < SIZE; i++ ) 23 printf( &quot;%4d&quot;, a[ i ] ); 24 25 printf( &quot;&quot; ); 26 27 return 0; 28 } 29 30 void bubbleSort( int *array, const int size ) 31 { 32 void swap( int *, int * ); Bubblesort gets passed the address of array elements (pointers). The name of an array is a pointer.
  • 151. 3. Function definitions Program Output Data items in original order 2 6 4 8 10 12 89 68 45 37 Data items in ascending order 2 4 6 8 10 12 37 45 33 int pass, j; 34 for ( pass = 0; pass < size - 1; pass++ ) 35 36 for ( j = 0; j < size - 1; j++ ) 37 38 if ( array[ j ] > array[ j + 1 ] ) 39 swap( &array[ j ], &array[ j + 1 ] ); 40 } 41 42 void swap( int *element1Ptr, int *element2Ptr ) 43 { 44 int hold = *element1Ptr; 45 *element1Ptr = *element2Ptr; 46 *element2Ptr = hold; 47 }
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.