Download presentation
Presentation is loading. Please wait.
Published byJaana Uotila Modified over 6 years ago
1
ㅎㅎ Fifth step for Learning C++ Programming Pointers Homework solution
Review
2
A pointer is a variable that holds the address of something else.
Pointers A pointer is a variable that holds the address of something else. ... MEMORY 1 2 3 4 5 81345 81346 81347 Address int foo; int *x; foo = 123; x = &foo; foo 123 x 3 10/2013
3
Assigning a value to a dereferenced pointer
A pointer must have a value before you can dereference it (follow the pointer). int *x; *x=3; int foo; int *x; x = &foo; *x=3; x doesn’t point to anything!!! ERROR!!! this is fine x points to foo 10/2013
4
[ practice 1 first pointer variable ]
5
[ explain 1 first pointer variable ]
6
[ practice 2 initialize a pointer ]
7
[ explain 2 initialize a pointer ]
8
[ practice 3 Arrays of Pointers ]
9
[ explain 3 Arrays of Pointers ]
= {a, b};
10
Exercise 1 HomeWork1 Condition Result (x>y) && !y
Given the following assignment of variables to values: Fill in the result values of the conditions in the table below Condition Result (x>y) && !y (item>MIN)||(DAY!=’M’) 1 ((num*128)<power)&&y (!(power!=MAX))&&(Sens==num) ((y+x)<num)||(DAY==’M’) (Sens*(!y))!=0 (!x||y)&&(!y||x) 10/2013
11
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; } else if((b > c ) && (b > a)) cout << "Maximum integer is " << b <<endl; else cout << "Maximum integer is " << c << endl; return 0; 10/2013
12
Exercise 3 HomeWork1 int main() { int a, sum = 0; do
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; } 10/2013
13
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. 10/2013
14
Exercise 4 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 : "; } 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 b = r; } while(1); return 0; Exercise 4 10/2013
15
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; 10/2013
16
Exercise 2 HomeWork2 void permutNumbers(int data[], int x, int n) {
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); 10/2013
17
cout << "Enter one natural number : "; cin >> a;
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/2013
18
Exercise 1 HomeWork3 19 Given the following function definition:
int sum_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 ? 19 10/2013
19
while (x <= x_final) sum = (x - 1) + 2 * sum; x = x + 1; }
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; 10/2013
20
HomeWork3 Exercise 1 X c) Assume that we change the parameter x to a reference parameter. So we get: int sum_down(int &x) Adapt your solution for b), so that it shows once again the same behaviour like the recursive definition. 10/2013
21
Exercise 1 Error!! x is would be -1 HomeWork3
d) 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 10/2013
22
Exercise 1 Possible!! HomeWork3
e) 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!! 10/2013
23
Exercise 1 Error!! HomeWork3
f) Is it ok to switch the function head to int sum_down(const int x)? Discuss your decision / give an argumentation. int sum_down(const int x) { if (x >= 0) x = x - 1; int y = x + sum_down(x); return y + sum_down(x); } else return 1; Error!! 10/2013
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.