עבור לתוכן

עזרה ביצירת פונקציה בשפת C

Featured Replies

פורסם

אני צריך לחשב מרחק וגובה של אובייקט המושלך בזווית. לכתוב פונקציה אחת שמחשבת את הגובה ואחת את המרחק(שימוש בנוסחאות (h = vyt - gt2 / 2; s = vxt. (g = 9.81 m/s2 )

התכנית תחשב ותדפיס את המרחק והגובה של הגוף עבור כל 0.1 שנייה.

** התוכנית תרוץ בלולאה אינסופית עד קבלת ^Z ((CTRL+Z

הנה הקוד שכתבתי אבל הוא לא עובד.. מי יכול לעזור?


#include <stdio.h>
#include <math.h>
#include <time.h>
float highet(float, int, float);
float horizontal(float, int, float);
int main()
{
float V,T;
int A;


while (V!=EOF || A!=EOF)
{
printf("Enter the speed (0.0-100.0 m/s) and the angel (0-90 degrees): ");
scanf("%f%d", &V, &A);
if (V>100.0 || V<0 || A>90|| A<0)
printf("Error invaild input");
for(T=0; h>0;T=T+0.1)
printf("time: %f s:%f h:%f\n",T, highet(V,A,T), horizontal(V,A,T);


printf("object was fallen");
return 0;
}
}


float highet(float v, int a, float t)
{
float h;
float g=9.81;
h = (v*sin(a)*t) - (g*(t*t) / 2);
return h;
}
float horizontal (float v, int a, float t)
{
float s;
float g=9.81;
s = (v*cos(a)*t) - (g*(t*t) / 2);
return s;


}

פורסם

מה זה "לא עובד"? מה קורה כשאתה מריץ את התוכנית?

כמה דברים:

א. הפונקציות sin ו-cos מקבלות את הקלט ברדיאנים, לא במעלות. אתה מוזמן לחפש את הנוסחה שממירה בין השניים.

ב. למה אתה משווה את V ו-A ל-EOF? מה זה אומר?

פורסם

עשיתי לך קצת שינויים. שים לב שהפונקציות מקבלות רדיאנים ואתה מעביר במעלות אז החישוב שגוי.

דבר שני חשוב לתת שמות משמעותיים למשתנים ופונקציות כי אחרת קשה להבין את הקוד.

דבר שלישי תמיד תכתוב הערות בקוד. זה יעזור למי שיקרא את הקוד ולך בעדו תקופה שלא תזכור.


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


const double g = 9.81;
const double PI = 3.14159;


const double degrees_to_radians(const double degrees){
return (degrees / 180) * PI;
}


const double calc_height(const double v, const double a, const double t)
{
return (v*sin(degrees_to_radians(a))*t) - (g*(t*t) / 2);// (Vy)*t -gt^2 = height
}
const double calc_distance(const double v, const double a, const double t)
{


return (v*cos(degrees_to_radians(a))*t); //(Vx)*t
}


int do_stuff();


void main(){
int ret = 0;
while (1){
if (do_stuff())
printf("An error occured. try again\n");
}
}


int do_stuff()
{
double velocity = 0, time = 0.1, angle = 0, height = 0, distance = 0;


//scan initial velocity and throwing angle
printf("Insert the initial speed (0-100 m/s) and angle (0-90 degrees): \n");
scanf_s("%lf%lf", &velocity, &angle);
//validate input
if (velocity > 100 || velocity < 0 || velocity>90 || velocity < 0){
printf("Error invaild input\n");
return -1;
}
//print distance and height every 0.1 seconds
while (1){
distance = calc_distance(velocity, angle, time);
height = calc_height(velocity, angle, time);
//when the object falls on the ground, stop
if (height < 0) break;
printf("time: %.1lf 'x axis' distance: %.2lf current height: %.2lf\n", time, distance, height);
time += 0.1;
}


printf("object felt on the ground\n\n");
return 0;
}






ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים