strange stuff and compile time exceeded......?

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;

int ROW=44;

int LN [][5]={
/* 0*/ {37,13, 20, 84, 90},
/* 1*/ {26,83, 90, 72, 79},
/* 2 */{41,80, 39, 60, 40},
/* 3*/ {4, 46, 85, 61, 9 },
/* 4*/ {44,62, 74, 70, 49},
/* 5*/ {28,33, 72, 62, 88},
/* 6*/ {20,5, 6, 25, 71},
/* 7 */ {3, 9, 15, 84 ,34},
/*8*/{7, 52, 25, 83, 88},
/* 9*/{15,35, 67, 78, 6},
/*10*/ {53,38, 2, 10, 49},
/*11*/ {43,83, 17, 37, 15},
/*12*/ {90,36, 59, 3, 64},
/*13*/ {59,19, 28, 58, 76},
/*14*/ {51,81, 2, 8, 67},
/* 15/*/{64,72, 45, 86, 5},
/* 16*/{77,89, 38, 72, 45},
/*17*/{34,1, 74, 61, 89},
/*18*/{31,25, 75, 37, 84},
/*19*/ {23, 59, 57, 50, 77},
/*20*/ {79,90, 1, 23, 16},
/*21*/ {25,34, 22, 84, 7},
/*22*/ {70,46, 30, 37, 90},
/*23*/ {72,55, 75, 64, 71},
/*24*/ {90,8, 84, 36, 30},
/*25*/ {34,79, 81, 20, 12},
/*26*/ {82,56, 77, 49, 69},
/* 27*/ {42,30, 89, 84, 59},
/*28*/ {10,6, 68, 50, 87},
/*29*/ {28,7, 55, 62, 58},
/*30*/ {31,25, 86, 84, 87},
/*31*/ {36,66, 53, 3, 77},
/*32*/ {25,13, 72, 70, 47},
/*33*/{34, 77,80, 53, 33},
/*34*/ {86, 71, 23, 90, 8},
/*35*/{81, 45, 69, 38, 72},
/*36*/ {24, 61, 74, 41, 72},
/*37*/ {30, 27, 66, 43, 13},
/*38*/ {69, 20, 15, 64,59},
/*39*/ {14, 28, 59, 42, 67},
/*40*/ {2, 31, 88, 28, 55},
/*41*/{60, 73, 63, 19, 82},
/*42*/{67, 4, 42, 27, 29},
/*43*/ {16, 2, 51, 19, 74}
};
int r, c, k, Possiblenumbers1, Numberofpossiblenum=0;

int main ()
{
vector<int> Tally;

//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!!!
closed account (o3hC5Di1)
Hi there,

The correct syntax of a do while loop is as such:

1
2
3
4
do
{  //notice the curly braces
    counter +=1;
} while (Tally[i]==Tally[j]);


Also, counter +=1; is most commonly written as counter++;, which means the same, ie increment counter by 1.

Hope that helps.

All the best,
NwN
Last edited on
@NwN: No, it is not necessary to put the curly braces.

@OP:
1
2
3
do
    counter +=1;
while (Tally[i]==Tally[j]);
¿when will that loop break?

Also, if it compiles, then it is not `compile time exceeded'
Last edited on
closed account (o3hC5Di1)
Oh apologies, I was sure do (counter +=1); while (Tally[i]==Tally[j]); was invalid so I looked no further. My bad!

-N
So non of you got an idea why this is happening? nothing better to say??? guys pls help me
I would agree with ne555 about that infinite loop.



[[And as far as compile time exceeded - I think that is an online compiler related thing]]
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....
ok so theres an infinite loop there..how do i go about it? what do you guys suggest???
don't make it infinite.
¿what were you trying to do?
Am trying to count the number of times numbers repeat in vector Tally..thats all...can you give me a short code to do that??? Thanks
An (incorrect) approach
1
2
3
4
5
6
7
for (size_t K=0; K<Tally.size(); ++K){
   size_t counter=0;
   for (size_t L=0; L<Tally.size(); ++L)
      if (Tally[K]==Tally[L])
         ++counter;
   //...
}


I'd rather sort the vector, and then just count until the element changes.
Last edited on
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    //**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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   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';

    }
Topic archived. No new replies allowed.