Professional Documents
Culture Documents
Lec 1: Programming Fundamentals I
Lec 1: Programming Fundamentals I
1. Practical – coding
2. Pointer & Dynamic Arrays
3. Function
Questions
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;
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;
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;
//int arr[rows][columns];
int **arr = new int*[rows];
for(int i=0;i <rows; i++)
{
arr[i]=new int[columns];
}
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
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;
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.
Notes:
Function Format
type name ( parameter1, parameter2, ...) { statements }
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 ( )
-----
function that sum two numbers and print out the result
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)
}
complete program in the next page
using namespace std;
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){
}
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