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

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

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

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

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

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

P a g e | 2 Two Dimensional

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

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 

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

P a g e | 3 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

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

Chapter 02

P a g e | 4 Functions

:Prepared By

Dr. Murad Magableh

2013

P a g e | 5 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

: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

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

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

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

P a g e | 6 .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

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

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

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

P a g e | 7 Chapter 03

Pointers

:Prepared By

Dr. Murad Magableh

2013

P a g e | 8 :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* >> {

P a g e | 9 :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 {

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

P a g e | 10 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

P a g e | 11 :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 {

P a g e | 12 Chapter 04

Recursion

:Prepared By

Dr. Murad Magableh

2013

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

Q2: Write a recursive function that prints the numbers between 1 to n in a reverse .order

: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 {

:Q4: Write a recursive function that prints the following shape

:Q5: Write a recursive function that prints the following shape

P a g e | 14 :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 {

Q7: Write a recursive function that calculates and returns the factorial of a number .x

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

P a g e | 15 :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 {

:Q10: Write the following function in a recursive way

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

P a g e | 16 Chapter 05

Strings

:Prepared By

Dr. Murad Magableh

2013

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

.Q2: Write a function that does the same as the function strcat

.Q3: Write a function that does the same as the function strcpy

.Q4: Write a function that does the same as the function strcmp

:Exercises Write a function that does the same as the function strncat .1 Write a function that does the same as the function strncpy .2 Write a function that does the same as the function strncmp .3

P a g e | 18 Chapter 06

File Processing

:Prepared By

Dr. Murad Magableh

2013

P a g e | 19 Q1: Write a program that copies a content of a file into a one-dimensional array. .Assume that the content of the file is integers

Q2: Write a program that copies a content of a one file into another file. Assume .that the content of the file is integers

Q3: Write a program that copies a content of a one file into another file in a reverse .order. Assume that the content of the file is integers

Q4: Assuming that a file contains integer numbers, write a program that reads these :values and calculates the following

The maximum value .1

The minimum value .2

The average of all values .3

How many prime numbers are in the file .4

How many perfect numbers are in the file .5

How many odd values are in the file .6

How many even values are in the file .7

How many negative values are in the file .8

The summation of the positive values .9

The summation of the even negative numbers .10

P a g e | 20