Beginners coding help?

When a ball is thrown with a speed U m/s at an angle of T radians
with the horizontal, the horizontal distance travelled or range,
R metres, is given by:-
R = U * U * sin( 2 * T ) / 9.8
Write a C program to produce a table showing the range of the ball
for speeds in the range 10 to 40 m/s in steps of 10 m/s and angles
with the horizontal in the range 15 to 75 degrees in steps of 15
degrees. The output from the program should be as follows:-
*****************************************
* Speed(m/s)* Angle(degrees) * Range(m) *
*****************************************
* 10 * 15 * *
* 10 * 30 * *
* 10 * 45 * *
* 10 * 60 * *
* 10 * 75 * *
* 20 * 15 * *
* 20 * 30 * *
* 20 * 45 * *
* 20 * 60 * *
* 20 * 75 * *
* 30 * 15 * *
* 30 * 30 * *
* 30 * 45 * *
* 30 * 60 * *
* 30 * 75 * *
* 40 * 15 * *
* 40 * 30 * *
* 40 * 45 * *
* 40 * 60 * *
* 40 * 75 * *
*****************************************
Hint: 180 degrees = 3.1416 radians

That's my question I have for class in 2 hours and i'm getting pretty worried i'm not going to be able to figure it out before getting there.

For those that are wondering why i've left it so soon before class it's because i've been trying to revise through the very basics of coding the last few days. Effectively i've jumped into a class that wasn't the beginners class, I messed up so i'm playing a lot of catch up.

Below is the code that I have, it's wrong....very wrong, I just don't understand how. Again, I am very new to coding hence why what i'm asking may sound a bit dumb.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include<stdio.h>
#include<math.h>
 int main ()
 {
int speed = 10;
int angle = 15;
int range = 0;
for (int i =1 ;i<10;i++)
 { 
printf("%d\n", i); 
}
 printf ("speed angle \n");
 temp_loop:
range = speed * speed * sin(2*angle)/9.8;
 printf ("%2d\t = %2d\n", speed, angle, range);
 speed+=10;
 if (speed <= 40 ) goto temp_loop; 
 }
edit : I have adjusted some things on it, seems like it makes slightly more sense now but it's still wrong, I feel like I have too many lines of code and i'm writing too much and it could be shortened down pretty easily could it not?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include<stdio.h>
#include<math.h>
 int main ()
 {
int speed = 10;
int angle = 15;
int range; 
for (int i =1 ;i<10;i++)
{
printf("%d\n", i); 
}
 printf ("speed angle \n");
range = speed * speed * sin(2*angle)/9.8;
 printf ("%2d\t = %2d\n", speed, angle, range);
 speed+=10;
 angle+=15;
 (speed <= 40 );  
 } 
DON'T try to write the whole of the code in one go. Break your problem down into a series of development steps. e.g.

Stage 1: fix the speed and see if you vary the angle of inclination from 15 to 75 degrees in steps of 15 and write this ANGLE out (don't worry yet about range or formatting) - clue: a for loop would do this.

Only once this is working ...

Stage 2: now wrap this loop in another loop so that the initial speed varies from 10 to 40 in steps of 10 and write speed and angle out.

Only once this is working ...

Stage 3: for each speed and angle work out the range (it's likely to be one line - similar to yours above but be careful to convert the angle to radians inside the sin() function.)

Finally ...

Stage 4: tart up the output format.
stage 1 : I don't understand how to fix the speed and vary the angle and why I need to and where i fix/vary it.

stage 2 : speed and angle was already written out so I would not need to write it out again after fixing the initial problem would I?

stage 3 : I don't understand how to work out the range? I thought that was in the question with the formulae I was given? which was R = U * U * sin( 2 * T ) / 9.8??


Sorry, I literally just don't understand anything, I don't know whether i'm just an idiot or i've been thrown in at the deep end or what it's really frustrating though and i'm sure it's frustrating for someone such as yourself that's trying to help me work it out.

Thanks for the reply by the way.
Step 1:
Calculate the range for 10m/s and 15deg by hand, so you know what the output should be.

Step 2:
Strip your program to the bare minimum required to calculate the range for 10m/s and 15 deg.
You are currently not using the hing about radians, so probably you will need to change something there.

After step 2 you have the right formula to have the program calculate the range, given the speed and the angle.
Step 3:
Start to variate the first variable. Keep one fixed and change the other. You could do this by placing the formula and the output in a for loop like this.
1
2
3
for(int speed = 10; speed <= 40; speed = speed + 10)
{
}


Step 4:
Check the output for different speeds and verify if your program is correct.

Step 5:
Start to variate the other variable. Do this by placing a simular for-loop inside the first for-loop (with a different variable name obviously).

Step 6:
Final testing.
Start with just your headers (preferably as the C++ versions) and the int main() {}. Delete the rest (or put it in another file if you can't bring yourself to delete this).


Stage 1: fix speed and vary angle.
i.e.
speed = 10 (for example) and write a "for" loop for angle with initial value 15, up to 75, incrementing by 15. (Read the section on loops in cplusplus tutorial on how to set these. You don't always need to increment by 1).

Make sure it can be printed out correctly:
15
30
45
60
75


Stage 2: another "for" loop around this to vary the speed, with first variable 10, up to 40, incrementing by 10 each time. Print speed and angle out:
10 15
10 30
10 45
10 60
10 75
20 15
20 30
etc. down to
40 75
(Nico has given you a typical control statement for this).


Stage 3: within the inner loop you should have correct speed (in m/s) and angle (in DEGREES). The only thing that is actually wrong with your one line calculating the range is that you have given it an angle in DEGREES and the sin() function - as in all decent programming languages, but probably unlike your calculator - expects it in RADIANS. So apply the appropriate multiplying factor within the sin() function (or create another variable in radians - your choice).

Write the range out along with your speed and angle.
10 15 5.102
10 30 8.837
etc. down to
40 75 81.63


Stage 4: I think you could probably sort out the formatting once you have finished; it isn't likely to be a high-mark part.


It's not frustrating for me. I need to learn how to teach just as much as you need to learn how to code. I am, however, trying to encourage you to face a multipart question in smaller steps.


Last edited on
well I did it in class with the help of my teacher but I still don't understand how I did it, he just told me what to write which didn't really teach me, so I deleted it, got home and re wrote the whole thing...i'm so close I know I am I just can't figure out what i'm missing.

This is what i've got, I KNOW 100000000% my 'for' loops are correct. it's maybe the bottom 2 lines of code and also i'm missing something with the PI as i'm sure I have to do something with PI???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 #include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main()
{
int speed, range, angle;
for ( speed  = 10; speed <= 40; speed+=10 )
{
 for ( angle = 15; angle <= 75; angle+=15 )
 {
    range = speed * speed * sin( 2 * angle ) / 9.8;
    printf("%2d, %2d, %2f" speed, range, angle );
}
}
}
Nico and I have both warned you that the sin() function expects an angle in radians. You are still giving it an angle in degrees. PI will be used as (part of) the conversion from degrees to radians. That is more of a maths question than a C++ one.
I got it, I don't really know how I got it though which is the frustrating part, I don't understand how I made it work but I made it work.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main ()
{
    int U, angle;
    double range, T; 
    for (U = 10; U <= 40; U+=10 )      
    {
        for ( angle = 15; angle <= 75; angle+=15 )
        { 
            T = angle * 2 * PI / 360;
            range = U * U * sin(2 * T ) / 9.8;
        printf ("%d * %d * %f\n", U, angle, range); 
        }
    }
} 


I need to somehow figure it out line and put it into simple basic terms for myself of what each line means etc.
You EVENTUALLY carried out Nico and my suggestion and converted degrees (in variable angle) to radians (in variable T) in your line
T = angle * 2 * PI / 360;

However, thanks for letting us know.
Topic archived. No new replies allowed.