solution to midterm exam 1. what is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0;...
TRANSCRIPT
![Page 1: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/1.jpg)
Solution to Midterm Exam
• 1. What is the output?#include <stdio.h>main(){ int i, j, k;
i = 2; j = 5; k = 0;
if(i == j) printf("i equals j\n"); else { while(j>k) { printf("%d\n", j); --j; ++k; } }}
543
![Page 2: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/2.jpg)
Solution to Midterm Exam
• 2. What is printed?#define VAL 4#include <stdio.h>main(){ int a[3];
a[0] = 1; a[1] = -1; a[2] = VAL;
printf("%d\n", a[0] < 1 || a[1] != 1);
printf("%d\n", !a[1]); printf("%d\n", a[0] | a[1]); printf("%d\n", a[a[0]+1]);}
1
0
-14
![Page 3: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/3.jpg)
Solution to Midterm Exam
• 3. Write a program that read standard input character by character, and print out the characters to the standard output, replacing spaces ' ' by underscores '\_'. You can use while loop, getchar(), and putchar() functions.
#include <stdio.h>main(){ int c;
while( (c=getchar()) != EOF) { if(c == ' ') putchar('_'); else putchar(c); }}
![Page 4: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/4.jpg)
Quizzes
• What is the output?
#include <stdio.h>main(){ char *s = "ABC xyz"; printf("%s\n", s+4); printf("%c\n", s[4]); printf("%c\n", *s); printf("%s\n", &s);}xyzxAP^U
![Page 5: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/5.jpg)
Quizzes
• What is the output?
#include <stdio.h>int i;void func(int j, int *p, int a[]);main(){ int j, k, *p, a[1]; i = 1; j = 2; k = 3; p = &k; a[0] = 0; func(j, p, a); printf("%d, %d, %d, %d, %d\n", i, j, k, *p,
a[0]);}void func(int j, int *p, int a[]){ i += 10; j += 10; *p += 10; a[0] += 10; p = &i;}
11, 2, 13, 13, 10
![Page 6: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/6.jpg)
Quizzes
• Write a function (with a name of your choice) which take a string and returns an int. If the string looks the same when read forward and backward, return 1, else return 0.
#include <string.h>int palindrome(char *s){ char *e;
e = s + strlen(s) -1; while( s < e ) { if (*s != *e) return 0; ++s; --e; } return 1;}
![Page 7: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/7.jpg)
Looking Toward C++
• Object-Oriented Design is characterized by– Objects (data together with
operations on the data)– Classes (abstract
characterizations of objects)
– Inheritance (code sharing)– Polymorphism (run-time
binding of operations to objects)
![Page 8: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/8.jpg)
Objects• The object-oriented programming
emphasizes objects, which consist of data and operations on the data. For example, we may have a figures like circle or line object with the operation of drawing the figures. If c is a circle object, we invoke its draw method (operation) by passing a message to c:– c.draw()
• While the traditional programming typically call a function like– draw(CIR)
• where CIR is a flag to indicate which figure to draw.
![Page 9: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/9.jpg)
Classes and Abstract Data Type
• A class is an abstract characterization of a set of objects. A class defines variable (data) and methods (operations) common to a set of objects.
• An abstract data type associates operations on the type but hides detail implementation.
![Page 10: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/10.jpg)
Class example• The following code declares a
class string:
class string { char data[80];public: void store(char*); int length();};
• The char data[80] is the data, store() and length() are methods. Only the two methods are accessible to the outside world. The data are private.
![Page 11: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/11.jpg)
Create Objects and Invoke Method
string s, t;• defines two object s and t
belonging to the class string.• Data member is referenced
by writings.xand the method f is invoked
by writing s.f(arguments)
• If ptr is a pointer to s, we use– s -> x or s -> f(arguments)
![Page 12: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/12.jpg)
Pass a Message to Object
class string { char data[80];public: void store(char *); int length();}
string s, t;
s.store("Hi Mom");len = s.length();
The user of the string class need not to know the implementation details.
![Page 13: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/13.jpg)
Inheritance
• A new class can be defined based on an old class. All the data and associated methods becomes part of the new class.
class pen { int x, y; int status;public: void set_status(int); void set_location(int, int);};
class colored_pen: public pen { int color;public: void set_color(int);};
![Page 14: Solution to Midterm Exam 1. What is the output? #include main() { int i, j, k; i = 2; j = 5; k = 0; if(i == j) printf("i equals j\n"); else { while(j>k)](https://reader036.vdocuments.us/reader036/viewer/2022082613/5697bf811a28abf838c858bb/html5/thumbnails/14.jpg)
Polymorphism
• Poly : Greek word meaning many.
• Morphism : Greek word meaning form.
• Polymorphism mean many forms. In object-oriented programming, polymorphism refers to identically named methods that have different behavior depending on the type of object that they reference.