עזרה למתכנת המתחיל בC++

Stick

New member
עזרה למתכנת המתחיל בC++

אוקיי. הנה שאלה של מתכנת מתחיל. קבלו את התוכנית הבאה שמדמה פעולה של אינטגרל. #include <iostream.h> float Integral(int square, int a, int b); float Integral(int square, int a, int b) { int t,z=1,r=1; float l=0; if (b>a) {t=b; b=a; a=t;} for (t=1; t<=square; t++) z*=a; for (t=1; t<=square; t++) r*=b; t=z-r; l=t/square; return(l); } main() { int x,y,z; float w; cout<<"this program simulate integral operation"<<´\n´; cout<<"input square,where square means x^square, input a,b (borders of integral)"<<´\n´; cin>>x>>y>>z; w=Integral(x+1,y,z); cout<<´\n´<<"sum is="<<w; return 0; } אני לא מקבל משום מה את התשובה ב float כמו שאני אמור לקבל אלא רק כמספר שלם וממשי. אולי מישהו יכול להגיד לי למה? התרגיל לקוח מ- c++ כשפת אם עמוד 69 תרגיל 6. אם מישהו יסביר לי מה זה קירוב, זה יהיה נהדר גם כן
 

antidot

New member
תזרוק את הספר....

יש כמה טעויות בספר כפי שאני רואה ראה הערות בגוף הקוד
#include <stdio.h> #include <iostream.h> float Integral(int square, int a, int b); int main(void) //it´s a good practice to have main() return a value. it might be usefull to debug program by return codes { int x,y,z; float w; cout << "this program simulate integral operation" << endl; // cout << ´\n´ will not compile on all C++ compilers and it´s C style // use "cout << endl;" instead cout<<"input square,where square means x^square, input a,b (borders of integral)"<< endl; cin >> x >> y >> z; w = Integral(x+1,y,z); cout<< endl << "sum is=" << w << endl; return 0; } float Integral(int square, int a, int b) { int t,z=1,r=1; float l=0; if (b>a) { t=b; b=a; a=t; } for (t=1; t<=square; t++) z*=a; for (t=1; t<=square; t++) r*=b; t=z-r; l=(float)t/square; //the problem is here // any integer devided by any number returns integer, so you have 2 options: // 1. cast the integer C-style // 2. declare the variable as float or double. return(l); }​
Antid0t
 
למעלה