Lab 6: ExDrop -- Part 1 Sample Output: Ok, I\'m now receiving data. I\'m Waiting
ID: 3673921 • Letter: L
Question
Lab 6: ExDrop -- Part 1
Sample Output:
Ok, I'm now receiving data.
I'm Waiting ..............
Help me! I'm falling!!!!!!!!!!!!!
Ouch! I fell 1.633 meters in 0.577 seconds.
After your code has read its first line of data, it should output "Ok, I'm now receiving data." This is so that we can tell that it receiving data from the explore.exe.
Each period on the “I'm Waiting” Line should correspond to some number of input lines from explore.exe. Try 10 or so to see what works well. The exclamation points on the falling line should work similarly. (Note: You may want to use a counter and mod (%) for this. (Challenge version, 20 bonus points: output them approximately every k milliseconds.) Whatever you do, be sure to use constants defined at the beginning of the program to specify how many lines or seconds equate to one period or punctuation mark.
Development Tips
Several loops in this program read and throw away data from the Esplora until some condition is met. Do one loop and see if it works, then add another feature and see if it works. Don't try to write it all at once!
=========================================================
#include <stdio.h>
#include <math.h>
#define TRUE 1
/* Lab 6 Function Prototype */
double x1(double g, double t);
double mag (double ax, double ay, double az);
const double g = 9.8; // m/sec^2
int close_to (double tolerance, double point, double value);
int main(void) {
int t, i;
double x1, g, ax, ay, az;
char period = '.';
char ex = '!';
while (i < 10){
printf("Ok, I'm now receiving data ");
printf("I'm waiting");
scanf("%lf, %lf, %lf", &ax, &ay, &az);
if(close_to (.01,1,(double)mag(ax,ay,az)) == 1){
printf("%c", period);
i = i + 1;
}}
printf(" Help me! I'm falling");
printf("%c",ex);
else if(close_to(.01,1,(double)mag(ax,ay,az)) != 1){
scanf("%d", &t);
printf("Ouch! I fell %lf meters in %lf seconds", x1, t);
}
}
}
/* Lab 6 Function */
double x1(double g, double t){
double x1 = g * t * t * 0.5;
return x1;
}
double mag (double ax, double ay, double az){
double mag = sqrt(ax*ax+ay*ay+az*az);
return mag;
}
int close_to (double tolerance, double point, double value) {
double upTolerance;
double downTolerance;
upTolerance = point + tolerance;
downTolerance = point - tolerance;
if (value < upTolerance && value > downTolerance) {
return 1;
}
return 0;
}
This is the code that I tried, but it doesn't work. Could you fix the code with the explanation?
Explanation / Answer
There was syntax problem in if else also improper closing of braces. I fixed them. It compiles and run.
#include <stdio.h>
#include <math.h>
#define TRUE 1
/* Lab 6 Function Prototype */
double x1(double g, double t);
double mag (double ax, double ay, double az);
const double g = 9.8; // m/sec^2
int close_to (double tolerance, double point, double value);
int main(void) {
int t, i;
double x1, g, ax, ay, az;
char period = '.';
char ex = '!';
while (i < 10) {
printf("Ok, I'm now receiving data ");
printf("I'm waiting");
scanf("%lf, %lf, %lf", &ax, &ay, &az);
if (close_to (.01,1,(double)mag(ax,ay,az)) == 1) {
printf("%c", period);
i = i + 1;
}
}
printf(" Help me! I'm falling");
printf("%c",ex);
if(close_to(.01,1,(double)mag(ax,ay,az)) != 1) {
scanf("%d", &t);
printf("Ouch! I fell %lf meters in %lf seconds", x1, t);
}
return 0;
}
/* Lab 6 Function */
double x1(double g, double t) {
double x1 = g * t * t * 0.5;
return x1;
}
double mag(double ax, double ay, double az) {
double mag = sqrt(ax*ax+ay*ay+az*az);
return mag;
}
int close_to (double tolerance, double point, double value) {
double upTolerance;
double downTolerance;
upTolerance = point + tolerance;
downTolerance = point - tolerance;
if (value < upTolerance && value > downTolerance) {
return 1;
}
return 0;
}
NOTE: Fixed compilation problems only.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.