You are on page 1of 12

University of Zakho Semester : 3rd Semester

Faculty of Science Course:Object Oriented Programming


Department of Computer Science Academic Year :2020-2021

Lec 1 : Programming Fundamentals I


 variables
 input/output , comments
 operations
 pointer I
 if & if else
 switch
 for, while & do-while
 arrays

Lec 2 : Programming Fundamentals II

1. Practical – coding
2. Pointer & Dynamic Arrays
3. Function

Questions

1. Write a program which will reverse digits of an integer number:


before int n= 873274; after n (472378)

2. Write a program which will sort digits of an integer number:


before int n= 873274; after n = 234778 )
initial values

3. Write a program to find which two neighboring digits are even in an array?
Ex: 40,24 ,5,8,19 ,5, 10, 8,86,13
Sol: (40,24) , (10, 8) , (8,86)

4. Write a program which will remove any given number from an integer ar-
ray?
int [ ]arr=Remove( {4,3,5,3,6,5,4,5} , 3); after arr ({4,5,6,5,4,5})

Lecturer
Wahab Kh. Arabo Page 1
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

Dynamic Array

A dynamic array is quite similar to a regular array, but its size is modifiable
during program runtime. Dynamic Array elements occupy a contiguous block
of memory.

#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of items:" << "\n";
cin >>n;
int *arr = new int[n];

cout << "Enter " << n << " items" << endl;

for (int i = 0; i < n; i++) {


cout << "Item " << i << " :" ;
cin >> arr[i];
}

cout << "You entered: ";


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}

address values
arr+0 *(arr+0) is 55 arr[i] is equivalent to *(arr+i)
arr+1 *(arr+1) is 33 So
arr+2 *(arr+2) is 22 instead-of cin>>arr[i]
arr+3 *(arr+3) is 11 you can use cin>>*(arr+i)
arr+4 *(arr+4) is 44
Arr+5 or Arr+(n-1) *(arr+5) is 66

Lecturer
Wahab Kh. Arabo Page 2
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

#include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of items:" << "\n";
cin >>n;
int *arr = new int[n];

cout << "Enter " << n << " items" << endl;

for (int i = 0; i < n; i++) {


cout << "Item " << i << " :" ;
cin >>*(arr+i);
}

cout << "You entered: ";


for (int i = 0; i < n; i++) {
cout << *(arr+i) << " ";
}
return 0;
}
to free memory delete []arr;

2D Dynamic Array

int **arr;
int **arr = new int*[R]; or arr= new int*[R]; //creating
rows
then
Where R is the number or rows , And C is
for(int i=0;i <rows; i++) the number or columns. R & C are inte-
gers , can be set in runtime
{
arr[i]=new int[C];
//set the length of ith row
}

Lecturer
Wahab Kh. Arabo Page 3
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

Example:
#include<iostream>
using namespace std;
int main() {
int rows ,columns;
cout << "Enter the number of rows:";
cin >>rows;

cout << "Enter the number of columns:";


cin>>columns;

//int arr[rows][columns];
int **arr = new int*[rows];
for(int i=0;i <rows; i++)
{
arr[i]=new int[columns];
}

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < columns; j++)
{
cout << "Enter a number :";
cin>>arr[i][j];
}
}

cout << "You entered: \n";


for (int i = 0; i < rows; i++)
to de-allocate the dynamically allocated 2D
{ array using delete operator is as follows
for (int j = 0; j < columns; j++)
{ for(int i = 0; i < rows; i++)
cout <<arr[i][j]<<" "; {
delete [] arr[i]; // release ith row
} }
cout<<endl; delete [] arr; // release row

Lecturer
Wahab Kh. Arabo Page 4
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

return 0;
}

in 2D Dynamic array the number of items can be vary from row to another.
int **arr = new int*[3]; //3 rows
arr[0] =new int[10]; //10 items
arr[1] =new int[5]; // 5 items
arr[2] =new int[3]; //3 items

int rows ,columns;


cout << "Enter the number of rows:";
cin >>rows;
int **arr = new int*[rows];
int *lengths = new int[rows];
for(int i=0;i <rows; i++)
{
cout << "Enter the number of columns for row "<<i<<":";
cin>>columns;
lengths[i]=columns;
arr[i]=new int[columns];
}

for (int i = 0; i < rows; i++)


{
for (int j = 0; j < lengths[i]; j++)
{
cout << "Enter a number :";
cin>>arr[i][j];
}

cout << "You entered: \n";


for (int i = 0; i < rows; i++)
{
for (int j = 0; j < lengths[i]; j++)
{
cout <<arr[i][j]<<" ";

Lecturer
Wahab Kh. Arabo Page 5
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

}
cout<<endl;

#include<iostream>
using namespace std;
int main() {
int len;
cout << "Enter the array size:";
cin >>len;
int *arr=new int[len];
for(int i=0;i<len;i++)
{
cout << "Enter Value "<<i<<" : ";
cin>>*(arr+i);
}
cout << "Values : ";
for(int i=0;i<len;i++)
{
cout << *(arr+i)<<" ";
}
cout<<endl;

int remove_value;
cout << "Which value you want to remove:";
cin >>remove_value;
int count=0;
for(int i=0;i<len;i++)
{
if(remove_value==arr[i])
{
count++;
}

}
int index=0;
int *temp=new int[len-count];
for(int i=0;i<len;i++)
{

Lecturer
Wahab Kh. Arabo Page 6
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

if(arr[i]!=remove_value)
{
temp[index]=arr[i];
index++;
}
}
If you remove this line delete [ ]arr;
delete []arr;
arr=temp; It will cause Memory Leak
len=len-count;
A memory leak is the loss of available memory space
cout << "Values : "; that occurs when dynamic data is allocated but never
de-allocated.
Example :
for(int i=0;i<len;i++) int* ptr = new int;
{ *ptr = 8;
cout << *(arr+i)<<" "; int* ptr2 = new int;
} *ptr2 = -5;
cout<<endl; ptr = ptr2;
return 0;
}

Q:

1- write a program to fill a dynamic array with all prime integers between Min
and Max;

2- create a 2D dynamic array


fill the first row with the letters(character) of your name
fill the second row with the letters(character) of your father name
fill the 3rd row with the letters(character) of your mother name
the find the most frequent character in the array.

Lecturer
Wahab Kh. Arabo Page 7
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

Function or Method

A function is a block of code which only runs when it is called. data can be
passed into a function, these data , known as parameters. Functions are used to
perform certain actions, and they are important for reusing code: Define the
code once, and use it many times.

Why to Use Methods?

 Better Structured Program and More Readable Code


 Avoid Duplicated Code
 Code Reuse

Notes:

Function Format
type name ( parameter1, parameter2, ...) { statements }

Type of data the will be returned by the function


it could be any datatype ( int , double ,bool ,….etc.)

The name of the function (functions should be carefully named )


GetAverage for example is a good name for a function that retrieve the
average of student .
A name like GetA is not clear for such function

Parameters: represents the information that pass/give to the function . Each


parameter consists of a data type specifier followed by an identifier, The differ-
ent parameters are separated by commas. , Parameters should be carefully
named.

Statements is the function's body. It is a block of statements surrounded by


braces{ }.

Lecturer
Wahab Kh. Arabo Page 8
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

examples:

void SayHi10Times( )
{
for(int i=0;i<10;i++)
{
cout<<”Hi”;
}
}

Function type : void it means this function will not return anything

name : SayHi10Times

parameters: no parameters ( )

and the body (statements) of function is


for(int i=0;i<10;i++)
{
cout<<”Hi”;
}

-----
function that sum two numbers and print out the result

void Sum(int x, int y)


{

int s=x+y; //s is called local variable


cout<<s;
}

Note the list of Parameters : int x, int y

Lecturer
Wahab Kh. Arabo Page 9
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

to write a function that find and return the maximum number between any 2
numbers, here we can easily determine the input and output of this
program(function)

input is 2 numbers : the parameters

output is a number : the type of function

best name for describing this function is Max

so the function name is Max

int Max( int n1 , int n2 )


{
if(n1 > n2)
return n1;
else
return n2;
}
when the return type of function is set to any type then the function must end
with return statement
return statement ( return a specified value ,and terminate the function)
-----------------------------------
Note the error in the following function:
bool isEven(int number)
{

if(number%2==0) //is even


{
return true;
}
//here if the number is not even what is the return value

}
complete program in the next page
using namespace std;

bool isEven(int number)


{

Lecturer
Wahab Kh. Arabo Page 10
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

if(number%2==0)
{
return true;
}
else
{
return false;
}

int main() {
int N=5;
bool b= isEven(N); // here is calling the function
if( b==true){

cout<<" N is even ";


}
else
{
cout<<" N is odd ";

}
return 0;
}

Note, Ho the value the produce by function is used in main , through storing it
in a local variable b. it also can be used as

if( isEven(N){
cout<<" N is even ";
}
else {
cout<<" N is odd ";
}

Lecturer
Wahab Kh. Arabo Page 11
University of Zakho Semester : 3rd Semester
Faculty of Science Course:Object Oriented Programming
Department of Computer Science Academic Year :2020-2021

Function Notes:

Lecturer
Wahab Kh. Arabo Page 12

You might also like