Advertisement
Khadija_Assem

Untitled

Nov 30th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. // print array of size n
  6. void print_array(int a[], int n) {
  7. int i = 0;
  8. while(i < n - 1 && a[i] == 0)i++;
  9. while(i < n) {
  10. printf("%d",a[i]);
  11. i++;
  12. }
  13. printf("\n");
  14. }
  15.  
  16. // scans a single integer into array with size n
  17. void scan_array(int a[], int n) {
  18. int i;
  19. for( i = 0; i < n; i++) {
  20. scanf("%1d", &a[i]);
  21. }
  22. }
  23.  
  24. // the multiplication operation
  25. // it is better to split the logic on several other functions
  26. // then make multiply function call them.
  27. int Calculate(int a[],int b[],int i,int n,int j,int Remainder,int m){
  28.  
  29. int Multi,Unit;
  30. int K[n][n+m];
  31. int U,P;
  32. static int Count=0,W=0;
  33. //Make K ==0;
  34. if ((i==n-1)&&(j==m-1)){
  35. for (P=0;P<n;P++){
  36. for(U=0;U<n+m;U++)
  37. K[P][U]=0;
  38. }
  39. }
  40. Multi =(a[i]*b[j])+Remainder;
  41. Unit = Multi%10;
  42. K[W][Count]=Unit;
  43. Count++;
  44.  
  45. if (i==0){
  46. K[W][Count]=(((a[i]*b[j])+Remainder)/10)%10;
  47. Count=0;
  48. W++;
  49. for (P=0;P<W;P++)
  50. Count++;
  51. if ((j!=0)&&(i!=0))
  52. return 0;
  53. }
  54. if ((j==0)&&(i==0)){
  55. int Multiplication[n+m];
  56. for (P=0;P<n+m;P++){
  57. Multiplication[P]=0;
  58. }
  59. int A,S,Sum=0;
  60. int Q,V=0;
  61. for (P=0;P<m+n-1;P++){
  62. Sum=0;
  63. for (U=0;U<n-1;U++){
  64. Sum+=K[U][P];
  65. //Rema
  66. }
  67. if (!P)
  68. Remainder=0;
  69. Multiplication[V]=Remainder+Sum%10;
  70. Remainder = Sum/10;
  71. V++;
  72. }
  73. for (P=V;P>=0;P--)
  74. printf("%d",Multiplication[P]);
  75. }
  76.  
  77. return 0;
  78. }
  79. void multiply(int a[], int n, int b[], int m, int r[], int t) {
  80. r[t]=0;
  81. int i,j;
  82. for (j=m-1;j>=0;j--){
  83. int Remainder=0;
  84. for (i=n-1;i>=0;i--){
  85. if(i==n-1)
  86. Remainder=0;
  87. else Remainder=(a[i+1]*b[j])/10;
  88. Calculate(a,b,i,n,j,Remainder,m);
  89. }
  90. }
  91. }
  92.  
  93. // don't change any thing in the main
  94. int main() {
  95.  
  96. int n;
  97. scanf("%d", &n);
  98. int x[n];
  99. scan_array(x, n);
  100.  
  101. int m;
  102. scanf("%d", &m);
  103. int y[m];
  104. scan_array(y, m);
  105. //Multiply
  106. int t = n + m;
  107. int ans[t];
  108.  
  109. multiply(x, n, y, m, ans, t);
  110. //print_array(ans, t);
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement