Professional Documents
Culture Documents
4. Func LL
Problema 4.1 LV MML VLMb @VMM VMVV VMVM
gMM M 1MLL g @MM @VMM gV @M M VMVM.
Rezolvare:
#include<stdio.h>
#include<conio.h>
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 LV MML V LV V L @MVM MM @MV b@V
MLV 1q LLMV ___q g VMMV MM @MV b@V
matricea B.
Rezolvare:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
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;
Structuri de date - Culegere de probleme in C++ 1MML
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 1MM LM MV VVV V L L
q WVL VMMV VVLM V bV MbMV L V MML patrat(),
WVb MV WV MVMV LM@ WV LVb M @MVM b@V
matricea pm.
Rezolvare:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int a[][3]={{1,2,3},{4,5,6},{7,8,9}};
int (*pa)[3][3],(*pm)[3][3];
int c[3][3];
Structuri de date - Culegere de probleme in C++ 1MML
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 WM MML LV LLMV @ Mq LMM MMM
MMM . 1M WM LM@MVMV V MMM VL WV @MV b@V
MML LM WVbVV @MMLV WV MV LV WM MML q Mb -le n
VVLM V. 1VM V WM V WV @ @MV b@V MML V g
MVbLMM LM@MVMVV VLMM WV @MV b@V MML V. 1g
rezultatele.
Rezolvare:
#include<stdio.h>
#include<conio.h>
Structuri de date - Culegere de probleme in C++ 1MML
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 LV gV @MM`
#include<stdio.h>
#include<conio.h>
float y[100];
Structuri de date - Culegere de probleme in C++ 1MML
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<n;i++){
f[i]=1;
a+=x[i]*f[i];
}
return a/n;
}
float med2(float x[100],int f[100],int n){
float a,b;
a=b=0;
for(int i=0;i<n;i++){
b+=f[i];
a+=x[i]*f[i];
}
return a/b;
}
float med3(float x[100],int f[100],int n){
float a,b;
a=b=0;
for(int i=0;i<n;i++){
b+=f[i];
a+=f[i]/x[i];
}
return b/a;
}
void main(){
functii[0]=med1;
functii[1]=med2;
functii[2]=med3;
printf("\nNr de elemente: ");fflush(stdin);
Structuri de date - Culegere de probleme in C++ 1MML
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("\n y[%d]= ",i+1);
scanf("%f",&y[i]);
}
for(i=0;i<3;i++){
xmed[i]=functii[i](y,f,n);
printf("\n%12.10f",xmed[i]);
}
getch();
}
Rezolvare:
`L 1111\L1L O L1L1L11L .\\ Z.\\ O.\\. `L N 11g .
2.0000000000
2.0000000000
1.6363636255
Problema 4.6 1VM MM Mb WMVMbM WV @MV b@V MML q
M q VVLM VbV LVbM Mb @V M g LMV.
1MML V g LMMV WVV.
#include<stdio.h>
#include<conio.h>
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++ 1MML
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();
}