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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
//File: Assignment 1 - Mode Problem
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int * copy_Array(int *,int);
int * mark_Sort(int *,int);
int * nMode(int *,int);//****Create This Function*****
int * fill_Array(int,int);
int * random_fill_Array(int,int);
void print_Array(string,int *,int,int);
void print_Modes(string eq,int * a,int pline);
int main(int argc, char** argv) {
//Read in or set the size of the array and it's limit values
int array_size=20,max_value=4,per_line=20;
srand(static_cast<unsigned int>(time(0)));
//Create a dynamic 1 dimensional array
int *one_Dim=fill_Array(array_size,max_value);
//Print out and examine before solving the problem
//with this test case
mark_Sort(one_Dim, array_size);
print_Array("The Sequential Array = ",one_Dim,array_size,per_line);
//Calculate the mode
int * mode_Array=nMode(one_Dim,array_size);
//Print the modes
print_Modes("The mode = ",mode_Array,per_line);
//Now try a few other examples
array_size=10;max_value=7;
delete [] one_Dim;
one_Dim=fill_Array(array_size,max_value);
mark_Sort(one_Dim, array_size);
print_Array("The Sequential Array = ",one_Dim,array_size,per_line);
delete [] mode_Array;
mode_Array=nMode(one_Dim,array_size);
print_Modes("The mode = ",mode_Array,per_line);
//Another example with no modes
array_size=7;max_value=6;
delete [] one_Dim;
one_Dim=fill_Array(array_size,max_value);
mark_Sort(one_Dim, array_size);
print_Array("The Sequential Array = ",one_Dim,array_size,per_line);
delete [] mode_Array;
mode_Array=nMode(one_Dim,array_size);
print_Modes("The mode = ",mode_Array,per_line);
//Another example with a random fill
array_size=7;max_value=6;
delete [] one_Dim;
one_Dim=random_fill_Array(array_size,max_value);
mark_Sort(one_Dim, array_size);
print_Array("The Random Array = ",one_Dim,array_size,per_line);
delete [] mode_Array;
mode_Array=nMode(one_Dim,array_size);
print_Modes("The mode = ",mode_Array,per_line);
array_size=14;max_value=5;
delete [] one_Dim;
one_Dim=random_fill_Array(array_size,max_value);
mark_Sort(one_Dim, array_size);
print_Array("The Random Array = ",one_Dim,array_size,per_line);
delete [] mode_Array;
mode_Array=nMode(one_Dim,array_size);
print_Modes("The mode = ",mode_Array,per_line);
//End of test
delete [] one_Dim;
delete [] mode_Array;
return 1;
}
//This is your homework.
//Right now this function is just a stub for you
//to get working
int * nMode(int *a,int len){
/////////////////////////////////////////////////////////////
//This is what I've done so far, but I can't quite get the
//results im looking for, I seem to be overlooking something
/////////////////////////////////////////////////////////////
int possible_number_modes=0;
int max_frequency = 0;
int current_frequency = 1;
for(int element=0;element<len;element++){
if(a[element]==a[element+1]){
current_frequency++;
}
if(current_frequency>max_frequency){
max_frequency = current_frequency;
current_frequency = 1;
}
}
cout << "max frequency: " << max_frequency << endl;
int *modes=new int [1]; // modes array
modes[0]=0;
/////////////////////////////////////////////////////////////
return modes;
}
//Printout the area in set notation standard array
//{0,1,2,3,etc....}
void print_Array(string eq,int * a,int len,int pline){
//Print as a set
cout<<eq<<"{";
for(int i=0;i<len-1;++i){
cout<<a[i]<<",";
if(i%pline==(pline-1))cout<<endl;
}
if(len>0)cout<<a[len-1]<<"}"<<endl;
else cout<<"}"<<endl;
}
//Printout the area in set notation for modes array
//{0,1,2,3,etc....} Note, the first position contains
//the number of modes, i.e. the size of the array.
void print_Modes(string eq,int * a,int pline){
//Print as a set
cout<<eq<<"{";
for(int i=1;i<a[0];++i){
cout<<a[i]<<",";
if(i%pline==(pline-1))cout<<endl;
}
if(a[0]>0)cout<<a[a[0]]<<"}"<<endl;
else cout<<"}"<<endl;
}
//This function creates a dynamic 1 dimensional array
//of any size. It is filled with numbers that repeat
//{0,1,2,...,modit,0,1,2,...,modit,...,0,1,2,...,modit,etc...}
//This is the only way to really test the mode function
int * random_fill_Array(int array_size,int modit){
int * fill=new int[array_size];
for(int i=0;i<array_size;++i){
fill[i]=rand()%(modit+1);
}
return fill;
}
//This function creates a dynamic 1 dimensional array
//of any size. It is filled with numbers that repeat
//{0,1,2,...,modit,0,1,2,...,modit,...,0,1,2,...,modit,etc...}
//This is the only way to really test the mode function
int * fill_Array(int array_size,int modit){
int *fill=new int[array_size];
for(int i=0;i<array_size;++i){
fill[i]=i%(modit+1);
}
return fill;
}
int * mark_Sort(int *a,int len){
for(int i=0;i<len-1;++i){
for(int j=i+1;j<len;++j){
if(a[i]>a[j]){
a[i]=a[i]^a[j];
a[j]=a[i]^a[j];
a[i]=a[i]^a[j];
}
}
}
return a;
}
int * copy_Array(int *a,int len){
int * c = new int[len];
for(int i=0;i<len;++i){
c[i]=a[i];
}
return c;
}
| |