dimanche 9 juillet 2017

[c][c++] Creating a 2D array with random numbers and arranging those numbers in increasing order (per row)

My task is to create a 10x10, 2 dimensional array using the randomizer,to arrange those numbers in an increasing order, and to get the average of those numbers per row. I have managed to create an random 10x10 array, but can't seem to arrange them in ascending order even though I'm quite sure my code is logically sound.

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

void arranger();
void line();

int main(){
    int x[10][10];
    int i,j;
    int temp[10][10];

    for(i=0; i<10; i++){
             for(j=0; j<=0; j++){
                      x[i][j]=rand();
                      }
             }

    for(i=0; i<10; i++){
             for(j=0; j<10; j++){
                      printf("%d\t",x[i][j]);
                      }
                      printf("\n");
             }

    for(i=0; i<10; i++){
             for(j=0; j<=0; j++){
                      temp[i][j]=x[i][j];
                      }
             }
    line();
    arranger(x);
    return 0;

}

void arranger(int x[10][10]){
    int a,i,j,k;

    for(i=0; i<10; i++){
             for(j=0; j<=0; j++){
                      for(k=j+1; k<10; k++){

                        if(x[i][j]>x[i][k]){
                            a = x[i][j];
                            x[i][j] = x[i][k];
                            x[i][k] = a;
                          }

                        }
                    }
             }

             printf("Array arranged in ascending order:\n");            
    for(i=0; i<10; i++){
        for(j=0; j<10; j++){
            printf("%d\t",x[i][j]);
        }   
        printf("\n");
    }
}


void line(){
    printf("\n----------------------------------------------------------------------------------\n");
}




Aucun commentaire:

Enregistrer un commentaire