Prg1:
#include <stdio.h>
#include <stdlib.h>
int multIter(int x,int y)
{
int prod=0;
for(;y>0;y--)prod+=x;
return prod;
}
int multRec(int x,int y)
{
if(y==0) return 0; //critere d'arret
return x+multRec(x,y-1); //sinon recursion
}
int main()
{
int a=11,b=3; //par exemple
char tmp;
printf("\nCode Iteratif: %d x %d = %d",a,b,multIter(a,b));
printf("\nTapez Entree SVP");scanf("%c",&tmp);
printf("\nCode Recursif: %d x %d = %d\n",a,b,multRec(a,b));
return 0;
}
Prg2:
#include <stdio.h>
#include <stdlib.h>
unsigned int nbrDisq;
void Hanoi(unsigned n, char x, char y,char z,unsigned matSocles[][3]),
deplacer (char x,char y, unsigned matSocles[][3]),
initMatSocles(unsigned matSocles[][3],unsigned nbrDisq),
modifMatSocles(char x,char y,unsigned matSocles[][3],unsigned nbrDisq),
affichMat(unsigned mat[][3],unsigned,unsigned);
int main()
{
printf("NbrDisques :");
scanf("%u",&nbrDisq);
unsigned matSocles[nbrDisq][3];
initMatSocles(matSocles,nbrDisq);
Hanoi(nbrDisq,'A','B','C',matSocles);
return 0;
}
void deplacer (char x, char y, unsigned matSocles[][3])
{
static unsigned mouvNbr=1;
printf("\n Mouvement %u : Deplacer un disque du socle %c vers le socle %c \n",mouvNbr++,x,y);
modifMatSocles(x,y,matSocles,nbrDisq);
affichMat(matSocles,nbrDisq,3);
}
void Hanoi(unsigned n,char x,char y,char z,unsigned matSocles[][3])
{
if (n>0){
Hanoi(n-1,x,z,y,matSocles);
deplacer(x,y,matSocles);
Hanoi(n-1,z,y,x,matSocles);
}
}
void initMatSocles(unsigned matSocles[][3],unsigned nbrDisq)
{
unsigned i;
for(i=0;i<nbrDisq;i++)matSocles[i][0]=i+1;
for(i=0;i<nbrDisq;i++){matSocles[i][1]=0;matSocles[i][2]=0;}
affichMat(matSocles,nbrDisq,3);
}
void modifMatSocles(char x,char y,unsigned matSocles[][3],unsigned nbrDisq)
{
unsigned i1,i2;
for(i1=0;(i1<nbrDisq)&&(matSocles[i1][x-65]==0);i1++){} //recherche du sommet du socle de depart
for(i2=0;(i2<nbrDisq)&&(matSocles[i2][y-65]==0);i2++){} //recherche du sommet d'arrivee
//transfert du disque
matSocles[i2-1][y-65]=matSocles[i1][x-65];//ajout dans l'arrivee
matSocles[i1][x-65]=0;//suppression dans le depart
}
void affichMat(unsigned mat[][3],unsigned n,unsigned m)
{
unsigned i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++)printf("%4d",mat[i][j]);
printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
int multIter(int x,int y)
{
int prod=0;
for(;y>0;y--)prod+=x;
return prod;
}
int multRec(int x,int y)
{
if(y==0) return 0; //critere d'arret
return x+multRec(x,y-1); //sinon recursion
}
int main()
{
int a=11,b=3; //par exemple
char tmp;
printf("\nCode Iteratif: %d x %d = %d",a,b,multIter(a,b));
printf("\nTapez Entree SVP");scanf("%c",&tmp);
printf("\nCode Recursif: %d x %d = %d\n",a,b,multRec(a,b));
return 0;
}
Prg2:
#include <stdio.h>
#include <stdlib.h>
unsigned int nbrDisq;
void Hanoi(unsigned n, char x, char y,char z,unsigned matSocles[][3]),
deplacer (char x,char y, unsigned matSocles[][3]),
initMatSocles(unsigned matSocles[][3],unsigned nbrDisq),
modifMatSocles(char x,char y,unsigned matSocles[][3],unsigned nbrDisq),
affichMat(unsigned mat[][3],unsigned,unsigned);
int main()
{
printf("NbrDisques :");
scanf("%u",&nbrDisq);
unsigned matSocles[nbrDisq][3];
initMatSocles(matSocles,nbrDisq);
Hanoi(nbrDisq,'A','B','C',matSocles);
return 0;
}
void deplacer (char x, char y, unsigned matSocles[][3])
{
static unsigned mouvNbr=1;
printf("\n Mouvement %u : Deplacer un disque du socle %c vers le socle %c \n",mouvNbr++,x,y);
modifMatSocles(x,y,matSocles,nbrDisq);
affichMat(matSocles,nbrDisq,3);
}
void Hanoi(unsigned n,char x,char y,char z,unsigned matSocles[][3])
{
if (n>0){
Hanoi(n-1,x,z,y,matSocles);
deplacer(x,y,matSocles);
Hanoi(n-1,z,y,x,matSocles);
}
}
void initMatSocles(unsigned matSocles[][3],unsigned nbrDisq)
{
unsigned i;
for(i=0;i<nbrDisq;i++)matSocles[i][0]=i+1;
for(i=0;i<nbrDisq;i++){matSocles[i][1]=0;matSocles[i][2]=0;}
affichMat(matSocles,nbrDisq,3);
}
void modifMatSocles(char x,char y,unsigned matSocles[][3],unsigned nbrDisq)
{
unsigned i1,i2;
for(i1=0;(i1<nbrDisq)&&(matSocles[i1][x-65]==0);i1++){} //recherche du sommet du socle de depart
for(i2=0;(i2<nbrDisq)&&(matSocles[i2][y-65]==0);i2++){} //recherche du sommet d'arrivee
//transfert du disque
matSocles[i2-1][y-65]=matSocles[i1][x-65];//ajout dans l'arrivee
matSocles[i1][x-65]=0;//suppression dans le depart
}
void affichMat(unsigned mat[][3],unsigned n,unsigned m)
{
unsigned i,j;
for(i=0;i<n;i++){
for(j=0;j<m;j++)printf("%4d",mat[i][j]);
printf("\n");
}
}