HI guys...ok so i post the code then underneath it i post my question...thanks
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//1ST rolling, roll will be minus 2
for (r=0; r<ROW-2; r++)for (c=0;c<5;c++) /*locking a particular row, in this case starting from row 0*/
{
if(LN[r][c]==LN [ROW-1][0]
||LN[r][c]==LN [ROW-1][1]
||LN[r][c]==LN [ROW-1][2]
||LN[r][c]==LN [ROW-1][3]
||LN[r][c]==LN [ROW-1][4]) /*if at least a figure of row analysed equal to a figure of jumping board row*/
{r+=1; Numberofpossiblenum+=5; // rolling one, would be r=+2 if rolling 2, or 3 if rolling 3 and so on...
cout<<endl<<"Row number:"<<r<<endl;
for (c=0;c<5;c++)
{Tally.push_back(LN[r][c]);
cout<<LN[r][c]<<", ";
}
}
}
cout<<endl<<endl<<"Number of possible numbers are: "<<Numberofpossiblenum<<endl<<endl;
cout<<"...and they are:\n";
for(int i=0;i<Tally.size();i++)
cout<<Tally[i]<<", ";
int i,n,j,counter;
cout<<endl<<endl<<"We will now find out how many times values are repeated in the Possible Numbers:"<<endl<<endl;
cout<<"Numbers place in order;"<<endl;
sort (Tally.begin(), Tally.end());
for(int i=0;i<Tally.size(); i++)
cout<<Tally[i]<<", ";
cout<<endl<<endl;
for (int i=0;i<Tally.size();i++)
for (int j=0;j<Tally.size();j++)
{
do (counter +=1); while (Tally[i]==Tally[j]);
cout<<Tally[i]<<" repeated "<<counter<<" times!";
}
return 0;
}
Ok so when i compile the code everything is ok...no problem at all..errors zero...but on the screen the program behaves as if from line 94 to 100 doesnt exit..i mean these lines
for (int i=0;i<Tally.size();i++)
for (int j=0;j<Tally.size();j++)
{
do (counter +=1); while (Tally[i]==Tally[j]);
cout<<Tally[i]<<" repeated "<<counter<<" times!";
}
doesnt do cout tally [] repeated counter times..doesnt write anything at all...and i find that soo strange..and i also noticed that it doesnt write program terminated return 0 time...press any key to continue... the cursor just keeps blinking as if it expecting me to insert something...till i close the window myself...
so i decided to use and online compiler..ideone.com was also telling me i exceeded compile time haha...what the hell is that? ok thats not the big deal...but ideone.com also ignored line 94 to 100 of my program just like my compiler...can anybody shed some light why this behavior? although there are no compile errors???
thanks in advance!!!
oh ok...thanks..guestgulkan...i didnt notice what ne555 wrote about the infinite loop thing..thanks again...i have to take a look at that again thanks....
ne555 what do you mean by an incorrect approach? i will love correct approaches thanks :)
but i already sorted out the thing...if you go a lil bit up in the lines you will see
sort (Tally.begin(), Tally.end()); and to make sure the program is doing the right thing i make it cout tally on the screen so i see for my self that is sorted out well and good ;)
now the problem is finding a way to count the thing...i believe it should be simple...but cant get the code to do that...guys we've been on this for soo long..i know you guys are experts...can someone pls go straight to the point with a short code to this simple 1,2,3 counting???
thanks again
//**Remove old infinite loop version**//
/*for (int i=0; i<Tally.size(); i++)
for (int j=0; j<Tally.size(); j++)
{
do
(counter +=1);
while (Tally[i]==Tally[j]);
cout<<Tally[i]<<" repeated "<<counter<<" times!";
}*/
for (int i=0; i<Tally.size(); ) //note the empty third part of this for loop
{
int val = Tally[i];
counter=0; //counter already declared somewhere above
while (Tally[i]==val && i < Tally.size())
{
(counter ++);
i++;
}
cout<< val<<" repeated "<<counter<<" times!" << '\n';
}
wow guestgulkan...this your method is fantastic... you know i started learning c++ about only a month and a half ago...can you please explain your for loop without the third part? ...not that the trick is not working...working fine..soo nice..am very glad..but for understanding and learning something new..I mean explain it in words...thanks again
for (int i=0; i<Tally.size(); ) //if we incremented i here, then we will have gone forward one index too many - see comments below as well
{
int val = Tally[i];
counter=0;
while (Tally[i]==val && i < Tally.size())
{
(counter ++);
i++; //i is incremented here
} //because each time this wile loop completes - i will be pointing to the right index.
cout<< val<<" repeated "<<counter<<" times!" << '\n';
}