0% found this document useful (0 votes)
3 views

Lab Lecture 5 (1)

The document outlines a lab lecture on programming fundamentals, focusing on arrays, pointers, searching and sorting algorithms, and matrix calculations. It includes a case study for a C++ program to compute student grades, details on search algorithms like linear and binary search, and sorting techniques such as selection and bubble sort. Additionally, it covers dynamic array allocation and provides exercises related to pointers and their outputs.

Uploaded by

kumalaadugna04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Lecture 5 (1)

The document outlines a lab lecture on programming fundamentals, focusing on arrays, pointers, searching and sorting algorithms, and matrix calculations. It includes a case study for a C++ program to compute student grades, details on search algorithms like linear and binary search, and sorting techniques such as selection and bubble sort. Additionally, it covers dynamic array allocation and provides exercises related to pointers and their outputs.

Uploaded by

kumalaadugna04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

ASTU

CSEg 1104 Fundamentals of Programming

Lab Lecture #5

2022

Computer Science & Engineering Program


The School of EE & Computing
Adama Science & Technology University
ASTU

• Arrays and Pointers Practice


– Case Study: [Data Processing] Students’ Grading
– Searching and Sorting
– Matrix Calculation
– Dynamic Array Allocation
– Pointer: Output Exercises

2
Case Study: [Data Processing] Students’ Grading ASTU

• Your professor has asked you to write a C+


+ program that determines grades at the
end of the semester.
• For each student, identified by an integer number
between 1 and 60, four exam grades must be kept,
and two final grade averages must be computed.
• The first grade average is simply the average of all
four grades.
• The second grade average is computed by
weighting the four grades as follows:
• The first grade gets a weight of 0.2, the second
grade gets a weight of 0.3, the third grade gets a
weight of 0.3, and the fourth grade gets a weight of
0.2. That is, the final grade is computed as follows:

3
ASTU

• Using this information, construct a


60-by-7 two-dimensional array,
– the first column is used for the student
number, the next four columns for the
grades, and the last two columns for the
computed final grades.
• The program’s output should be a
display of the data in the completed
array.

4
ASTU

• For testing purposes, the professor


has provided the following data:
Student Grade 1 Grade 2 Grade 3 Grade 4
1 100 100 100 100

2 100 0 100 0

3 82 94 73 86

4 64 74 84 94

5 94 84 74 64

5
Searching and Sorting ASTU

• Sorting: Arranging data in ascending


or descending order for some
purpose
• Searching: Scanning through a list of
data to find a particular item

6
Search Algorithms ASTU

• Searches can be faster if the data is in


sorted order
• Two common methods for searching:
– Linear search
– Binary search
• Linear search is a sequential search
– Each item is examined in the order it
occurs in the list
• Average number of comparisons
required to find the desired item is n/2
for a list of n items
7
Linear Search ASTU

• Each item in the list is examined in


the order in which it occurs
• Not a very efficient method for
searching
• Advantage is that the list does not
have to be in sorted order
• On average, the number of required
comparisons is n/2, where n is the
number of elements in the list

8
Linear Search ASTU

• Pseudocode for a linear search

9
Linear Search ASTU

10
Binary Search ASTU

• Binary search requires that the list is


stored in sorted order
• Desired item is compared to the
middle element, with three possible
outcomes:
– Desired element was found: finished
– Desired element is greater than the
middle element, so discard all elements
below
– Desired element is less than the middle
element, so discard all elements above
11
Binary Search ASTU

• Pseudocode for a binary search

12
Binary Search ASTU

13
Binary Search ASTU

14
Binary Search ASTU

• On each pass of binary search, the


number of items to be searched is
cut in half
• After p passes through the loop,
there are n/(2p) elements left to
search

15
Linear and Binary Search ASTU

16
Sort Algorithms ASTU

• Two major categories of sorting


techniques exist
– Internal sort: Use when data list is
small enough to be stored in the
computer’s memory
– External sort: Use for larger data sets
stored on external disk
• Internal sort algorithms
– Selection sort
– Exchange sort

17
Selection Sort ASTU

• Smallest element is found and


exchanged with the first element
• Next smallest element is found and
exchanged with the second element
• Process continues n-1 times, with
each pass requiring one less
comparison

18
Selection Sort ASTU

• Pseudocode for a selection sort

19
Selection Sort ASTU

20
Selection Sort ASTU

21
Selection Sort ASTU

• Selection sort advantages :


– Maximum number of required moves is
n-1
– Each move is a final move
• Selection sort disadvantages:
– n(n-1)/2 comparisons are always
required
– Order of magnitude of selection sort:
O(n2)

22
Exchange (Bubble) Sort ASTU

• Successive values in the list are


compared
• Each pair is interchanged if needed to
place them in sorted order
• If sorting in ascending order, the largest
value will “bubble up” to the last position
in the list
• Second pass through the list stops
comparing at second-to-last element
• Process continues until an entire pass
through the list results in no exchanges
23
Exchange (Bubble) Sort ASTU

• Pseudo code for an exchange sort

• Convert to a c++ program


24
Exchange (Bubble) Sort ASTU

• Number of comparisons
O(n2)
• Maximum number of comparisons
n(n-1)/2
• Maximum number of moves
n(n-1)/2
• Many moves are not final moves

25
Matrix Calculation ASTU

26
Matrix Calculation ASTU

27
Matrix Calculation ASTU

28
Assignment II ASTU

• Write a c++ program to do all types


of matrix multiplications, including
transpose. Use functions to divide
your code into manageable chunks

29
Dynamic Array Allocation ASTU

• As each variable is defined in a program,


sufficient storage for it is assigned from a pool of
computer memory locations made available to
the compiler
• After memory locations have been reserved for a
variable, these locations are fixed for the life of
that variable, whether or not they are used
• An alternative to fixed or static allocation is
dynamic allocation of memory
• Using dynamic allocation, the amount of storage
to be allocated is determined or adjusted at run
time
– Useful for lists because allows expanding or contracting
the memory used
30
Dynamic Array Allocation ASTU

• new and delete operators provide


the dynamic allocation mechanisms
in C++

31
Dynamic Array Allocation ASTU

• Dynamic storage requests for scalar


variables or arrays are made as part
of a declaration or an assignment
statement
– Example:
int *num = new int; // scalar
– Example:
int *grades = new int[200];// array
• Reserves memory area for 200 integers
• Address of first integer in array is value of
pointer variable grades
32
Dynamic Array Allocation Example ASTU

33
Pointer: Output Exercises ASTU

• Determine the Output of the following code


fragments in the table cells
int *p; int *secret; int a=3,b=5;
int *q; int j ; int *p1,*p2;
p=new int; secret=new int[10]; p1=&b;
q=p; secret[0]=10; p2=&a;
*p=46; for(j=1;j<10;j++) cout<<*(&b)
*q=39; secret[j]=secret[j- <<" "<<*p2<<endl;
cout<<*p<<""<< 1]+5; p1=p2;
*q for(j=0;j<10;j++) cout<<*p1<<"
<<endl; cout<<secret[j]<<" "<<*p2<<endl;
"; cout<<a<<" "<<b<<endl;
cout<<endl; *p2=4;
*p1=*p2
cout<<a<<" "<<b;

34
Pointer: Output Exercises ASTU

35

You might also like