Assignment #3 CMP2122 MSCIT 1st 2017

Max Marks: 10
Course: CMP-2122 (MSCIT 1st 2017)
Class: MSCIT 1st 2017
Submission Date: 08/01/2018

Write a function to find mode value from array elements:

Mode value is the most repeated value from the elements. In following simple program we will create a function which will accept an array as an input. Furthermore that function will return mode value from the array elements.

[js]

#include <stdio.h>

int mode(int arr[], int size); // function prototype

int main(void){

int arrayName[10] = {2,4,4,4,1,2,3,4,2,3}; // defined an array with 10 elements

//output each element of array
printf("Original values of the array\n\n");
printf("Array index\t\t\tValue\n");
for(int j=0; j<10; j++){
printf("%11d\t\t\t%4d\n", j, arrayName[j]);
}

int a = mode(arrayName, 10); // function call

printf("\n\nMode Value is: %d", a);

return 0;
}

//function definition
int mode(int arr[], int size){
int freq[size] = {0};
for(int i=0; i<size; i++){
freq[arr[i]]++;
}

printf("\nFrequency of each array element\n\n");
printf("Array index\t\t\tValue\n");
for(int j=0; j<10; j++){
printf("%11d\t\t\t%4d\n", j, freq[j]);
}
int largest = 0;
for (int j=0; j<size; j++){
if(freq[j]>freq[largest]){
largest = j;
}
}

printf("\nFrequency of each array element (graphical representation)\n\n");
printf("Array index\t\t\tFrequency\n");
for(int i=0; i<size; i++){
printf("%11d\t\t\t",i);
for(int j=0; j<freq[i]; j++){
printf("*");
}
printf("\n");
}
return largest;
}

[/js]

Output:

Results

Sr# Name Roll No Marks
1 16-mscit-21 Shamraiz Iqbal (Repeater) 16-mscit-21 NA
2 16-mscit-29 Iram Naz (Repeater) 16-mscit-29 NA
3 17-mscit-01 Ayesha Kanwal 17-mscit-01 NA
4 17-mscit-02 Arbaz Naeem 17-mscit-02 NA
5 17-mscit-03 Adeela Rani 17-mscit-03 NA
6 17-mscit-04 Shaista Khan 17-mscit-04 NA
7 17-mscit-05 Asma Benish 17-mscit-05 NA
8 17-mscit-06 Maria Ummar 17-mscit-06 NA
9 17-mscit-07 Azka Riaz 17-mscit-07 NA
10 17-mscit-08 Shar Bano 17-mscit-08 NA
11 17-mscit-09 Sahrish Ansar 17-mscit-09 NA
12 17-mscit-10 Maryam Akram 17-mscit-10 NA
13 17-mscit-11 Seerat Zhra 17-mscit-11 NA
14 17-mscit-12 Sana Sultan 17-mscit-12 NA
15 17-mscit-13 Kiran Yasmeen 17-mscit-13 NA
16 17-mscit-14 Bilal Afzal 17-mscit-14 NA
17 17-mscit-15 Pakeeza Arshad 17-mscit-15 NA
18 17-mscit-16 Muhammad Zia Awan 17-mscit-16 NA
19 17-mscit-17 Fariha Maryam 17-mscit-17 NA
20 17-mscit-18 Muhammad Jahangir Alam 17-mscit-18 NA
21 17-mscit-19 Tahseena Kanwal 17-mscit-19 NA
22 17-mscit-20 Arslan Habib 17-mscit-20 NA