Universiti Malaysia Perlis

EKT120: COMPUTER PROGRAMMING

LAB MODULE v1.0

LAB 9: ARRAY II

Puan Salina Mohd Asi

Pusat Pengajian Kejuruteraan Komputer Dan Perhubungan

Universiti Malaysia Perlis

Objective:

  1. Introduction
  2. Bubble Sort
  3. Exchange Maximum Sort
  4. Exchange Minimum Sort

1. Introduction:

A fundamental operation on arrays is sorting them, that is arranging the elements such that they are in a specific order usually from minimum value to maximum value.

To do this rearrangement, we need to do a considerable amount of swapping of array element values. The goal of writing sorting algorithms is to do the swapping efficiently.

For this week lab exercise we do a program for sorting. There are three (3) sorting type in the program below. 1. Bubble sort, 2. Exchange Maximum Sort and 3. Exchange Minimum Sort.

Refer to the data initialize at the exercise question for the explaination

2. Bubble Sort

I / j / j+1 / Action / Comment
0 / 0 / 1 / Larger of b[0] and b[1] put into b[1]
1 / 2 / Larger of b[1] and b[2] put into b[2]
2 / 3 / Larger of b[2] and b[3] put into b[3] / b3] is the largest of the four values
1 / 0 / 1 / Larger of b[0] and b[1] put into b[1]
1 / 2 / Larger of b[1] and b[2] put into b[2] / b2] is the largest of three values
2 / 0 / 1 / Larger of b[0] and b[1] put into b[1] / b1] is the largest of two values
3 / No action / b0] is automatically the smallest

3. Exchange Maximum Sort

Refer to the data initialize at the exercise question

i / j / Action / Comment
3 / 3 / max = c[3] / For our data
Max = 44, wheremax = 1, 44 move to c[3] and c[3] value (22) moves to c[1]
2 / if c[2]>max, max = c[2], wheremax = 2
1 / if c[1]>max, max = c[1], wheremax = 1
0 / if c[0]>max, max = c[0], wheremax = 0
2 / 2 / Max = c[2] / For our data, max=33, wheremax=0, 33 moves to c[2] and the c[2] value (11) move to c[0]
For our data, max = 22, wheremax = 1, no further movement needed
1 / If c[1]>max, max = c[1], wheremax = 1
0 / If c[0]>max, max = c[0], wheremax = 0
1 / 1 / If c[1]>max, max = c[1], wheremax = 1
0 / If c[0]>max, max = c[0], wheremax = 0
0 / No action / c[0] automatically the smallest

4. Exchange Minimum Sort

i / j / Action / Comment
0 / 0 / min = c[0] / For our data
Min = 11, wheremin = 2, 11 move to c[0] and c[0] value (33) moves to c[2]
1 / if c[1]<min, min = c[1], wheremin = 1
2 / if c[2]<min, min = c[2], wheremin = 2
3 / if c[3]<min, min = c[3], wheremax = 3
1 / 1 / Max = c[1] / For our data, min=22, wheremin=3, 22 moves to c[1] and the c[1] value (44) move to c[1]
For our data, min = 33, wheremin = 2, no further movement needed
2 / If c[2]<min, min = c[2], wheremax = 2
3 / If c[3]<min, min = c[3], wheremax = 3
2 / 2 / If c[2]<min, min = c[2], wheremax = 2
3 / If c[3]<min, min = c[3], wheremin = 3
3 / No action / c[3] automatically the largest

Exercise 1:

Below is a program on sorting. This program will sort the value in the array A. It sort the values in three way of sorting algorithm, i) buble sort, ii) Exchange Max, iii) Exchange Min. Type the program, compile and run. Then get the output of the program and understand the program.

#include <math.h>

#include <stdio.h>

#define start 0

#define end 4

#define size 10

int main(void) {

int i, j, k, b[size], c[size], d[size];

int temp, max, wheremax = end-1, min, wheremin=start;

int a[end]={33,44,11,22};

/***************************************

** Initialize an array **

***************************************/

for (i=start; i<end; i++)

b[i]=c[i]=d[i]=a[i];

/***************************************

** Buble Sort **

***************************************/

for (i=start; i<end; i++){

for (j=start; j<end-i-1; j++){

if (b[j] > b[j+1]){

temp=b[j+1];

b[j+1]=b[j];

b[j]=temp;

}

}

}

/***************************************

** Exchange Max Sort **

***************************************/

for (i=end; i>start; i--){

max=c[i];

for (j=i; j>=start; j--){

if (max<=c[j]){

max=c[j];

wheremax=j;

}

}

c[wheremax]=c[i];

c[i]=max;

}

/***************************************

** Exchange Min Sort **

***************************************/

for (i=start; i<end;i++){

min=d[i];

for (j=i; j<end; j++){

if (min>=d[j]){

min=d[j];

wheremin=j;

}

}

d[wheremin]=d[i];

d[i]=min;

}

/***************************************

** Printing Results **

***************************************/

for (i=start; i<end; i++)

printf("i=%d, a[i]=%2d, b[i]=%2d, c[i]=%2d d[i]=%2d\n", i, a[i], b[i], c[i], d[i]);

return 0;

}

Q1: Write down the output of the program. Discuss with your group and write down the group comment of the program.

Exercise 2:

Type the below program, compile it and explain your output:

Note: getsis a function in the C standard library, declared in the header file stdio.h, that reads a line from the standard input and stores it in a buffer provided by the caller.

Exercise 3:

Write a program that inputs a line of text with function gets into char array s [100]. Output the line in uppercase letters and lowercase letters.

1