GettingStartedCpp Practice



Comments



Description

C/C++ workshopPractice You should do the high priority (70% score) first and then do another (30% score) Contents Each ½ day, you will do each of below part: Part1: Question 1, 2, 3, 4, 8 (high priority: 4, 8) Part2: Question 5, 6, 7 (high priority: 5, 6, 7) Part3: Question 9, 10, 11, 12 (high priority: 9, 10, 11, 12) Then, commit all to SVN Question 1: Hello world.............................................................................................. 2 Question 2: Variable................................................................................................... 3 Question 3: Primary data type.................................................................................... 4 Question 4: Array....................................................................................................... 5 Question 5: Pointer..................................................................................................... 6 Question 6: String....................................................................................................... 8 Question 7: Struct.................................................................................................... 10 Question 8: Function................................................................................................. 12 Question 9: Class...................................................................................................... 14 Question 10: C/C++ advance features.....................................................................15 Question 11: STL...................................................................................................... 16 Question 12: Convert from Java to C........................................................................17 Notes     Each question has 100 points (100%). Score of each question is depend on how many percent this question has been completed. Score of sub-questions in a question are the same, and counted by 100/number-of-sub-questions. Score of practice is average score of all questions Candidate zip all answers (doc file and zipped-project file in <your_name>.zip 1 Extensions to Delete on Clean. n). args[i]). i++) { printf("param[%d] = %s\n".2 Open project property. Intermediate Directory. char** args) { printf("Number of Param %d\n". i. (use google) Q 1. i < n.Question 1: Hello world Write your answer in word document (includes code) in Q1_<yourname>.1 Use Visual Studio. Try to explain some elements in tab General: Output Directory. 2 . display “hello C/C++” in console screen Q 1. Build Log File. for (int i = 0. code a simple program. and Configuration Type.doc Q 1.h> #include <iostream> int main(int n.3 Modified entry point like: #include <stdio. calculate(i). printf("%d %d\n". limit frame-rate to 5.cpp. Each frame. print to console screen current time. In another. HINT:  use clock(). In main .h. char** args) { int i.4 Code a program: make an infinity loop.} system("pause").cpp. Q 1. calculate(j).doc Q 2. time() of time. he needs to print value of myVar into screen.cpp. } Try to run.value. we use three ways:  Global variable  Static variable inside function  Static variable outside function Implements those ways. and watch the result in console screen and explain. static int value. And he tries the following 3 .3 A programmer codes his program with 2 files: main.cpp and another. j. } Look at the result and explain why? Q 2.2 For counting number of times a function has been called. and compare Q 2.h Question 2: Variable Write your answer in word document (includes code) in Q2_<yourname>. he declares a global variable named myVar with initial value is 10. } int main(int n. value = val. count++).  use sleep() of windows.1 Implement the following void calculate(int val) { static int count=10. Try to give a solution to get j’s value equal to i’s value. int j = c.cpp  Declare myVar in another. printf("%d". Q 2. explain the reason why the third is OK and try to implement it.ways:  Declare myVar in another. He’s got problem with the first and second way. } Question 3: Primary data type Write your answer in word document (includes code) in Q3_<yourname>.cpp.cpp. what will happen. If replacing “char c” with “unsigned char c”. and print it. } Q 3.cpp and print it  Declare myVar in another. int main(int n.2 4 . j).h> const int xyz. declare myVar with extern keyword. Explain the result. (does not used unsigned char) #include <stdio.cpp. The third is run well. char** args) { int i = 140. char c = i.doc Q 3. and in print it in main() in main.cpp. char** args) { xyz = 0.h> #include <iostream> int main(int n. In main. Supposing that “i” is always from 0 to 255.1 (*)     Run the following code. system("pause"). and explain the result. and why? Set “i” value to 260. Please explain what happen with the first and second.4 Detect & solve problem of the following code: #include <stdio. xyz). Declare again myVar in main. printf("%d". h> #include <iostream> int main(int n. &a). i++) { a[i] = i*10. The program should put into c the appending of b to a. The program must compute and write the number of integers greater or equal to 10.1 Write program to ask the user to type 10 integers of an array. char** args) { int a[10]. *a + 1.doc Q 4. for (int i = 0. char** args) { int i = 5. i--. the latter 10 from b.3 (*) Explain the result: #include <stdio. i < 10. Then the program should display c. } printf("0x%x 0x%x 0x%x\n". a and b.4 5 . &a + 1). Q 4. *a. c is an array with 20 integers. a. and explain the result. *(a + 1). (a + 1).  Use memcpy  Without memcpy Q 4. } Q 4. } } Question 4: Array Write your answer in word document (includes code) in Q4_<yourname>.h> #include <iostream> int main(int n. the first 10 integers of c from array a. printf("0x%x 0x%x 0x%x 0x%x\n". while (i = 5) { printf("Inside loop\n").2 Write a program which takes 2 arrays of 10 integers each. #include <stdio.Run the following code.  void Invert(int input[]. 2. For example: a[5] = {100. input array is not changed Question 5: Pointer Write your answer in word document (includes code) in Q5_<yourname>. int num_element): invert input array. cout << *ptr_a << " " << *ptr_b << "\n". ptr_b = ptr_a. *ptr_b = 9. ptr_a = new int. 6 . cout << *ptr_a << " " << *ptr_b << "\n". delete ptr_a. 3}  sort(a. typedef int *IntPtrType. ptr_a = NULL. delete ptr_a. ptr_a = ptr_b. int output[]): generate output array from inverted input. 6. int num_element. 3. Note: after executing this function. *ptr_a = 3. int offset.1 (*) Explain the result #include <stdio. 1. int count) Q 4. 6} void BubbleSort(int arr[]. cout << *ptr_a << " " << *ptr_b << "\n". ptr_b.doc Q 5. int main() { IntPtrType ptr_a. cout << *ptr_a << " " << *&*&*&*&*ptr_b << "\n".Implement bubble sort algorithm (sort acceding) to sort an array from “offset” with “count” elements. cout << *ptr_c << " " << **ptr_c << "\n". 2. 4. ptr_c = &ptr_a. *ptr_b = *ptr_a.h> #include <iostream> using namespace std. 4)  {100. *ptr_c.5 (*) Implement functions to invert member of an array  void Invert(int input[]. 4. ptr_b = new int. 2 Introduce int variables x and y and int* pointer variables p and q. z. *r. p. *p. 2. Print with labels the values of x. y. 1. The address of x and the value of x. Execute the swap code: z = x. p to the address of x. Set p. Set x to 2. Use the Hex function to print all pointer/address values and format the output so it is easy to make comparisons. Set x. r. 4. 4. z. void main() { int a[4] = {1. 3. 7 . r. Set x. Execute the swap code: r = p. y.5 (*) Draw diagrams to explain the results: #include <iostream> using namespace std. *q. Print the message: Swapping pointers. z to three distinct values. q. *p. *q. y. The value of p and the value of *p. 3. r to the addresses of x. 3. q. *p.3 Introduce int variables x. q. y to 8. y. y. The value of q and the value of *q. and q to the address of y. *r. q. 2. 1. z to three distinct values. r. Set p. *q. 2. q = r. 2. q. r. p. q. r. 3. z respectively. *q. y = z. 5. The address of y and the value of y. y. Draw diagrams to explain the results Q 5. y. p. *p. y. Draw diagrams to explain the results Q 5. p. y. z and int* pointer variables p. p = q. q. Print the message: Swapping values. r to the addresses of x. y.4 Introduce int variables x. r. z and int* pointer variables p. Print with labels the values of x. z respectively. Then print the following information: 1. Print with labels the values of x. *r. z. 4. The address of q (not its contents!). Q 5. Print with labels the values of x. The address of p (not its contents!).return 0. *r. 4}. x = y. } Q 5. 6. q. z. *(a + 1).int *p = a. &a). delete p. &p). 3. a. (p + 1). &a + 1). b) ((a) < (b) ? (a):(b)) void main() { int *p = new int[COUNT]. i++) { a[i] = i. } Q 5. p. delete p2. *a.6 (*) Detect and solve problems of following program: void main() { int a[4] = {1.8 (*) Detect and solve problems of following program: #include <iostream> using namespace std. 2. *p + 1. } for (int i = 0. int a[COUNT]. Write a demo Q 5. printf("0x%x 0x%x 0x%x\n". i < sizeof(a). *a + 1. (a + 1). 4}. delete a. printf("0x%x 0x%x 0x%x\n". *(p + 1). &p + 1).7 (*)  Why should we use delete?  When we use delete?  Difference between delete and delete[]. int *p2 = new int. i++) { 8 . for (int i = 0. printf("0x%x 0x%x 0x%x 0x%x\n". } Q 5. printf("0x%x 0x%x 0x%x 0x%x\n". #define COUNT 10 #define MAX(a. i < sizeof(a). int *p = a. *p. by using:    char *st = “Hello world” char st[] = {‘H’. ‘e’. char *output) Q 6. i++) { printf("%d %d\n". char *output. int output_offset) o input: input string o offset: starting position for copy o length: length of substring to be copied o invert: invert the result o output: output string o output_offset: the beginning of copied position of output for example: st = “Hello world” st2 = “My name is C++” copyString(st. Q 6. 6. Implement this function in 3 ways:  using memcpy  using strcpy  without memcpy and strcpy 9 . a[i].p[i] = i. 5.doc Q 6. } for (int i = 0. st2. true. } } Question 6: String Write your answer in word document (includes code) in Q6_<yourname>. …. bool invert.2 Implement a function to clone a string:  void clone(char *input. int length. i < MAX(sizeof(a). 11)  st2 = “My name is dlr”.} char *st = new char[…] …. sizeof(p)).3 (**) Implement a function to copy a string: void copyString(char *input. int offset. p[i]).1 Print a string “Hello world” to screen.  Implement a function to input the student information (check if duplicated student ID. day. print size of struct in two above cases and explain the result. Q 7.1.12 (unsigned char)  Year: xxxx (unsigned int) Implement:  Define struct of date  Get date of system and save to a struct instance  Print struct information as format: o dow. day. dd-mm-yyyy o dd/mm/yyyy o mm-dd-yyyy o dow dd. (List of student is stored in an array).1. Implement:  Print size of struct  Declare year in second (dow. Zip and send your project Q 7.31 (unsigned char)  Month: 0..Question 7: Struct Use visual studio.4 (*) Student information include  name (char *)  id (int)  score (float) (0 to 10) Implement:  Struct of student  A class room is included ten students..  Implement function check valid date (valid of dow. month). month year) Q 7. and value of score must be in range)  Sort list of student by score.3 (**) Use struct of 7. Draw diagrams to explain the result  Build project with 1 byte alignment.1 Date information includes:  Day of week : 0 for Sunday and 6 for Saturday (unsigned char)  Day: 0. day.yy Q 7. Implement:  Constructor with dow. year. and print size of struct. month.mm. named your project as Q7_<yourname>. 10 . year.2 (*) Use struct of 7. and value of score must be in range) Sort list by score Remove all students get score less than 5 11 . } Student. int ID. Zip and send your project. struct _Student *next. Same as 7. students are stored in a linked-list structure: typedef struct _Student { char *Name.4. however. Named your project as Q7_5_<yourname>.    Input student information until ID is equal to zero (check if duplicated student ID.Q 7.5 (**) Use visual studio. float Score. create new project. void sort(int *arr. print to screen “hello world” and that string  A string.doc Q 8.3.2 (*) Implement a sort function (bubble sort) with unknown order preference (ascending or descending).} int CheckSmaller(int a. int (*order) (int. The order preference is decided by a pointer function parameter. and a bool: If bool value is true. Same requirement as Q9.Question 8: Function Write your answer in word document (includes code). Named your project as Q10_4_<yourname>. print to screen “hello world” and modified string. int b) {return a>b. sort function is implemented in another file:  “sort_cpp. int count. int)). int CheckBigger(int a. in Q9_<yourname>.3 (**) Use visual studio. however. create new project. (bool parameter has default value is false).1 Implement a function function SayHello. to print screen “Hello world”  No parameter  A string.cpp” (use extern)  “sort_c. Zip and send your project.} Q 8. int b) {return a<b. Q 8.c” (use extern “C”) 12 . convert to string to capital. B). &B).m_pVal. %d\n". ClassA tmp = A. %d\n". B. printf("%d %d Todo4(A. } void main() { ClassA *A = new ClassA(10).m_pVal. B. ClassA *&B) { ClassA *tmp = A. }. C->m_pVal). ClassA &B) { { ClassA *tmp = A. void Todo1(ClassA *A. C->m_pVal). A = B. C->m_pVal). B. B).4 (*) Introduce classA and several functions. B = tmp.m_pVal.m_pVal. printf("%d %d Todo1(A.m_pVal. B = tmp. C->m_pVal). A = B. } 13 . A->m_pVal. A->m_pVal. ClassA *C = &B. ClassA B(0). C->m_pVal). B = tmp. A = B. Draw diagrams to explain the results: class ClassA { public: ClassA(int val = 0) {m_pVal = val. B = tmp. printf("%d %d Todo2(*A. A = B. B. A->m_pVal. ClassA *B) void Todo2(ClassA &A. } void Todo4(ClassA *&A. B. A->m_pVal. %d\n".Q 8. printf("%d %d Todo3(*A. printf("%d %d %d\n".} int m_pVal. C). } } void Todo3(ClassA A. %d\n". A->m_pVal. ClassA B) { ClassA tmp = A. Node* next. PHYS.virtual] PrintInfo: display information o [public] Get/set name. birthday. 14 . Zip and send your project. ENGLISH}  Methods: o [public] PrintInfo: display teacher information (+ people and staff information) o [public] Get/set class.1 Introduce class for people:  Properties o [protected] Name: char* o [protected] Birthday: struct Date o [protected] ID code: int  Methods: o [public . ENGLISH} o [private] Second subject: enum {MATHS. }. Q 9. subject. PHYS. ID code Introduce class for staff: (inherit from people)  Properties o [protected] Department ID: int o [protected] Joined Date: structDate o [protected] Level: int  Methods: o [public . second subject All people are stored in a linked-list: class Node { public: People* data. score o [private] Calculate average score o [public] Get average score Introduce class for teacher: (inherit from people and staff)  Properties o [private] Class: int o [private] Subject: enum {MATHS. joined date. number of project. create new project.virtual] PrintInfo: display information (+ people information) o [public] Get/set department ID.Question 9: Class Use visual studio. Named your project as Q10_<yourname>. level Introduce class for student: (inherit from people)  Properties o [private] Grade: int o [private] Number of project: int o [private] Score of each project: float*  Methods: o [public] PrintInfo: display student information (+ people information) o [public] Get/set grade. 5 teacher. Zip and send your project. staffs. and student last  Print information of all students have had average score more than 5. teachers. students are created (use static member)  In node. (When delete student. Named your project as Q11_2_<yourname>. define a template class node 15 .1 Write your answer in word document (includes code) in Q11_1_<yourname>.Implement above introduction.1. and 5 student  Sort list group by type: staff first. T* list) Q 10.0 Q 9. Use 10.2 Implements:  Implements counting number of peoples.doc Define a template function to calculate average values of an array: template <class R. Make sure class memory was clean up. teacher next. using copy constructor for each element Q 9. class T> R Average(int count.4 (*) Implements:  Implement static methods remove element for node. Q 9. memory of people MUST BE clean too – use virtual destructor and println to check when an instance is destroyed).5 (**) Implements:  Save a list to of student to file  Load list of student from file Question 10: C/C++ advance features Q 10. Q 9. implement static function to add element  Add to list 5 staff. create new project.3 (*) Implements:  Implement copy constructor  Clone current list.2 Use visual studio. template <class T> class Node { public: T* data. Named your project as Q12_<yourname>. Zip and send your project.4 Use visual studio. support out of bound exception.  Supposing that we have an instance of teacher. staff. staff. Use 10. Node* next. create new project. create new project. }. division by zero exception: o float Average(int count. Named your project as Q11_4_<yourname>.1 Implement 10. student. Zip and send your project. but use STL list instead of Node structure. try to cast to o People o Teach By using explicit casting. Named your project as Q11_3_<yourname>. (Test by call all member methods after casting) Q 10. Support:   Add element (people. create new project. Implement methods:  Add a node  Remove a node  Remove all nodes Q 10.1. teacher) Modified people. int offset. student. dynamic/static casting. float *arr) Question 11: STL Use visual studio. Zip and send your project. Implement  Divide function. Q 11.1. teacher information 16 . support division by zero exception  Calculate average of a list.3 Use visual studio. try to cast to o Staff o People  Supposing that we have an instance of student. create new project. Attach your makefile into Q12_<yourname> package Question 12: Convert from Java to C Use visual studio. check memory clean up Use VLD to detect leak Q 11. private int total. // to indicate failure } } /** * remove an item by obeying LIFO rule */ public Object pop() 17 .2 Compile 12. Named your project as Q13_<yourname>. Zip and send your project.  Remove element. Q 12. // increment item counter return true. // to indicate success } else { return false. // set number of items to zero } /** * add an item to the array */ public boolean push(Object obj) { if ( isFull() == false) // checks if space in stack { stack[total] = obj.1 Convert the following code from Java to C++ Note: Object in Java is same as void* in C++ /* class Stack */ public class Stack { private Object[] stack . // create array total = 0. // to track number of items public Stack(int size) { stack = new Object[size]. // add item total++.1 by using GNU GCC through Cygwin. } else { return false. // to indicate failure } } /* * checks if array is empty */ public boolean isEmpty() { if (total ==0) { return true. } else { return false. // return item } else { return null. // update total return obj. // get last item stack[total-1]= null. // ith item at position i-1 } /** * return the number of items in the array */ 18 . } } /** * checks if array is full */ public boolean isFull() { if (total ==stack.{ if (isEmpty() == false) // check stack is not empty { // reduce counter by one Object obj = stack[total-1].length) { return true. } } /** * returns the item at index i */ public Object getItem(int i) { return stack[i-1]. // remove item from array total--. public int getTotal() { return total. write an C/C++ demo with stack of integer 19 . } } Q 12.1.2 Use 13.
Copyright © 2024 DOKUMEN.SITE Inc.