trolley buses (program is crashing)

This is a discussion on trolley buses (program is crashing) within the C Programming forums, part of the General Programming Boards category; Problem - A - Codeforces i was solving this question.. using dynamically allocated memory as well as declaring arrays.. in ...

  1. #1
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25

    Unhappy trolley buses (program is crashing)

    Problem - A - Codeforces


    i was solving this question..
    using dynamically allocated memory as well as declaring arrays..
    in both methods
    after i give the input..
    input : 3 10 1000
    0 10
    program is not responding (it is crashing)

    i dunno why please help me..

    the code is (i feel i followed a indent style )

    Code:
     
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        long int n;
        float a,d,v_check,d_check,t_check,d_temp,t_temp;  //v_check uses 
        int i;
        float t_initial[1000],v[1000],t_final[1000];
        v_check=(float)sqrt(2*a*d);
        scanf("%d%f%f",&n,&a,&d);
        /*t_initial=(int *)malloc(n*sizeof(int));
        t_final=(int *)malloc(n*sizeof(int));
        v=(int *)malloc(n*sizeof(int));*/
        for(i=1;i<=n;i++)
        {
            t_initial[i]=0;
            t_final[i]=0;
            v[i]=0;
        }
         
         
            for(i=1;i<=n;i++)
        {
            scanf("%f%f",t_initial[i],v[i]);
        }   
        
        for(i=1;i<=n;i++)
        {
           if(v_check>v[i])
           {
              t_check=(float)v[i]/a;
              d_check=(float)(a*t_check*t_check);
           }                              
           d_temp=(float)(d-d_check);
           t_temp=(float)d_temp/v[i];
           t_final[i]=(float)(t_initial[i]+t_check+t_temp);         
        }
        for(i=1;i<=n;i++)
        {
           if(t_final[i]>t_final[0]&&t_final[i]>t_initial[0])
           {
              t_final[i]=(float)t_final[0];
           }                                      
        }   
    
    
        for(i=1;i<=n;i++)
        {
           printf("%f\n",t_final[i]);             
        }
        getch();
        
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by thriller500
    program is not responding (it is crashing)

    i dunno why please help me..
    You're writing past the bounds of the int array v.
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    If reading a long int, use "%ld" format specifier. Using "%d" gives undefined behaviour.

    The code, as you have posted it, comments out the malloc() calls.

    Type conversions are not needed for malloc(). If you need them to get the code to compile, it usually means you have forgotten to #include <stdlib.h> .... which you have. And forcing the code to compile in that way usually results in a program that crashes (due to round trip conversions between pointers and int losing information).

    In the loops, the indexing (for dynamically allocated arrays on n elements) runs from zero to n-1. Your code is indexing from 1 to n. The last time through the loop will fall off the end of the arrays, and give undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25
    ^^i eliminated the dynamic allocation because i felt that is causing the problem..
    i gave 3 inputs not n inputs ( i dunno think thats a problem) ..

    Zuk
    @ used it problem persists

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Show us what you have changed so far.
    Kurt

  6. #6
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25
    i got the problem...i didnt add & in the scanf function

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    I gave you a list of other problems with your code, but you appear to have ignored that.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why is this program crashing.
    By juice in forum C Programming
    Replies: 4
    Last Post: 12-21-2011, 08:38 AM
  2. crashing program, help
    By xniinja in forum Windows Programming
    Replies: 1
    Last Post: 07-07-2010, 03:57 PM
  3. crashing program, help
    By xniinja in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 11:42 AM
  4. Program crashing
    By bijan311 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2010, 08:55 AM
  5. Program crashing when I use IO
    By legit in forum C++ Programming
    Replies: 9
    Last Post: 05-31-2009, 07:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21