Ex. 16 Ch. 4

133 views
Skip to first unread message

5at5ish

unread,
Dec 25, 2009, 10:52:35 PM12/25/09
to PPP-public
Hi guys! Can't solve that exercise. I'm able to find integers that
appears more than one time, but can not figure out which of them
appears most times:

'In the drill, you wrote a program that, given a series of numbers,
found
the max and min of that series. The number that appears the most times
in a sequence is called the mode. Create a program that finds the mode
of
a set of positive integers."

Art Werschulz

unread,
Dec 26, 2009, 6:26:44 PM12/26/09
to ppp-p...@googlegroups.com
Hi.

Here's a start: Creaate
vector<int> numbers; // ints read from user input
and store input values into numbers. Now sort numbers.

Is this enough to get you started?

PS: The STL contains something called "map", which makes this problem
much simpler. Unfortunately, these aren't introduced until Chapter 21.

Art Werschulz
207 Stoughton Avenue, Cranford NJ 07016-2838
(908) 272-1146

5at5ish

unread,
Dec 27, 2009, 1:08:10 AM12/27/09
to PPP-public
At the moment I got just these:

#include<iostream>
#include<vector>
using namespace std;


vector<int> seq;
vector<int> modes;

bool is_mode(int a)
{


int count = 0;
for (int x = 0; x < seq.size(); ++x) {
if (a == seq[x])
++count;

if (count > 1) {
return true;
}

}
return false;
}


int main()
{
int number = 0;
int check = -1;

cout<< "Please enter the sequence of integers: " << endl;
while (cin>> number) {
seq.push_back(number);
}

sort(seq.begin(), seq.end());

for (int i = 0; i < seq.size(); ++i) {

if (is_mode(seq[i]) && seq[i] != check) {

modes.push_back(seq[i]);
check = seq[i];
}

}

cout<< "Modes:" << endl;

for (int z = 0; z < modes.size(); ++z) {
cout<< modes[z] << endl;

}

So, I can find numbers that appears more than one time, but I need to
find the number that appears most times.

Message has been deleted
Message has been deleted

pantera

unread,
Dec 28, 2009, 11:02:41 PM12/28/09
to PPP-public
Hi, this is my solution... I was to have written a function, but I
just couldn't figure out how. Is it possible to write a function with
one of the arguments as a vector? I've tried that, but the results
didn't come out as expected, i.e. int mode(vector<int> seq, int
vector_size, int value)... The first argument, vector<int> seq, is a
problem when I called the function. Seems like vector can't be an
argument? Maybe an array, which is to be covered in later chapters???

// Exercise 16 - Chapter 4

#include "std_lib_facilities.h"

int main()
{
int x = 0;
unsigned int i = 0;
unsigned int j = 0;
vector<int> numbers;
cout << "Enter a series of positive integers: ";
while (cin>>x) {
numbers.push_back(x);
}

cout << "You have entered: ";
for (i=0; i<numbers.size(); ++i)
cout << numbers[i] << ' ';

// Mean
int sum = 0;
sort(numbers.begin(), numbers.end());
for (i=0; i<numbers.size(); ++i)
sum += numbers[i];
cout << "\nMean: " << sum/numbers.size() << '\n';

// Median
int median = 0;
for (j=0; j<numbers.size(); ++j) {
if ((numbers.size()%2)==0)
median = 0.5*numbers[numbers.size()/2] + 0.5*numbers[numbers.size()/
2-1];
else
median = numbers[numbers.size()/2];
}
cout << "Median: " << median << '\n';

// Mode
int prev_count = 0;
int curr_count = 0;
int mode = 0;
for (i=0; i<numbers.size(); ++i) {
for (j=0; j<numbers.size(); ++j) {
if (numbers[j]==numbers[i])
++curr_count;
}

if (curr_count>prev_count) {
mode = numbers[i];
prev_count = curr_count;
}

curr_count = 0;
}

if (prev_count==1)
cout << "There's no mode\n";
else {
cout << "\nMode: " << mode << "\nCount: " << prev_count << '\n';

5at5ish

unread,
Dec 30, 2009, 6:14:04 PM12/30/09
to PPP-public
Thanks a lot Pantera, very helpful!

Lantos István

unread,
Feb 28, 2017, 1:22:17 PM2/28/17
to PPP-public
Thank You Pantera! I struggled with this for days.

Someone can provide an explanation that why this code works:

for (int j{ 0 }; j < v.size(); ++j) {
  if (v[i] == v[j])
    ++curr_count;
}

But this one why not?

if (i == 0 || v[i - 1] == v[i]) {
  ++curr_count;
}

I felt the solution for this exercise was a little bit hackery and this method (you have to use another loop inside a loop) doesn't mentoined in previous chapters.

Art Werschulz

unread,
Feb 28, 2017, 4:23:56 PM2/28/17
to PPP-public
Hi.

> On Feb 28, 2017, at 1:22 PM, Lantos István <keroz...@gmail.com> wrote:
>
> Someone can provide an explanation that why this code works:
>
> for (int j{ 0 }; j < v.size(); ++j) {
> if (v[i] == v[j])
> ++curr_count;
> }
>
> But this one why not?
>
> if (i == 0 || v[i - 1] == v[i]) {
> ++curr_count;
> }
>
> I felt the solution for this exercise was a little bit hackery and this method (you have to use another loop inside a loop) doesn't mentioned in previous chapters.

The former is an iterative statement; it successively sets j to the values 0, 1, 2, ..., v.size()-1.

The latter is a selection statement. If i is 0 or if the (i-1)st and i-th values in v agree, curr_count is incremented. (Presumably the value of i would've been set before the if-statement itself.) Unless this statement is within a loop, it will only execute once.


Art Werschulz
a...@comcast.net



Reply all
Reply to author
Forward
0 new messages