IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

probleme pour inverser un tableau en faisant une fonction


Sujet :

C

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Lycéen
    Inscrit en
    Août 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Lycéen
    Secteur : Conseil

    Informations forums :
    Inscription : Août 2013
    Messages : 35
    Points : 5
    Points
    5
    Par défaut probleme pour inverser un tableau en faisant une fonction
    bonjour a tous,
    voila j'apprends petit à petit et j'en suis aux tableaux et fonctions, j'ai donc voulu mettre les 2 en même temps mais ça ne marche pas
    aidez-moi s'il vous plait ..
    voici mon code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    inverser(int *tab,int*tab2,int*i){
        int a=0;
        while(i!=-1){
            *tab2[a]=*tab[i]  ;
            i--;a++;
        }
    }
    int main(){
        int *tab[100];int *tab2[100];int c;int *i=0;
        while(i<5){
            scanf("%d",&c);
            tab[i]=c;
            i++;
        }
     
        i--;
        inverser(&tab,&tab2,&i);
        printf("%d",tab2[0]);printf("%d",tab2[1]);printf("%d",tab2[2]);printf("%d",tab2[3]);printf("%d",tab2[4]);
    }

  2. #2
    Membre averti
    Avatar de Snack3r
    Homme Profil pro
    Doctorant à l'Université Cheikh Anta Diop de Dakar
    Inscrit en
    Octobre 2013
    Messages
    118
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : Mauritanie

    Informations professionnelles :
    Activité : Doctorant à l'Université Cheikh Anta Diop de Dakar
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2013
    Messages : 118
    Points : 444
    Points
    444
    Par défaut
    Bonsoir,

    Code corrigé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #include <stdio.h>
     
    void inverser(int src[], int length) {
        int tmp,* left = &src[0], * right = &src[length - 1];
        for (; left < right; left++, right--) {
            tmp = *left;
            *left = *right;
            *right = tmp;
        }
    }
     
    int main() {
        int tab[] = {1, 2, 3, 4, 5};
        inverser(tab, 5);
        for (int i = 0; i < 5; i++)
            printf("%d ", tab[i]);
        return 0;
    }
    C++ and Java, say, are presumably growing faster than plain C, but I bet C will still be around. ― Dennis Ritchie.

  3. #3
    Modérateur
    Avatar de Obsidian
    Homme Profil pro
    Développeur en systèmes embarqués
    Inscrit en
    Septembre 2007
    Messages
    7 368
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Développeur en systèmes embarqués
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2007
    Messages : 7 368
    Points : 23 620
    Points
    23 620
    Par défaut
    Bonjour,

    Remplace tous tes « *i » et « &i » par des « i » tout court.

  4. #4
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 684
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 684
    Points : 30 973
    Points
    30 973
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par Snack3r Voir le message
    Bonsoir,

    Code corrigé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #include <stdio.h>
     
    void inverser(int src[], int length) {
        int tmp,* left = &src[0], * right = &src[length - 1];
        for (; left < right; left++, right--) {
            tmp = *left;
            *left = *right;
            *right = tmp;
        }
    }
     
    int main() {
        int tab[] = {1, 2, 3, 4, 5};
        inverser(tab, 5);
        for (int i = 0; i < 5; i++)
            printf("%d ", tab[i]);
        return 0;
    }
    Salut
    Dans son ersatz de code, sa fonction remplit un second tableau à partir des données du premier... mais sans toucher au premier...

    Code corrigé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #include <stdio.h>
     
    void inverser(int src[], int dest[], int length) {
        int *p1, *p2, i;
        for (i=0, p1=src, p2=dest + length - 1; i < length; i++, p1++, p2--)
            (*p2)=(*p1);
    }
     
    int main() {
        int tab1[] = {1, 2, 3, 4, 5};
        int tab2[5];
        inverser(tab1, tab2, 5);
        for (int i = 0; i < 5; i++)
            printf("tab1[%d]=%d - tab2[%d]=%d\n", i, tab1[i], i, tab2[i]);
        return 0;
    }
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  5. #5
    Membre averti
    Avatar de Snack3r
    Homme Profil pro
    Doctorant à l'Université Cheikh Anta Diop de Dakar
    Inscrit en
    Octobre 2013
    Messages
    118
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : Mauritanie

    Informations professionnelles :
    Activité : Doctorant à l'Université Cheikh Anta Diop de Dakar
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2013
    Messages : 118
    Points : 444
    Points
    444
    Par défaut
    Dans son ersatz de code, sa fonction remplit un second tableau à partir des données du premier... mais sans toucher au premier...
    Code modifié :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #include <stdio.h>
     
    void inverser(int src[], int dest[], int length) {
        int *left = &src[0], *right = &dest[length - 1];
        while(length-- >= 0)
            *right-- = *left++;
    }
     
    int main() {
        int tab1[] = {1, 2, 3, 4, 5},tab2[5];
        inverser(tab1,tab2, 5);
        printf("Tab1\tTab2\n\n");
        for (int i = 0; i < 5; i++)
            printf("%d\t%d\n", tab1[i],tab2[i]);
        return 0;
    }
    C++ and Java, say, are presumably growing faster than plain C, but I bet C will still be around. ― Dennis Ritchie.

  6. #6
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 369
    Points : 41 518
    Points
    41 518
    Par défaut
    Ajout d'un peu de const-correctness:
    Code C99 : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include <stdio.h>
     
    void inverser(int const src[], int dest[], int length) {
        int const *left = &src[0];
        int *right = &dest[length - 1];
        while(length-- >= 0)
            *right-- = *left++;
    }
     
    int main() {
        int tab1[] = {1, 2, 3, 4, 5},tab2[5];
        inverser(tab1,tab2, 5);
        printf("Tab1\tTab2\n\n");
        for (int i = 0; i < 5; i++)
            printf("%d\t%d\n", tab1[i],tab2[i]);
        return 0;
    }
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

Discussions similaires

  1. [AC-2010] Probleme pour effectuer un tableau croisé : regrouper plusieurs données en une colonne
    Par misterlagaffe dans le forum Requêtes et SQL.
    Réponses: 1
    Dernier message: 09/10/2013, 11h26
  2. Réponses: 2
    Dernier message: 23/04/2010, 01h16
  3. Réponses: 3
    Dernier message: 08/08/2007, 09h47
  4. Réponses: 1
    Dernier message: 08/06/2007, 09h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo