Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char **argv){
  5. int *key = NULL;
  6. int *n = NULL;
  7. int *sortedKey = NULL;
  8. int *position = NULL;
  9. int *result = NULL;
  10. int keyLength, numbLength;
  11. int i,j;
  12. int nLength;
  13. int q;
  14. int r;
  15. int tmp;
  16. int modif=1;
  17. int cpt, cpt2;
  18. int h;
  19.  
  20. printf("Entrez la longueur de votre clé\n");
  21. scanf("%d", &keyLength);
  22. key = malloc(sizeof(int)*keyLength);
  23. sortedKey = malloc(sizeof(int)*keyLength);
  24. position = malloc(sizeof(int)*keyLength);
  25. printf("Entrez les valeurs de votre clé\n");
  26. for(i = 0; i < keyLength; i++){
  27. scanf("%d",&key[i]);
  28. sortedKey[i] = key[i];
  29. }
  30.  
  31. while(modif==1){
  32. modif=0;
  33. for (j = 0; j < keyLength-1; j++){
  34. if(sortedKey[j]>sortedKey[j+1]){
  35. tmp=sortedKey[j];
  36. sortedKey[j]=sortedKey[j+1];
  37. sortedKey[j+1]=tmp;
  38. modif=1;
  39. }
  40. }
  41. }
  42. for(i = 0; i < keyLength; i++){
  43. position[i]=0;
  44. }
  45. for(i = 0; i < keyLength; i++){
  46. for(j = 0; j < keyLength; j++){
  47. if(sortedKey[i]==key[j]){
  48. if(position[j]==0){
  49. position[j]=i+1;
  50. break;
  51. }
  52. }
  53. }
  54. }
  55. for(i = 0; i < keyLength; i++){
  56. printf("%d ", position[i]);
  57. }
  58. printf("\n");
  59. printf("Entrez la longueur de votre message\n");
  60. scanf("%d", &nLength);
  61. n = malloc(sizeof(int)*nLength);
  62. printf("Entrez les valeurs de votre message");
  63. for(i = 0; i < nLength; i++){
  64. scanf("%d",&n[i]);
  65. }
  66. if(nLength%keyLength==0){
  67. h=nLength/keyLength;
  68. } else {
  69. q=nLength/keyLength;
  70. r=nLength%keyLength;
  71. h=q+1;
  72. }
  73. int **finalTab=malloc(sizeof(int*)*h+2);
  74. for(i = 0; i < h+2; i++){
  75. finalTab[i] = malloc(sizeof(int)*keyLength);
  76. }
  77. cpt2 = 0;
  78. for(i = 0; i < h+2; i++){
  79. cpt=0;
  80. for(j = 0; j < keyLength; j++){
  81. if(i==0){
  82. finalTab[i][j]=key[j];
  83. }
  84. if (i==1){
  85. finalTab[i][j]=position[j];
  86. }
  87. if (i>1){
  88. if(cpt2<nLength){
  89. if(cpt<keyLength){
  90. finalTab[i][j]=n[cpt2];
  91. cpt++;
  92. cpt2++;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. for (i = 0; i < h + 2; i++){
  99. for(j = 0; j <keyLength; j++){
  100. printf("%d ",finalTab[i][j]);
  101. }
  102. printf("\n");
  103. }
  104.  
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement