I was working on simple vector sorting.. I do know it is a no no.. and that
it should be used wisely still I wanted to test vectors since with user input
it helps a lot . I do know you could still initialize arrays with users inputting a size then inputting values. Anyhow it was the same program I worked in the wee hours yesterday.. so this time I though mode ok now median.. then maybe sort strings but I wanted to see if the numbers worked with a pilot
program.. so I added a function that finds the median, and returns as a double.
I copied the segment looked at it and put it on .
Question , program runs, at the median output last few lines the program hangs, did I do a mismatch somwhere ? I use dev c++.. An idea would be good .. the program just hangs at the end.. "<filename.cpp> has stopped working"
It just hangs and won't display the median or lack thereof..
It works ok if I switch off the median function and related lines.. the program
outputs and displays the sorted array, but if I switch on the median function it leaves the median output blinking at cursor.. then the above mentioned dialogue box..
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
|
#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);
double findMedian(vector<int>&,int);
int main ()
{
vector<int> values;
int Size;
int mode=0;
int mid=0;
double median=0.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<<endl;
cout<<"User Input..\n";
showData(values);
cout<<endl;
selectionSort(values,Size);
cout<<endl;
cout<<"Sorted..."<<endl;
sortedShowData(values,Size);
cout<<endl;
cout<<"Median:"<< endl;
median=findMedian(values,Size);
system("pause");
return 0;
}
void showData(vector<int> vect)
{
for(int count=0; count<vect.size();count++)
cout<<vect[count]<<" ";
}
void selectionSort(vector<int>& ArrayOne,int)
{
int startScan, minIndex, minValue;
int size=ArrayOne.size();
for (int startScan=0; startScan<size-1;startScan++)
{
minIndex =startScan;
minValue = ArrayOne[startScan];
for (int index =startScan+1; index<size;index++)
{
if (ArrayOne[index]<minValue)
{
minValue =ArrayOne[index];
minIndex=index;
}
}
ArrayOne[minIndex]=ArrayOne[startScan];
ArrayOne[startScan]=minValue;
}
}
void sortedShowData(vector<int>& ArraySort, int size)
{ size=ArraySort.size();
for (int c=0;c<size; c++)
cout<<ArraySort[c]<<" ";
}
double findMedian(vector<int>& ArraySet,int size)
{
size=ArraySet.size();
int Size;
double median;
int mid = 0;
if(Size % 2 == 0)
{
mid = (Size/2);
median = ((ArraySet[mid]+ArraySet[mid + 1])/2.0);
}
else
{
mid = (Size/2);
median = ArraySet[mid+1];
}
return median;
}
|