O‘zbekiston respublikasi axborot texnologiyalari va kommunikatsiyalarini rivojlantirish vazirligi muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti urganch filali dasturlash 1 fanidan Mustaqil ish Gurux: 972-21


Download 68.39 Kb.
bet2/2
Sana14.03.2023
Hajmi68.39 Kb.
#1268107
1   2
Bog'liq
8-mutsaqil ish

Funksiya va massivlar
Ko‘p o‘lchamli massivlarni parametr sifatida ishlatishda bir nechta usullardan foydalanish mumkin:
1-usul. Massivning ikkinchi o‘lchamini o‘zgarmas ifoda (son) bilan ko‘rsatish:


  1. float sum (int n, float x[][10])

  2. {float s=0.0;

  3. for(int i=0;i

  4. for(int j=0;j

  5. s+=x[i][j];

  6. return s;}


2-usul. Ikki o‘lchamli massiv ko‘rsatkichlar massivi ko‘ri-nishida aniqlangan holatlar uchun ko‘rsatkichlar massivini (matritsa satrlar adreslarini) berish orqali:

  1. float sum (int n, float x[][10])

  2. {float s=0.0;

  3. for(int i=0;i

  4. for(int j=0;j

  5. s+=x[i][j];

  6. return s;}

  7. int main()

  8. { float x[][4]={{11,-12,13,14},{21,22,23,24},{31,32,33,34},{41,42,43,44}};

  9. float *ptr[4];

  10. for(int i=0;i<4;i++)

  11. ptr[i]=(float*)&x[i];

  12. cout<


3-usul. Ko‘rsatkichlarga ko‘rsatkich ko‘rinishida aniqlangan dinamik massivlarni ishlatish bilan:

  1. float sum(int n,float **x)

  2. { float s=0.0;

  3. for(int i=0;i

  4. for(int j=0;j

  5. s+=x[i][j];

  6. return s;}

  7. int main()

  8. {float **ptr;

  9. int n;

  10. cin>>n;

  11. ptr=new float * [n];

  12. for(int i=0;i

  13. {

  14. ptr[i]=new float [n];

  15. for(int j=0;j

  16. ptr[i][j]=(float)((i+1)*10+j;

  17. }

  18. cout<

  19. for(int i=0;i

  20. delete ptr[i];

  21. delete []ptr;

  22. }


Dinamik matritsalar
Masala: Matritsani o’lchamini kiriting va unga dastur davomida xotiradan joy ajrating.
Muammo : Matritsa o’lchami oldindan ma’lum emas.
Yechish usullari:

Xar bir satr uchun xotira bloke
Matritsa adresi:




  1. typedef int *pInt;

  1. int main()

  2. {

  3. int M, N, i;

  4. pInt *A;

  5. A = new pInt[M];// Ko’rsatgichlar massivini ajratish

  6. for ( i = 0; i < M; i ++ )

  7. A[i] = new int[N];// Massivning har- bir satr uchun

  8. for ( i = 0; i < M; i ++ )

  9. delete A[i];// Har – bir satr o’chiriladi

  10. delete A; // Massiv o’chiriladi

  11. }

Masala 2: Bir o’lchovli massivni elementlarini yig’indisini funksiya yordamida toping


#include
using namespace std;
int sum(int a[], int n)
{
int s=0;
for(int i=0; is+=a[i];
return s;
}
int main()
{
int n,s=0;
cout<<"Massiv o'lchami: ";
cin>>n;
int a[n];
for(int i=0; icin>>a[i];
s=sum(a,n);
cout<<"Yig'indi = "<}



Download 68.39 Kb.

Do'stlaringiz bilan baham:
1   2




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