Hi, i have this much done and i believe i have the right function down to find the mean of my randomly sorted array. However, i am having a problem figureing out where to call the MODE fuction and how to call can. Can you help? Thanks
and i realize i have a lot of other formating errors and such but i just want to make sure it works first. thanks
Code:
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
void bubbleSort();
void selectionSort();
void insertionSort();
int main(int argc, char *argv[])
{
int sort;
system("color e0");
do
{
cout << "Select a sort to run: \n";
cout << "\t1: Bubble sort\n";
cout << "\t2: Selection sort\n";
cout << "\t3: Insertion sort\n";
cout << "\t0: Exit\n";
cin >> sort;
system("cls");
if (sort==1) bubbleSort();
else if (sort==2) selectionSort();
else if (sort==3) insertionSort();
} while (sort != 0);
system("PAUSE");
return EXIT_SUCCESS;
}
void bubbleSort()
{ srand ((unsigned) time(NULL));
int size;
int main();
cout << "Array Size: "<<endl;
cin >> size;
system("cls");
cout << "Array Size: " << size <<endl<<endl;
int tosort[size];
for (int i=0;i<size;i++)
{
int x = (rand() %100);
tosort [i] = x;
cout << x << " " ;
}
cout<<endl;
int tmp;
for (int y=0; y < size; y++)
{
for (int x=0; x < size; x++)
{
if(tosort[x] > tosort[x+1])
{
tmp = tosort[x];
tosort[x] = tosort[x+1];
tosort[x+1] = tmp;
}
cout << tosort[x] << " ";
}
cout << endl;
}
double mean = 0, median = 0, mode= 0,;
float dev=0;
double sum, sum2;
for (int meancount=0; meancount<size; meancount++)
{
sum=sum + tosort[meancount];
sum2=sum2 + tosort[meancount]*tosort[meancount];
}
double tot;
int med=size/2;
mean = sum/size;
dev = (sum2 - size*mean*mean);
tot= sqrt( dev /size);
cout <<endl<<endl<< "Mean: " << mean <<endl;
cout <<"Range: " << abs( tosort[0] - tosort[size-1]) <<endl;
cout <<"Sum: " << sum << endl;
if (size % 2 ==0)
{
double ave=0;
ave= tosort[med-1] + tosort[med];
double final=0;
final = ave/2;
cout << "Median: " << final << endl;
}
else
{ cout << "Median: " << tosort[ med ] << endl; }
cout << "Standard Deviation: " <<tot << endl;
system("pause");
system("cls");
main();
}
void selectionSort()
{
srand ((unsigned) time(NULL));
int size;
cout << "Array Size: ";
cin >> size;
cout << endl;
int tosort[size];
for (int i=0;i<size;i++)
{
int x = (rand() %100);
tosort [i] = x;
cout << x << " " ;
}
cout<<endl;
int minimum, tmp;
for (int r = 0; r < size; r++)
{
minimum = r;
for (int q = r; q < size; q++)
{
if (tosort[q] < tosort[minimum])
{
minimum = q;
}
}
tmp = tosort[minimum];
tosort[minimum] = tosort[r];
tosort[r] = tmp;
for (int x=0; x<size; x++)
cout << tosort[x] << " ";
cout << endl;
}
double mean = 0, median = 0, mode= 0;
float dev=0;
double sum, sum2;
for (int meancount=0; meancount<size; meancount++)
{
sum=sum + tosort[meancount];
sum2=sum2 + tosort[meancount]*tosort[meancount];
}
double tot;
int med=size/2;
mean = sum/size;
dev = (sum2 - size*mean*mean);
tot= sqrt( dev /size);
cout <<endl <<endl<< "Mean:" << mean<<endl;
cout << "Range: " << abs( tosort[0] - tosort[size-1])<<endl;
cout <<"Sum: " << sum << endl;
if (size % 2 ==0)
{
double ave=0;
ave= tosort[med-1] + tosort[med];
double final=0;
final = ave/2;
cout << "Median: " << final << endl;
}
else
{ cout << "Median: " << tosort[ med ] << endl; }
cout << "Standard Deviation: " <<tot << endl;
system("pause");
system("cls");
}
void insertionSort()
{
int tmp, a;
srand ((unsigned) time(NULL));
int size;
cout << "Array Size: "<<endl;
cin >> size;
cout << endl;
int tosort[size];
for (int i=0;i<size;i++)
{
int x = (rand() %100);
tosort [i] = x;
cout << x << " " ;
}
cout << endl;
for (int t=1; t<size; t++)
{
tmp = tosort[t];
for (a = t-1; (a >= 0 && tmp < tosort[a]); a--)
{
tosort[a+1] = tosort[a];
}
tosort[a+1] = tmp;
for (int x=0; x<size; x++)
cout << tosort[x] << " ";
cout << endl;
}
double mean = 0, median = 0, mode= 0;
float dev=0;
double sum, sum2;
for (int meancount=0; meancount<size; meancount++)
{
sum=sum + tosort[meancount];
sum2=sum2 + tosort[meancount]*tosort[meancount];
}
double tot;
int med=size/2;
mean = sum/size;
dev = (sum2 - size*mean*mean);
tot= sqrt( dev /size);
cout <<endl<<endl<< "Mean: " << mean<<endl;
cout << "Range: " << abs(tosort[0] - tosort[size-1])<<endl;
cout <<"Sum: " << sum << endl;
if (size % 2 ==0)
{
double ave=0;
ave= tosort[med-1] + tosort[med];
double final=0;
final = ave/2;
cout << "Median: " << final << endl;
}
else
{ cout << "Median: " << tosort[ med ] << endl; }
cout << "Standard Deviation: " <<tot << endl;
system("pause");
system("cls");
}
void mode (int tosort[], int arraysize)
{
int mode[101]={};
int x=0;
int y=0;
int max=0;
for (x=0; x<101; x++)
{
mode[x]=0;
}
for (x=0; x<101; x++)
{
for(y=0; y<arraysize; y++)
{
if (tosort[y]==x)
{
mode[x]=mode[x]+1;
}
}
}
for (x=0; x<101; x++)
{
if (max<mode[x])
{
max=mode[x];
}
}
for (x=0; x<101; x++)
{
if (max==mode[x])
{
cout << "Mode: " << x;
}
}
}