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
ADVERTISEMENT
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
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
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
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
Oct 24, 2013
I am just starting out programming and have an assignment due in the near future. the problem is I cant seem to figure it out. I started it already but got stuck cant seem to figure it out. Here is the assignment.
Create a program that will generate a list of 200 random numbers (ranging from 1-1000) and determine the medium, mode, and average of the list of numbers. Have the program display the original list ant then display the list in ascending and descending order on the screen.
View 2 Replies
View Related
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
Apr 4, 2013
Code:
if (IS_LEAP_YEAR(year))
const int days_per_month[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
else
const int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Is it ok to declare the array in this manner or is it bad?
And i have to ask the user for a date to enter in my program. So should I use scanf or should I store the date in a string and then use sscanf. I have to check for valid input for everything like day, month, year etc. I did it as below..
Code:
int assignments;
assignments = scanf("%d / %d / %d", &month, &day, &year);
fflush(stdin);
if (assignments != 3)
{
printf("Retry: ");
}
else
error checking.
View 9 Replies
View Related
Apr 26, 2014
This code i made divided a user input into characters and non character. The problem im having is that if a mixed sentence is created, such as 32B, it will only print the 'char string' and not the 'non char string'. But when the sentence is just non characters like 32 it will print the 'non-char string'. So essentially if a mixed sentences is created both of the strings won't be created or printed.(This is only a function by the way).
Code:
Count_chars( char Input[]) {
int i;
for(i = 0; Input[i]; i++) {
if((Input[i] <= 122) &&(Input[i] >= 97)){
printf("Char %c ",Input[i]);
Char[i] = Input[i];
[Code]...
View 4 Replies
View Related
May 5, 2013
I've a problem with the map construct. I wrote this class in which there are maps:
class Features {
private:
map<const string,GenericFeatureContainer*> featuremap;
map<const string,GenericFeatureContainer*>::iterator it;
int size;
public:
}
And I have the following constructor:
Features::Features() {
map<NULL,NULL> featuremap;
it=NULL;
size=0;
}
Is that correct? otherwise what is the correct syntax?
View 2 Replies
View Related
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
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
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
Apr 13, 2014
This code i made, utilizing the type casting construct, isn't outputting what i wanted. The output for 'Dollars' and 'Cents' are returning '0' for both. instead all i want it to do is seperate the two. for example changing the float value of amount to an integer, giving a dollar value.
Code:
#include <stdio.h>
int main()
{
}
[code]....
View 1 Replies
View Related
Jun 3, 2013
How to construct a simple hash function ? When the program executes, it will ask for your name. Type your name and it will print out a "hash code" (a number) from that name.
View 2 Replies
View Related
Jun 27, 2014
have a problem with my code (I wish for answer with code). The conditions I have to grant:
- **Only** a pointer to an object must be saved in a **standard** class vector (e.g vector<ShapePtr>)
- Base class must be a polymorphic class (I call this class ShapePtr)
- I **must** have a deep copy process
- Constructor is **not** suppose to do a deep copy
- **clone()** is suppose to do the deep copy
Here is the code **provided** to me:
main.cpp
int main() {
typedef shared_ptr<Shape> ShapePtr;
vector<ShapePtr> shapevec;
[Code].....
View 1 Replies
View Related
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
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
View Related
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
Feb 26, 2015
If I have an integer variable like int a=9 then in the switch case If i write :
switch(a) {
case 4+a: printf("hii");
}
Then why is this statement a compile-time error that variables cannot be used inside a case statement why does the compiler not subtitutes the values in place of the variables.
View 1 Replies
View Related
Apr 12, 2014
This code i made is a cent converter from 5 to 95 cents. The problem i'm receiving is when the 'cents' function is sent back to the 'main' function it only prints one line. It seems to just print the first if construct that complies with the statement. Is there anyway i can have this function print multiple cent values? For example if 60 cents was entered it would only print '50c', and i want it to print '50c' and '10c' instead.
Code:
#include <stdio.h>
int x;
void check(int x)
{
if( x < 5)
printf("Less then 5 cannot be calculated
");
else if(x > 95)
[code]....
View 3 Replies
View Related
Mar 13, 2013
how to read characters from user and construct a sting and then extract part of it using substring?
View 2 Replies
View Related
Nov 5, 2013
I am trying to construct a program without a main function that outputs stars but at the same time outputs the number and a colon after the number but before the number of star. This is the coding i have so far.
void output_stars(int num){
if (num > 0){
cout << "*";
output_stars(num-1)
[Code] ....
The program is working the way it should i just can't figure out how to output the number and a colon. The main function is written as such:
output_stars(1);
output_stars(2);
output_stars(5);
output_stars(3);
output_stars(4);
output_stars(7);
How to get the numbers of the main function to show up with the stars of the previous function.
View 6 Replies
View Related
Nov 29, 2014
I'm trying to read a "pointer-based" maze .txt. The first letter in each row corresponds to the room letter...then the letters that follow are North node, East node, South node, and West node respectively. The asterisk indicates an empty room or not a valid option.
Here is what I have come up with, what is happening is after the file is parsed by read_maze it is calling my is_empty function indicating that there is no maze because it doesn't go into the else statement here.
I've attached a sample input file:
maze.txt (130bytes)
Number of downloads: 19
We can't assume the rooms will be in order alphabetically A - Z, We are expecting a maximum of 12 rooms and there is a space between each letter or asterisk.
void Maze::read_maze(string FileName){
string line;
ifstream inStream;
inStream.open(FileName.c_str());
int test = inStream.peek();
int i = 0;
if (!(inStream.fail())){
while (!inStream.eof() && test != EOF){
[code]....
View 5 Replies
View Related
Feb 26, 2015
I just wanted to ask the reason that why is the below code not checking the case -1 while working for the other case values.
#include<stdio.h>
#include<conio.h>
int main() {
int i=-1;
switch(i-2) {
[Code] ....
So why in the below code the case -1 doesnt run,when the value evaluated by the switch construct is actually a negative integer constant.
View 1 Replies
View Related
Apr 19, 2014
I need to construct Truncated Gaussian distribution in the range of (5-20 microns), specifyed by initial standard deviation.
Particularly i need input which comprises of minimum value(5 microns), maximum value(20 microns) and standard deviation of (5,10,20 etc..). In this regard i tried with Box-Muller but it doesn't fulfuil the goal.
View 2 Replies
View Related