C++ :: Sorting And Calculating Median Of A Vector?
Dec 1, 2014
I am trying to to pull data from a file into a vector, then calculate different statistics from said vector. I've got everything working great except for one part. The program requires the median be calculated, which means I need to sort the data in the vector. However I can't do a quick easy sort, I'm required to use the functions swap(double, double), sort(vector), and index_of_smallest.
how to get these all working in sync to calculate the median(not to mention my algorithms appear to be wrong as I return nothing but 0's for the median value.)
I'm either not sorting right, or the index_of_smallest function is supposed to be doing something I don't realize.
#include <iostream> // for screen and kbd io
#include <iomanip>
#include <fstream> // file io
#include <cstdlib> // for exit()
#include <cmath> // for sqrt
#include <string>
#include <vector>
using namespace std;
void fill_vector(ifstream& in_file,vector<double>& v);
I am having trouble calculating the median and mode in a program that I am writing. When I run the program, lets say there are 5 students being surveyed. The input is as follows, Student #1 sees 2 movies, student #2 sees 6,student #3 sees 5 movies, student #4 sees 2 movies, and student #5 sees 1 movie. However, for the median, I get 5, because that is at element 2, and thus midway through the count, so the program interprets that number as the median. For the mode, even though 5 is repeated twice, the program displays that there was no mode, or displays "-1" (indicating no mode). Here is my code:
Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should perform the following steps:
A) Ask the user how many students were surveyed. An array of integers with this many elements then be dynamically allocated.
B)/> Allow the user to enter the number of movies each student saw into the array.
C) Calculate and display the average, median, and mode of the values entered.
Input Validation: Do not accept negative numbers for input.
#include <iostream> #include <iomanip> using namespace std; // Function prototypes double calculateMean(int *, int); double calculateMedian(int *, int); int calculateMode(int *, int);
I'm trying to make a c++ program of this but i don't know how to use the bubble sorting.
Write a C++ application that asks the user to enter 10 numbers. The program then stores those numbers in an Array. The program should display the Mean , Median, and Mode.
Mean is the average of the 10 numbers. Median is the average of the 5th and the 6th numbers. (But you have to arrange the numbers in ascending order first using Bubble Sort before calculating the Median.) Mode is the most frequent number among the 10 numbers.
I needed to compute average median and mode for numbers in a vector. Im having a few issues I have already figured out how to calculate average and median just not mode. Also the program crashes when 0 is entered instead of exiting nicely. Also i keep getting a error on line 47 saying primary expression expected before else.
I'm new to C++ especially vectors.. I've a question regarding sorting of object in a vector.. My object consists of x,y and civ. so I would like to know how do I sort the value of the civ in an descending order but at the same time retaining the value of x and y attached to it..
I'm implementing kruskal's algorithm and ran into some troubles compiling. I need to sort a vector of objects by value. Here is my code and the error I'm getting.
Code:
These are the two functions in graph.cpp (there are more but are unrelated)
#include <vector> #include <algorithm> #include <iostream> using namespace std; #include "graph.h" #include "edge.h" using std::vector; void graph::sort_edgesArray() {
[code].....
//This is the error I'm getting.
graph.cpp: In member function "void graph::sort_edgesArray()": graph.cpp:39:33: error: no matching function for call to sort (std::vector<edge>::iterator&, std::vector<edge>::iterator&, <unresolved overloaded function type>) /usr/include/c++/4.5/bits/stl_algo.h:5236:18: note: candidate is: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<edge*, std::vector<edge> >, _Compare = bool (graph::*)(edge&, edge&)]
Suppose I have a two-dimensional stl vector (a vector of vectors), for example:
Code : vector<vector<int> > x;
And then I want to sort the outer vectors in order of the size of their inner vectors. How do I do this?
Usually, with a one-dimensional vector, I can just create a comparison function and use the sort function. For example, if my vector is defined as:
Code: vector<int> y;
And I want to sort if in terms of the int values of each vector element, then I can sort by using the stl::sort function, with the comparison function defined as:
I'm trying to write a program that reads in from a .txt file the movie title, rating, director, actors etc. All I want to do is to just sort movie titles alphabetically and print all its attributes(director, actors). I tried using sort( movies.begin(), movies.end()) method, but it doesn't work.
writing a sorting function that has an argument for a vector of ints rather than an array; it should use a selection sort algorithm.Here is what I have:
#include <iostream> #include <vector> #include <conio.h> using namespace std; void fillVector(vector<int> &aVector); // PRECONDITION: number declared size of array a. // POSTCONDITION: number_used is the number of values stored in a //a[0] through a[number_used-1] have been filled with nonnegative int.
I am trying to use std::sort to sort a vector of complex objects using a custom function. However, it keeps erroring "Unresolved overloaded function type".
encounter::encounter(){ // ... cut std::sort (allpeople.begin(), allpeople.end(), sortByInit);} bool encounter::sortByInit (character& a, character& b) { if (a.getinit () == b.getinit ()) {
I tried to sort a large numbers of vector of random integers with std::sort(), but when the number increases over 10M, std::sort returns all zero in values. Does std::sort have a limitation of input numbers?
I'm trying to use a priority queue sorting in reverse order to take a vector or 2d array. The problem is that I want to sort by the vector/array cell value but keep the reference to the vector/array index of the value. I don't know quite howto keep them both related so when I pop. I can find the corresponding cell.
Write a program that can be used to gather statistical data about the number of movies college students see in a month. The program should ask the user how many students were surveyed and dynamically allocate an array of that size. The program should then allow the user to enter the number of movies each student has seen. The program should then calculate the average, and median of the values entered.
My problem is that i keep getting a error box that says: Run-Time Check Failure #2 - Stack around the variable 'sNUM' was corrupted. and then my program shuts down!
This is what i have so far
Code: #include <iostream> #include <iomanip> using namespace std; void movAVG(int *, int); void movMED(int *, int); int main() { int sNUM, sARRAY, *sARRAYptr;
#include <iostream> #include <conio.h> #include <cstdlib> #include <ctime> using namespace std; int main { int* randomArray(int, int); /*This function creates an array of random numbers of a given size within a given range*/
I'm trying to write code that implements [median filtering] on a two-dimensional array.
Here's an image to illustrate:
The program starts at the beginning of the array. The maximum array size is 100. I know that I can use an array like:
int a[100][100];
to store the input, and that I can iterate over a part of this array using two `for` loops like this:
for(i=0;i<size_filter;i++) for(j=0;j<size_filter;j++) temp[i][j]=a[i][j] // not so sure
But how can I make this code loop over the neighbors of every element in the array, calculate their median, and replace the center element with the median?
For some examples of what I'm trying to do, let's say that the input is a 5x5 matrix, so the input size is 5. And I want to run a 3x3 median filter on it, i.e. each element should be replaced by the median of the 3x3 elements surrounding it.
The program starts at the corner index (0,0). For this index, it scans the 3x3 region surrounding it (of which only four indexes actually lie within the input array), which contains the values 0, 0, 1, and 0. The median of these values is 0, so that's what the code should output for this array index.
In the picture below, the number in -band - is the center cell, and the plain * and * numbers are its neighbors within the 3x3 region surrounding it:
This time, the elements in the 3x3 region (excluding those outside the input array) have the values 0, 0, 0, 1, 0, and 0, and again, their median is therefore 0.
Here's yet another example, this time from the middle of the input, at center index (3,2):
So I am trying to find the min, max, range, mean, median, and mode. The max number is right but the min isnt. Once that is corrected, i can get range and mean easily. Also, how to get mode and median. I am guessing the values in the arr2 would have to be sorted. This refers to the values in arr2 which are the calculated values in the formula. You can enter -2 and 2 for userMin and userMax.
#include <iostream> #include <iomanip> using namespace std; int main() { cout << fixed << setprecision(1); float userMin;
The assignment is to create a program that displays the median of an array using pointers. Assume the array is already in ascending or descending order.I'm getting errors currently on the bottom two "return median;" statements.The code that I have so far is as follows...
#include <iostream> using namespace std; char again; int getMedian(int*, int); const int constant = 100;