2 / 2
Jun 2019

Can anyone tell me, why the first code is compiled correctly, and the second one has an execution error (SIGSEGV).
What is the difference ?

FIRST CODE:
#include
#include <math.h>
using namespace std;

int testy, obz, i_cias_na_pud, czas_jedzenia=86400;
float suma_zj_ciast=0, i_pud_z_ciastk;

int main()
{
cin>>testy; // number of tests

for(int i=1; i<=testy; i++)                 // depending on the number of tests
{
    int t[i-1];                                      // t = "time"
    int liczba_zj[i-1];                         // the number of cakes eaten
    cin>>obz>>i_cias_na_pud;       // number of eaters, the number of cookies for each box

    for(int i=1; i<=obz; i++)              // depending on the eaters
    {
        int t[i-1];
        int liczba_zj[i-1];
        cin>>t[i-1];                             // load the time
        liczba_zj[i-1]=czas_jedzenia/(t[i-1]);  /*the number of cakes eaten = time of eaten (86400) /  time of the first person */
        suma_zj_ciast+=liczba_zj[i-1];          //the sum of cakes eaten
    }

    i_pud_z_ciastk=suma_zj_ciast/i_cias_na_pud;           //the number of boxes with cookies

    cout<<ceil(i_pud_z_ciastk)<<endl;

    liczba_zj[i-1]=0;
    t[i-1]=0;
    suma_zj_ciast=0;
}
return 0;

}

                                                                            SECOND CODE:

#include
#include <math.h>
using namespace std;
int testy, obz, i_cias_na_pud, czas_jedzenia=86400;
float suma_zj_ciast=0, i_pud_z_ciastk;

int main()
{
cin>>testy; // number of tests

for(int i=1; i<=testy; i++)                 // depending on the number of tests
{
    int t[i-1];                                      // t = "time"
    int liczba_zj[i-1];                         // the number of cakes eaten
    cin>>obz>>i_cias_na_pud;       // number of eaters, the number of cookies for each box

    for(int i=1; i<=obz; i++)              // depending on the eaters
    {            
        cin>>t[i-1];                             // load the time
        liczba_zj[i-1]=czas_jedzenia/(t[i-1]);   /*the number of cakes eaten = time of eaten (86400) /  time of the first person */
        suma_zj_ciast+=liczba_zj[i-1];          //the sum of cakes eaten
    }

    i_pud_z_ciastk=suma_zj_ciast/i_cias_na_pud;           //the number of boxes with cookies

    cout<<ceil(i_pud_z_ciastk)<<endl;

    liczba_zj[i-1]=0;
    t[i-1]=0;
    suma_zj_ciast=0;
}
return 0;

}

  • created

    Jun '19
  • last reply

    Jun '19
  • 1

    reply

  • 820

    views

  • 2

    users

  • 1

    like

Did you notice that you’ve used the same loop counter i for both loops? So in First, the t array is sized for the number of eaters, and in Second, t is sized for the number of tests.

Also, there are two t arrays in First, each sized differently.