Vector Median Sort Array Prog Hangs


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;
}
Last edited on
The variable Size, declared on line 84, is never assigned a meaningful value, so it's probable you're accessing memory you don't own in findMedian.

You should flip lines 38 and 39.

In findMedian, you don't corrrectly calculate the median.

If size is 4, your mid would be 2. Valid indexes for an array of size 4 are 0, 1, 2 and 3.

mid = 2. so median = (Arrayset[2] + ArraySet[3])/2.0.. which is the average of the last 2 elements.
Will do let me get to it asap.. so I can rework the bugs

thanks CIRE

Again.. working or learning to program.. at 32 can be enlightening at the same time difficult.. the errors were mere logical and minor arrangement of statements.. still I think asking for another opinion.. is good esp. in the wee hours of the morn... Thanks C++!!! Here is the modified prog. with the corrections by Cire and some additional help on mean mode at http://www.purplemath.com/modules/meanmode.htm
anyways...

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
#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;
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;
 
 
  median=findMedian(values,Size);
   cout<<"Median:"<<median<< endl;
  
  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=0.0;
	   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];
	}
	return median;
}


The defective segments were here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double findMedian(vector<int>& ArraySet,int size)
{ 
       size=ArraySet.size();
        double median=0.0;
        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];
	}
	return median;
}


and the wrong sequence of function call and output
corrected notice the function should precede the print command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 cout<<endl;
  cout<<"User Input..\n";
  showData(values);
  cout<<endl; 
  selectionSort(values,Size);
  cout<<endl;
  cout<<"Sorted..."<<endl;
  sortedShowData(values,Size);
  cout<<endl;
 
 
  median=findMedian(values,Size);
   cout<<"Median:"<<median<< endl;
  



Anyways I think you guys are great.. not that I 'd like to be spoon fed.. the comments are great... even if my questions are really trivial.. thanks again Peter87 and CIRE!!!!

Mack
Last edited on
Topic archived. No new replies allowed.