1-misol. Y = x² funksiyasini hisoblash uchun dastur tuzing. Bu erda x – ixtiyoriy haqiqiy son. 1-variant. # include void main( ) { float x, y; cin >> x; y = x*x; cout << “y=“<< y << endl; } 2-variant. # include < iostream. h > # include < math. h > void main( ) { float x = 3.26, y; y = pow ( x, 2); cout << “y=“<< y << endl; } 3-variant. # include void main( ) { float x=5.6, y; cout << “y=“<< x*x << endl; } Qoida: agar o’zgaruvchi butun tipli deb e’lon qilinsa va unga haqiqiy qiymat berilsa, natija noto’g’ri bo’ladi. Agar o’zgaruvch haqiqiy deb e’lon qilinsa va unga butun son berilsa, natija ozgarmaydi. Masalan:
int x=1.2, y;
y=x+5;
cout << y;
Natija: 6 (-)
|
float x=1.2,y;
y=x+5;
cout<< y;
Natila: 6.2 (+)
|
float y; int x=1.2;
y=x+5;
cout<< y;
Natila: 6 (-)
|
int x=1.2,y;
y=sin(x)+5;
cout<< y;
Natila: 6 (-)
|
float x=1.2,y;
y=sin(x)+5;
cout<< y;
Natila:5.932 (+)
|
float y; int x=1.2;
y=sin(x)+5;
cout<< y;
Natila:5.841 (-)
| O’zgaruvchilarni bevosita ishlatishdan oldin e’lon qilsa ham bo’ladi. Bunday holatda o’zgaruvchining tipi qavslarda yoziladi. Masalan: (float) b= 2+sin(a*a); (double) p=5.5e-7/exp(x-3); 2-misol. Uchburchakning 2ta tomoni va ular orasidagi burchagi berilgan. Qolgan parametrlarini topish uchun dastur tuzing. (xatosini tuzatamiz) # include void main ( ) { float a, b, c, r, R, s, alf, bet, gam, x, pi; cout << ‘uchburchak tomonlarini kiriting:\n’; cin >a>b; gam=pi/3; c = sqrt (a*a+b*b-2*a*b*cos(gam); (to’g’ri varianti) # include # include void main ( ) { float a, b, c, r, R, s, alf, bet, gam, x, pi=3.14; cout << “uchburchak tomonlarini kiriting:\n”; cin >>a>>b; gam=pi/3; c = sqrt (a*a+b*b-2*a*b*cos(gam)); Davomi: (xatosini tuzatamiz) s = a*b*sin(gam)/2; r = a*b*c / 4*s; R = 2*s / (a+b+c); x = b *sin(gam) / c; bet = atan (x)sqrt (1- x*x/x); alf = pi – bet – gam; cout << “Natijalar:”; cout << “c=”<< c < Davomi: s = a*b*sin(gam)/2; R = a*b*c / (4*s); r= 2*s / (a+b+c); x = b *sin(gam) / c; bet = atan (x/sqrt (1- x*x)); alf = pi – bet – gam; cout.precision(4); cout << “Natijalar:”< Quyidagi dasturning natijasini toping: # include void main() { int a, b; a = 5; b = a+2; a = (a + 2) * (b - 3); b = a / 5; a = a % b; a ++; b = (a + 14) % 7; cout << “a=“<< a << “\tb=“<< b << endl; }
Do'stlaringiz bilan baham: |