Thread: Homework Help URGENT

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    41

    Homework Help URGENT

    The user is supposed to make a selection of an element in the array(whether Rocket 1 , Rocket 2 ,...) then use that selection to access the element of the array with the data (u,m0,...) and use it in my calculation.

    However, program shuts down after i enter the time.

    I don't know what's going on I mean i am trying to make the "selection" a number of an array so that in computeSpeed it can be used to indicate which element the user selected


    Code:
    #include <stdio.h>
    #include <math.h>
    #define G 9.81 // m/s
    
    
    typedef struct
    {
      char* name;
      double u;
      double m0;
      double q;
    
    
    }ROCKET ;
    
    
     ROCKET selectRocket(ROCKET *rockets, int selection);
     double computeSpeed(double t , ROCKET *rockets, int selection);
    
    
    void main()
    {
        double t;
        double v;
        int selection;
    
    
        ROCKET rockets[4]=
    {
                 {"Rocket 1", 2000.0, 150000.0, 2700.0},
                 {"Rocket 2", 1596.0, 300000.0, 5367.0},
                 {"Rocket 3", 3267.0, 543135.0, 8900.0},
                 {"Rocket 4", 984.0, 5468.0, 89.5}
     };
    
    
    
    
        selectRocket(rockets, selection);
        selection= &rockets;
    
    
    
    
       printf("Give the time t (s):");
       scanf("%lf",&t);
    
    
    
    
      v = computeSpeed (t, rockets,selection);
    
    
    
    
      printf(" v =%lf ",v);
    
    
    
    
    }
    ROCKET selectRocket(ROCKET *rockets, int selection)
    {
    
    
    printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[0].name ,rockets[0].u,rockets[0].m0,rockets[0].q);
    printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[1].name ,rockets[1].u,rockets[1].m0,rockets[1].q);
    printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[2].name ,rockets[2].u,rockets[2].m0,rockets[2].q);
    printf("%s : u = %.2lf  m0 = %.2lf   q = %.2lf \n", rockets[3].name ,rockets[3].u,rockets[3].m0,rockets[3].q);
    
    
    printf("Please select a rocket (1 to 4): ");
    scanf("%d",&selection);
    
    
    }
    
    
    double computeSpeed(double t , ROCKET *rockets, int selection)
    {
    
    
      double v;
    
    
    
    
       v = rockets[selection].q;
    
    
       return v;
    
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > ROCKET selectRocket(ROCKET *rockets, int selection)
    You need to make this return either the selection, or the rocket.
    At the moment, it's doing neither.

    > selection= &rockets;
    If your compiler isn't giving you a warning about this, then you need to upgrade.

    main returns int, not void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    it does give me an error and i removed it since it really didn't change anything , however even after making selectRocket return selection my program is still shutting down everytime before returning v.I also fixed the types considering it will be return an integer
    Last edited by Kamal Joub; 10-11-2017 at 11:08 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Well you need to post your latest code if you're still stuck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    this might help, it gets you a return so you can know which rocket to work on.
    Code:
    #include <stdio.h>
    #include <math.h>
    #define G 9.81 // m/s
    
    /**
     * The user is supposed to make a selection of an element in the array(whether Rocket 1 , Rocket 2 ,...)
     * then use that selection to access the element of the array with the data (u,m0,...) and use it in my calculation.
    
    However, program shuts down after i enter the time.
    
    I don't know what's going on I mean i am trying to make the "selection"
    * a number of an array so that in computeSpeed it can be used to indicate which element the user selected
    ***/
    
    typedef struct
    {
      char* name;
      double u;
      double m0;
      double q;
    }ROCKET;
    
    
        int selectRocket(ROCKET rockets[]);
        double computeSpeed(double t , ROCKET *rockets, int selection);
    
    int main()
    {
        double t;
        double v;
        int sel;
    
        ROCKET rockets[4]= {
                 {"Rocket 1", 2000.0, 150000.0, 2700.0},
                 {"Rocket 2", 1596.0, 300000.0, 5367.0},
                 {"Rocket 3", 3267.0, 543135.0, 8900.0},
                 {"Rocket 4", 984.0, 5468.0, 89.5}
     };
    
        sel =  selectRocket(rockets);
        printf("s = %d selection is = %s %f  %f  %f\n", sel, rockets[sel].name, rockets[sel].u,rockets[sel].m0,rockets[sel].q);
    
        // v = computeSpeed (t, rockets,selection);
        //  printf(" v =%lf ",v);
    return 0;
    }
    
    int selectRocket(ROCKET rockets[])
    {
     int s;
    
    printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[0].name ,rockets[0].u,rockets[0].m0,rockets[0].q);
    printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[1].name ,rockets[1].u,rockets[1].m0,rockets[1].q);
    printf("%s : u = %.2lf m0 = %.2lf q = %.2lf \n", rockets[2].name ,rockets[2].u,rockets[2].m0,rockets[2].q);
    printf("%s : u = %.2lf  m0 = %.2lf   q = %.2lf \n", rockets[3].name ,rockets[3].u,rockets[3].m0,rockets[3].q);
    
    
    printf("Please select a rocket (1 to 4): ");
    scanf("%d",&s);
    
    //to compensate for zero base array.
    return ( s - 1 );
    
    }
    double computeSpeed(double t , ROCKET *rockets, int selection)
    {
        double v;
        // no math computations ??
       v = rockets[selection].q;
       return v;
    }
    Results:
    Code:
    [userx@void bin]$ ./a.out
    Rocket 1 : u = 2000.00 m0 = 150000.00 q = 2700.00
    Rocket 2 : u = 1596.00 m0 = 300000.00 q = 5367.00
    Rocket 3 : u = 3267.00 m0 = 543135.00 q = 8900.00
    Rocket 4 : u = 984.00  m0 = 5468.00   q = 89.50
    Please select a rocket (1 to 4): 3
    
    s = 2 selection is = Rocket 3 3267.000000  543135.000000  8900.000000
    
    [userx@void bin]$ ./a.out
    Rocket 1 : u = 2000.00 m0 = 150000.00 q = 2700.00
    Rocket 2 : u = 1596.00 m0 = 300000.00 q = 5367.00
    Rocket 3 : u = 3267.00 m0 = 543135.00 q = 8900.00
    Rocket 4 : u = 984.00  m0 = 5468.00   q = 89.50
    Please select a rocket (1 to 4): 4
    
    s = 3 selection is = Rocket 4 984.000000  5468.000000  89.500000
    Last edited by userxbw; 10-12-2017 at 09:09 AM.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    oh yea, in main
    Code:
        sel =  selectRocket(rockets);
        printf("s = %d selection is = %s %f  %f  %f\n", sel, rockets[sel].name, rockets[sel].u,rockets[sel].m0,rockets[sel].q);
        printf(" v = %lf \n",computeSpeed(rockets,sel));
    function
    Code:
    double computeSpeed(ROCKET rockets[], int sel)
    {// no math computations
       return ( rockets[sel].q );
    }
    output
    Code:
    [userx@void bin]$ ./a.out
    Rocket 1 : u = 2000.00 m0 = 150000.00 q = 2700.00
    Rocket 2 : u = 1596.00 m0 = 300000.00 q = 5367.00
    Rocket 3 : u = 3267.00 m0 = 543135.00 q = 8900.00
    Rocket 4 : u = 984.00  m0 = 5468.00   q = 89.50
    Please select a rocket (1 to 4): 4
     
    s = 3 selection is = Rocket 4 984.000000  5468.000000  89.500000
     v = 89.500000
    if you're not done with your homework yet, if yes you maybe should post how to fixed it so others can learn from your results.

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    you were very helpful , i finished the homework thanks to you , the problem was i didnt not assign the selected value by the user to anything so when you used the "sel=" it kinda fixed the problem , also the fact that it's a zero base array and i needed to return a "s-1"helped because i kept getting the element in the array that was after the one chosen.

    btw FYI ,i do have math calculations but that wasn't the problem so i just used a "dummy equation" as a test.

    thank you very much for your help and just a quick question , in the protypes you used " intselectRocket(ROCKET rockets[]) " were the brackets after rockets to indicate that its an array ?

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kamal Joub View Post
    you were very helpful , i finished the homework thanks to you , the problem was i didnt not assign the selected value by the user to anything so when you used the "sel=" it kinda fixed the problem , also the fact that it's a zero base array and i needed to return a "s-1"helped because i kept getting the element in the array that was after the one chosen.

    btw FYI ,i do have math calculations but that wasn't the problem so i just used a "dummy equation" as a test.

    thank you very much for your help and just a quick question , in the protypes you used " intselectRocket(ROCKET rockets[]) " were the brackets after rockets to indicate that its an array ?
    Yes

    as well as adding more variables to be passed within the function. as you should have noticed the differences in that too with your functions and the ones I redid for you. also a different means of returning a value instead of adding it to a variable first then returning that just do it like how your computeSpeed is returning it : it saves on memory usage and time to execute the function.

    and you're welcomed, happy to have helped.
    Last edited by userxbw; 10-12-2017 at 05:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Correction !!Urgent!!
    By andryjohn in forum C Programming
    Replies: 12
    Last Post: 07-10-2009, 12:29 PM
  2. need urgent Homework help
    By zakimasmoudi in forum C Programming
    Replies: 2
    Last Post: 11-11-2007, 03:54 PM
  3. Urgent homework help for NEWBIE
    By Kazasan in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2004, 04:23 PM
  4. Urgent - Help - Homework Question
    By Sway2 in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2002, 01:35 PM
  5. Urgent Maths Homework :(
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2002, 09:01 PM

Tags for this Thread