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);

[code]...

View 1 Replies


ADVERTISEMENT

C/C++ :: Calculating Median And Mode In Movie Statistics Program

May 5, 2014

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);

[Code] .....

View 2 Replies View Related

C++ :: Store User Input In Array And Calculate Mean / Median And Mode - Bubble Sorting

Mar 26, 2014

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.

View 5 Replies View Related

C++ :: Compute Average Median And Mode For Numbers In Vector?

Feb 23, 2014

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.

#include <iostream>
#include <conio.h>
#include <math.h>

[Code]....

View 7 Replies View Related

C++ :: Sorting Of Object In A Vector

Oct 24, 2013

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..

original
X: 4 Y: 5 CIV: 10
X: 3 Y: 2 CIV: 30
X: 3 Y: 3 CIV: 05

sorted
X: 3 Y: 2 CIV: 30
X: 4 Y: 5 CIV: 10
X: 3 Y: 3 CIV: 05

missionplan.cpp
void MissionPlan::topfives() {
stable_sort (topfive.begin(), topfive.end());
pointtwoD = topfive.at(i);
pointtwoD.displayPointdata();
}

View 2 Replies View Related

C++ :: Sorting A Vector Of Objects?

Feb 11, 2012

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&)]

View 3 Replies View Related

C++ :: Sorting On Two-dimensional Vector

Oct 30, 2012

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:

Code:
static bool SortFunction(int val1, int val2) {
if (val1 > val2) {
return true;
} else {
return false;
}
};

However, with my two-dimensional vector, I use the following comparison function:

Code:
static bool SortFunction(vector<int> vec1, vector<int> vec2) {
if (vector1.size() > vector2.size()) {
return true;
} else {
return false;
}
};

I get a runtime error:

"Debug Assertion Failed!
File: c:Program Files (x86)Microsoft Visual Studio 10.0VCincludealgorithm
Line 3686
Expression: invalid operator<"

Why is this?

View 2 Replies View Related

C++ :: Sorting Vector Of Objects By One Of Their Attributes

May 1, 2013

I have a vector of Car* objects - vector<Car*> cars

Each object in the vector has an integer attribute called passengers, which can be returned using the function getPassengers().

How do I sort the vector by the objects' passenger int? I know I need to use sort() and presumably a comparison function but I'm not quite sure how.

View 2 Replies View Related

C++ :: Sorting Vector Of Strings Alphabetically

Aug 30, 2014

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.

here's my code btw:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>
#include "Movie.h"

[Code]...

View 2 Replies View Related

C++ :: Sorting Function - Vector Swap

Jun 26, 2013

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.

[code].....

View 7 Replies View Related

C++ :: Vector Int Sorting In Ascending Order

Feb 17, 2015

I want to sort a vector int in ascending order, but when I test, the output isn't correct - the vector is still unsorted. Am I calling it incorrectly?

int sorted (vector <int> a) {
int size = a.size();
sort(a.begin(), a.end());

View 2 Replies View Related

C++ ::  Sorting Float In A Vector Of Objects

Nov 2, 2014

I've a vector of objects that I can get my float value from by accessing it through a method.It's not a pointer of vector of objects though.

Meaning it's something like vector[i].getFloatValue().

However,how do I sort the float values by descending order since I access it through a method?

View 6 Replies View Related

Visual C++ :: Sorting A Vector Alphabetically?

Aug 25, 2014

I am reading a file and storing the data (all strings) into a vector. How would I sort this alphabetically?

View 4 Replies View Related

C++ :: Sorting Vector String In Alphabetical Order

Oct 26, 2013

I know there is a function in algorithm class called sort() but I need to sort ignoring the case of the alphabet

Is there any function that does that?

View 2 Replies View Related

C++ :: Sorting Vector Of Complex Objects Using Custom Function

Mar 6, 2014

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 ()) {

[Code] ....

View 6 Replies View Related

C++ :: Sorting Large Numbers Of Vector Of Random Integers Returns All Zero

Jul 31, 2014

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?

View 1 Replies View Related

C/C++ :: Unable To Use Priority Queue Sorting In Reverse Order To Take Vector Or 2D Array

Aug 7, 2014

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.

priority_queue<int, vector<int>, greater<int> > Open;

View 2 Replies View Related

C++ :: Find Median Of Array?

Jul 4, 2013

I am trying to find the median of an array in c++.

This is the array that I would like to find the median for.

scores[14] = {62,70,98,71,81,99,74,80,73,88,73,72,95,71};

View 5 Replies View Related

C++ :: Average And Median Of Arrays

Apr 12, 2013

I need to calculate the average and median of an array. I do not know how to put it in code.

View 1 Replies View Related

C++ :: Pointers (Average And Median)

Apr 29, 2014

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;

[Code] ....

View 2 Replies View Related

C++ :: Compute Mean / Median / Mode For Map Construct

May 28, 2013

I am writing code, which reads an input file (280 MB) containing a list of words. I would like to compute

1) total # of words
2) total # of unique/distinct words
3) mean/median/mode of word count (Link)

I managed to get done with 1) and 2), but my program crashes for 3). I am not quite sure whether there are memory issues or some bug in the code.

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

[Code] .....

View 2 Replies View Related

C++ :: How To Make Median Display As Double

Feb 8, 2015

How do i make the median display as a double?

#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*/

[Code]...

View 2 Replies View Related

C/C++ :: Two Dimensional Array Median Filtering?

Nov 12, 2014

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:

-0- *0* 0 0 0
*1* *0* 0 1 0
1 1 0 0 0
0 1 1 0 0
0 0 0 0 0

Here's another example, this time with the center index (0,1):

*0* -0- *0* 0 0
*1* *0* *0* 1 0
1 1 0 0 0
0 1 1 0 0
0 0 0 0 0

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):

0 0 0 0 0
*1* *0* *0* 1 0
*1* -1- *0* 0 0
*0* *1* *1* 0 0
0 0 0 0 0

This time, the elements within the 3x3 region have the values 1, 0, 0, 1, 1, 0, 0, 1, and 1, and their median in therefore 1.

Final example:

<size of array><size filter> <data>
8
3
0 0 0 0 0 0 0 0
0 5 0 0 6 0 0 0
0 0 0 0 0 7 0 0
0 0 0 0 5 0 0 0
0 0 0 5 6 0 0 0
0 0 8 5 5 0 0 0
0 0 0 7 0 0 9 0
0 0 0 0 0 0 0 0

Output:

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 5 5 0 0 0
0 0 0 5 5 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

View 4 Replies View Related

C++ :: Calculate Mode / Mean And Median Of Array Of Integers

Mar 17, 2013

I'm writing a program that calculates the mode, mean and median of an array of integers. I've done the mean and median but i can't figure out mode.

I have to find the number of modes.

Ex array of integers: 10, 10, 4, 5, 6,5, 6, 7

My function has to return the number 3. Because there are 3 sets of mode; 10,5,6.

How do i implement this in my function because for now it just calculates the mode. It only return the number mode 10.

View 5 Replies View Related

C/C++ :: Find Minimum - Getting Median And Mode In Array

Feb 7, 2015

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;

[Code] .....

View 13 Replies View Related

C++ :: Create Program That Displays Median Of Array Using Pointers

Feb 27, 2013

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;

[code]....

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved