Hamid (Lab 13)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Department of Computing

Topic: Sorting

CS-110: Fundamentals of Computer Programming

Mr. Jaudat Mamoon

CS-113 Introduction to Programming


Learning Objectives
After completing this section you will be able to:

1. Use arrays to solve computer problems like sorting

Lab Tasks

1. Read 10 random numbers from the user into an array. Sort the data in ascending order
using the simplest form of bubble sort algorithm that you learned during the previous
lecture. Present the sorted data on the screen.

2. The bubble sort algorithm is inefficient for large arrays. Make the following simple
modifications to improve the performance of the bubble sort.

a) After the first pass, the largest number is guaranteed to be in the


highest-numbered element of the array; after the second pass, the two
highest numbers are “in place,” and so on. Instead of making nine
comparisons on every pass, modify the bubble sort to make eight
comparisons on the second pass, seven on the third pass and so on.

b) The data in the array may already be in the proper order or near-proper
order, so why make nine passes if fewer will suffice? Modify the sort to
check at the end of each pass if any swaps have been made. If none has
been made, then the data must already be in the proper order, so the
program should terminate. If swaps have been made, then at least one
more pass is needed.

Your program output should look like the following sample output:

CS-113 Introduction to Programming Page 2


3. Read 10 random numbers from the user into an array. Sort the data in ascending order
using the selection sort algorithm. Look over internet how the selection sort algorithm
works. Present the sorted data on the screen.

4. In statistics mean, median, and mode are three very important terminologies that are used
to analyze the results of surveys and opinion polls. Consider the following set of data :

2, 10, 5, 6, 2, 8, 4, 2, 10, 5, 8

The mean is the arithmetic average of the given values, where the values are summed
first and then divided by the total number of values. The mean for the example data
would be sum (2, 10, 5, 6, 2, 8, 4, 2, 10, 5, 8) / 11 which is 6.

The median is the middle value after the given values have been sorted into an ascending
order. When the number of elements is even, the median should be calculated as the
mean of the two middle elements.The medianfor the exmaple data is 5 which is the
middle element of the sorted data 2,2,2,4,5,5,6,8,8,10,10

The mode is the value that occurs most frequently among all the given values. The mode
for the example data is 2 which is the most frequent value with a frequency of 3.

CS-113 Introduction to Programming Page 3


a) Write the missing code for the function sort.

void sort (int arr[SIZE]) {

int i,hold,j;//hold, holds a value for swapping


for(i=0;i<SIZE;i++)
{
scanf_s("%d",&arr[i]);//Enter your number
}
for(i = 0;i<SIZE-1;i++)//i for passes
{
for(j=0;j < SIZE-i-1;j++)
{
if(arr[j]>arr[j+1])//comparison of consecutive
values
{
hold = arr[j];//swapping
arr[j]=arr[j+1];
arr[j+1] = hold;
}
}
}
}

b) Write the missing code for the function mean.

int mean (int arr[SIZE]) {

int sum,i;
sort(arr);
for(i=0;i<SIZE;i++)
{
sum = sum + arr[i];
printf("%d",arr[i]);
}
return sum/SIZE;

CS-113 Introduction to Programming Page 4


c) Write the missing code for the function mode.

int mode (int arr[SIZE]) {

int sum,i,frequency[10]={0},freq,value=0;
sort(arr);//sorted array
for(i=0;i<SIZE;i++)
{
++frequency[arr[i]];
}
freq = frequency[0];
for(i=1;i<=9;i++)//Numbers are from 0 to 9
{
if(freq<frequency[i])//comparison of occurrence
{
freq = frequency[i];
value = i;
}
}
printf("mod occurs at %d times\n",freq);
return value;

d) Write the missing code for the function median.

int median (int arr[SIZE]) {


int sum,i;
sort(arr);//sorted array
return SIZE/2;
}

e) Now, Using the above defined functions, write a program that generates a 99-
element array of random numbers between 1 and 100 (inclusive) and prints on
screen its mean, median, and the mode.

CS-113 Introduction to Programming Page 5


“Task 1”
Code 1:
//Hamid Muzaffar Khan
//CMS ID : 356209
#include<stdio.h>
#define SIZE 10
int main()
{
int i, arr[SIZE], j, hold;
for (i = 0; i<SIZE; i++)
{
scanf_s("%d", &arr[i]);//Take the array elements
}
for (i = 0; i<SIZE - 1; i++)
{
int swap = 0;//Count no. of swapping
for (j = 0; j < SIZE - i - 1; j++)
{
if (arr[j]>arr[j + 1])
{
hold = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = hold;
}
}
}
printf("Data Items in ascending order\n");
for (i = 0; i <= 9; i++)
{
printf("%d\t", arr[i]);//sorted array
}
puts("");
return 0;
}

CS-113 Introduction to Programming Page 6


Output:

Task 2
Code:
//Hamid Muzaffar Khan
//CMS ID : 356209
#include<stdio.h>
#define SIZE 10
int main()
{
int i, arr[SIZE], j, hold, count;
for (i = 0; i<SIZE; i++)
{
scanf_s("%d", &arr[i]);//Take the array elements
}
for (i = 0; i<SIZE - 1; i++)
{
int swap = 0;//Count no. of swapping
for (j = 0; j < SIZE - i - 1; j++)
{
if (arr[j]>arr[j + 1])
{
hold = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = hold;
swap++;//Swap no. 1

CS-113 Introduction to Programming Page 7


}
}
if (swap == 0)//if no swap
{
break;
}
printf("%d pass : ", i + 1);
for (count = 0; count<SIZE - i; count++)
{
printf("%d", arr[count]);//after every swap the condition of array
}
puts("");
}
printf("Data Items in ascending order\n");
for (i = 0; i <= 9; i++)
{
printf("%d\t", arr[i]);//sorted array
}
puts("");
return 0;
}

Output:

“Task 3”

CS-113 Introduction to Programming Page 8


Code:
//Hamid Muzaffar Khan
//CMS ID : 356209
//BS-CS 10-B
#include<stdio.h>
#define SIZE 10
int main()
{
int i, j, arr[SIZE], min, hold;
for (i = 0; i <= SIZE - 1; i++)
{
scanf_s("%d", &arr[i]);//Initialize array
}
for (i = 0; i<SIZE - 1; i++)
{
min = i;//Index of minimum number
for (j = i + 1; j <= SIZE - 1; j++)
{
if (arr[min]>arr[j])//compare with next numbers
{
min = j;
}
}
hold = arr[i];
arr[i] = arr[min];//minimum is at 1st position
arr[min] = hold;
}
for (i = 0; i <= SIZE - 1; i++)
{
printf("%d\t", arr[i]);
}
puts("");
return 0;
}

Output:

CS-113 Introduction to Programming Page 9


“Task 4”
Code:
//Hamid Muzaffar Khan
//CMS ID : 356209
//BS-CS 10-B
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 99
//---Function defination---
void sort(int arr[]);//Bubble sorting
int mode(int arr[]);//mode
int median(int arr[]);//median
int mean(int arr[]);
//-------------------------
int main()
{
int arr[SIZE], i, mod, med, avg;
sort(arr);
for (i = 0; i<99; i++)
{
printf("%d\t", arr[i]);
}
puts("");
mod = mode(arr);
printf("and mode is %d\n", mod);//mod

CS-113 Introduction to Programming Page 10


med = median(arr);
printf("Median is at %d and is %d", med, arr[med]);//median
avg = mean(arr);
printf("\nMean : %d\n", avg);
}
int mode(int arr[SIZE])
{
int sum, i, frequency[101] = { 0 }, freq, value = 0;//frequency counts occurrence
for (i = 0; i<SIZE; i++)
{
++frequency[arr[i]];
}
freq = frequency[0];//frequency of 0
for (i = 1; i <= 100; i++)
{
if (freq<frequency[i])//comparison with other frequencies
{
freq = frequency[i];
value = i;//value gives the number
}
}
printf("mod occurs at %d times\n", freq);
return value;
}
int median(int arr[SIZE])
{
return SIZE / 2;//Median returns position
}
int mean(int arr[SIZE])//Although mean is in float but in question it requires integer
{
int sum = 0, i;
for (i = 0; i<SIZE; i++)
{
sum = sum + arr[i];
}
return sum / SIZE;
}
void sort(int arr[SIZE])
{
int i, hold, j;//hold, holds a value for swapping
srand(time(0));
for (i = 0; i<SIZE; i++)
{
arr[i] = 1 + rand() % 100;//Enter your number
}
for (i = 0; i<SIZE - 1; i++)//i for passes
{
for (j = 0; j < SIZE - i - 1; j++)
{
if (arr[j]>arr[j + 1])//comparison of consecutive values
{
hold = arr[j];//swapping
arr[j] = arr[j + 1];

CS-113 Introduction to Programming Page 11


arr[j + 1] = hold;
}
}
}
}

Output:

CS-113 Introduction to Programming Page 12

You might also like