THE CODE CALCULATES THE MEAN MEDIAN AND MODE OF THE SET OF NUMBERS THE CODE IS WRITTEN IN C++
PLEASE FIX THE MODE FUNCTION ! SO THAT IT OUTPUTS 0 WHEN THERE ARE NO REPETITIONS IN THE DATASET eg. for 1,2,3,4,5 mode should be zero but it gives 1 !
#include<iostream>
#include<math.h>
using namespace std;
class statistical{
protected:
double *dataArray;
int size;
public:
statistical(double a[], int s){
dataArray = new double[s];
for (int i = 0; i<s; i++){
dataArray[i] = a[i];
}
size = s;
}
virtual void print() = 0;
};
class Mean : public statistical{
public:
Mean(double a[]=0, int s=0) :statistical(a, s){
}
double mean()
{
double Sum = dataArray[0];
for (int i = 1; i < size; ++i) {
Sum += dataArray[i];
}
return Sum / size;
}
void print(){
cout << mean();
}
};
class Median : public statistical{
public:
Median(double a[], int s) :statistical(a, s){
}
void print(){
// Allocate an array of the same size and sort it.
double* Sorted = new double[size];
for (int i = 0; i < size; ++i) {
Sorted[i] = dataArray[i];
}
for (int i = size - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (Sorted[j] > Sorted[j + 1]) {
double Temp = Sorted[j];
Sorted[j] = Sorted[j + 1];
Sorted[j + 1] = Temp;
}
}
}
// Middle or average of middle values in the sorted array.
double Median = 0.0;
if ((size % 2) == 0) {
Median = (Sorted[size / 2] + Sorted[(size / 2) - 1]) / 2.0;
}
else {
Median = Sorted[size / 2];
}
delete[] Sorted;
cout << Median;
}
};
class Mode : public statistical{
public:
Mode(double a[], int s) :statistical(a, s){
}
void print(){
int* ipRepetition = new int[size];
for (int i = 0; i < size; ++i) {
ipRepetition[i] = 0;
int j = 0;
//bool bFound = false;
while ((j < i) && (dataArray[i] != dataArray[j]))
{
if (dataArray[i] != dataArray[j]) {
++j;
}
}
++(ipRepetition[j]);
}
int iMaxRepeat = 0;
for (int i = 1; i < size; ++i) {
if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
iMaxRepeat = i;
}
}
delete[] ipRepetition;
cout << dataArray[iMaxRepeat];
}
};
int main()
{
int s;
cout<<endl<<"How many numbers do you want to
enter:";
cin>>s;
cout<<endl;
cout<<"*********************************"<<endl;
double d[s];
for(int i=0;i<s;i++)
{
cout<<"Enter "<<i+1<<"th number:";
cin>>d[i];
}
Mean mean(d, s);
cout << endl << "Mean is = ";
mean.print();
Mode mode(d, s);
cout << endl << "Mode is = ";
mode.print();
Median median(d, s);
cout<<endl;
cout << "Median is = ";
median.print();
cout << endl;
return 0;
}
Solution
Code
#include<iostream>
#include<math.h>
using namespace std;
class statistical{
protected:
double *dataArray;
int size;
public:
statistical(double a[], int s){
dataArray = new double[s];
for (int i = 0; i<s; i++){
dataArray[i] = a[i];
}
size = s;
}
virtual void print() = 0;
};
class Mean : public statistical{
public:
Mean(double a[]=0, int s=0) :statistical(a, s){
}
double mean()
{
double Sum = dataArray[0];
for (int i = 1; i < size; ++i) {
Sum += dataArray[i];
}
return Sum / size;
}
void print(){
cout << mean();
}
};
class Median : public statistical{
public:
Median(double a[], int s) :statistical(a, s){
}
void print(){
// Allocate an array of the same size and sort it.
double* Sorted = new double[size];
for (int i = 0; i < size; ++i) {
Sorted[i] = dataArray[i];
}
for (int i = size - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (Sorted[j] > Sorted[j + 1]) {
double Temp = Sorted[j];
Sorted[j] = Sorted[j + 1];
Sorted[j + 1] = Temp;
}
}
}
// Middle or average of middle values in the sorted array.
double Median = 0.0;
if ((size % 2) == 0) {
Median = (Sorted[size / 2] + Sorted[(size / 2) - 1]) / 2.0;
}
else {
Median = Sorted[size / 2];
}
delete[] Sorted;
cout << Median;
}
};
class Mode : public statistical{
public:
Mode(double a[], int s) :statistical(a, s){
}
void print(){
int* ipRepetition = new int[size];
for (int i = 0; i < size; ++i) {
ipRepetition[i] = 0;
int j = 0;
//bool bFound = false;
while ((j < i) && (dataArray[i] != dataArray[j]))
{
if (dataArray[i] != dataArray[j]) {
++j;
}
}
++(ipRepetition[j]);
}
int iMaxRepeat = 0;
int flag = 0;
for (int i = 1; i < size; ++i) {
if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
iMaxRepeat = i;
flag = 1;
}
}
if(flag == 1)
cout << dataArray[iMaxRepeat];
else
cout << "0";
delete[] ipRepetition;
}
};
int main()
{
int s;
cout<<endl<<"How many numbers do you want to
enter:";
cin>>s;
cout<<endl;
cout<<"*********************************"<<endl;
double d[s];
for(int i=0;i<s;i++)
{
cout<<"Enter "<<i+1<<"th number:";
cin>>d[i];
}
Mean mean(d, s);
cout << endl << "Mean is = ";
mean.print();
Mode mode(d, s);
cout << endl << "Mode is = ";
mode.print();
Median median(d, s);
cout<<endl;
cout << "Median is = ";
median.print();
cout << endl;
return 0;
}


Here , i have taken a flag variable . Only if there is a number occurring twice will the second for loop in the if condition be entered , otherwise it would not . So if the flag remains 0 then there are no numbers occuring more than once , hence print 0. If not the usual function will print the mode that is number occurring most number of times .
THE CODE CALCULATES THE MEAN MEDIAN AND MODE OF THE SET OF NUMBERS THE CODE IS...
When running the program at the destructor an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public: varArray(); // void constructor int arraySize() const { return size; } // returns the size of the array int check(double number); // returns index of element containg "number" or -1 if none void addNumber(double); // adds number to the array void removeNumber(double); // deletes the number from the array ...
/* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public: // default constructor Student() { studentID = 0; studentFirst = "First"; studentMiddle = "Middle"; studentLast = "Last"; } void SetStudentID(int number) { studentID = number; } void SetStudentFirst(string name) { studentFirst = name; } void SetStudentMiddle(string name) { studentMiddle =...
Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() { const int n = 50; int *arr,...
Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() { int n; double avg, sum = 0;; string in_file, out_file; cout << "Please enter the name of the input file: "; cin >> in_file; ...
C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...
I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink { string name; double cost; int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() { const int size = 5; Drink drinks[size] = { {"Cola", 0.65, 2}, {"Root Beer", 0.70, 1}, {"Grape Soda", 0.75, 5}, {"Lemon-Lime", 0.85, 20}, {"Water", 0.90, 20} }; cout <<...
I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...
The mean of a list of numbers is its arithmetic average. The median of a list is its middle value when the values are placed in order. For example, if an ordered list contains 1, 2, 3, 4, 5, 6, 10, 11, and 12, then the mean is 6, and their median is 5. Write an application that allows you to enter nine integers and displays the values, their mean, and their median. Correct program import java.util.Scanner; import java.util.*; class...
81. The following function call doesn’t agree with its prototype: cout << BoxVolume( 10, 5 ); int BoxVolume(int length = {1}, int width = {1}, int height = {1}); T__ F__ 82. The following function is implemented to swap in memory the argument-values passed to it: void swap(int a, int b) { int temp; temp = a; a = b; b = temp; ...
Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...