Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Homework 1 Homework 2 Homework 3 Homework 4 PowerPoint Presentation
Download Presentation
Homework 1 Homework 2 Homework 3 Homework 4

Homework 1 Homework 2 Homework 3 Homework 4

3 Views Download Presentation
Download Presentation

Homework 1 Homework 2 Homework 3 Homework 4

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. ㅎㅎ Fifth step for Learning C++ Programming • Homework1 • Homework2 • Homework3 • Homework4

  2. HomeWork1 • Exercise 1 • Given the following assignment of variables to values: • Fill in the result values of the conditions in the table below

  3. HomeWork1 • Exercise 2 • Write a program in C++ that performs the following tasks: • Read three integer values using cin. • Determine the maximum of the three values entered by the user. • Print the maximum of this three values using cout. • int main() { int a, b, c; cout << "Enter the first integer : " ; cin >> a; cout << "Enter the second integer : " ; cin >> b; cout << "Enter the third integer : " ; cin >> c ; cout << endl; if((a > b) && (a > c)) { cout << "Maximum integer is " << a <<endl; } elseif((b > c ) && (b > a)) cout << "Maximum integer is " << b <<endl; else cout << "Maximum integer is " << c << endl; return 0; }

  4. HomeWork1 • Exercise 3 • Write a program that asks the user to type numbers. After each entry, the program should report the cumulative sum of the entries. The program should terminate when the user enters 0. • int main() { int a, sum = 0; do { cout << "Enter the number : " ; cin >> a; sum = sum + a; }while( a!=0); cout << "Sum is " << sum << endl; return 0; }

  5. HomeWork1 • Exercise 4 Create a program to determine the GCD (Greatest Common Divisor) of two integers x and y using a ‘while loop’. Formal description of the Euclidean algorithm • Input Two positive integers, a and b. • Output The greatest common divisor, g, of a and b. • Internal computation • If a<b, exchange a and b. • Divide a by b and get the remainder, r. If r=0, report b as the GCD of a and b. • Replace a by b and replace b by r. Return to the previous step.

  6. HomeWork1 int main() { int a, b, r; cout << "Enter the two integers to determine the GCD : "; cin >> a >> b; while((a < 0) || (b < 0)) { cout << "Don't enter the negative number! Please.. enter again : "; cin >> a >> b; } if(a < b) { int temp; temp = a; a = b; b = temp; } do { r = a % b; if( r == 0) { cout << "The GCD of a and b is "<< b << endl; break; } else { a = b; b = r; } } while(1); return 0; } • Exercise 4

  7. HomeWork2 • Exercise 1 1) Write a function that computes the value of the binomial coefficient • 2) Embed your function into a little program that reads two integers n and r from std::cin and writes the value of the binomial coefficient to std::cout • int fac(int x) { if(x <= 0) return 1; else return x * fac(x-1); } int bc(int n, int r) { return fac(n) / (fac(n-r)*fac(r)); } void main() { int n, r; std:: cout << "Enter two natural numbers: "; std:: cin >> n >> r; if(n < 0 && r < 0) { do { std::cout << "Please again enter two natural numbers !! " <<std::endl; std:: cout << "Enter two natrural number "; std::cin >> n >> r; } while(n < 0 || r < 0); } std::cout << "C(" << n << "," << r << ") = " << bc(n, r) << std::endl; }

  8. HomeWork2 • Exercise 2 • Write a function permutNumbers that prints all n! many permutations of the numbers 1 to n on std::out. Example: the output for permutNumbers (3) shall be: 123, 132, 213, 231, 312, 321 void permutNumbers(int data[], int x, int n) { int k, temp; if(x==n-1) { for(k=0; k<n; k++) cout << data[k]; if(k==n) cout << " "; } else { for(k=x; k<n; k++) { temp=data[k]; data[k]=data[x]; data[x]=temp; permutNumbers(data, x+1, n); temp=data[k]; data[k]=data[x]; data[x]=temp; } } }

  9. HomeWork2 • Exercise 2 • Write a function permutNumbers that prints all n! many permutations of the numbers 1 to n on std::out. Example: the output for permutNumbers (3) shall be: 123, 132, 213, 231, 312, 321 int main() { int a, i; int data[MAX]; cout << "Enter one natural number : "; cin >> a; for(i=0; i<a; i++) data[i] = i+1; permutNumbers(data, 0, a); return 0; }

  10. HomeWork3 • Exercise 1 • Given the following function definition: intsum_down(int x) { if (x >= 0) { x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else { return 1; } } a) What is this smallest integer value of the parameter x, so that the returned value is greater than 1.000.000 ? • 19

  11. HomeWork3 • Exercise 1 b) Rewrite the function, so that it is free of recursion. I.e. give an iterative definition on the foundation of a loop-construct.TIP: First transform the recursive definition, so that you have just a single recursive call. int sum_down_while(int x_final) { int sum = 1; int x = 0; while (x <= x_final) { sum = (x - 1) + 2 * sum; x = x + 1; } return sum; }

  12. HomeWork3 • Exercise 1 c) Is it ok to switch the type of the parameter x to unsigned int? Discuss your decision / give an argumentation. int sum_down(unsigned int x) { if (x >= 0) { x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else { return 1; } } Error!! x is would be -1

  13. HomeWork3 • Exercise 1 d) Is it ok to switch the type of the parameter x to double? Discuss your decision / give an argumentation. int sum_down(double x) { if (x >= 0) { x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else { return 1; } } Possible!!

  14. HomeWork3 • Exercise 1 e) Is it ok to switch the function head to int sum_down(const int x)? Discuss your decision / give an argumentation. int sum_down(constint x) { if (x >= 0) { x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else { return 1; } } Error!!

  15. HomeWork4 • Exercise 1. Given the C++ variable declarations below. int m[3][3] = {{2, 3, 6}, {8, 4, 2}, {7, 4, 9}}; int a[] = {10, 20, 30, 40, 50, 60, 70 ,80}; int b[] = {-5, -6, -7, -5, -3, -5, -6}; int *x[2] = {a, b}; int *p = a; int *q = &a[3]; • Evaluate the following C++ expressions and give the results of each expression

  16. HomeWork4

  17. HomeWork4 • Exercise 2 • A rotation of some array A is the cyclic shifting of all elements according to some given offset, Example: Array A: {1, 2, 3, 4, 5, 6, 7, 8} If we rotate the elements of array A by 4 positions, then we get: {5, 6, 7, 8, 1, 2, 3, 4} Create and implement a function void rotate (int a[], int len, int offset) in C++. Meaning of the arguments: a[] the input array, len : the size of array a[] (number of elements in array a, offset : the shifting offset

  18. HomeWork4 • Exercise 2