Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char **argv){
- int *key = NULL;
- int *n = NULL;
- int *sortedKey = NULL;
- int *position = NULL;
- int *result = NULL;
- int keyLength, numbLength;
- int i,j;
- int nLength;
- int q;
- int r;
- int tmp;
- int modif=1;
- int cpt, cpt2;
- int h;
- printf("Entrez la longueur de votre clé\n");
- scanf("%d", &keyLength);
- key = malloc(sizeof(int)*keyLength);
- sortedKey = malloc(sizeof(int)*keyLength);
- position = malloc(sizeof(int)*keyLength);
- printf("Entrez les valeurs de votre clé\n");
- for(i = 0; i < keyLength; i++){
- scanf("%d",&key[i]);
- sortedKey[i] = key[i];
- }
- while(modif==1){
- modif=0;
- for (j = 0; j < keyLength-1; j++){
- if(sortedKey[j]>sortedKey[j+1]){
- tmp=sortedKey[j];
- sortedKey[j]=sortedKey[j+1];
- sortedKey[j+1]=tmp;
- modif=1;
- }
- }
- }
- for(i = 0; i < keyLength; i++){
- position[i]=0;
- }
- for(i = 0; i < keyLength; i++){
- for(j = 0; j < keyLength; j++){
- if(sortedKey[i]==key[j]){
- if(position[j]==0){
- position[j]=i+1;
- break;
- }
- }
- }
- }
- for(i = 0; i < keyLength; i++){
- printf("%d ", position[i]);
- }
- printf("\n");
- printf("Entrez la longueur de votre message\n");
- scanf("%d", &nLength);
- n = malloc(sizeof(int)*nLength);
- printf("Entrez les valeurs de votre message");
- for(i = 0; i < nLength; i++){
- scanf("%d",&n[i]);
- }
- if(nLength%keyLength==0){
- h=nLength/keyLength;
- } else {
- q=nLength/keyLength;
- r=nLength%keyLength;
- h=q+1;
- }
- int **finalTab=malloc(sizeof(int*)*h+2);
- for(i = 0; i < h+2; i++){
- finalTab[i] = malloc(sizeof(int)*keyLength);
- }
- cpt2 = 0;
- for(i = 0; i < h+2; i++){
- cpt=0;
- for(j = 0; j < keyLength; j++){
- if(i==0){
- finalTab[i][j]=key[j];
- }
- if (i==1){
- finalTab[i][j]=position[j];
- }
- if (i>1){
- if(cpt2<nLength){
- if(cpt<keyLength){
- finalTab[i][j]=n[cpt2];
- cpt++;
- cpt2++;
- }
- }
- }
- }
- }
- for (i = 0; i < h + 2; i++){
- for(j = 0; j <keyLength; j++){
- printf("%d ",finalTab[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement