SUMTRIAN C code runtime error

help
c
code
runtime-error

#1

please help run this C code…

http://www.codechef.com/viewsolution/1944333

producing runtime error… works well on my pc…
PLEASE HELP…


#2

@hitesh091 your logic is right.but you have used an array b[max] where max=100 which is main cause for SIGSEGV error i.e. Segmentation fault(using array index out of limit) because number of test cases in problem is 1000 there just declare b as b[1002] for a safer side and your code will successfully run.

but what is need to write all output at a time you can output whenever your logic has been done.here you can see your code without such type of output CLICK HERE


#3

@ajay154

hi…
i want to know how to print decimal numbers after 6 digits of precision in a float-double type.


#4

@hitesh


it is simple:


you can use double as your datatype for more than 6 digit of precision and use %[n]f as format specifier in printf.where n is number of digit after decimal you want to print .as the example shows



double y = 0.012345678910; printf ("y=%.10lf ",y);


will output 0.0123456789.
and the presion of calculation will also be more than that of float datatype.

#5

One small tweak you can make is instead of storing results in another array, print it directly once you calculate the result.

for : i<-1 to t

   scan t
   do all the work to get the answer.// logic
   print result //instead of storing the result in an separate array. print it here itself.

ex:1-

scan the testcase:

print the answer

scan another testcase

print the answer.

and so on…
why this approach?
think of the problems where answer is very large and there are many test cases.
initialising another array is a waste of space right?


#6

@ajay154 thanks, a lot…


#7

ur welcome…


#8

thank u very very much… :slight_smile:


#9

ur welcum :slight_smile:


#10

@srinu634
thanks a lot…