1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#include <iostream>
using namespace std;
void getMode(int num[], int freq[], int size);
int main()
{
const int SIZE = 100;
int i, z, a, m, size, index, temp, freq[SIZE], num[SIZE];
int mode = freq[SIZE];
int counter = 0;
int counterMode = 0;
bool swapNum;
bool found;
cout << "Enter size for Array: ";
cin >> size;
for (index = 0; index < size; index++)
{
cout << "Enter Values for the Array "
<< (index +1) << ": ";
cin >> num[index];
}
for (index = 0; index < size; index++)
{
cout << "The Values you have input are: " << num[index] <<endl;
}
do
{ swapNum = false;
for (z = 0; z < size-1; z++)
{
if(num[z] > num[z+1])
{
temp = num[z];
num[z] = num[z+1];
num[z+1] = temp;
swapNum = true;
}
}
} while(swapNum);
for (index = 0; index < size; index++)
{
found = false;
cout << "freq count loop" << endl;
freq[index] = 1;
int m=index+1;
while (m<size)
{
cout << "inner freq count loop" << endl;
if (num[m] == num[index])
{
cout << "inc freq count loop" << endl;
freq[index]++;
}
else
{
for(int i =index+1; i<m; i++)
{
freq[i] = freq[index];
}
index = m-1;
}
m++;
}
}
for (a = 0; a < size; a++)
{
cout<<"The Values you have input in Ascending Order are: " << num[a] << " times appeared: " << freq[a] << endl;
}
getMode(num, freq, size);
return 0;
}
void getMode(int num[], int freq[], int size)
{
int counter=0, counterMode=0;
int mode;
for( int i = 0; i < size; i++)
{
if (num[i] == freq[i])
{
++counter;
}
else
{
if (freq[i] > counterMode)
mode = freq[i];
}
counter = 1;
freq[i] = num[i];
}
cout<<"The Mode is :"<< mode << endl;
}
|