quadratic function

Mar 10, 2013 at 5:10am
When a ball is thrown upward at an angle of x degrees and with initial velocity V0 the height of the ball after t seconds is
h(t) = V0t sin( x) - 16 t2

Using an initial velocity of 144 feet per second and x = 60 degrees, write a program to output a table giving the height of the ball for each value of t from 0.5 to 8.0 in increments of 0.5 seconds.

This is what I had so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
#include <math.h>


int main()
{
	int x=60; //x is the measure of angle in degrees.
	int V= 144; //Initial velocity
	int t; //Seconds
	int h; // height
	h = V*t*sin(x)- 16t^2;

	cout << setw(3) << "Time" << setw(3)
	<< "Height" << endl;
	cout <<set(3) << "0.5" << h

Mar 10, 2013 at 5:18am
closed account (3qX21hU5)
Ok so what are you having trouble with?

I see some things wrong with it already but they might be just because the program is not complete but I'll list them anyways


1) in h = V*t*sin(x)- 16t^2; you are using t even though it has not yet been defined so you will get undefined behavior since you have no clue what number will be in int t. Never use a variable unless you are assigning to it without first defining it.

2) Probably nothing but on line 16 you are missing a semicolon.

But again we cant help you at all unless you ask a question. Just stating your assignment and what you have doesn't let us know what you are stuck on.
Mar 10, 2013 at 5:28am
How to calculate and output each value of h for every value of t in seconds starting from 0.5 to 8 by .5s
Last edited on Mar 10, 2013 at 5:29am
Mar 10, 2013 at 5:39am
closed account (3qX21hU5)
You would do it in a loop that starts at .5 and keeps looping until it hits 8. Here is some framework for your problem.

1
2
3
4
5
6
7
8
9
10
    double t = 0.5; // Remember to use double or float when working with decimals
    // Once t is equal to 8 the loop will stop.
    while (t != 8)
    {
        // This is where you will put the math to calculate h for every .5 seconds
        // You will also put the output here.
        
        //This increases t by .5 every time it goes through the loop.
        t += 0.5;
    }


Hope this helps.
Last edited on Mar 10, 2013 at 5:39am
Mar 10, 2013 at 7:45am
The way you have the equation written is incorrect in c++. Sin takes radians not degrees and t^2 isn't t squared. Try this instead h = V*t*sin(x*M_PI/180) - 16*t*t;
Mar 10, 2013 at 12:28pm

Is this right?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int x=60; //x is the measure of angle in degrees.
	int V= 144; //Initial velocity
	double t= 0.5; //Seconds
	int h; // height
	

	cout << setw(3) << "Time" << setw(3)
	<< "Height" << endl;
	while(t!=8)
	{
	h = V*t*sin(x*M_PI/180)- 16t^2;
	t += 0.5;
	cout << set(3) << t << set(3) << h << end;l
	
	}
	return 0;
Last edited on Mar 10, 2013 at 12:28pm
Mar 10, 2013 at 4:10pm
No, 16t^2 isn't doing what you think it is, it is not 16t squared. You need to change it to 16*t*t. See this working code http://ideone.com/LLVyIl
Mar 11, 2013 at 5:59am
closed account (3qX21hU5)
Also your increasing t before you output, make sure you output the result first then increase t.
Mar 12, 2013 at 12:31am
naraku,PEMDAS. Exponents come before multiplication. So it is 16* (t^2).
Parentheses around the t^2 here to emphasize the order of op. But thanks for trying to help anyway.
Mar 12, 2013 at 12:33am
^ doesn't have anything to do with exponents in C++.
Mar 12, 2013 at 12:34am
t to the 2nd power.
Mar 12, 2013 at 12:39am
closed account (3qX21hU5)
I don't think you understand
^ doesn't have anything to do with exponents in C++.


^ is the XOR (Exclusive Or) operator. This operation is performed between two bits (a and b). The result is 1 if either one of the two bits is 1, but not in the case that both are. There for, if neither or both of them are equal to 1 the result is 0.

They are giving you the right answer but you are unwilling to take it.

Mar 12, 2013 at 12:41am
I just wasn't sure if they were correct. I am still learning and I think I am allowed to doubt.
Mar 12, 2013 at 12:45am
I am still learning and I think I am allowed to doubt.


Because you're still learning, you should question yourself. Not the myriad people who say you're wrong.

At any rate, if you have an assumption and you believe it to be correct despite someone saying it's not... test it instead of being ignorant and asserting you are correct.
Mar 12, 2013 at 12:48am
closed account (3qX21hU5)
Your right you can doubt it if you want, but generally when you are still just learning and 2 experienced C++ programmer try and give you advice it is usually a good idea to listen to it instead of doubt it. Some mathematical operators are the same in C++ but a lot of them are different and it can get confusing sometimes.
Mar 12, 2013 at 1:39am
I have a quadratic formula program:

Sorry, haven't modified it yet to do i.

Modify to your needs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    double variable_a,variable_b,variable_c;
    cout << "This program calculates the roots of a trinomial" << endl;
    cout << "Enter the values for a,b,c: "<< endl;
    cout << "a: ";
    cin >> variable_a;
    cout << endl;
    cout << "b: ";
    cin >> variable_b;
    cout << endl;
    cout << "c: ";
    cin >> variable_c;
    cout << endl;

    double variable_b2 = variable_b * -1;
    double deviance = (variable_b * variable_b) - (4 * (variable_a * variable_c));
    double param, result;
    int dividend = 2 * variable_a;
    param = deviance;
    result = sqrt (param);
    if(deviance < 0)
    {
        cout << "There are no solutions";
        return 0;
    }

    double pre_total1 = variable_b2 + result;
    double total1 = pre_total1 / dividend;

    double pre_total2 = variable_b2 - result;
    double total2 = pre_total2 / dividend;
    if(total1 == total2)
    {
        printf ("Root: %lf\n", total1);
        system("PAUSE");
        return 0;
    }

  printf ("Roots: %lf , %lf\n", total1, total2 );
    system("PAUSE");
    return 0;
}
Last edited on Mar 12, 2013 at 1:40am
Topic archived. No new replies allowed.