Part 1

advertisement
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.
|Page2
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:
V1
V2
V3
V4
V5
V6
V7
V8
V9
V10
V11
V12
V13
V14
V15
V16
V6
V11
V16
The one-dimensional array will be:
V1
|Page3
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.
|Page4
Chapter 02
Functions
Prepared By:
Dr. Murad Magableh
2013
|Page5
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:
1. Function celsius_at_depth should compute and return the Celsius temperature at a
depth measured in kilometers.
2. Function fahrenheit should convert a Celsius temperature to Fahrenheit.
Q2: Write a program to find the following using functions:
1. Sphere surface area (4 π r2)
2. Sphere volume (4/3 π r3)
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
|Page6
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.
|Page7
Chapter 03
Pointers
Prepared By:
Dr. Murad Magableh
2013
|Page8
Q1: Write the output of the following code:
#include<iostream>
using namespace std;
void main()
{
int a = 8;
int b = 4;
int *p, *q;
p = &a;
q = &b;
cout << a << "
<< *p <<
*p+=12;
a++;
cout << a << "
<< *p <<
*p = 100;
*q = 200;
cout << a << "
<< *p <<
int * t;
t = q;
q = p;
p = t;
cout << a << "
<< *p <<
t = &a;
p = t;
q = t;
cout << a << "
<< *p <<
}
" << b << " "
" " << *q << endl;
" << b << " "
" " << *q << endl;
" << b << " "
" " << *q << endl;
" << b << " "
" " << *q << endl;
" << b << " "
" " << *q << endl;
|Page9
Q2: Write the output of the following code:
#include<iostream>
using namespace std;
void main()
{
int a[5] = {2, 4, 6, 8, 10};
int *p;
p = a;
if(a==p) p++;
cout << *p << endl;
p+=3;
cout << *p << endl;
for(int i=100; i<105; i++)
{
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 onedimensional 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:
#include<iostream>
using namespace std;
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:
#include<iostream>
using namespace std;
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:
#include<iostream>
using namespace std;
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:
#include<iostream>
using namespace std;
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<size; 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<y; i++)
result *= x;
return result;
}
| 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:
1. Write a function that does the same as the function strncat
2. Write a function that does the same as the function strncpy
3. Write a function that does the same as the function strncmp
| 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:
1. The maximum value
2. The minimum value
3. The average of all values
4. How many prime numbers are in the file
5. How many perfect numbers are in the file
6. How many odd values are in the file
7. How many even values are in the file
8. How many negative values are in the file
9. The summation of the positive values
10. The summation of the even negative numbers
| P a g e 20
Download