I was assisted by Peter87, about passing by reference regarding vectors and sorting.. I attempted another try at introducing mode of array by way of vectors,
I know it works for preconfigured arrays, but I am really interested in vector arrays so I spliced it with a mode block from an array initialized by a specific
size .
I would like another brutal input or pointers on how vectors,arrays and modes work I have an idea I ran it through 20 times 2 hours sleep. The prog returns mode -1
though I have the values. I know it has something to do with vectors and passing
by reference , how do I break it down so I see the problem by hand tracing or by
line reading the prog without running it.. since I tried it on paper..
I am trying to understand algorithm of sorting.. how do you see the algo
for vecs..
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
|
#include<iostream>
#include<vector>
using namespace std;
void showData(vector<int>);
void selectionSort(vector<int>&, int);
void sortedShowData(vector<int>&, int);
void findFreq( vector<int>&,int);
int main ()
{
vector<int> values;
int Size;
int mode=0;
cout<<"input the size of the array:";
cin>>Size;
for (int counter=0;counter<Size;counter++)
{
int no;
cout<<"Value ["<<(counter+1)<<"]:";
cin>>no;
values.push_back(no);
}
cout<<"User Input..\n";
showData(values);
selectionSort(values,Size);
cout<<endl;
cout<<"sorted"<<endl;
sortedShowData(values,Size);
cout<<endl;
findFreq(values,Size);
system("pause");
return 0;
}
void showData(vector<int> vect)
{
for(int count=0; count<vect.size();count++)
cout<<vect[count]<<endl;
}
void selectionSort(vector<int>& a,int)
{
int startScan, minIndex, minValue;
int s=a.size();
for (int startScan=0; startScan<s-1;startScan++)
{
minIndex =startScan;
minValue = a[startScan];
for (int index =startScan+1; index<s;index++)
{
if (a[index]<minValue)
{
minValue =a[index];
minIndex=index;
}
}
a[minIndex]=a[startScan];
a[startScan]=minValue;
}
}
void sortedShowData(vector<int>& b, int size)
{ size=b.size();
for (int c=0;c<size; c++)
cout<<b[c]<<endl;
}
void findFreq( vector<int>& cc, int)
{
int mode=-1;
int pos=0;
int hi;
int size=cc.size();
int fqncy[size];
int fval;
int icount;
for( icount=0; icount<size; icount++)
fqncy[icount]=0;
for(int kcount=0;kcount<size; kcount++)
{
if(cc[icount]==cc[kcount]&& &cc[icount]!=&cc[kcount])
fqncy[kcount]+=fqncy[kcount+1];
fval=fqncy[kcount];
}
hi=fqncy[0];
for (int ind=1; ind<size;ind++)
{
if (fqncy[ind]>hi)
{
hi= fqncy[ind];
pos=+ind;
}
}
for (int ind=0; ind<size; ind++)
if (fqncy[ind]!=fqncy[ind+1]&&(ind+1)<size)
{
mode= cc[pos];
}
cout<<"Mode:"<<mode<<endl;
cout<<"Freq Val:"<<fval<<endl;
}
|
Sorry .. I found out I was missing a loop and wrong values for the array indexes.. am figuring out the freq val being invalid right now if it works I'll post it again
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
|
#include<iostream>
#include<vector>
using namespace std;
void showData(vector<int>);
void selectionSort(vector<int>&, int);
void sortedShowData(vector<int>&, int);
void findFreq( vector<int>&,int);
int main ()
{
vector<int> values;
int Size;
int mode=0;
cout<<"input the size of the array:";
cin>>Size;
for (int counter=0;counter<Size;counter++)
{
int no;
cout<<"Value ["<<(counter+1)<<"]:";
cin>>no;
values.push_back(no);
}
cout<<"User Input..\n";
showData(values);
selectionSort(values,Size);
cout<<endl;
cout<<"sorted"<<endl;
sortedShowData(values,Size);
cout<<endl;
findFreq(values,Size);
system("pause");
return 0;
}
void showData(vector<int> vect)
{
for(int count=0; count<vect.size();count++)
cout<<vect[count]<<endl;
}
void selectionSort(vector<int>& a,int)
{
int startScan, minIndex, minValue;
int s=a.size();
for (int startScan=0; startScan<s-1;startScan++)
{
minIndex =startScan;
minValue = a[startScan];
for (int index =startScan+1; index<s;index++)
{
if (a[index]<minValue)
{
minValue =a[index];
minIndex=index;
}
}
a[minIndex]=a[startScan];
a[startScan]=minValue;
}
}
void sortedShowData(vector<int>& b, int size)
{ size=b.size();
for (int c=0;c<size; c++)
cout<<b[c]<<endl;
}
void findFreq( vector<int>& cc, int)
{
int mode=-1;
int pos=0;
int hi;
int size=cc.size();
int fqncy[size];
int fval;
int count;
for( count=0; count<size; count++)
fqncy[count]=0;
for(int kcount=0;kcount<size; kcount++)
{
for(int icount=1;icount<size; icount++)
{
if(cc[kcount]==cc[icount]&& &cc[icount]!=&cc[kcount])
fqncy[kcount]+=1;
fval=fqncy[kcount];
}
}
hi=fqncy[0];
for (int ind=1; ind<size;ind++)
{
if (fqncy[ind]>hi)
{
hi= fqncy[ind];
pos=+ind;
}
}
for (int ind=0; ind<size; ind++)
if (fqncy[ind]!=fqncy[ind+1]&&(ind+1)<size)
{
mode= cc[pos];
}
cout<<"Mode:"<<mode<<endl;
cout<<"Freq Val:"<<fval<<endl;
}
|