Advertisement
Guest User

3rd problem

a guest
Nov 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. unsigned addnum(unsigned x[],unsigned y[],unsigned z[])
  5. {
  6.         unsigned max,i;
  7.         unsigned carry=0;
  8.         if (x[0]>y[0])
  9.             max=x[0];
  10.         else max=y[0];
  11.         for (i=1;i<=max;i++)
  12.         {
  13.             z[i]=(x[i]+y[i]+carry)%10;
  14.             carry=(x[i]+y[i]+carry)/10;
  15.         }
  16.         if (carry!=0)
  17.         {
  18.             z[i]=carry;
  19.             z[0]=max+1;
  20.         }
  21.         else z[0]=max;
  22.         return z[0];
  23. }
  24.  
  25.  
  26.  
  27. int main()
  28. {
  29.     unsigned x[7]={3,7,1,3},y[7]={4,4,2,9,9},z[7]={0};
  30.     printf("%u\n",addnum(x,y,z)); // should be 5 for x[7]={3,7,1,3},y[7]={4,4,2,9,9},z[7]={0}
  31.  
  32.     for (int i=5;i>=1;i--)
  33.         printf("%u",z[i]); // should be 10241 for x[7]={3,7,1,3},y[7]={4,4,2,9,9},z[7]={0}
  34.     printf("\n");
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement