This commit is contained in:
“[JMF]” 2021-10-21 12:47:20 +02:00
parent 501ccbc95a
commit f479d8d205
4 changed files with 65 additions and 7 deletions

BIN
TP5/a.out

Binary file not shown.

51
TP5/carre.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#define N 3
int main () {
// int A[N][N] = {1,2,3,4,5,6,7,8,9}; // Initialisation pas magique
int A[N][N] = {4,2,3,2,3,4,3,4,2}; // Initialisation pas magique
int i , j;
// diagonale
int sommed1 = 0, sommed2 = 0;
for(i=0;i<N;i=i+1)
{
sommed1 = sommed1 + A[i][i];
sommed2 = sommed2 + A[i][N-1-i];
}
if ( sommed1 != sommed2 )
{
printf("stop, pas magique\n");
return 1;
}
// hors diagonale
int sommeV , sommeH ;
for(i=0;i<N;i=i+1)
{
sommeH = 0 ;
sommeV = 0 ;
for (j=0;j<N;j++)
{
sommeH = sommeH + A[i][j] ;
sommeV = sommeV + A[j][i] ;
}
if ( sommeH != sommed1 || sommeV != sommed1 )
{
printf("stop, pas magique\n");
return 1;
}
}
printf("carre magique = %d \n",sommed1);
return 1;
}

View File

@ -5,12 +5,19 @@ int main () {
float a = 1.;
int b = 1 ;
FILE *point = fopen("data.dat","r");
FILE *point = fopen("data.dat","r"); // ouverture file "r" lecture
fscanf(point,"%f %d \n",&a,&b);
// test necessaire pour ouverture en lecture
if(point==NULL) {
puts("Echec de l'ouverture. Fin du programme.");
return 0;
}
printf("%f %d \n",a,b);
fscanf(point,"%f %d \n",&a,&b); // lecture file
fclose(point);
printf("%f %d \n",a,b); // print
fclose(point); // fermeture file
}

View File

@ -5,10 +5,10 @@ int main () {
float a = 1.;
int b = 1 ;
FILE *point = fopen("data.dat","w");
FILE *point = fopen("data.dat","w"); // ouverture file "w" ecriture
fprintf(point,"%f %d \n",a,b);
fprintf(point,"%f %d \n",a,b); // ecriure file
fclose(point);
fclose(point); // fermeture
}