Thread: Sigesgv error

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    1

    Sigesgv error

    CAN ANYONE TELL ME AS TO WHY AM I GETTING SIGSEGV ERROR WHILE SUBMITTING THIS CODE

    Code:
    1. #include<stdio.h>
    2. #include<math.h>
    3. #include<stdlib.h>
    4. #include<string.h>
    5. #define MAX 10000
    6. char * multiply(char [],char[]);
    7. int main()
    8. {
    9. int t;
    10. scanf("%d",&t);
    11. while(t--)
    12. {
    13. char a[MAX];
    14. char b[MAX];
    15. char *c;
    16. int la,lb;
    17. int i;
    18. scanf("%s",a);
    19. scanf("%s",b);
    20. c = multiply(a,b);
    21. int sum=0;
    22. for(int q=0;c[q]!='\0';q++)
    23. sum=sum+c[q];
    24. sum=sum%3;
    25. printf("%d\n",sum);
    26. }
    27. return 0;
    28. }
    29. char * multiply(char a[],char b[]){
    30. static char mul[MAX];
    31. char c[MAX];
    32. char temp[MAX];
    33. int la,lb;
    34. int i,j,k=0,x=0,y;
    35. long int r=0;
    36. long sum = 0;
    37. la=strlen(a)-1;
    38. lb=strlen(b)-1;
    39. for(i=0;i<=la;i++){
    40. a[i] = a[i] - 48;
    41. }
    42. for(i=0;i<=lb;i++){
    43. b[i] = b[i] - 48;
    44. }
    45. for(i=lb;i>=0;i--){
    46. r=0;
    47. for(j=la;j>=0;j--){
    48. temp[k++] = (b[i]*a[j] + r)%10;
    49. r = (b[i]*a[j]+r)/10;
    50. }
    51. temp[k++] = r;
    52. x++;
    53. for(y = 0;y<x;y++){
    54. temp[k++] = 0;
    55. }
    56. }
    57. k=0;
    58. r=0;
    59. for(i=0;i<la+lb+2;i++){
    60. sum =0;
    61. y=0;
    62. for(j=1;j<=lb+1;j++){
    63. if(i <= la+j){
    64. sum = sum + temp[y+i];
    65. }
    66. y += j + la + 1;
    67. }
    68. c[k++] = (sum+r) %10;
    69. r = (sum+r)/10;
    70. }
    71. c[k] = r;
    72. j=0;
    73. for(i=k-1;i>=0;i--){
    74. mul[j++]=c[i] + 48;
    75. }
    76. mul[j]='\0';
    77. return mul;
    78. }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Is there a reason you're using strings instead of numbers for your input?

    Without any prompts for the data entry you're asking for problems and the lack of meaningful variable names makes following the logic difficult if not impossible.

    By the way the program seems to run without crashing for me, be careful with those greater than and equal operations in your for loops remember that arrays stop at size - 1.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What's your input?

    What do you mean by 'submit'?
    Are you using some online programming contest / auto verification tool?

    > int t;
    > scanf("%d",&t);
    One which provides 'n' example tests on stdin?

    > y += j + la + 1;
    It seems to me you're adding la a lot of times.
    Are you sure y stays in-bounds?


    Also, lay off the all-caps.
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    If your inputs can be 10000 digits long and you're reading them as a string, you need 10001 chars to be able to hold the terminating '\0'.

    Multiplying two 10000 digit numbers can give a 20000 digit answer. So mul in multiply should be MAX*2+1 (at least).

    However, since you ultimately want the mod 3 of the result it's a waste of time multiplying the two large numbers. Just calculate the mod 3 of each of them (by adding their digits (sum of a[i]-'0')) and taking the mod 3 of that), multiply those together, and then take the mod 3 of that.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  2. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 3
    Last Post: 10-02-2007, 09:12 PM

Tags for this Thread