0

I am printing scorecard using global variables. For some reason the loop is getting terminated after 9 iterations even with an infinite loop.

Here is my code:

int runs1[12]={7,128,143,213,2,102,5,1,0,0,0,9};
int balls1[12]={13,221,362,267,15,160,14,5,0,0,0,0};
int minutes1[12]={17,313,501,386,19,191,10,1,0,0,0,0};
int fours1[12]={1,11,14,17,0,8,0,0,0,0,0,0};
int sixes1[12]={0,1,0,2,0,1,0,0,0,0,0,0};
int runs2[12]={23,155,23,243,1,65,4,9,5,0,0,8};
int balls2[12]={35,267,39,287,5,102,16,19,4,0,0,0};
int minutes2[12]={47,350,47,447,6,129,38,28,20,0,0,0};
int fours2[12]={4,13,4,25,0,7,0,1,1,0,0,0};
int sixes2[12]={0,0,0,0,0,2,0,0,0,0,0,0};
char names[12][10] = {"KL Rahul","M Vijaya","CA Pujara","V Kohli","AM Rahane","RG Sharma","R Ashwin","WP Sharma","RP Jadeja","UT Yadav","I Sharma","Extra Runs"};
void displayScorecard(){
    int i = 0;
    int sr;
    printf("\nScorecard 1:");
    printf("\nPlayer name\tRuns\tBalls faced\tMinutes spend\t4's\t6's\tStrike rate");
    for( i = 0 ; i < 11 ; i++ ){
        printf("\n%d",i);
        sr = runs1[i]*100/balls1[i];
        if( i == 3 ){
            printf("\n%s\t\t%d\t%d\t\t%d\t\t%d\t%d\t%d",names[i],runs1[i],balls1[i],minutes1[i],fours1[i],sixes1[i],sr);  
            continue;  
        }
        printf("\n%s\t%d\t%d\t\t%d\t\t%d\t%d\t%d",names[i],runs1[i],balls1[i],minutes1[i],fours1[i],sixes1[i],sr);
    }
    printf("\nScorecard 2:");
    printf("\nPlayer name\tRuns\tBalls faced\tMinutes spend\t4's\t6's\tStrike rate");
    for( i = 0 ; i < 11 ; i++ ){
        sr = (runs2[i]*100/balls2[i]);
        if( i == 3 ){
            printf("\n%s\t\t%d\t%d\t\t%d\t\t%d\t%d\t%d",names[i],runs2[i],balls2[i],minutes2[i],fours2[i],sixes2[i],sr);  
            continue;  
        }
        printf("\n%s\t%d\t%d\t\t%d\t\t%d\t%d\t%d",names[i],runs2[i],balls2[i],minutes2[i],fours2[i],sixes2[i],sr);
    }
}
  • 4
    Please provide a [mre], which includes a function `main` and all `#include` directives. – Andreas Wenzel Jun 07 '21 at 10:33
  • 2
    Which loop? There are two loops in the function, which one terminates after 9 iterations? – CiaPan Jun 07 '21 at 10:39
  • 1
    there are two loops, none of them are infinite loop if u run them from 0 to 10. – Raj Singh Jun 07 '21 at 10:45
  • 1
    you reserved sparce for 12 '10' character names. Some names are longer (i.e. 'Extra runs'), so you might corrupt memory. – Serge Jun 07 '21 at 10:47
  • I would advise you to learn how to use a debugger and step through your code. Then you would have found where it goes wrong. – JHBonarius Jun 07 '21 at 10:49
  • To satisfy the (perfectly valid) comment made by @AndreasWenzel, just add the following (suitably formatted) to the end of your code block: `int main() { displayScorecard(); return 0; }`. (It's generally frowned upon on SO for other folks to edit your code!) – Adrian Mole Jun 07 '21 at 10:51
  • 1
    @Serge memory corruption will not happen ([read this](https://stackoverflow.com/a/13490908)), the bigger problem is that the strings are no longer zero-terminated. – JHBonarius Jun 07 '21 at 10:53
  • @AdrianMole: `"just add the following [...] to the end of your code block"` -- In my experience, it is generally a bad idea to tell OP exactly what to do in order to create a [mre], because that encourages them to simply edit the question accordingly, without verifying beforehand that the example actually reproduces the problem. Therefore, I generally recommend that when you do this, that you explicitly mention that OP should verify that the example actually reproduces the problem, before making the edit. However, in this specific case, based on your answer, it does not seem necessary. – Andreas Wenzel Jun 07 '21 at 11:18
  • @AndreasWenzel Yeah - I wouldn't normally be so direct, but that's all *I* needed to do, given the current code (which I just copy/pasted into my IDE). – Adrian Mole Jun 07 '21 at 11:27

1 Answers1

3

On the 9th run through your for loops, you are attempting an integer "divide-by-zero" operation (in both loops), as the balls1[i] and balls2[i] values when i is 8 (or more) are zero. An integer divide-by-zero causes undefined behaviour, which could include crashing the program: C Integer Behavior: Dividing by Zero.

To fix this, add a conditional calculation for your sr value; the following assigns zero to sr if the balls1[i] value is zero:

    for (i = 0; i < 11; i++) {
        printf("\n%d", i);
        if (balls1[i] == 0) sr = 0;
        else sr = runs1[i] * 100 / balls1[i];
        //...

And a similar modification for the balls2[] loop...

    for (i = 0; i < 11; i++) {
        if (balls2[i] == 0) sr = 0;
        else sr = (runs2[i] * 100 / balls2[i]);
        //...

Note: As mentioned in the comments, some of your names strings have more than 10 characters (don't forget to count the terminating nul character); you should thus increase the size of those strings:

char names[12][12] = {  "KL Rahul", "M Vijaya", "CA Pujara", "V Kohli", "AM Rahane", "RG Sharma", "R Ashwin",
                        "WP Sharma", "RP Jadeja", "UT Yadav", "I Sharma", "Extra Runs" };
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • ... and here's an alternative ordering of the data: [example](https://godbolt.org/z/havYdoEGM) – Ted Lyngmo Jun 07 '21 at 11:08
  • Instead of expanding the rectangular array of characters I'd rather advise to let the compiler allocate memory for strings and just declare an array of pointers to them: `char* names[12] = { "KL Rahul", "M Vijaya", ....}` – CiaPan Jun 07 '21 at 14:48
  • @CiaPan Sure, that would work very well for this example, but the pointer-array system can make things a bit ticklish should the OP ever want to serialize the data. – Adrian Mole Jun 07 '21 at 15:59
  • 1
    ... but I *do* like the structured approach suggested by @Ted. – Adrian Mole Jun 07 '21 at 16:00
  • 1
    @AdrianMole Thanks. Come to think of it, [this](https://godbolt.org/z/5P59xMEMP) would probably be even better. – Ted Lyngmo Jun 07 '21 at 16:24
  • @AdrianMole Serializing is a separate problem which requires a careful design itself, and fixing the data field size is not a cure (consider a file format compatibility should the application ever have to handle strings longer than initially expected). – CiaPan Jun 07 '21 at 16:44