Chapter 01

Arrays

:Prepared By

Dr. Murad Magableh

2013 One Dimensional

Q1: Write a program that declares two arrays of integers and fills them from the .user. Then exchanges their values and display the resulted two arrays

:Solution

()void main } ;[int arr1[10], arr2[10 ;"cout << "Enter the first array values\n (++for (int i=0; i<10; i ;[cin >> arr1[i ;"cout << "Enter the second array values\n (++for (int i=0; i<10; i ;[cin >> arr2[i

;int temp (++for (int i=0; i<10; i } ;[temp = arr1[i ;[arr1[i] = arr2[i ;arr2[i] = temp {

;"cout << "The first array values now are:\n (++for (int i=0; i<10; i ;" " >> [cout << arr1[i ;cout << endl

;"cout << "The second array values now are:\n (++for (int i=0; i<10; i ;" " >> [cout << arr2[i ;cout << endl {

P a g e | 2 Q2: Write a program that takes 10 integers as input and prints the largest integer .and its location in the array

:Solution

()void main } ;[int arr[10 ;"cout << "Enter the array values\n (++for (int i=0; i<10; i ;[cin >> arr[i

;int max = arr[0], loc = 0, i (++for (i=1; i<10; i } (if (arr[i] > max } ;[max = arr[i ;loc = i {

{

cout << "The maximum value is: " << max ;and it is in location " << loc << endl " >>

{

P a g e | 3 Q3: Write a program that defines a one dimensional array of integers of size 10 and fill it from the user. Then find the negative values and store them in another array .and display new array

:Solution

()void main } ;[int arr[10 ;"cout << "Enter the array values\n (++for (int i=0; i<10; i ;[cin >> arr[i

;int new_arr[10], index = 0 (++for (int i=0; i<10; i (if (arr[i] < 0 ;[new_arr[index++] = arr[i

;"cout << "The new array values are:\n (++for (int i=0; i> [cout << new_arr[i ;cout << endl {

P a g e | 4 Q4: Write a program that defines a one dimensional array of integers of size 11 and fill the first 10 values from the user. Then calculate the summation of the 10 values .and store it in the 11th location

:Solution

()void main } ;[int arr[11 ;arr[10] = 0 ;"cout << "Enter the array values\n (++for (int i=0; i<10; i } ;[cin >> arr[i ;[arr[10] += arr[i {

;"cout << "The new array values are:\n (++for (int i=0; i<11; i ;" " >> [cout << arr[i ;cout << endl {

P a g e | 5 Q5: Write a C++ program that reverses the order of a one-dimensional array of size .10 without using another array

:Solution

()void main } ;{int x, j=9, a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10

}(++for(int i=0; i<(10/2); i ;[x = a[i ;[a[i] = a[j ;a[j] = x ;--j {

(++for(int i=0; i<10; i ;" " >> [cout << a[i ;cout << endl {

P a g e | 6 Q6: Write a C++ program that declares two arrays of size 5. Read the first array from the user and store the factorial of each element in this array in its corresponding .location in the second array

:Solution

()void main } ;[double a[5], b[5 ;int fact ;"cout << "Enter the first array values\n (++for(int i=0; i<5; i } ;[cin >> a[i ;fact = 1 (++for(int j=1; j<=a[i]; j ;fact *= j ;b[i] = fact {

;"cout << "The factorials array values are:\n (++for (int i=0; i<5; i ;" " >> [cout << b[i ;cout << endl {

P a g e | 7 Two Dimensional

Q7: Write a program that adds up two [4x4] arrays and stores the sum in a third .array

:Solution

()void main } ;[int a[4][4], b[4][4 ;"cout << "Enter the first array values\n (++for(int i=0; i<4; i (++for(int j=0; j<4; j ;[cin >> a[i][j

;"cout << "Enter the second array values\n (++for(int i=0; i<4; i (++for(int j=0; j<4; j ;[cin >> b[i][j

;[int c[4][4 (++for(int i=0; i<4; i (++for(int j=0; j<4; j ;[c[i][j] = a[i][j] + b[i][j

;"cout << "The array values are:\n (++for(int i=0; i<4; i } (++for(int j=0; j<4; j ;" " >> [cout << c[i][j ;cout << endl { ;cout << endl {

P a g e | 8 Q8: Write a C++ program that declares a two dimensional array of size [4x4] and :generates the values of its elements using conditional statements as the following

The main diagonal contains 0 in all its locations  The upper triangle contains 1 in all its locations  The lower triangle contains 2 in all its locations 

:Solution

}()void main ;[int a[4][4 (++for(int i=0; i<4; i (++for(int j=0; j<4; j } ;if(i

;"cout << "The array values are:\n (++for(int i=0; i<4; i } (++for(int j=0; j<4; j ;" " >> [cout << a[i][j ;cout << endl { ;cout << endl {

P a g e | 9 Q9: Write a program that defines a two-dimensional array of integers of size 4*4. The program will fill the array of values using the equation array_name[i][j] = i+j+2 (i refers to the row index, j refers to the column index). Then, define a one-dimensional array of size 4. The one-dimensional array should be filled with the .values along the main diagonal of the two-dimensional array

:For example

:If the two-dimensional array is

V4 V3 V2 V1

V8 V7 V6 V5

V12 V11 V10 V9

V16 V15 V14 V13

:The one-dimensional array will be

V16 V11 V6 V1

:Solution

}()void main ;[int a[4][4 (++for(int i=0; i<4; i (++for(int j=0; j<4; j ;a[i][j] = i+j+2 ;int b[4], index=0 (++for(int i=0; i<4; i (++for(int j=0; j<4; j (if(i==j } ;[b[index] = a[i][j ;++index { {

P a g e | 10 Q10: Write a program that defines a two-dimensional array of integers of size 10*10. The program will fill each location of the array by its index summation (array[i][j] = i+j). Then print the summation of the elements on the array circumference as shown :below

:Solution

}()void main ;[int a[4][4 (++for(int i=0; i<4; i (++for(int j=0; j<4; j ;a[i][j] = i+j ;int sum = 0 (++for(int i=0; i<4; i (++for(int j=0; j<4; j (if(i==0 || j==0 || i==9 || j==9 ;[sum += a[i][j

;cout << "The result is: " << sum << endl {

P a g e | 11 Q11: Write a program that stores the grades of 5 students in a two-dimensional array. Each student has 3 marks. Each row will represent a student and the last cell in the row will store the calculated average for the student's marks. Finally, display the .average of all student averages

:Solution

}()void main ;float a[5][4], sum (++for(int i=0; i<5; i } ;sum = 0 cout << "Enter the 3 marks for the ;student number: " << i+1 << endl (++for(int j=0; j<3; j } ;[cin >> a[i][j ;[sum += a[i][j { ;a[i][3] = sum/3 {

":cout << "The students marks and averages are ;endl >> (++for(int i=0; i<5; i } (++for(int j=0; j<4; j ;" " >> [cout << a[i][j ;cout << endl { ;cout << endl

;sum = 0 (++for(int i=0; i<5; i ;[sum += a[i][3

" :cout << "The AVG for all sudents is ;float)sum/5 << endl) >> {

P a g e | 12 Chapter 02

Functions

:Prepared By

Dr. Murad Magableh

2013

P a g e | 13 Q1: Write a program to take a depth (in kilometers) inside the earth as input data; compute and display the temperature at this depth in degrees Celsius and degrees :Fahrenheit, The relevant formulas are

Celsius = 10 (depth) + 20 

Fahrenheit = 1.8 (Celsius) + 32 

:Include two functions in your program

Function celsius_at_depth should compute and return the Celsius temperature at a .1 .depth measured in kilometers

.Function fahrenheit should convert a Celsius temperature to Fahrenheit .2

:Solution

(float celsius_at_depth(float depth } ;(return ( 10 * depth + 20 {

(float fehrenheit(float celsius } ;(return ( 1.8 * celsius + 32 {

()void main }

;float depth, celsius ;"cout << "Enter the depth inside the earth:\n ;cin >> depth ;(celsius = celsius_at_depth(depth " :cout << "The temperature at this depth in degree Celsius is ;celsius << endl >> " :cout << "The temperature at this depth in degree Fehrenheit is ;fehrenheit(celsius) << endl >> {

P a g e | 14 :Q2: Write a program to find the following using functions

(Sphere surface area (4 π r2 .1 (Sphere volume (4/3 π r3 .2

NOTE: Use functions to find powers of the radius

:Solution

(float power2(float radius } ;(return ( radius * radius {

(float power3(float radius } ;(return ( radius * radius * radius { (float surface_area(float radius } ;((return ( 4 * 3.14 * power2(radius {

(float volume(float radius } ;((return ( 4.0/3.0 * 3.14 * power3(radius {

()void main } ;float radius ;" :cout << "Enter the radius of the sphere ;cin >> radius ;cout << "The surface area is: " << surface_area(radius) << endl ;cout << "The volume is: " << volume(radius) << endl

{

P a g e | 15 Q3: Write a program (using a function) that takes a positive number with a fractional part and rounds it to two decimal places. For example, 32.4851 would round to 32.49, and 32.4431 would round to 32.44

:Solution

(float round(float number } ;int num = number * 100 ;++if ( (number*100-num) >= 0.5) num ;return num/100.0 {

()void main } ;float number ;" :cout << "Enter a number ;cin >> number " :cout << "The round to two decimal places is ;round(number) << endl >> {

P a g e | 16 Q4: Write a program (using a function) that takes a positive integer number and displays .PRIME if the number is prime and NOT PRIME otherwise

NOTE: a prime number is an integer that has no integral factors but itself and 1

:Solution

(void isPrime(int x } ;int flag = 0 (++for(int i=1; i<=x; i ;++if(x%i==0) flag ;if(flag<=2) cout << "Prime" << endl ;else cout << "Not Prime" << endl {

()void main } ;int number ;" :cout << "Enter a number ;cin >> number ;(isPrime(number {

P a g e | 17 Q5: Write a function that takes a positive integer number and returns TRUE if the number is prime and FALSE otherwise. Use the function in the main to display PRIME if the number .is prime and NOT PRIME otherwise

NOTE: a prime number is an integer that has no integral factors but itself and 1

:Solution

(bool isPrime(int x } ;int flag = 0 (++for(int i=1; i<=x; i ;++if(x%i==0) flag ;if(flag<=2) return true

???return false; // "else" is not necessary here. WHY {

()void main } ;int number ;" :cout << "Enter a number ;cin >> number ;if(isPrime(number)) cout << "Prime" << endl ;else cout << "Not Prime" << endl {

P a g e | 18 .Q6: Write a function that displays the perfect numbers between 1 and 1000

NOTE: A perfect number is a positive integer that is equal to the sum of its positive integral .factors, including 1 but excluding itself

:Solution

(bool isPerfect(int x } ;int flag = 0 (++for(int j=1; j

()void perfect_list } ;int sum = 0, index = 1 (++for(int i=1; i<=1000; i ((if(isPerfect(i " :cout << "Perfect number no. " << index++ << " is ;i << endl >> {

()void main } ;()perfect_list {

P a g e | 19 Q7: Write a function that takes a salary of an employee and increases it by 100 (Just to .(introduce the concept of Call-By-Reference

:(Solution 01 (Call-By-Reference

(void sal_inc(int &sal } ;sal += 100 {

()void main } ;int salary ; " :cout << "Enter a salary ;cin >> salary ;(sal_inc(salary ;cout << "The salary after the increment is: " << salary << endl {

:(Solution 01 (Call-By-Value

(int sal_inc(int sal } ;return sal + 100 {

()void main } ;int salary ; " :cout << "Enter a salary ;cin >> salary ;(salary = sal_inc(salary ;cout << "The salary after the increment is: " << salary << endl {

P a g e | 20 Q8: Write a function that takes a one-dimensional array and its size, and then fill each .location in the array by the factorial of the location's index

NOTE: The factorial of an integer number is the product of that integer and all the .integers below it

:Solution

(int fact(int x } ;int f = 1 (++for(int j=1; j<=x; j ;f *= j ;return f {

(void fill_array(int a[], int size } a[0] = 1; // The factorial of Zero is 1 (++for(int i=1; i

()void main } ;{int a[5] = {2, 4, 1, 9, -3 ;cout << "The array before calling the function: " << endl (++for(int i=0; i<5; i ;" " >> [cout << a[i

;(fill_array(a, 5

;cout << "\nThe array after calling the function: " << endl (++for(int i=0; i<5; i ;" " >> [cout << a[i {

P a g e | 21 Q9: Write a function that takes a one-dimensional array and its size, and then returns the .summation of prime numbers in the array

:Solution

(bool isPrime(int x } ;int flag = 0 (++for(int i=1; i<=x; i ;++if(x%i==0) flag ;if(flag<=2) return true

???return false; // "else" is not necessary here. WHY {

(int array_sum(int a[], int size } ;int sum = 0 (++for(int i=0; i

()void main } ;{int arr[10] = {1, 2, 3, 17, 5, 4, 8, 11, 20, 30 " :cout << "The summation of prime numbers in this array is ;array_sum(arr, 10) << endl >> {

P a g e | 22 Chapter 03

Pointers

:Prepared By

Dr. Murad Magableh

2013

P a g e | 23 :Q1: Write the output of the following code

()void main } ;int a = 8 ;int b = 4 ;int *p, *q ;p = &a ;q = &b " " >> cout << a << " " << b ;p << " " << *q << endl* >> ;p+=12* ;++a " " >> cout << a << " " << b ;p << " " << *q << endl* >> ;p = 100* ;q = 200* " " >> cout << a << " " << b ;p << " " << *q << endl* >> ;int * t ;t = q ;q = p ;p = t " " >> cout << a << " " << b ;p << " " << *q << endl* >> ;t = &a ;p = t ;q = t " " >> cout << a << " " << b ;p << " " << *q << endl* >> {

:Solution

4 8 4 8

4 21 4 21

200 100 200 100

100 200 200 100

100 100 200 100

P a g e | 24 :Q2: Write the output of the following code

> cout << *p ;p-- { ;cout << endl ;p = a (++for(int i=0; i<5; i ;" " >> [cout << p[i ;cout << endl (++for(int i=0; i<5; i ;" " >> [cout << a[i ;cout << endl (--for(int i=4; i>=0; i ;" " >> (cout << *(p+i ;cout << endl (for(int i=1; i<5; i+=2 ;" " >> (cout << *(p+i ;cout << endl (++for(int i=0; i<5; i ;" " >> cout << *p ;cout << endl {

:Solution

4

10

2 4 6 8 10

10 8 6 4 2

10 8 6 4 2

2 4 6 8 10

P a g e | 25 8 4

2 2 2 2 2

Q3: Write a program that reads a size of a one-dimensional array from the user. The size will be passed to a function that creates an array of this size. The function will also fill the array elements with the square of each element's index. Finally, the .function will print the array

:Solution

(void dynamic_array_size(int size } ;[int *arr = new int[size (++for(int i=0; i> [cout << arr[i ;cout << endl {

()void main } ;int s ;" :cout << "Please insert the array size ;cin >> s ;(dynamic_array_size(s {

P a g e | 26 Q4: Write a program that reads 10 integers from the user and stores them in a one- dimensional array. Then, store the odd values in another array. The size of the new array should be exactly the same as the number of the odd values (No empty .(locations is allowed in the new array

:Solution

()void main } ;[int a[10 ;int count = 0 ;cout << "Please enter 10 values:" << endl (++for(int i=0; i<10; i } ;[cin >> a[i ;++if(a[i]%2==1) count { ;[int *new_arr = new int[count ;int index = 0 (++for(int i=0; i<10; i (if(a[i]%2==1 ;[new_arr[index++] = a[i

(++for(int i=0; i> [cout << new_arr[i ;cout << endl {

P a g e | 27 :Q5: Write the output of the following code

()void main } ;{int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;int *front, *rear ;[front = a; // OR front = &a[0 ;[rear = &a[9 (while(front<=rear } ;int t = *front ;front = *rear* ;rear = t* ;--rear ;++front { (++for(int i=0; i<10; i ;" " >> [cout << a[i ;cout << endl {

:Solution

1 2 3 4 5 6 7 8 9 10

P a g e | 28 Chapter 04

Recursion

:Prepared By

Dr. Murad Magableh

2013

P a g e | 29 .Q1: Write a recursive function that prints the numbers between 1 to n

:Solution

(void print_numbers(int n } ;(if(n>1) print_numbers(n-1 ;" " >> cout << n {

()void main } ;int num ;" :(cout << "Please insert the number (n ;cin >> num ;(print_numbers(num ;cout << endl {

P a g e | 30 Q2: Write a recursive function that prints the numbers between 1 to n in a reverse .order

:Solution

(void print_numbers(int n } ;" " >> cout << n ;(if(n>1) print_numbers(n-1 {

()void main } ;int num ;" :(cout << "Please insert the number (n ;cin >> num ;(print_numbers(num ;cout << endl {

P a g e | 31 :Q3: What is the output of the following code

(void print_numbers(int n } ;" " >> cout << n ;(if(n>1) print_numbers(n-1 ;" " >> cout << n {

()void main } ;(print_numbers(5 ;cout << endl {

:Solution

5 4 3 2 1 1 2 3 4 5

P a g e | 32 :Q4: Write a recursive function that prints the following shape

:Solution

(void print_stars(int n } ;(if(n>1) print_stars(n-1 (++for(int i=1; i<=n; i ;"*" >> cout ;cout << endl {

()void main } ;(print_stars(5 ;cout << endl {

P a g e | 33 :Q5: Write a recursive function that prints the following shape

:Solution

(void print_stars(int n } (++for(int i=1; i<=n; i ;"*" >> cout ;cout << endl ;(if(n>1) print_stars(n-1 {

()void main } ;(print_stars(5 ;cout << endl {

P a g e | 34 :Q6: What is the output of the following code

;int global = 5

(void print_stars(int n } (++for(int i=1; i<=(global-n); i ;"*" >> cout ;cout << endl ;(if(n>=1) print_stars(n-1 (++for(int i=1; i<=(global-n); i ;"*" >> cout ;cout << endl {

()void main } ;(print_stars(4 ;cout << endl {

:Solution

P a g e | 35 Q7: Write a recursive function that calculates and returns the factorial of a number .x

:Solution

(int fact(int x } ;if(x==1) return 1 ;(return x*fact(x-1 {

()void main } ;cout << fact(5) << endl {

P a g e | 36 Q8: Write a recursive function that receives a one-dimensional array and reverses .the order of its elements

:Solution

(void reverse_array(int a[], int front, int rear } ;(if(front<(rear-1)) reverse_array(a, front+1, rear-1 ;[int t = a[front ;[a[front] = a[rear ;a[rear] = t {

()void main } ;{int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (++for(int i=0; i<10; i ;" " >> [cout << arr[i ;cout << endl

;(reverse_array(arr, 0, 9

(++for(int i=0; i<10; i ;" " >> [cout << arr[i ;cout << endl {

P a g e | 37 :Q9: What is the output of the following code

void reverse_array(int a[], int front, int rear, int (size } ;(if(front<(rear-1)) reverse_array(a,front+1,rear-1,size ;[int t = a[front ;[a[front] = a[rear ;a[rear] = t (++for(int i=0; i> [cout << a[i ;cout << endl {

()void main } ;{int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;(reverse_array(arr, 0, 9, 10 {

:Solution

10 9 8 7 5 6 4 3 2 1

10 9 8 4 5 6 7 3 2 1

10 9 3 4 5 6 7 8 2 1

10 2 3 4 5 6 7 8 9 1

1 2 3 4 5 6 7 8 9 10

P a g e | 38 :Q10: Write the following function in a recursive way

(int power(int x, int y } ;int result = x (++for(int i=1; i

:Solution

(int power(int x, int y } ;if(y==1) return x ;(return x*power(x, y-1 {

()void main } ;cout << power(2, 3) << endl {

P a g e | 39 Chapter 05

Strings

:Prepared By

Dr. Murad Magableh

2013

P a g e | 40 Q1: Write a function that receives a string and returns its length without using the .strlen function

:Solution

(int my_strlen(char *s } ;int c = 0 ;++while(s[c] != '\0') c ;return c {

()void main } ;"char *s1 = "Philadelphia University ;cout << my_strlen(s1) << endl {

P a g e | 41 .Q2: Write a function that does the same as the function strcat

:Solution

(char* my_strcat(char *s1, char *s2 } ;int s2_index = 0 (++for(int i=strlen(s1); i

()void main } ;"char s1[100] = "Philadelphia ;"char s2[100] = "University ;cout << my_strcat(s1, s2) << endl {

P a g e | 42 .Q3: Write a function that does the same as the function strcpy

:Solution

(char* my_strcpy(char *s1, char *s2 } ;int i (++for(i=0; i $now.offset().top) { pfLoading = true; endPage++; if (endPage > totalPage) return; var imgEle = new Image(); var imgsrc = "//data.docslib.org/img/635e624fd83b0a7207c175b330204f1b-" + endPage + (endPage > 3 ? ".jpg" : ".webp"); imgEle.src = imgsrc; var $imgLoad = $('

'); $('.article-imgview').append($imgLoad); imgEle.addEventListener('load', function () { $imgLoad.find('img').attr('src', imgsrc); pfLoading = false }); if (endPage < 7) { adcall('pf' + endPage); } } }, { passive: true });