«Aniq fanlar» kafedrasi fanidan ma’ruzalar matni
Download 1.15 Mb.
|
dasturlash fanidan maruzalar matni 2
Funksiya va massivlar: Funksiyalar massivni parametr sifatida ishlatishi va uni funksiyaning natijasi ko‘rinishida qaytarishi mumkin.
Agar massiv parametr orqali funksiyaga uzatilsa, elementlar sonini aniqlash muammosi tug‘iladi, chunki massiv nomidan uning uzunligini aniqlashning iloji yo‘q. Ayrim hollarda, masalan, belgilar massivi sifatida aniqlangan satr (ASCIIZ satrlar)bilan ishlaganda massiv uzunligini aniqlash mumkin, chunki satrlar ‘\0’ belgisi bilan tugaydi (8-bobga qarang). Misol uchun: #include int len(char s[])//massivni parametr sifatida ishlatish { int m=0;
while(s[m++]); return m-1; } void main() { char z[]=”Ushbu satr uzunligi =”; cout< }
#include float sum(int n,float *x) //bu ikkinchi usul
{
float s=0; return s; }
void main() float E[]={1.2,2.0,3.0,4.5,-4.0}; cout< }
void vector_01(int n,int*x,int*y) //bu ikkinchi usul {
for (int i=0; i }
void main() int a[]={1,2,-4,3,-5,0,4}; int c[7]; vector_01(7,a,c); for (int i=0; i<7;i++) cout<<’\t’< }
bir o’lchamli ikkita massivlarni yagona massivga, tartiblanish saqlangan holda birlashtirish amalga oshirilsin.
Programma matni: \\butun turdagi massivga ko’rsatkich qaytaradigan funksiya int *massiv_ulash(int,int*,int,int*);
void main() {
int c[]={-1,2,5,10},d[]={1,7,8}; h=massiv_ulash(5,c,3,d); for(int i=0;i<8;i++) cout<<’\t’< delete[]h; }
int *massiv_ulash(int n,int *a,int m,int *b); int *x=new int[n+m]; int ia=0,ib=0,ix=0; while (ia a[ia]>b[ib]?x[ix++]=b[ib++]:x[ix++]=a[ia++];
while (ib while (ia return x; }
1-usul. Massivning ikkinchi o’lchamini o’zgarmas ifoda (son) bilan ko’rsatish: {
float s=0.0; for (int j=0; j s+=x[i][j];
return s; }
{
float s=0.0; for(int j=0;j s+=p[i][j]; \\”p[i][j]” emas,chunki massivga murojat
return s; }
void main() float *x[][4]={{11,12,13,14},{21,22,23,24}, {31,32,33,34},{41,42,43,44}};
float *ptr[4]; for (int=0;i<4;i++) ptr[i]=(float*)&x[i];
cout< }
{float s=0.0; for(int i=0;i for(int j=0;j s+=x[i][j];
return s;} void main()
{
float **ptr; cin>>n;
for(int i=0;i {
ptr[i]=new float[n] ptr[i][j]=(float)((i+1)*10+j); }
cout< delete[]ptr; }
#include int **rmatr(int n,int m)
{int **ptr; {ptr=new int *[n];
for(int i=0;i {ptr[i]=new int[m];
for(int j=0;j }
return ptr; int sum(int n,int m,int **ix) {float s=0;
for(int i=0;i for(int j=0;j return s; }
void main() int n,m;
int **matr; randomize();
matr=rmatr(n,m); for(int i=0;i {cout< { for(int j=0;j }
cout< delete[]matr; }
|
ma'muriyatiga murojaat qiling