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 :

Tableau carré en spirale


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2019
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2019
    Messages : 2
    Par défaut Tableau carré en spirale
    Salut, je besoin de l'aide a faire ce programme:


    Faites un programme en C qui range les N premiers nombres naturels dans un tableau carré en spirale depuis le coin supérieur gauche vers la droite. Faites et utilisez un sous-programme pour l'affichage des résultats

    et merci!

  2. #2
    Expert confirmé
    Avatar de gerald3d
    Homme Profil pro
    Conducteur de train
    Inscrit en
    Février 2008
    Messages
    2 315
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Conducteur de train
    Secteur : Transports

    Informations forums :
    Inscription : Février 2008
    Messages : 2 315
    Billets dans le blog
    5
    Par défaut
    Bonjour.

    Je t’en prie.

    De rien 😂

  3. #3
    Membre prolifique
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 837
    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 837
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par Lichping Voir le message
    Salut
    Bonjour

    Citation Envoyé par Lichping Voir le message
    et merci!
    Mais de rien
    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]

  4. #4
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Décembre 2019
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Décembre 2019
    Messages : 2
    Par défaut

  5. #5
    Membre éclairé Avatar de Bayard
    Inscrit en
    Juin 2002
    Messages
    863
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 863
    Par défaut
    Bonsoir

    Ha ha ha !

    Merci de poster du code en langage C et de ne pas dire "je ne sais pas par où commencer".

    Et il n'y a pas de quoi.

    Et bonne soirée.

    Cordialement

  6. #6
    Expert confirmé
    Homme Profil pro
    Analyste/ Programmeur
    Inscrit en
    Juillet 2013
    Messages
    4 771
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Analyste/ Programmeur

    Informations forums :
    Inscription : Juillet 2013
    Messages : 4 771
    Par défaut
    À tester à fond

    Code c : 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
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
     
     
    /********************************  CONSTANTS  ********************************/
     
    #define G_NUMBER 76
     
    /* (46340 * 46340 < INT_MAX). And this way, INT_MAX is a sentinel. */
    #define G_SQUARE_MAX 46340
     
     
    /**********************************  MACRO  **********************************/
     
    #define SPIRAL_MOVE \
        if (nb_cases < tmp_length) { \
            ++nb_cases; \
        } else { \
            ++nb_changes; \
            nb_cases = 0; \
             \
            switch (nb_changes) { \
            case 1: --tmp_length; break; \
            case 2: direction = ((direction > 0)? -1: 1); nb_changes = 0; break; \
            default: break; \
            } \
        } \
         \
        switch (nb_changes) { \
        case 0: col = (col + direction); break; \
        case 1: row = (row + direction); break; \
        default: break; \
        }
     
     
    /*********************************  PROGRAM  *********************************/
     
    /* XXX Warning : nb_digits should be <= 9 */
    void print_array(size_t* array, size_t length) {
        if ((array != NULL) && (length > 0)) {
            char format[6];
            size_t row, col, number, nb_digits;
     
    /*      Find the max of number of digits */
            number    = *(array); /* top-left corner value is the max */
            nb_digits = 0;
     
            while (number > 9) {
                number /= 10;
                ++nb_digits;
            }
     
            ++nb_digits;
    /*      printf("print_array - debug : nb digits %lu\n", nb_digits); */
     
    /*      Create the printf format with the max of number of digits */
            format[0] = '%';
            format[1] = (nb_digits + '0');
            format[2] = 'l';
            format[3] = 'u';
            format[4] = ' ';
            format[5] = '\0';
     
    /*      Print the array */
            for(row=0; row < length; ++row) {
                for(col=0; col < length; ++col) {
                    number = *(array + (row * length) + col);
     
                    if (number != INT_MAX) {
                        printf(format, number);
                    } else {
                        printf(format, 0);
                    }
                }
     
                printf("\n");
            }
        }
    }
     
     
    void print_array01(size_t* array, size_t length) {
        if ((array != NULL) && (length > 0)) {
            size_t row, col, number;
            int nb_digits;
     
    /*      Find the max of number of digits */
            number    = *(array); /* top-left corner value is the max */
            nb_digits = 0;
     
            while (number > 9) {
                number /= 10;
                ++nb_digits;
            }
     
            ++nb_digits;
    /*      printf("print_array - debug : nb digits %lu\n", nb_digits); */
     
    /*      Print the array */
            for(row=0; row < length; ++row) {
                for(col=0; col < length; ++col) {
                    number = *(array + (row * length) + col);
     
                    if (number != INT_MAX) {
                        printf("%*lu ", nb_digits, number);
                    } else {
                        printf("%*u ", nb_digits, 0);
                    }
                }
     
                printf("\n");
            }
        }
    }
     
     
    int main(int argc, char** argv)
    {
        if ((G_NUMBER > 4) && (G_NUMBER <= G_SQUARE_MAX)) {
            size_t* array;
            size_t length;
     
    /*      Find length */
            length = 1;
     
            while ((length * length) < G_NUMBER) {
                ++length;
            }
     
    /*      Create the array */
            array = malloc(length * length * sizeof(size_t));
     
            if (array != NULL) {
                size_t tmp, tmp_length, col, row, nb_cases, nb_changes;
                int direction;
     
                for(tmp=G_NUMBER, tmp_length=(length - 1), row=0, col=0, nb_cases=0, direction=1, nb_changes=0; tmp > 0; --tmp) {
                    *(array + (row * length) + col) = tmp;
    /*              printf("main - debug : case %lu %lu - val : %lu\n", row, col, tmp); */
     
                    SPIRAL_MOVE
                }
     
                for(tmp=((length * length) - G_NUMBER); tmp > 0; --tmp) {
                    *(array + (row * length) + col) = INT_MAX; /* sentinel */
    /*              printf("main - debug : case %lu %lu - sentinel\n", row, col); */
     
                    SPIRAL_MOVE
                }
     
    /*          print_array(array, length); */
                print_array01(array, length);
     
    /*          Free the array */
                free(array);
    /*          array = NULL; */
            } else {
                printf("main - error : malloc\n");
            }
        } else {
            printf("main - debug : nothing to do\n");
        }
     
     
        return EXIT_SUCCESS;
    }


    Édit : edgarjacobs dans un autre fil de discussion a utilisé la largeur non spécifiée dans le format de la fonction printf.
    Ceci permet d'avoir un nombre de chiffres supérieur à 9 pour la largeur du tableau. Mais cette largeur ne peut pas dépasser 46340 donc il n'y a pas de problème.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] Tracer une spirale carrée
    Par ilyeshassaine dans le forum MATLAB
    Réponses: 9
    Dernier message: 16/10/2017, 17h13
  2. [Dev-Pascal] Afficher un tableau carré et ses valeurs maximale et minimale
    Par hamzan95 dans le forum Autres IDE
    Réponses: 1
    Dernier message: 27/04/2016, 02h50
  3. Dessiner une spirale carrée
    Par Rekin85 dans le forum Langage
    Réponses: 8
    Dernier message: 18/07/2010, 21h36
  4. Créer un tableau en spirale.
    Par Dougui_bzh dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 25/11/2007, 15h39
  5. Réponses: 4
    Dernier message: 25/07/2007, 10h29

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