MA'RUZA. C++ BUILDERDA MASSIVLAR BILAN ISHLASH

Massiv biror tipga tegishli bo’lgan kattaliklar to’plamini bir nom bilan saqlashga mo’ljallangan tuzilma hisoblanadi. Demak, massiv o’zining nomi, elementlari tipi va ularning soni hamda tartibi bilan aniqlanadilar.

Bir o’lchamli massivlar.

Bu massivlar quyidagicha e’lon qilinadilar:

Massivning tipi  massiv nomi[o’lcham];

Misol: int K[15];-nomi K bo’lgan 16 ta butun sonlardan iborat bo’lgan massivni ifodalashga mo’ljallangan, bunda:

K[0]-massivning birinchi elementi;

….

K[15]-massivning oxirgi  elementi;

Fikrimizni tasdiqlash uchun dasturdan lavha keltiramiz:

Fibonachchi sonlarining dastlabki 25 ta hadini hisoblovchi dastur:

void __fastcall TForm1::Button1Click(TObject *Sender)

{

int i;

long a[30];

  a[1]=1;a[2]=1;

  for (i=3;i<26;i++)

  {

  a[i]=a[i-1]+a[i-2];

  ListBox1->ItemRect(a[i]);

  ListBox1->Items->Add(a[i]);

  }

}

//-------------------------------

Massiv elementlari ixtiyoriy tipdan iborat bo’lishi mumkin. Masalan, Char S[10]; belgilar massivini e’lon qiladi.

Shuningdek massivlarni e’lon qilish jarayonida uning elementlari qiymatini ham berish mumkin va u qyidagi tartibda amalga oshiriladi:

 

int A[8]={11,22,33,44,55,66,77,88};

char S[8]={“abcdefghi\0”};  bunda qymatlar soni  massiv elementlari sonidan kam bo’lsa, uning qolgan elementlariga avtomatik tarzda 0 qiymati beriladi.

 Masalan, int a[10]={1,2,3,4} ; holatda massivning oxirgi 7 elmenti qiymati 0 –ga teng bo’ladi.


Massivni quyidagi tartibda e’lon qilish uning barcha elementlariga bir xil qiymat berishni aniqlaydi:

Int a[15]={0};

Massivni ro’yxatli e’lon qilishda massiv elementlari sonini ko’rsatmaslik ham mumkin. Bu holatda uning elementlari soni ro’yxatdagi elementlar soniga teng bo’ladi. Masalan:

int A[]={11,22,33,44,55 }; // 5 elementli massiv

char S[]={“abcd”};  // 5 elementli massiv(0-bilan va u satrning oxirini anglatadi)

Ayrim hollarda massiv elementlari qiymatini doimiy yordamida berishga to’g’ri keladi, masalan:

void __fastcall TForm1::Button1Click(TObject *Sender)

{

Const AnsiString day[]={"dushanba","seshanba","chorshanba","payshanba","juma","shanba","yakshanba"};

  Label1->Caption=day[4]; }

 

 Natija:

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{ int i,Sum;

  int S[20];

  Sum=0;

    for (i=0;i<5;i++)

  {

    S[i]=random(10)+1;

    Sum+=S[i]; // Sum=Sum+S[i];

    ListBox1->Items->Add(FloatToStr(i)+"-"+S[i]);

  }

  Label1->Caption=Sum;

}

 


#include "Unit1.h"

 #include "math.h"

   #include "DateUtils.hpp"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

Graphics::TBitmap * bgp;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

int z; int i;

Canvas->Font->Size=45;

Canvas->Font->Color=clRed;

Canvas->TextOutA(0,10,"anvar");

bgp= new Graphics::TBitmap();

    bgp->LoadFromFile("7777.bmp");

             z=10;

       for (i=1;i<10;i++)

       {

    Canvas->Draw(z,0,bgp);

    z=z+200; }

Label1->Caption=FloatToStr(1/3.);

 

 float a; a=15.56677;

 Memo1->Clear(); randomize();

for (i=1;i<5;i++)

  Memo1->Lines->Add(random(200));

  Memo1->Lines->Add(FloatToStrF(a,ffFixed,6,3));

  Memo1->Lines->Add(Now());

  Memo1->Lines->Add(DateToStr(MinuteOf(128)));

}

//---------------------------------------------------------------------------

Fayllar bilan ishlaSH.

//---------------------------------------------------------------------------

 

#pragma hdrstop

#include  <vcl.h>

  #include  <iostream.h>

  #include  <fstream.h>

  #include  <conio.h>

 

 

//---------------------------------------------------------------------------

 

#pragma argsused

int main(int argc, char* argv[])

{

char ch;

int i;

 

       ofstream fin1;

       fin1.open("as1.dat");

         ofstream fout("as1.dat");

       for (i=1;i<111;i++)

       {

       fout<<(i);

       //fout<<'\n';

       cout << i;

         cout <<'\n';

       }

       fout.close();

       ifstream fin2;

        fin2.open("as1.dat");

 

getch();

 

 

        return 0;

}

//---------------------------------------------------------------------------

Комментарии

Популярные сообщения из этого блога