revision inf3135_les exercices du profs_revisionLongeuilPlusTpSurDrive.docx

Revision inf3135_les exercices du profs_revisionLongeuilPlusTpSurDrive.docx

This preview shows page 1 - 2 out of 27 pages.

Makefile 164 Bytes exemple : exemple.o gcc -o exemple exemple.o exemple.o : exemple.c gcc -c exemple.c.PHONY : exec clean exec : exemple ./exemple clean : rm -f * .o * .out exemple Ajout du chapitre 2 : Introduction à C. exemple.c #include <stdio.h> /* directives au préprocesseur */ #define DEBUT -2 #define FIN 2 #define MSG "Programme de démonstration\n" int carre(int x); /* déclaration des fonctions */ int cube(int x); int main() { /* début du bloc principal */ int i; /* définition des variables locales */ printf(MSG); for (i = DEBUT; i <= FIN; i++) { printf("%d carré: %d cube: %d\n", i, carre(i), cube(i)); } return 0; } /* fin du bloc principal */ int cube(int x) { /* définition de la fonction cube */ return x * carre(x); } int carre(int x) { /* définition de la fonction carre */ return x * x; } exo4.c #include <stdio.h> int main() { char x = -1, y = 20, v; unsigned char z = 254; unsigned short t; unsigned short u; t = x; u = y; v = z; printf("%d %d %d\n", t, u, v); // Affiche 65535 20 -2 } Exo5.c int a = 1, b; b = (a++, a + 2); printf("%d\n", b); // Affiche 4 exo6.c #include <stdio.h> int main() { int x = 1, y, z; y = (x-- == 0 ? 1 : 2); z = (++x == 1 ? 1 : 2); printf("%d %d\n", y, z); } exo7.c #include <stdio.h> int main() { unsigned char x = 255; printf("%d\n", x); // Affiche 255 printf("%d\n", (signed char)x); // Affiche -1 int y = 3, z = 4; printf("%d %f\n", z / y, ((float)z) / y); // Affiche 1 1.333333 } exo8.c #include <stdio.h> int main() { int donnees[] = {12,-12,34,13,43}; int a, b; a = donnees[2]; b = donnees[5]; printf("%d %d\n", a, b); /* que vaut b ? */ } longueur.c #include <stdio.h> unsigned int longueur(char chaine[]) { unsigned int i = 0; while (chaine[i] != '\0') { ++i; } return i; } int main() { char s[] = "tomate"; printf("La longueur de \"%s\" est %d\n", s, longueur(s)); return 0; } pointeur1.c int main() { int x = 210; printf("La variable x vaut %d et est stockée à l'adresse hexa %p.\n", x, &x); } pointeur2.c int main() { int *pi, x = 104; pi = &x; printf("x vaut %d et se trouve à l'adresse %p\n", x, &x); printf("pi vaut %p et pointe sur la valeur %d\n", pi, *pi); *pi = 350; printf("x vaut %d et se trouve à l'adresse %p\n", x, &x); printf("pi vaut %p et pointe sur la valeur %d\n", pi, *pi); } pointeur3.c #include <stdio.h> 1
Image of page 1

Subscribe to view the full document.

int main() { int a[3] = {1,2,3}, *pi; pi = a; /* initialisation de pi */ printf("%p %p %d %d %d %d\n", a, pi, a[0], a[1], a[2], *pi); } pointeur4.c int main() { int a[3] = {1, -1, 2}, *pi, *pi2; pi = a; pi2 = &a[2]; printf("%d ", pi2 - pi); printf("%d ", *(--pi2)); printf("%d\n", *(pi + 1)); if (pi + 1 == pi2) printf("pi et pi2 pointent vers la même case mémoire.\n"); } stats-solution.c #include <stdio.h> #include <stdlib.h> #include <math.h> int max(int valeurs[], int nbValeurs) { int m = valeurs[0]; int i; for (i = 1; i < nbValeurs; ++i) { if (valeurs[i] > m) m = valeurs[i]; } return m; } int min(int valeurs[], int nbValeurs) { int m = valeurs[0]; int i; for (i = 1; i < nbValeurs; ++i) { if (valeurs[i] < m) m = valeurs[i]; } return m; } float moyenne(int valeurs[], int nbValeurs) { int i; float s = 0.0; for (i = 0; i < nbValeurs; ++i) { s += valeurs[i]; } return s / nbValeurs; } float ecartType(int valeurs[], int nbValeurs) { int i; float xbar = moyenne(valeurs, nbValeurs); float s = 0.0; for (i = 0; i < nbValeurs; ++i) { s += (valeurs[i] - xbar) * (valeurs[i] - xbar); } return sqrt(s / (nbValeurs - 1)); } int obtenirValeurs(const char s[], int valeurs[]) { char *reste; int valeur; int i; valeur = strtol(s, &reste, 10); i = 0; while (*reste != '\0') { valeurs[i] = valeur; ++reste; valeur = strtol(reste, &reste, 10); ++i; } valeurs[i] = valeur; return i + 1; } int main(int argc, char *argv[]) { int valeurs[80]; int nbValeurs; if (argc != 2) { printf("Nombre d'arguments invalides!n"); printf(" USAGE: %s <nombres>", argv[0]); return 1; } else { nbValeurs = obtenirValeurs(argv[1], valeurs);
Image of page 2
You've reached the end of this preview.
  • Fall '09

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern