5-Mavzu: ref va out parametrlari. Ko’rsatkichli va qiymatli turlar


Download 29.24 Kb.
bet2/3
Sana02.12.2023
Hajmi29.24 Kb.
#1779486
1   2   3
Mavzuga doir bir nechta misollar:

  1. N haqiqiy toifadagi son berilsa, uning butun va kasr qismini ajratuvchi dastur tuzing.

C#

class Decompose
{
public int parts(double n, out double frac) {
int whole;
whole = (int) n;
frac = n - whole;
return whole;
}
}
class Program
{
static void Main(string[] args)
{
Decompose ob = new Decompose();
int i;
double f;
double n = double.Parse(Console.ReadLine());
i = ob.parts(n, out f);
Console.WriteLine("Sonning butun qismi " + i);
Console.WriteLine("Sonning kasr qismi" + f);
Console.ReadKey();
}
}



  1. Ref parametridan foydalangan holda ixtiyoriy butun sonning kvadratini hisoblovchi dastur tuzing.

C#

class RefTest
{
public void sqr(ref int i) {
i = i * i;
}
}
class Program
{
static void Main(string[] args)
{
RefTest ob = new RefTest();
int a = int.Parse(Console.ReadLine());
Console.WriteLine("a dastur boshida " + a);
ob.sqr(ref a);
Console.WriteLine("a dastur so'ngida " + a);
Console.ReadKey();
}
}



  1. Haqiqiy toifadagi soni berilgan. shu sonning yarmini hisoblovchi dastur tuzing. Dasturda ref parametridan foydalaning.

C#

class refexam
{
public void show(ref double x)
{
x = x / 2;
}
}
class Program
{
static void Main(string[] args)
{
refexam obj = new refexam();
double a = double.Parse(Console.ReadLine());
Console.WriteLine("before call a :" + a);// this will display 35
obj.show(ref a);
Console.WriteLine("after call a :" + a);// this will display 17
Console.ReadKey();
}
}



  1. Butun toifadagi a soni kiritilsin. Kiritilgan a sonini, uning yarmini butun qismini va a sonining 2 ga bo’lganddagi qoldiqini chiqaruvchi dastur tuzing. Dasturda ref va out parametrlaridan foydalaning.

C#

class refexam
{
public void show(ref int x, out int y)
{
y = x % 2;
x = x / 2;
}
}
class Program
{
static void Main(string[] args)
{
int a=int.Parse(Console.ReadLine());
int reminder;
refexam obj = new refexam();
Console.WriteLine("dastlabki a :" + a);
obj.show(ref a, out reminder);
Console.WriteLine("keyingi a :" + a);
Console.WriteLine("dastlabki a ni keyingi a ga bo'lgandagi qoldiq :" + reminder);
Console.ReadKey();
}
}



Download 29.24 Kb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling