|
\'10.04.2020y-C da funksiyalar
Misol 1 : To'g'ri burchakli uchburchakning katetlari berilgan. (3, 4), (6, 8), (12, 5)
bo'lgan xollar uchun uchburchak gipotenuzasini hisoblovchi dastur tuzilsin.
1) Parametrli funksiya
#include
#include
// funksiya prototipi
float hisobla(float , float );
int main()
{
float c;
c = hisobla(3, 4);
cout << c << endl;
c = hisobla(6, 8);
cout << c << endl;
c = hisobla(12, 5);
cout << c << endl;
system ("pause");
return 0;
}
float hisobla(float a, float b)
{
//lokal o'zgaruvchi
float natija;
natija = sqrtf(a*a + b*b);
return natija;
}
2) void toifasidagi parametrli funksiya
#include
#include
// funksiya prototipi
void hisobla(float , float );
int main()
{
hisobla(3, 4);
hisobla(6, 8);
hisobla(12, 5);
system ("pause");
return 0;
}
void hisobla(float a, float b)
{
float c;
c = sqrtf(a*a + b*b);
cout << c << endl;
}
Misol 2: Global va lokal o'zgaruvchilarga murojaatni o'rganish
#include
int x = 5; // global o'zgaruvchi
int main()
{
int x = 9; // lokal o'zgaruvchi
std::cout << "lokal x=" << x << std::endl;
std::cout << "global x=" << ::x << std::endl;
system ("pause");
return 0;
}
Misol 3: Kiritilgan n sonini 3 - darajasini hisoblovchi funksiya tuzilsin
#include
void kub (int *);
int main()
{
int n;
cout << "n="; cin >> n;
kub (&n);
cout << "n ning qiymati =" << n << endl;
system ("pause");
return 0;
}
void kub (int *nPtr)
{
*nPtr = *nPtr * *nPtr * *nPtr;
}
Misol 4: Ikkita son yig'indisini funksiya orqali hisoblovchi dastur tuzilsin
#include
// funksiya prototipi
int sum(int , int);
void sum(int , int, int *);
int sum(int *, int *);
void sum(int *, int *, int *);
int main()
{
int a, b, c;
cout << "a="; cin >> a;
cout << "b="; cin >> b;
c = sum(a, b);
cout << "1-sul natijasi=" << c << endl;
sum(a, b, &c);
cout << "2-sul natijasi=" << c << endl;
c = sum(&a, &b);
cout << "3-usul natijasi=" << c << endl;
sum(&a, &b, &c);
cout << "4-usul natijasi=" << c << endl;
system ("pause");
return 0;
}
// 1 - usul
int sum(int son1, int son2)
{
int natija;
natija = son1 + son2;
return natija;
}
// 2 - usul
void sum(int son1, int son2,int *natija)
{
*natija = son1 + son2;
}
// 3 - usul
int sum(int *son1, int *son2)
{
int natija;
natija = *son1 + *son2;
return natija;
}
// 4 - usul
void sum(int *son1, int *son2,int *natija)
{
*natija = *son1 + *son2;
}
1-Misol
Default qiymatli funksiya
#include
using namespace std;
void text(string ismlar="Umar"){
cout<
}
int main()
{
text("Marufxo'ja");
text("Anora");
text("Dilnoza");
text();
return 0;
}
2-Misol
Multiple Parameteres Function
#include
using namespace std;
void text(string ismlar,int yosh,string millati){
cout<
}
int main()
{
text("Marufxo'ja",23,"uzbek");
text("Anora",19,"uzbek");
text("Dilnoza",20,"uzbek");
text("Durdona",20,"tojik");
return 0;}
3-Misol
#include
#include
using namespace std;
void funksiya(int x,float y){
cout<
}
int main()
{ int x;
float y;
cout<<"X ni qiymatini kiriting"<
cin>>x;
cout<<"****************************************************
****"<
y=pow(x,3);
cout<<" X= "<
return 0;
}
4-Misol
Kvadrat tenglamani ildizlarini chiqaruvchi dastur tuzing
#include
#include
using namespace std;
void kvadratfunksiya(float a,float b,float c,float d,double x1,double x2){
cout<
}
int main()
{ float a,b,c,d;
double x1,x2;
cout<<"a ni qiymatini kiriting"<
cin>>a;
cout<<"***************************************************
*****"<
cout<<"b ni qiymatini kiriting"<
cin>>b;
cout<<"***************************************************
*****"<
cout<<"c ni qiymatini kiriting"<
cin>>c;
cout<<"***************************************************
*****"<
d=pow(b,2)-4*a*c;
if(d<0){
cout<<"d = "<
cout<<"Tenglama ildizga ega emas "<
}
else{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
cout<<" a= "<
"<
cout<<"***************************************************
*****"<
kvadratfunksiya(a,b,c,d,x1,x2);
}
return 0;
}
#include
#include
using namespace std;
void yoqlama(string
borlar="Marufxo'ja,Umar,Durdona,Anora,Shohsanam,Muxriddin,Dilnoz
a,Aziz darsda bor talabalar") {
cout<
}
int main()
{
yoqlama();
yoqlama("Ravshan yoqku");
yoqlama("Munisa dars o'tvotti");
return 0;
}
#include
#include
using namespace std;
void kvadratfunksiya(float a,float b,float c,float d,float x1,float x2) {
cout<
}
int main()
{
float a,b,c,d,x1,x2;
cout<<"a ni kiriting = "<
cin>>a;
cout<<"********************************************"<
cout<<"b ni kiriting = "<
cin>>b;
cout<<"********************************************"<
cout<<"c ni kiriting = "<
cin>>c;
cout<<"********************************************"<
d=pow(b,2)-4*a*c;
if(d<0){
cout<<"d="<
cout<<"Tenglama ildizga ega emas"<
}
else{
x1=(-b+sqrt(d))/2*a;
x2=(-b-sqrt(d))/2*a;
cout<<"x1="<
cout<<"x2="<
kvadratfunksiya(a,b,c,d,x1,x2);
}
return 0;
}
Dostları ilə paylaş: |
|
|