C++ gettinng Segmenation fault

I was trying the question http://www.spoj.com/problems/JULKA/.
I used vector to solve it i m getting right answer on codeblocks as well as on ideone.But getting segmentation fault on spoj.
Can anyone plz tell me the possible reason for this.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
vector<int> a,b,sum,minus1;
vector<int>::reverse_iterator i,j;
inline long int inputA()
{
    long int n=0;
    char c=getchar();
	while(c<'0' || c>'9')
	c=getchar();
	while(c<='9' && c>='0')
	{
      a.push_back(c-'0');
      n=(n<<1)+(n<<3)+c-'0';
      c=getchar();
	}
	return n;
}
inline long int  inputB()
{
    long int n=0;
    char c=getchar();
	while(c<'0' || c>'9')
	c=getchar();
	while(c<='9' && c>='0')
	{
      b.push_back(c-'0');
      n=(n<<1)+(n<<3)+c-'0';
      c=getchar();
	}
	return n;
}

inline void Add();

inline void sub();




int main()
{
int t=10;long int x,y;
while(t--)
{
    x=inputA();
    y=inputB();
    if(x<100)
    {
        cout<<(x+y)/2<<"\n"<<(x-y)/2;
    }
    else{
    Add();
    cout<<"\n";
    sub();
    }
    a.clear();
    b.clear();
}
return 0;
}

inline void Add()
{
    j=b.rbegin();
    i=a.rbegin();
    int c =0;
    while(j!=b.rend())
    {
        if(*i+*j+c>=10)
        {
            sum.push_back(*i+*j+c-10);
            c=1;
        }
        else
        {
            sum.push_back(*i+*j+c);
            c=0;
        }
        i++;j++;
    }
    while(c!=0||i!=a.rend())
    {
        if(*i+c>=10)
        {
            sum.push_back(*i+c-10);
            c=1;
        }
        else
        {
            sum.push_back(*i+c);
            c=0;
        }
        i++;
        if(i==a.rend()){break;}
    }
    if(c)
    {
        sum.push_back(1);
    }
     //division by 2
    int r=0,temp1;//remainder
    for(i=sum.rbegin();i!=sum.rend();i++)
    {
        if(r==0)
        {    r=(*i)%2;
             *i=*i/2;
        }
        else
        {
            temp1=(r*10+*i)%2;
            *i=(r*10+*i)/2;
            r=temp1;
        }
    }
    //for ignoring leading zeroes
    i=sum.rbegin();
    while(*i==0)
    {
        i++;
    }
    while(i!=sum.rend())
    {
        cout<<*i;
        i++;
    }
    sum.clear();
}

inline void sub()
{
    j=b.rbegin();
    i=a.rbegin();
    int c =0;
    while(j!=b.rend())
    {
        if(c==0)
        {
            if(*i-*j>=0)
            {
                minus1.push_back(*i-*j);
            }
            else
            {
                minus1.push_back(10+*i-*j);
                c=1;
            }
        }
        else
        {
            if(*i-*j-1>=0)
            {
                minus1.push_back(*i-*j-1);
                c=0;
            }
            else
            {
                minus1.push_back(9+*i-*j);
            }
        }
        i++;j++;
    }
    while(c!=0||i!=a.rend())
    {
        if(*i-c>=0)
        {
            minus1.push_back(*i-c);
            c=0;
        }
        else
        {
            minus1.push_back(9+*i);
            c=1;
        }
        i++;
    }
    //division by 2
    int r=0,temp1;//remainder
    for(i=minus1.rbegin();i!=minus1.rend();i++)
    {
        if(r==0)
        {    r=(*i)%2;
             *i=*i/2;
        }
        else
        {
            temp1=(r*10+*i)%2;
            *i=(r*10+*i)/2;
            r=temp1;
        }
    }
    //for ignoring leading zeroes
    i=minus1.rbegin();
    while(*i==0&&i!=minus1.rend())
    {
        i++;
    }
    if(i==minus1.rend()){cout<<"0";}

    else{//output
    while(i!=minus1.rend())
    {
        cout<<*i;
        i++;
    }
    }
    minus1.clear();
}
What are your inputs?
inputA and inputB are identical except for the vector that they populate. Why not create one function that takes the vector as a parameter:
long input(vector<int> &vec);

n=(n<<1)+(n<<3)+c-'0';
It would be much clearer to say n = 10*n+c-'0';. Let the compiler decide if the optimization is worth it.

inputA() and inputB() are attempting to return the number, but the number can be as high as 10^100, which will overflow the value of a long int. Since you only care if the result is large, consider using a double, or just always using your vector to determine the answer.

Line 50: what if A<100 but B is 10^50?
Line 84 handles extra digits in A. What if the extra digits are in B, not A?
Line 165: Same problem as line 84.


Last edited on
@dhayden: B<=A

The overflow in input{A,B}() is an issue


@OP: /*165*/while(c!=0||i!=a.rend())
if the carry were in the last digit then you would try to dereference an invalid iterator
Last edited on
Topic archived. No new replies allowed.