Returning Angle Theta

I was almost really proud of myself and nearly ready to wake up my wife to tell her I have written my first program without the help of the internet or a book but… no such luck. She will be spared. I woke up at 3 am with a vision to output the hypotenuse of a triangle, it was easy. Too easy. I got greedy. I wanted theta. Now I’m stuck. Looking for a pointer(pardon the pun). Oh god nerd jokes. Jerk faces, please do what you do and destroy this but remember, nice people are cooler than you. Here is what I have.
Thanks,
Tyler
//
// main.c
// Pythagorean
//
// Created by Tyler Meek on 11/20/12.
// Copyright © 2012 Tyler Meek. All rights reserved.
//

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
// declair variables

int leg1;
int leg2;

              
// input leg1 (ordinate)
printf("What is the  length of leg1?\n");
scanf("%d", &leg1);
printf("leg 1 = %d\n", leg1);

// input leg2 (abbscissa)
printf("What is the lenth of leg2?\n");
scanf("%d", &leg2);
printf("leg 2 = %d\n", leg2);

// output hypotenuse
printf("Based on the Pythagorean Therom the hypotenuse is %.2f\n", sqrtf(leg1 * leg1 + leg2 * leg2));

// output theta
printf(" The angle theta is %.2f", atanhf(leg2 / leg1));


return 0;

I figured it out.
It works great

[code]
#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
// declair variables
float leg1;
float leg2;

// input leg1 (ordinate)
printf("What is the  length of leg1?\n");
scanf("%f", &leg1);
printf("leg 1 = %.2f\n", leg1);

// input leg2 (abbscissa)
printf("What is the lenth of leg2?\n");
scanf("%f", &leg2);
printf("leg 2 = %.2f\n", leg2);

// output hypotenuse
printf("Based on the Pythagorean Therom the hypotenuse is %.2f\n", sqrtf(leg1 * leg1 + leg2 * leg2));

// output theta in radians and degrees
float oppOverAdj = leg1 / leg2;
float radians = atanf(oppOverAdj);
float degrees = radians * (180/M_PI);
                      
printf("Theta is %.2f radians or %.2f degrees.\n", radians, degrees);[/code]

You have all the makings of an intrepid programmer in the making!

Here is an end of year challenge for you.

  1. Write a function that returns a pointer to another function that calculates the angle from the two sides of a right triangle. Call the pointer-returning function to get the angle-calculating function’s pointer and then invoke the function through the pointer to calculate the angle.

  2. Write a function that returns a struct containing pointers to two functions, one for calculating the angle, the other for calculating the hypotenuse from the two sides of a right triangle. Call the struct-returning function and then invoke the functions through the pointers to calculate the angle and hypotenuse.

Tip:

//  main.m

#import <Foundation/Foundation.h>

// radian_type is a typename for float
typedef float radian_type;

// PI is a typename name for a pointer to a function that returns a value of radian_type
typedef radian_type (*PI)();

// A function returning a pointer to a function
PI MyPI ();

int main (int argc, const char * argv[])
{
    PI pi = MyPI ();
    NSLog (@"PI=%f", pi ());
    NSLog (@"PI=%f", (*pi) ());
    return 0;
}

PI MyPI ()
{
    float ValueOfPI ();
    return ValueOfPI;
}

radian_type ValueOfPI ()
{
    return 4 * atan (1.0);
}

I’ll do it.
I just finished this and was going to hand it out to my physics classmates for next weeks lab. It is horribly repetitive and I am certain there is better way. I’ll bet there is way of setting up a framework and calling each new variable into the framework as needed with my yet to be understood pointers?
Thanks for the challenge.

[code]
//
// main.c
// Conservation of Momentum Lab Calculator
//
// Created by Tyler Meek on 11/21/12.
// Copyright © 2012 Tyler Meek. All rights reserved.
//
/* This is a program to speed the calculation time of completeing the Conservation of Momentum Lab 14 on page 149 of David Loyd’s Physics Laboratory Manual. */

#include <stdio.h>
#include <math.h>

// Initialization of all variables used in computation

float mass1, mass2, mass3, distance1To2, distance3To4, t1_1mass1B4, t2_1mass1B4, t3_1mass1B4, t4_1mass1B4, t5_1mass1B4,t1_1mass2Aft, t2_1mass2Aft, t3_1mass2Aft, t4_1mass2Aft, t5_1mass2Aft, t1_2mass1B4, t2_2mass1B4, t3_2mass1B4, t4_2mass1B4, t5_2mass1B4, t1_2mass1Aft, t2_2mass1Aft, t3_2mass1Aft, t4_2mass1Aft, t5_2mass1Aft, t1_2mass3Aft,t2_2mass3Aft, t3_2mass3Aft, t4_2mass3Aft, t5_2mass3Aft, t1_3mass1B4, t2_3mass1B4, t3_3mass1B4, t4_3mass1B4, t5_3mass1B4, t1_3mass3B4, t2_3mass3B4, t3_3mass3B4, t4_3mass3B4, t5_3mass3B4, t1_3mass1Aft, t2_3mass1Aft, t3_3mass1Aft, t4_3mass1Aft, t5_3mass1Aft, t1_3mass3Aft, t2_3mass3Aft, t3_3mass3Aft, t4_3mass3Aft, t5_3mass3Aft;

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

// Input of known values

//  General Overview of instruction to user
printf("Input Mass1, Mass2 and Mass3 in kilograms and the distance from point 1 to 2 and 3 to 4 in meters.\n");
printf("\n");

//  User input of mass 1
printf("What is Mass1? = \n");
scanf("%f", &mass1);

//  User input of mass 2
printf("What is Mass2? = \n");
scanf("%f", &mass2);

//  User input of mass 3
printf("What is Mass3? = \n");
scanf("%f", &mass3);

//  User input of the distance between point 1 and point 2
printf("What is the distance between point 1 and point 2?\n");
scanf("%f", &distance1To2);

//  User input of the distance between point 3 and point 4
printf("What is the distance between point 3 and point 4?\n");
scanf("%f", &distance3To4);

// BEGIN calculations for table 1

//  Trial 1

//  User input of time values for mass 1 before collision
printf("What is the time for trial 1 mass 1 before the collision?\n");
scanf("%f", &t1_1mass1B4);

printf("What is the time for trial 1 mass 2 after the collision?\n");
scanf("%f", &t1_1mass2Aft);

//  Calculate Velocity of mass 1 before collision
float velocity1m1 = distance1To2 / t1_1mass1B4;
printf("The velocity of mass 1, trial 1, before the collision is...%.3f (m/s)\n", velocity1m1);

//  Calculate Momentum of mass 1 before the collision
float momentum1m1 = velocity1m1 * mass1;
printf("The momentum is...%.3f(kg-m/s)\n", momentum1m1);

//  Calculate Velocity of mass 2 after the collision
float velocity1m2 = distance3To4 / t1_1mass2Aft;
printf("The velocity of mass 2, trial 1, after the collision is...%.3f (m/s)\n", velocity1m2);

//  Calculate Momentum of mass 2 after the collision
float momentum1m2 = velocity1m2 * mass2;
printf("The momentum is...%.3f(kg-m/s)\n", momentum1m2);

//  Calculate the percent difference (|P2-P1|/(|P1+P2|/2)) * 100
printf("The percent difference of trail 1 is %.2f\n", ((momentum1m2 - momentum1m1) / ((momentum1m1 + momentum1m2) / 2)) * 100);
printf("\n");
       

//  Trial 2

//  User input of time values for mass 1 before collision
printf("What is the time for trial 2 mass 1 before the collision?\n");
scanf("%f", &t2_1mass1B4);

printf("What is the time for trial 2 mass 2 after the collision?\n");
scanf("%f", &t2_1mass2Aft);

//  Calculate Velocity of mass 1 before collision
float velocity2m1 = distance1To2 / t2_1mass1B4;
printf("The velocity of mass 1, trial 2, before the collision is...%.3f (m/s)\n", velocity1m1);

//  Calculate Momentum of mass 1 before the collision
float momentum2m1 = velocity2m1 * mass1;
printf("The momentum is...%.3f(kg-m/s)\n", momentum2m1);

//  Calculate Velocity of mass 2 after the collision
float velocity2m2 = distance3To4 / t2_1mass2Aft;
printf("The velocity of mass 2, trial 2, after the collision is...%.3f (m/s)\n", velocity2m2);

//  Calculate Momentum of mass 2 after the collision
float momentum2m2 = velocity2m2 * mass2;
printf("The momentum is...%.3f(kg-m/s)\n", momentum2m2);

//  Calculate the percent difference (|P2-P1|/(|P1+P2|/2)) * 100
printf("The percent difference of trail 2 is %.2f\n", ((momentum2m2 - momentum2m1) / ((momentum2m1 + momentum2m2) / 2)) * 100);
printf("\n");


//  Trial 3

//  User input of time values for mass 1 before collision
printf("What is the time for trial 3 mass 1 before the collision?\n");
scanf("%f", &t3_1mass1B4);

printf("What is the time for trial 3 mass 2 after the collision?\n");
scanf("%f", &t3_1mass2Aft);

//  Calculate Velocity of mass 1 before collision
float velocity3m1 = distance1To2 / t3_1mass1B4;
printf("The velocity of mass 1, trial 3, before the collision is...%.3f (m/s)\n", velocity3m1);

//  Calculate Momentum of mass 1 before the collision
float momentum3m1 = velocity3m1 * mass1;
printf("The momentum is...%.3f(kg-m/s)\n", momentum3m1);

//  Calculate Velocity of mass 2 after the collision
float velocity3m2 = distance3To4 / t3_1mass2Aft;
printf("The velocity of mass 2, trial 3, after the collision is...%.3f (m/s)\n", velocity3m2);

//  Calculate Momentum of mass 2 after the collision
float momentum3m2 = velocity3m2 * mass2;
printf("The momentum is...%.3f(kg-m/s)\n", momentum3m2);

//  Calculate the percent difference (|P2-P1|/(|P1+P2|/2)) * 100
printf("The percent difference of trail 3 is %.2f\n", ((momentum3m2 - momentum3m1) / ((momentum3m1 + momentum3m2) / 2)) * 100);
printf("\n");


//  Trial 4

//  User input of time values for mass 1 before collision
printf("What is the time for trial 4 mass 1 before the collision?\n");
scanf("%f", &t4_1mass1B4);

printf("What is the time for trial 4 mass 2 after the collision?\n");
scanf("%f", &t4_1mass2Aft);

//  Calculate Velocity of mass 1 before collision
float velocity4m1 = distance1To2 / t4_1mass1B4;
printf("The velocity of mass 1, trial 4, before the collision is...%.3f (m/s)\n", velocity4m1);

//  Calculate Momentum of mass 1 before the collision
float momentum4m1 = velocity4m1 * mass1;
printf("The momentum is...%.3f(kg-m/s)\n", momentum4m1);

//  Calculate Velocity of mass 2 after the collision
float velocity4m2 = distance3To4 / t4_1mass2Aft;
printf("The velocity of mass 2, trial 4, after the collision is...%.3f (m/s)\n", velocity4m2);

//  Calculate Momentum of mass 2 after the collision
float momentum4m2 = velocity4m2 * mass2;
printf("The momentum is...%.3f(kg-m/s)\n", momentum4m2);

//  Calculate the percent difference (|P2-P1|/(|P1+P2|/2)) * 100
printf("The percent difference of trail 4 is %.2f\n", ((momentum4m2 - momentum4m1) / ((momentum4m1 + momentum4m2) / 2)) * 100);
printf("\n");


//  Trial 5

//  User input of time values for mass 1 before collision
printf("What is the time for trial 5 mass 1 before the collision?\n");
scanf("%f", &t5_1mass1B4);

printf("What is the time for trial 5 mass 2 after the collision?\n");
scanf("%f", &t5_1mass2Aft);

//  Calculate Velocity of mass 1 before collision
float velocity5m1 = distance1To2 / t5_1mass1B4;
printf("The velocity of mass 1, trial 5, before the collision is...%.3f (m/s)\n", velocity5m1);

//  Calculate Momentum of mass 1 before the collision
float momentum5m1 = velocity5m1 * mass1;
printf("The momentum is...%.3f(kg-m/s)\n", momentum5m1);

//  Calculate Velocity of mass 2 after the collision
float velocity5m2 = distance3To4 / t5_1mass2Aft;
printf("The velocity of mass 2, trial 5, after the collision is...%.3f (m/s)\n", velocity5m2);

//  Calculate Momentum of mass 2 after the collision
float momentum5m2 = velocity5m2 * mass2;
printf("The momentum is...%.3f(kg-m/s)\n", momentum5m2);

//  Calculate the percent difference (|P2-P1|/(|P1+P2|/2)) * 100
printf("The percent difference of trail 2 is %.2f\n", ((momentum5m2 - momentum5m1) / ((momentum5m1 + momentum5m2) / 2)) * 100);
printf("\n");

// END of Calculations for Table 1
printf(“THIS IS A MARKER TO DECIPHER THE END OF TABLE 1 and THE BEGINNING OF TABLE 2_______________________________________________\n”);
printf("\n");
printf("\n");

// BEGIN Calculations for Table 2

//  Trial 1

//  User input of time value  for mass 1 before collision
printf("What is the time for trial 1, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t1_2mass1B4);

//  User input of time value for mass 1 after collision
printf("What is the time for trial 1, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t1_2mass1Aft);

//  User input of time value for mass 3 after collision
printf("What is the time for trial 1, mass 3, after after the collision between points 3 and 4?\n");
scanf("%f", &t1_2mass3Aft);

//  Calculate the velocity of mass 1 before collision
float twoVelocity1m1 = distance1To2 / t1_2mass1B4;
printf("The velocity of mass 1, trial 1 before the collision is %.3f (m/s).\n", twoVelocity1m1);

//  Calculate the momentum of mass 1 before collision
float twoMomentum1m1 = twoVelocity1m1 * mass1;
printf("The momentum of mass 1, trial 1 before the collision is %.3f (kg-m/s).\n", twoMomentum1m1);

//  Calculate the velocity of mass 1 after collision
float twoVelocity1m1Aft = distance1To2 / t1_2mass1Aft;
printf("The velocity of mass 1, trial 1 after the collision is %.3f (m/s).\n", twoVelocity1m1Aft);

//  Calculate the momentum of mass 1 after the collision
float twoMomentum1m1Aft = twoVelocity1m1Aft * mass1;
printf("The momentum of mass 1, trial 1 after the collision is %.3f (kg-m/s).\n", twoMomentum1m1Aft);

//  Calculate the velocity  of mass 3 after the collision
float twoVelocity1m3 = distance3To4 / t1_2mass3Aft;
printf("The velocity of mass 3, trial 1 after the collision is %.3f (m/s).\n",twoVelocity1m3);

//  Calculate the momentum of mass 3 after the collision
float twoMomentum1m3 = twoVelocity1m3 * mass3;
printf("The momentum of mass 3, trial 1 after the collision is %.3f (kg-m/s).\n",twoMomentum1m3);

//  Total momentum after the collision in the system p1 + p3
float twoMomentum1m1m3 = twoMomentum1m1 + twoMomentum1m3;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", twoMomentum1m1m3);

//  Calculate the percent difference (|P3-P1|/(|P1+P3|/2)) * 100
printf("The percent difference of trial 1 is %.3f.\n", ((twoMomentum1m3 - twoMomentum1m1) / ((twoMomentum1m3 + twoMomentum1m1) / 2)) * 100);
printf("\n");


//  Trial 2

//  User input of time value  for mass 1 before collision
printf("What is the time for trial 2, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t2_2mass1B4);

//  User input of time value for mass 1 after collision
printf("What is the time for trial 2, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t2_2mass1Aft);

//  User input of time value for mass 3 after collision
printf("What is the time for trial 2, mass 3, after after the collision between points 3 and 4?\n");
scanf("%f", &t2_2mass3Aft);

//  Calculate the velocity of mass 1 before collision
float twoVelocity2m1 = distance1To2 / t2_2mass1B4;
printf("The velocity of mass 1, trial 2 before the collision is %.3f (m/s).\n", twoVelocity2m1);

//  Calculate the momentum of mass 1 before collision
float twoMomentum2m1 = twoVelocity2m1 * mass1;
printf("The momentum of mass 1, trial 2 before the collision is %.3f (kg-m/s).\n", twoMomentum2m1);

//  Calculate the velocity of mass 1 after collision
float twoVelocity2m1Aft = distance1To2 / t2_2mass1Aft;
printf("The velocity of mass 1, trial 2 after the collision is %.3f (m/s).\n", twoVelocity1m1Aft);

//  Calculate the momentum of mass 1 after the collision
float twoMomentum2m1Aft = twoVelocity2m1Aft * mass1;
printf("The momentum of mass 1, trial 2 after the collision is %.3f (kg-m/s).\n", twoMomentum2m1Aft);

//  Calculate the velocity  of mass 3 after the collision
float twoVelocity2m3 = distance3To4 / t2_2mass3Aft;
printf("The velocity of mass 3, trial 2 after the collision is %.3f (m/s).\n",twoVelocity2m3);

//  Calculate the momentum of mass 3 after the collision
float twoMomentum2m3 = twoVelocity2m3 * mass3;
printf("The momentum of mass 3, trial 2 after the collision is %.3f (kg-m/s).\n",twoMomentum2m3);

//  Total momentum after the collision in the system p1 + p3
float twoMomentum2m1m3 = twoMomentum2m1 + twoMomentum2m3;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", twoMomentum2m1m3);

//  Calculate the percent difference (|P3-P1|/(|P1+P3|/2)) * 100
printf("The percent difference of trial 2 is %.3f.\n", ((twoMomentum2m3 - twoMomentum2m1) / ((twoMomentum2m3 + twoMomentum2m1) / 2)) * 100);
printf("\n");

//  Trial 3

//  User input of time value  for mass 1 before collision
printf("What is the time for trial 3, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t3_2mass1B4);

//  User input of time value for mass 1 after collision
printf("What is the time for trial 3, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t3_2mass1Aft);

//  User input of time value for mass 3 after collision
printf("What is the time for trial 3, mass 3, after after the collision between points 3 and 4?\n");
scanf("%f", &t3_2mass3Aft);

//  Calculate the velocity of mass 1 before collision
float twoVelocity3m1 = distance1To2 / t3_2mass1B4;
printf("The velocity of mass 1, trial 3 before the collision is %.3f (m/s).\n", twoVelocity3m1);

//  Calculate the momentum of mass 1 before collision
float twoMomentum3m1 = twoVelocity3m1 * mass1;
printf("The momentum of mass 1, trial 3 before the collision is %.3f (kg-m/s).\n", twoMomentum3m1);

//  Calculate the velocity of mass 1 after collision
float twoVelocity3m1Aft = distance1To2 / t3_2mass1Aft;
printf("The velocity of mass 1, trial 3 after the collision is %.3f (m/s).\n", twoVelocity3m1Aft);

//  Calculate the momentum of mass 1 after the collision
float twoMomentum3m1Aft = twoVelocity3m1Aft * mass1;
printf("The momentum of mass 1, trial 3 after the collision is %.3f (kg-m/s).\n", twoMomentum3m1Aft);

//  Calculate the velocity  of mass 3 after the collision
float twoVelocity3m3 = distance3To4 / t3_2mass3Aft;
printf("The velocity of mass 3, trial 3 after the collision is %.3f (m/s).\n",twoVelocity3m3);

//  Calculate the momentum of mass 3 after the collision
float twoMomentum3m3 = twoVelocity3m3 * mass3;
printf("The momentum of mass 3, trial 3 after the collision is %.3f (kg-m/s).\n",twoMomentum3m3);

//  Total momentum after the collision in the system p1 + p3
float twoMomentum3m1m3 = twoMomentum3m1 + twoMomentum3m3;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", twoMomentum3m1m3);

//  Calculate the percent difference (|P3-P1|/(|P1+P3|/2)) * 100
printf("The percent difference of trial 3 is %.3f.\n", ((twoMomentum3m3 - twoMomentum3m1) / ((twoMomentum3m3 + twoMomentum3m1) / 2)) * 100);
printf("\n");

//  Trial 4

//  User input of time value  for mass 1 before collision
printf("What is the time for trial 4, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t4_2mass1B4);

//  User input of time value for mass 1 after collision
printf("What is the time for trial 4, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t4_2mass1Aft);

//  User input of time value for mass 3 after collision
printf("What is the time for trial 4, mass 3, after after the collision between points 3 and 4?\n");
scanf("%f", &t4_2mass3Aft);

//  Calculate the velocity of mass 1 before collision
float twoVelocity4m1 = distance1To2 / t4_2mass1B4;
printf("The velocity of mass 1, trial 4 before the collision is %.3f (m/s).\n", twoVelocity4m1);

//  Calculate the momentum of mass 1 before collision
float twoMomentum4m1 = twoVelocity4m1 * mass1;
printf("The momentum of mass 1, trial 4 before the collision is %.3f (kg-m/s).\n", twoMomentum4m1);

//  Calculate the velocity of mass 1 after collision
float twoVelocity4m1Aft = distance1To2 / t4_2mass1Aft;
printf("The velocity of mass 1, trial 4 after the collision is %.3f (m/s).\n", twoVelocity4m1Aft);

//  Calculate the momentum of mass 1 after the collision
float twoMomentum4m1Aft = twoVelocity4m1Aft * mass1;
printf("The momentum of mass 1, trial 4 after the collision is %.3f (kg-m/s).\n", twoMomentum4m1Aft);

//  Calculate the velocity  of mass 3 after the collision
float twoVelocity4m3 = distance3To4 / t4_2mass3Aft;
printf("The velocity of mass 3, trial 4 after the collision is %.3f (m/s).\n",twoVelocity4m3);

//  Calculate the momentum of mass 3 after the collision
float twoMomentum4m3 = twoVelocity4m3 * mass3;
printf("The momentum of mass 3, trial 4 after the collision is %.3f (kg-m/s).\n",twoMomentum4m3);

//  Total momentum after the collision in the system p1 + p3
float twoMomentum4m1m3 = twoMomentum4m1 + twoMomentum4m3;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", twoMomentum4m1m3);

//  Calculate the percent difference (|P3-P1|/(|P1+P3|/2)) * 100
printf("The percent difference of trial 4 is %.3f.\n", ((twoMomentum4m3 - twoMomentum4m1) / ((twoMomentum4m3 + twoMomentum4m1) / 2)) * 100);
printf("\n");

//  Trial 5

//  User input of time value  for mass 1 before collision
printf("What is the time for trial 5, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t5_2mass1B4);

//  User input of time value for mass 1 after collision
printf("What is the time for trial 5, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t5_2mass1Aft);

//  User input of time value for mass 3 after collision
printf("What is the time for trial 5, mass 3, after after the collision between points 3 and 4?\n");
scanf("%f", &t5_2mass3Aft);

//  Calculate the velocity of mass 1 before collision
float twoVelocity5m1 = distance1To2 / t5_2mass1B4;
printf("The velocity of mass 1, trial 5 before the collision is %.3f (m/s).\n", twoVelocity5m1);

//  Calculate the momentum of mass 1 before collision
float twoMomentum5m1 = twoVelocity5m1 * mass1;
printf("The momentum of mass 1, trial 5 before the collision is %.3f (kg-m/s).\n", twoMomentum5m1);

//  Calculate the velocity of mass 1 after collision
float twoVelocity5m1Aft = distance1To2 / t5_2mass1Aft;
printf("The velocity of mass 1, trial 5 after the collision is %.3f (m/s).\n", twoVelocity5m1Aft);

//  Calculate the momentum of mass 1 after the collision
float twoMomentum5m1Aft = twoVelocity5m1Aft * mass1;
printf("The momentum of mass 1, trial 5 after the collision is %.3f (kg-m/s).\n", twoMomentum5m1Aft);

//  Calculate the velocity  of mass 3 after the collision
float twoVelocity5m3 = distance3To4 / t5_2mass3Aft;
printf("The velocity of mass 3, trial 5 after the collision is %.3f (m/s).\n", twoVelocity5m3);

//  Calculate the momentum of mass 3 after the collision
float twoMomentum5m3 = twoVelocity5m3 * mass3;
printf("The momentum of mass 3, trial 5 after the collision is %.3f (kg-m/s).\n",twoMomentum5m3);

//  Total momentum after the collision in the system p1 + p3
float twoMomentum5m1m3 = twoMomentum5m1 + twoMomentum5m3;
printf("The total momentum in the system after the collision is %.3f (m/s).\n", twoMomentum5m1m3);

//  Calculate the percent difference (|P3-P1|/(|P1+P3|/2)) * 100
printf("The percent difference of trial 5 is %.3f.\n", ((twoMomentum5m3 - twoMomentum5m1) / ((twoMomentum5m3 + twoMomentum5m1) / 2)) * 100);
printf("\n");

//  END of Calculations for Table 2
printf("THIS IS A MARKER TO DECIPHER THE END OF TABLE 2 and THE BEGINNING OF TABLE 3_______________________________________________\n");
printf("\n");
printf("\n");

// Begin Calculations for Tabel 3

//TRIAL 1

//  User input of time value for mass 1 before the collisiion
printf("What is the time for trial 1, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t1_3mass1B4);

//  User input of time value for mass 3 before the collision
printf("What is the time for trial 1, mass 3, before the collision between points 4 and 3?\n");
scanf("%f", &t1_3mass3B4);

//  User input of time value for mass 1 after the collision
printf("What is the time for trial 1, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t1_3mass1Aft);

//  User input of time value for mass 3 after the collision
printf("What is the time for trial 1, mass 3, after the collision between points 3 and 4?\n");
scanf("%f", &t1_3mass3Aft);

//  Calculate the velocity of mass 1 before the collision
float threeVelocity1m1 = distance1To2 / t1_3mass1B4;
printf("The velocity of mass 1, trial 1 before the collision is %.3f (m/s).\n", threeVelocity1m1);

//  Calculate the momentum of mass 1 before the collision
float threeMomentum1m1 = threeVelocity1m1 * mass1;
printf("The momentum of mass 1, trial 1 before the collision is %.3f (kg-m/s).\n", threeMomentum1m1);

//  Calculate the velocity of mass 3 before the collision
float threeVelocity1m3 = distance3To4 / t1_3mass3B4;
printf("The velocity of mass 3, trial 1 before the collision is %.3f (m/s).\n", threeVelocity1m1);

//  Calculate the momentum of mass 3 before the collision
float threeMomentum1m3 = threeVelocity1m3 * mass1;
printf("The momentum of mass 3, trial 1 before the collision is %.3f (kg-m/s).\n", threeMomentum1m3);

//  Claculate the velocity of mass 1 after the collision
float threeVelocity1m1Aft = distance1To2 / t1_3mass1Aft;
printf("The velocity of mass 1, trial 1 after the collision is %.3f (m/s).\n", threeVelocity1m1Aft);

//  Calculate the momentum of mass 1 after the collision
float threeMomentum1m1Aft = threeVelocity1m1Aft * mass1;
printf("The momentum of mass 1, trial 1 after the collision is %.3f (kg-m/s).\n", threeMomentum1m1Aft);

//  Calculate the velocity of mass 3 after the collision
float threeVelocity1m3Aft = distance3To4 / t1_3mass3Aft;
printf("The velocity of mass 3, trial 1 after the collision is %.3f (m/s).\n", threeVelocity1m1Aft);

//  Calcculate the momentum of mass 3 after the collision
float threeMomentum1m3Aft = threeVelocity1m3Aft * mass1;
printf("The momentum of mass 3, trial 1 after the collision is %.3f (kg-m/s).\n", threeMomentum1m3Aft);

//  Total momentum in the system before the collision (P1 + P3)
float threeMomentum1m1m3 = threeMomentum1m1 + threeMomentum1m3;
printf("The total momentum in the system before the collision is %.3f (kg-m/s).\n", threeMomentum1m1m3);

//  Total momentum in the system after the collision (P1 + P3)
float threeMomentum1m1m3Aft = threeMomentum1m1Aft + threeMomentum1m3Aft;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", threeMomentum1m1m3Aft);

//  Trial 2
//  User input of time value for mass 1 before the collisiion
printf("What is the time for trial 2, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t2_3mass1B4);

//  User input of time value for mass 3 before the collision
printf("What is the time for trial 2, mass 3, before the collision between points 4 and 3?\n");
scanf("%f", &t2_3mass3B4);

//  User input of time value for mass 1 after the collision
printf("What is the time for trial 2, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t2_3mass1Aft);

//  User input of time value for mass 3 after the collision
printf("What is the time for trial 2, mass 3, after the collision between points 3 and 4?\n");
scanf("%f", &t2_3mass3Aft);

//  Calculate the velocity of mass 1 before the collision
float threeVelocity2m1 = distance1To2 / t2_3mass1B4;
printf("The velocity of mass 1, trial 2 before the collision is %.3f (m/s).\n", threeVelocity2m1);

//  Calculate the momentum of mass 1 before the collision
float threeMomentum2m1 = threeVelocity2m1 * mass1;
printf("The momentum of mass 1, trial 2 before the collision is %.3f (kg-m/s).\n", threeMomentum2m1);

//  Calculate the velocity of mass 3 before the collision
float threeVelocity2m3 = distance3To4 / t2_3mass3B4;
printf("The velocity of mass 3, trial 2 before the collision is %.3f (m/s).\n", threeVelocity2m1);

//  Calculate the momentum of mass 3 before the collision
float threeMomentum2m3 = threeVelocity2m3 * mass1;
printf("The momentum of mass 3, trial 2 before the collision is %.3f (kg-m/s).\n", threeMomentum2m3);

//  Claculate the velocity of mass 1 after the collision
float threeVelocity2m1Aft = distance1To2 / t2_3mass1Aft;
printf("The velocity of mass 1, trial 2 after the collision is %.3f (m/s).\n", threeVelocity2m1Aft);

//  Calculate the momentum of mass 1 after the collision
float threeMomentum2m1Aft = threeVelocity2m1Aft * mass1;
printf("The momentum of mass 1, trial 2 after the collision is %.3f (kg-m/s).\n", threeMomentum2m1Aft);

//  Calculate the velocity of mass 3 after the collision
float threeVelocity2m3Aft = distance3To4 / t2_3mass3Aft;
printf("The velocity of mass 3, trial 2 after the collision is %.3f (m/s).\n", threeVelocity2m1Aft);

//  Calcculate the momentum of mass 3 after the collision
float threeMomentum2m3Aft = threeVelocity2m3Aft * mass1;
printf("The momentum of mass 3, trial 2 after the collision is %.3f (kg-m/s).\n", threeMomentum2m3Aft);

//  Total momentum in the system before the collision (P1 + P3)
float threeMomentum2m1m3 = threeMomentum2m1 + threeMomentum2m3;
printf("The total momentum in the system before the collision is %.3f (kg-m/s).\n", threeMomentum2m1m3);

//  Total momentum in the system after the collision (P1 + P3)
float threeMomentum2m1m3Aft = threeMomentum2m1Aft + threeMomentum2m3Aft;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", threeMomentum2m1m3Aft);

//  Trial 3

//  User input of time value for mass 1 before the collisiion
printf("What is the time for trial 3, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t3_3mass1B4);

//  User input of time value for mass 3 before the collision
printf("What is the time for trial 3, mass 3, before the collision between points 4 and 3?\n");
scanf("%f", &t3_3mass3B4);

//  User input of time value for mass 1 after the collision
printf("What is the time for trial 3, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t3_3mass1Aft);

//  User input of time value for mass 3 after the collision
printf("What is the time for trial 3, mass 3, after the collision between points 3 and 4?\n");
scanf("%f", &t3_3mass3Aft);

//  Calculate the velocity of mass 1 before the collision
float threeVelocity3m1 = distance1To2 / t3_3mass1B4;
printf("The velocity of mass 1, trial 3 before the collision is %.3f (m/s).\n", threeVelocity3m1);

//  Calculate the momentum of mass 1 before the collision
float threeMomentum3m1 = threeVelocity3m1 * mass1;
printf("The momentum of mass 1, trial 3 before the collision is %.3f (kg-m/s).\n", threeMomentum3m1);

//  Calculate the velocity of mass 3 before the collision
float threeVelocity3m3 = distance3To4 / t3_3mass3B4;
printf("The velocity of mass 3, trial 3 before the collision is %.3f (m/s).\n", threeVelocity3m1);

//  Calculate the momentum of mass 3 before the collision
float threeMomentum3m3 = threeVelocity3m3 * mass1;
printf("The momentum of mass 3, trial 3 before the collision is %.3f (kg-m/s).\n", threeMomentum3m3);

//  Claculate the velocity of mass 1 after the collision
float threeVelocity3m1Aft = distance1To2 / t3_3mass1Aft;
printf("The velocity of mass 1, trial 3 after the collision is %.3f (m/s).\n", threeVelocity3m1Aft);

//  Calculate the momentum of mass 1 after the collision
float threeMomentum3m1Aft = threeVelocity3m1Aft * mass1;
printf("The momentum of mass 1, trial 3 after the collision is %.3f (kg-m/s).\n", threeMomentum3m1Aft);

//  Calculate the velocity of mass 3 after the collision
float threeVelocity3m3Aft = distance3To4 / t1_3mass3Aft;
printf("The velocity of mass 3, trial 3 after the collision is %.3f (m/s).\n", threeVelocity1m1Aft);

//  Calcculate the momentum of mass 3 after the collision
float threeMomentum3m3Aft = threeVelocity3m3Aft * mass1;
printf("The momentum of mass 3, trial 3 after the collision is %.3f (kg-m/s).\n", threeMomentum3m3Aft);

//  Total momentum in the system before the collision (P1 + P3)
float threeMomentum3m1m3 = threeMomentum3m1 + threeMomentum3m3;
printf("The total momentum in the system before the collision is %.3f (kg-m/s).\n", threeMomentum3m1m3);

//  Total momentum in the system after the collision (P1 + P3)
float threeMomentum3m1m3Aft = threeMomentum3m1Aft + threeMomentum3m3Aft;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", threeMomentum3m1m3Aft);

//  Trial 4
//  User input of time value for mass 1 before the collisiion
printf("What is the time for trial 4, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t4_3mass1B4);

//  User input of time value for mass 3 before the collision
printf("What is the time for trial 4, mass 3, before the collision between points 4 and 3?\n");
scanf("%f", &t4_3mass3B4);

//  User input of time value for mass 1 after the collision
printf("What is the time for trial 4, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t4_3mass1Aft);

//  User input of time value for mass 3 after the collision
printf("What is the time for trial 4, mass 3, after the collision between points 3 and 4?\n");
scanf("%f", &t4_3mass3Aft);

//  Calculate the velocity of mass 1 before the collision
float threeVelocity4m1 = distance1To2 / t4_3mass1B4;
printf("The velocity of mass 1, trial 4 before the collision is %.3f (m/s).\n", threeVelocity4m1);

//  Calculate the momentum of mass 1 before the collision
float threeMomentum4m1 = threeVelocity4m1 * mass1;
printf("The momentum of mass 1, trial 4 before the collision is %.3f (kg-m/s).\n", threeMomentum4m1);

//  Calculate the velocity of mass 3 before the collision
float threeVelocity4m3 = distance3To4 / t4_3mass3B4;
printf("The velocity of mass 3, trial 4 before the collision is %.3f (m/s).\n", threeVelocity4m1);

//  Calculate the momentum of mass 3 before the collision
float threeMomentum4m3 = threeVelocity4m3 * mass1;
printf("The momentum of mass 3, trial 4 before the collision is %.3f (kg-m/s).\n", threeMomentum4m3);

//  Claculate the velocity of mass 1 after the collision
float threeVelocity4m1Aft = distance1To2 / t4_3mass1Aft;
printf("The velocity of mass 1, trial 4 after the collision is %.3f (m/s).\n", threeVelocity4m1Aft);

//  Calculate the momentum of mass 1 after the collision
float threeMomentum4m1Aft = threeVelocity4m1Aft * mass1;
printf("The momentum of mass 1, trial 4 after the collision is %.3f (kg-m/s).\n", threeMomentum4m1Aft);

//  Calculate the velocity of mass 3 after the collision
float threeVelocity4m3Aft = distance3To4 / t4_3mass3Aft;
printf("The velocity of mass 3, trial 4 after the collision is %.3f (m/s).\n", threeVelocity4m1Aft);

//  Calcculate the momentum of mass 3 after the collision
float threeMomentum4m3Aft = threeVelocity4m3Aft * mass1;
printf("The momentum of mass 3, trial 4 after the collision is %.3f (kg-m/s).\n", threeMomentum4m3Aft);

//  Total momentum in the system before the collision (P1 + P3)
float threeMomentum4m1m3 = threeMomentum4m1 + threeMomentum4m3;
printf("The total momentum in the system before the collision is %.3f (kg-m/s).\n", threeMomentum4m1m3);

//  Total momentum in the system after the collision (P1 + P3)
float threeMomentum4m1m3Aft = threeMomentum4m1Aft + threeMomentum4m3Aft;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", threeMomentum4m1m3Aft);

//  Trial 5
//  User input of time value for mass 1 before the collisiion
printf("What is the time for trial 5, mass 1, before the collision between points 1 and 2?\n");
scanf("%f", &t5_3mass1B4);

//  User input of time value for mass 3 before the collision
printf("What is the time for trial 5, mass 3, before the collision between points 4 and 3?\n");
scanf("%f", &t5_3mass3B4);

//  User input of time value for mass 1 after the collision
printf("What is the time for trial 5, mass 1, after the collision between points 2 and 1?\n");
scanf("%f", &t5_3mass1Aft);

//  User input of time value for mass 3 after the collision
printf("What is the time for trial 5, mass 3, after the collision between points 3 and 4?\n");
scanf("%f", &t5_3mass3Aft);

//  Calculate the velocity of mass 1 before the collision
float threeVelocity5m1 = distance1To2 / t5_3mass1B4;
printf("The velocity of mass 1, trial 5 before the collision is %.3f (m/s).\n", threeVelocity5m1);

//  Calculate the momentum of mass 1 before the collision
float threeMomentum5m1 = threeVelocity5m1 * mass1;
printf("The momentum of mass 1, trial 5 before the collision is %.3f (kg-m/s).\n", threeMomentum5m1);

//  Calculate the velocity of mass 3 before the collision
float threeVelocity5m3 = distance3To4 / t5_3mass3B4;
printf("The velocity of mass 3, trial 5 before the collision is %.3f (m/s).\n", threeVelocity5m1);

//  Calculate the momentum of mass 3 before the collision
float threeMomentum5m3 = threeVelocity5m3 * mass1;
printf("The momentum of mass 3, trial 5 before the collision is %.3f (kg-m/s).\n", threeMomentum1m3);

//  Claculate the velocity of mass 1 after the collision
float threeVelocity5m1Aft = distance1To2 / t5_3mass1Aft;
printf("The velocity of mass 1, trial 5 after the collision is %.3f (m/s).\n", threeVelocity5m1Aft);

//  Calculate the momentum of mass 1 after the collision
float threeMomentum5m1Aft = threeVelocity5m1Aft * mass1;
printf("The momentum of mass 1, trial 5 after the collision is %.3f (kg-m/s).\n", threeMomentum5m1Aft);

//  Calculate the velocity of mass 3 after the collision
float threeVelocity5m3Aft = distance3To4 / t5_3mass3Aft;
printf("The velocity of mass 3, trial 5 after the collision is %.3f (m/s).\n", threeVelocity5m1Aft);

//  Calcculate the momentum of mass 3 after the collision
float threeMomentum5m3Aft = threeVelocity5m3Aft * mass1;
printf("The momentum of mass 3, trial 5 after the collision is %.3f (kg-m/s).\n", threeMomentum5m3Aft);

//  Total momentum in the system before the collision (P1 + P3)
float threeMomentum5m1m3 = threeMomentum5m1 + threeMomentum5m3;
printf("The total momentum in the system before the collision is %.3f (kg-m/s).\n", threeMomentum5m1m3);

//  Total momentum in the system after the collision (P1 + P3)
float threeMomentum5m1m3Aft = threeMomentum5m1Aft + threeMomentum5m3Aft;
printf("The total momentum in the system after the collision is %.3f (kg-m/s).\n", threeMomentum5m1m3Aft);
printf("\n");
printf("\n");
printf("\n");

// This Marks the end of the lab and also the end of this program
printf(“Thank you for using a Your Momma program. If you would like to donate a buck please feel free to give it to your nearest homeless person.”);

return 0;[/code]

You should always check the return value of scanf (…) to make sure that the input was successfully read, for the user my have entered some garbage.

Here is an example:

//  main.m

#import <Foundation/Foundation.h>

// type for real numbers
typedef float real_value_type;

// 1 for good input, 0 for garbage
typedef BOOL isv_type;

isv_type GetRealValue (const char *prompt, real_value_type *v);

int main (int argc, const char * argv[])
{
    real_value_type v;
    GetRealValue ("What is Mass1?", &v);
    printf ("Mass1 = %f\n", v);

    return 0;
}

isv_type ParseRealValue (real_value_type *v);

isv_type GetRealValue (const char *prompt, real_value_type *v)
{
    isv_type rv;
    do
    {
        printf ("%s = \n", prompt);
        rv = ParseRealValue (v);
    }
    while (!rv);
    return rv;
}


isv_type ParseRealValue (real_value_type *v)
{
    if (scanf ("%f", v) == 1) {
        return 1;
    }
    else {
        // Eat the offending characters
        printf ("Input is garbage!\n");
        printf ("Skipping the offending characters...\n");
        int c;
        while ((c = getchar ()) != EOF)
        {
            if (isdigit (c))
            {
                // put it back
                ungetc (c, stdin);
                break;
            }
        }
        return 0;
    }
}

This seemingly easy task, reading in input, is not so easy after all!

man scanf:

I’m having trouble understanding the example. Let me work on my explanation of what I think is happening and I’ll post it in the next day or so. I tried to run it as is and it failed with about 19 comments (don’t tell me why) On a side note I re-ran my program with erroneous input (like the letter p) and it executed the program fully and all at once. As you know it failed. It didn’t even occur to guard against it. Lots to learn.
Tyler

It looks like I am going to need a little help. Here is where I am at. The example can’t be ran in C because it calls a .h library. In using the typedef command you initially declare a floating variable and a boolean variable, float allows for (up to) 8 bytes and Bool is either true(1) or false(0). In this line {isv_type GetRealValue (const char *prompt, real_value_type *v);} you have two pointers. One points to prompt and the other points to v. This line also attaches real_value_type to v. From there the program asks “What is Mass1?” and assigns v to Mass1.
Why it there a “return 0” here?

int main (int argc, const char * argv[])
{
real_value_type v;
GetRealValue (“What is Mass1?”, &v);
printf (“Mass1 = %f\n”, v);

return 0;

The rest I am still trying to understand.