7-amaliy mashg’ulot mavzu: ko’rsatkichlar va dinamik xotira bilan ishlash. Dinamik massiv. Nazariy qism


Download 0.74 Mb.
bet4/4
Sana05.01.2022
Hajmi0.74 Mb.
#205419
1   2   3   4
Bog'liq
7- Amaliyot (1)

#include

  • #include

  • #include

  • using namespace std;

  • int main ()

  • { srand(time(0));

  • int i,j,n,s=0;

  • cout << "n=";

  • cin >> n; //Massiv elementlari soni

  • float *b=new float [n]; // b massivga dinamik xotira ajratish

  • //b massivga element kiritish va chop qilish

  • cout<<"\n b massiv elementlari \n\n";

  • for(j=0;j

  • b[j]=rand()%100+1;

  • cout << "b["<

  • s+=b[j];

  • }

  • // massivni xotiradan o'chirish, xotirani tozalash

  • delete [] b;

  • cout<<"\n yig'indi="<

  • return 0;

  • }

    Dastur natijasi:



    Misol. Elementlari o'sish tartibida tartiblangan 2 ta butun sonli massivlar berilgan. Bu massivlardagi elementlarning barchasi yangi massivga o'tkazilsin va tashkil etilgan massiv elementlarining o'sish tartibida bo'lishi ta'minlansin.

    1. #include

    2. using namespace std;

    3. // butun turdagi massivga ko'rsatkich qaytaradigan funksiya

    4. int *massivlarni_birlashtirish(int,int*,int,int*);

    5. int main()

    6. {

    7. int c[]={-4,7,18,21,85},d[]={3,8,74};

    8. int *h;

    9. h=massivlarni_birlashtirish(5,c,3,d);

    10. for(int i=0;i<8;i++) cout<<"\t"<11. delete[]h;}

    12. int *massivlarni_birlashtirish(int n,int *a,int m,int *b)

    13.{int *x=new int[n+m];

    14.int ia=0,ib=0,ix=0;

    15.while (ia

    16.a[ia]>b[ib]?x[ix++]=b[ib++]:x[ix++]=a[ia++];

    17.while (ib

    18.while (ia

    19.return x;



    20.}

    Dastur natijasi:



    Misol: Ikki o’lchovli dinamik massivni elon qilish unga element kiritish va ularni ekranga chiqarish


    1. #include

    2. using namespace std;

    3. int main()

    4. { int i,j,n;

    5. cout<<"n="; cin>>n;

    6. double **q = new double *[n];

    7. for (i = 0; i < n; i++) q[i] = new double[n];

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

    9. { for (j = 0; j < n; j++){

    10. cout << (q[i][j] = (i+1)*(j+1))<< "\t";}

    11. cout << endl;}

    12. for (i = 0; i < n; i++) delete[] q[i];

    13. delete[] q;

    14. }

    Dastur natijasi:



    Misol: Matritsa va vektorni ko'paytirish masalasi uchun dinamik massivlardan foydalanish

    1. #include

    2. #include

    3. #include

    4. using namespace std;

    5. int main ()

    6. { srand(time(0));

    7. int i,j,n,m;

    8. cout << "n=";

    9. cin >> n; //Matritsa ustunlari soni

    10. cout << "m=";

    11. cin >> m; //Matritsa satrlari soni

    12. float *b=new float [n]; // b massivga dinamik xotira ajratish

    13. float *c=new float [m]; // c massivga dinamik xotira ajratish

    14. float ** a =new float *[m] ; // ko'rsatkichlar massiviga xotira ajratish

    15. for (i=0;i

    16. a[i]=new float [n]; // dinamik xotira ajratish

    17. //a matritsaga element kiritish va chop qilish

    18. cout<<"\n a massiv elementlari \n\n";

    19. for (i=0;i

    20. for(j=0;j

    21. a[i][j]=rand()%10+1;

    22. cout<

    23. cout<

    24. //b massivga element kiritish va chop qilish

    25. cout<<"\n b massiv elementlari \n\n";

    26. for(j=0;j

    27. b[j]=rand()%10+1;

    28. cout<

    29. // a matritsa bilan b massivni qo'shib c massivga yuklash

    30. cout<<"\n\n c massiv elementlari \n\n";

    31. for (i=0; i

    32. { float s=0;

    33. for (j=0; j

    34. s+=a[i][j]+b[j] ;}

    35. c[i]=s;

    36. cout << "c["<

    37. // massivlarni xotiradan o'chirish, xotirani tozalash

    38. delete [] b;

    39. delete [] c;

    40. for (i=0;i

    41. delete []a; //massivni o'zi o'chirilyapti

    42. return 0;}

    Dastur natijasi:




    Download 0.74 Mb.

    Do'stlaringiz bilan baham:
  • 1   2   3   4




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