Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- // print array of size n
- void print_array(int a[], int n) {
- int i = 0;
- while(i < n - 1 && a[i] == 0)i++;
- while(i < n) {
- printf("%d",a[i]);
- i++;
- }
- printf("\n");
- }
- // scans a single integer into array with size n
- void scan_array(int a[], int n) {
- int i;
- for( i = 0; i < n; i++) {
- scanf("%1d", &a[i]);
- }
- }
- // the multiplication operation
- // it is better to split the logic on several other functions
- // then make multiply function call them.
- int Calculate(int a[],int b[],int i,int n,int j,int Remainder,int m){
- int Multi,Unit;
- int K[n][n+m];
- int U,P;
- static int Count=0,W=0;
- //Make K ==0;
- if ((i==n-1)&&(j==m-1)){
- for (P=0;P<n;P++){
- for(U=0;U<n+m;U++)
- K[P][U]=0;
- }
- }
- Multi =(a[i]*b[j])+Remainder;
- Unit = Multi%10;
- K[W][Count]=Unit;
- Count++;
- if (i==0){
- K[W][Count]=(((a[i]*b[j])+Remainder)/10)%10;
- Count=0;
- W++;
- for (P=0;P<W;P++)
- Count++;
- if ((j!=0)&&(i!=0))
- return 0;
- }
- if ((j==0)&&(i==0)){
- int Multiplication[n+m];
- for (P=0;P<n+m;P++){
- Multiplication[P]=0;
- }
- int A,S,Sum=0;
- int Q,V=0;
- for (P=0;P<m+n-1;P++){
- Sum=0;
- for (U=0;U<n-1;U++){
- Sum+=K[U][P];
- //Rema
- }
- if (!P)
- Remainder=0;
- Multiplication[V]=Remainder+Sum%10;
- Remainder = Sum/10;
- V++;
- }
- for (P=V;P>=0;P--)
- printf("%d",Multiplication[P]);
- }
- return 0;
- }
- void multiply(int a[], int n, int b[], int m, int r[], int t) {
- r[t]=0;
- int i,j;
- for (j=m-1;j>=0;j--){
- int Remainder=0;
- for (i=n-1;i>=0;i--){
- if(i==n-1)
- Remainder=0;
- else Remainder=(a[i+1]*b[j])/10;
- Calculate(a,b,i,n,j,Remainder,m);
- }
- }
- }
- // don't change any thing in the main
- int main() {
- int n;
- scanf("%d", &n);
- int x[n];
- scan_array(x, n);
- int m;
- scanf("%d", &m);
- int y[m];
- scan_array(y, m);
- //Multiply
- int t = n + m;
- int ans[t];
- multiply(x, n, y, m, ans, t);
- //print_array(ans, t);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement