Structuri de date - Culegere de probleme in C++
)XQF LL
4. Func LL Problema 4.1
6FULH L IXQF LD UHFXUVLY
SHQWUX JHQHUDUHD WHUPHQLORU
úLUXOXLOXL)LERQDFFLúLSURJUDPXOSHQWUXDILúDUHDSULPLORUWHUPHQL
Rezolvare: #include #include int fibb(int n){ if(n<2)return 1; else return(fibb(n-1)+fibb(n-2)); } void main(){ clrscr(); for(int i=1;i<=10;i++)printf("\n %d ***%2d ",i,fibb(i)); } Problema 4.2
6FULH L R IXQF LH FDUH DUH FD SDUDPHWUX XQ SRLQWHU VSUH
PDWULFHD $ FDOFXOHD]
ELM
DLM DLM úL UHWXUQHD]
matricea B. Rezolvare:
#include #include #include int a[][3]={{1,2,3},{4,5,6},{7,8,9}}; int (*pa)[3][3],(*pm)[3][3]; int (*patrat (int(*pb)[3][3]) )[3][3]{ int c[3][3]; int (*pc)[3][3]; pc=&c;
XQ SRLQWHU VSUH
)XQF LL
Structuri de date - Culegere de probleme in C++
for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ printf("\n"); c[i][j]=( (*pb)[i][j] * (*pb)[i][j] ); printf(" [%d][%d]= %d",i,j,c[i][j]); } printf("\n"); } return pc; } void main(){ clrscr(); pa=&a; pm=patrat(pa); for(int i=0;i<3;i++){ puts("\n"); for(int j=0;j<3;j++) printf(" %d ",(*pm)[i][j]); } getch(); } Problema 4.3 JOREDO
3URJUDPXO FRQ LQH UHIHULUHD YDULDELOHL F FD YDULDELO
GHFL OD WHUPLQDUHD H[HFX LHL VH WUDQVPLWH F
DGUHVD ]RQHL GH PHPRULH RFXSDW
GH DFHDVW
matricea pm. Rezolvare:
#include #include #include int a[][3]={{1,2,3},{4,5,6},{7,8,9}}; int (*pa)[3][3],(*pm)[3][3]; int c[3][3];
WUH IXQF LD
YDULDELO
patrat(),
vQ SRLQWHUXO VSUH
)XQF LL
Structuri de date - Culegere de probleme in C++
int (*patrat (int(*pb)[3][3]) )[3][3]{ int (*pc)[3][3]; pc=&c; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ printf("\n"); c[i][j]=( (*pb)[i][j] * (*pb)[i][j] ); printf(" [%d][%d]= %d",i,j,c[i][j]); } printf("\n"); } return pc; } void main(){ clrscr(); pa=&a; pm=patrat(pa); for(int i=0;i<3;i++){ puts("\n"); for(int j=0;j<3;j++) printf(" %d ",(*pm)[i][j]); } getch(); }
Problema 4.4 Defini QXP
L GRX
U ,QL LDOL]D L GRX
IXQF LL FDUH FDOFXOHD]
S
WUDWXO FXEXO XQXL
FRPSRQHQWH DOH XQXL YHFWRU GH SRLQWHUL VSUH
IXQF LLFXDGUHVHOHSXQFWHORUGHLQWUDUHDFHORUGRX H[HFX LH 'HILQL L DOWH GRX
IXQF LLODQVD L
-le în
YDULDELOH GH WLS SRLQWHU VSUH IXQF LH úL
LQWHUVFKLPED L FRPSRQHQWHOH YHFWRUXOXL GH SRLQWHUL VSUH IXQF LH $ILúD L
rezultatele. Rezolvare: #include #include
Structuri de date - Culegere de probleme in C++
typedef int zzz(int x); zzz *(yyy[2]); zzz *ppatrat,*pcub; int patrat(int x){ return (x*x); } int cub(int x){ return (x*x*x); } void main(){ clrscr(); int a=4; yyy[0]=patrat; int b=yyy[0](a); printf("\nx*x= %d",b); yyy[1]=cub; b=yyy[1](a); printf("\nx*x*x= %d",b); ppatrat=yyy[0]; pcub=yyy[1]; yyy[0]=pcub; yyy[1]=ppatrat; printf("\nInversari cub cu patrat! %d ... %d",yyy[0](a),yyy[1](a)); getch(); }
Problema 4.5 Indica #include #include float y[100];
LFHDILúHD]
SURJUDPXO
)XQF LL
Structuri de date - Culegere de probleme in C++
int f[100],n; float xmed[3]; float (*functii[3])(float x[100],int f[100],int n); float med1(float x[100],int f[100],int n){ float a=0,b; for(int i=0;i
)XQF LL
Structuri de date - Culegere de probleme in C++
)XQF LL
scanf("%d",&n); for(int i=0;i
2.0000000000 2.0000000000 1.6363636255
Problema 4.6
'HILQL L XQ PDVLYELGLPHQVLRQDO GH SRLQWHULVSUH IXQF LLvO
LQL LDOL]D L HIHFWXD L WUDYHUVDUHD DFHVWXL PDVLY SH OLQLL úL FRORDQH )XQF LLOHYRUDILúDFXYLQWHGLIHULWH
#include #include typedef char* f(); f *a[2][2];//o matrice de pointeri spre functii char *s; char *f1(){ return "functia1"; } char* f2(){ return "functia2"; }
char* f3(){
Structuri de date - Culegere de probleme in C++
return "functia3"; } char* f4(){ return "functia4"; } void main(){ clrscr(); a[0][0]=f1; a[0][1]=f2; a[1][0]=f3; a[1][1]=f4; for(unsigned char i=0;i<2;i++) for(unsigned char j=0;j<2;j++){ s=a[i][j](); printf("\n %s",s); } getch(); }
)XQF LL