Streaks!

C

curiousEngine

a program that reads in a sequence of positive integers and prints out
the
longest streak of the same value. eg:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 4 2's in a row.

int main(){
int inputInteger, prevInteger, streak(1), streakInteger;
cout << "Enter positive integers (end by negative integer) "<<endl;

do{
cin >> inputInteger;
if (prevInteger == inputInteger){
streak++;
streakInteger = inputInteger;
}

prevInteger = inputInteger;

}while(inputInteger > 0);

cout << streak<<"of"<<streakInteger;

How to determine the longest streak???
 
J

Jim Langston

curiousEngine said:
a program that reads in a sequence of positive integers and prints out
the
longest streak of the same value. eg:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 4 2's in a row.

int main(){
int inputInteger, prevInteger, streak(1), streakInteger;
cout << "Enter positive integers (end by negative integer) "<<endl;

do{
cin >> inputInteger;
if (prevInteger == inputInteger){
streak++;
streakInteger = inputInteger;
}

prevInteger = inputInteger;

}while(inputInteger > 0);

cout << streak<<"of"<<streakInteger;

How to determine the longest streak???

Obvoiusly homework so can't show code but... Keep track of the longest
streak length and digit also. Then you can compare if the current streak is
longer than the longest, and if so change the longest to the current. Since
this is homework this is about much help as I can give.
 
K

Kai-Uwe Bux

curiousEngine said:
a program that reads in a sequence of positive integers and prints out
the
longest streak of the same value. eg:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 4 2's in a row.

int main(){
int inputInteger, prevInteger, streak(1), streakInteger;
cout << "Enter positive integers (end by negative integer) "<<endl;

do{
cin >> inputInteger;
if (prevInteger == inputInteger){
streak++;
streakInteger = inputInteger;
}

prevInteger = inputInteger;

}while(inputInteger > 0);

cout << streak<<"of"<<streakInteger;

How to determine the longest streak???

Hint: You could have a

std::map< int, unsigned int > the_longest_run;

so that

the_longest_run[ i ]

will always equal the lengths of the longest streak of the value i in the
sequence as read so far (you update that map each time you read a new
integer).


Best

Kai-Uwe Bux
 
L

LR

Kai-Uwe Bux said:
curiousEngine said:
a program that reads in a sequence of positive integers and prints out
the
longest streak of the same value. eg:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 4 2's in a row.

int main(){
int inputInteger, prevInteger, streak(1), streakInteger;
cout << "Enter positive integers (end by negative integer) "<<endl;

do{
cin >> inputInteger;
if (prevInteger == inputInteger){
streak++;
streakInteger = inputInteger;
}

prevInteger = inputInteger;

}while(inputInteger > 0);

cout << streak<<"of"<<streakInteger;

How to determine the longest streak???

Hint: You could have a

std::map< int, unsigned int > the_longest_run;

so that

the_longest_run[ i ]

will always equal the lengths of the longest streak of the value i in the
sequence as read so far (you update that map each time you read a new
integer).

Maybe think about a std::multimap? I remember when I did this a long
time ago for fun in HP Basic I ran a test with with something like,
2 2 4 4 4 1 1 2 2 4 4 4 9 9 9
as data.

I also found it useful to keep track of where each run started, so maybe
std::multimap< int, std::pair<unsigned int, unsigned int> >?

LR
 
J

Jerry Coffin

a program that reads in a sequence of positive integers and prints out
the
longest streak of the same value. eg:
2 6 4 3 3 2 3 2 2 2 2 6 6 6 1 6 6 6
Streak of 4 2's in a row.

int main(){
int inputInteger, prevInteger, streak(1), streakInteger;
cout << "Enter positive integers (end by negative integer) "<<endl;

do{
cin >> inputInteger;
if (prevInteger == inputInteger){
streak++;
streakInteger = inputInteger;
}

prevInteger = inputInteger;

}while(inputInteger > 0);

cout << streak<<"of"<<streakInteger;

How to determine the longest streak???

Use equal_range to find the size of an individual streak. Start from the
beginning, and each streak starts at the element following the end of
the previous streak. Once you've found the length of one streak, compare
it to the longest streak you've found so far, and if it's larger, save
it as the longest found so far.
 
J

Jerry Coffin

[ ... ]
Use equal_range to find the size of an individual streak.

I'm not sure what I was thinking there -- equal_range will _not_ work
for what's being done here. I apologize for the stupid advice. The
algorithm you (probably) want is find_end rather then equal_range.
 
C

curiousEngine

It works, it works!!! :D
Any improvements, suggestions please....

#include<iostream>
using namespace std;

int main(){
int inputInt, streakInt, prevInt, max, streakCount(1);

cout<<"Enter the integers (end by ending -ve):"<<endl;

do{
cin>>inputInt;

if (inputInt == prevInt){
streakCount++;
if (streakCount > max){
max = streakCount;
streakInt = inputInt;
}
}
else{
streakCount = 1;
}

prevInt = inputInt;

}while(inputInt > 0);

cout<<"Streak of"<<max<<" "<<streakInt<<"'s"<<"in a row"<<endl;
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top