Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5982231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:03:29+00:00 2026-05-22T22:03:29+00:00

I am attempting to compile and run a C program in Xcode. This program

  • 0

I am attempting to compile and run a C program in Xcode. This program requires a text file for reading input and another text file for writing the data. I have put the program, and these two text files in the Source folder. The program builds successfully, but when I try to run the program I get the error: GDB: Program received signal: “EXC_BAD_ACCESS”

What could be causing this?

 int main() {

     FILE *fp; 
     FILE *fr;

     //Declare and intialize all variables to be used
     float ax = 0, ay = 0, x = 0, y = 0, vx = 0, vy = 0; 
     float time = 0, deltaTime = .001; 
     float timeImpact = 0, vyImpact = 0, vxImpact = 0, xImpact = 0, yImpact = 0; 

     int numBounces = 0;

     //Coefficient of Restitution; epsilon = ex = ey
     float ex = .5;
     float ey = .5;

     fr = fopen("input_data.txt", "rt"); //Open file for reading

     fp = fopen( "output_data.txt", "w" ); // Open file for writing

     if(fr == NULL){ printf("File not found");} //if text file is not in directory...

     if(fp == NULL){ printf("File not found");} //if text file is not in directory...

     fscanf(fr, "ax: %f ay: %f x: %f y: %f vx: %f vy: %f\n", &ax, &ay, &x, &y, &vx, &vy); 

     while (time < 5) {

          //time = time + deltaTime
          time = time + deltaTime;
          //velocity[new] = velocity[old] + acc * deltaTime
          vx = vx + ax*deltaTime;
          vy = vy + ay*deltaTime;
          //position[new] = position[old] + velocity*deltaTime + .5*acc*(deltaTime)^2
          x = x + vx*deltaTime + (.5*ax*deltaTime*deltaTime);
          y = y + vy*deltaTime + (.5*ay*deltaTime*deltaTime);  

          //fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time);

               //Collision occurs; implement collision response
               if (y < 0) {

                  //Find time of collision by solving for t in equation vy*t + .5*ay*t^2 + y = 0
                  timeImpact = (-vy + sqrt((vy*vy) - (2*ay*y)) / (2*.5*ay)); //Collision time = 3.7?

                  //velocity = -epsilon*velocity[Impact] + acc*time
                  vy = (-1*ey)*vyImpact + ay*((deltaTime - time) - timeImpact);
                  vx = (-1*ex)*vxImpact + ay*((deltaTime - time) - timeImpact);                            

                  //Position = position[Impact] - epsilon*velocity[Impact]*time + 1/2*acc*time^2
                  x = xImpact - ex*vxImpact*((deltaTime - time) - timeImpact) + .5*ax*       ((deltaTime - time) - timeImpact) * ((deltaTime - time) - timeImpact);
                  y = yImpact - ey*vyImpact*((deltaTime - time) - timeImpact) + .5*ay*((deltaTime - time) - timeImpact) * ((deltaTime - time) - timeImpact);

                  //velocity = v[o] + ay(time)
                  vyImpact = vy + ay*(timeImpact - time);
                  vxImpact = vx + ax*(timeImpact - time); 

                  //position = position[o] + velocity(time) + 1/2*acc*time^2
                  xImpact = x + vx*(timeImpact - time) + .5*ax*(timeImpact - time)*(timeImpact - time);  
                  //yImpact = y + vy*(timeImpact - time) + .5*ay*(timeImpact - time)*(timeImpact - time); 

                  numBounces++; //Increment number of bounces ball takes

                  //fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time);
                  printf("timeImpact: %f\nxImpact: %f\nyImpact: %f\nvxImpact: %f\nvyImpact: %f\n", timeImpact, xImpact, yImpact, vxImpact, vyImpact);
                  printf("Number of Bounce(s): %d\n\n", numBounces); 
               }
     }

     fclose(fp); //Close output file
     fclose(fr); //Close input file

     system ("PAUSE"); 
     return 0;
  }

Sample Input:

ax: 0 ay: -9.8 x: 0 y: 50 vx: 8.66 vy: 5

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:03:30+00:00Added an answer on May 22, 2026 at 10:03 pm

    The program works almost properly. I got this output:

    timeImpact: 28.457277
    xImpact: 7798.595703
    yImpact: 0.000000
    vxImpact: 315.561859
    vyImpact: 73.370865
    Number of Bounce(s): 1
    
    timeImpact: -315.561096
    xImpact: 984718.250000
    yImpact: 0.000000
    vxImpact: -3213.589600
    vyImpact: 36.704834
    Number of Bounce(s): 2
    
    timeImpact: 3088.760254
    xImpact: 94468824.000000
    yImpact: 0.000000
    vxImpact: 31913.345703
    vyImpact: 55.056641
    Number of Bounce(s): 3
    
    timeImpact: -30290.189453
    xImpact: 9086046208.000000
    yImpact: 0.000000
    vxImpact: -312763.843750
    vyImpact: 45.875000
    Number of Bounce(s): 4
    
    timeImpact: 296834.687500
    xImpact: 872571076608.000000
    yImpact: 0.000000
    vxImpact: 3065398.750000
    vyImpact: 50.500000
    Number of Bounce(s): 5
    
    timeImpact: -2908993.750000
    xImpact: 83802579795968.000000
    yImpact: 0.000000
    vxImpact: -30040802.000000
    vyImpact: 48.000000
    Number of Bounce(s): 6
    
    timeImpact: 28507292.000000
    xImpact: 8047926899113984.000000
    yImpact: 0.000000
    vxImpact: 294391936.000000
    vyImpact: 64.000000
    Number of Bounce(s): 7
    
    timeImpact: -279371488.000000
    xImpact: 772922520746590208.000000
    yImpact: 0.000000
    vxImpact: -2885036544.000000
    vyImpact: 0.000000
    Number of Bounce(s): 8
    
    timeImpact: 2737840640.000000
    xImpact: 74231486855715487744.000000
    yImpact: 0.000000
    vxImpact: 28273358848.000000
    vyImpact: 0.000000
    Number of Bounce(s): 9
    
    timeImpact: -26831695872.000000
    xImpact: 7129642377640744583168.000000
    yImpact: 0.000000
    vxImpact: -277087289344.000000
    vyImpact: 0.000000
    Number of Bounce(s): 10
    
    timeImpact: nan
    xImpact: nan
    yImpact: 0.000000
    vxImpact: nan
    vyImpact: nan
    Number of Bounce(s): 11
    
    sh: PAUSE: command not found
    

    The last line might be the only problem. What the system("PAUSE") was supposed to do?

    EXC_BAD_ACCESS appears in the following line:

    fscanf(fr, "ax: %f ay: %f x: %f y: %f vx: %f vy: %f\n", &ax, &ay, &x, &y, &vx, &vy);
    

    Setting a breakpoint in it showed that fr was NULL, and that’s the problem. The path to the files were set relatively to the executable location, but notice that the executable goes to a build folder, which does not contain the input and output files (these are located in your project folder).

    In Xcode, select targets and then right click on your program. Choose the “Show in Finder” item, which will open a folder where the program is located. Drop your input file there and it should work.


    To clarify the arguments:

    In your main function, change the parameters to match this:

    int main (int argc, char *argv[])
    

    In short, there are two parameters. Every executable can receive arguments on its launch, and these parameter capture those arguments. For example, consider when you launch your program from the command line:

    $ ./myprogram file1.txt file2.txt
    

    The file1.txt and file2.txt are arguments, they will be passed to main. The main function has two arguments: argc and argv. The first is an int, holds the number of arguments, and the second is an array of strings, the arguments themselves.

    In the example I gave, argc is set to 3, as the first argument will be the program name (that’s a little detail). So:

    argv[1] == "file1.txt"
    argv[2] == "file2.txt"
    

    To do it in Xcode, first modify your main function to accept parameters. Then, modify this line:

    fr = fopen("input_data.txt", "rt"); //Open file for reading
    

    To this:

    fr = fopen(argv[1], "rt"); //Open file for reading
    

    Now, considering you’re on Xcode4 choose the “Product > Edit Scheme…” menu item. In the run configuration (side bar), choose the arguments tab and add a path to the desired input file in the “Arguments passed on Launch” section.

    That’s it. Your program will now run as before, but when you need to use another file, you don’t need to recompile the program. Just change the path in the argument list. This may appear to be more work, but once you have larger programs and starting using the command line, it will make more sense.

    Now go read a good book on C and another on Unix that explain the details I didn’t 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When attempting to compile my C# project, I get the following error: 'C:\Documents and
All this originated from me poking at a compiler warning message (C4267) when attempting
I am receiving the following compile time error when attempting to extend the SimpleButton
Just attempting this question I found in a past exam paper so that I
In attempting to run ./manage.py runserver or shell or any other command for that
while attempting to run rake, I run into the following error: heroku rake db:migrate
Attempting to insert an escape character into a table results in a warning. For
Attempting to print out a list of values from 2 different variables that are
Attempting to deploy a MOSS solution to a UAT server from dev server for
When attempting to call functions in math.h , I'm getting link errors like the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.