0

Here is the link to the problem. http://www.spoj.com/problems/TOANDFRO/ And here is the link to my code http://ideone.com/w5wafl On codepad,it says floating point exception. Here is link to codepad http://codepad.org/FW9bKp5k (remove the space bw code--pad) why is it giving runtime error. is it because of error in strings or array. I am putting the code here.

#include <stdio.h>
#include <string.h>

int main(void) 
{
    int width_of_array, length_of_array, i,j , k;
    k = 0;
    char input_array[300], answer[300][300];

    scanf("%d", &width_of_array);
    scanf("%s", input_array);

    length_of_array = strlen(input_array) / width_of_array;
    for (i = 0 ; i < length_of_array ; i++)
     {
        if (i % 2 == 0)
         {  
            for ( j = 0 ; j < width_of_array ; j++)
              {
                answer[i][j] = input_array[k++];
              }
         }
        else
         {
            for (j = width_of_array - 1 ; j >= 0 ; j--)
              {
                answer[i][j] = input_array[k++];
              }
         }
    }
    for (j = 0 ; j < width_of_array ; j++)
     {
        for (i = 0 ; i < length_of_array ; i++)
         {
            printf("%c", answer[i][j]);
         }
     }
    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97

1 Answers1

1

The line length_of_array = strlen(input_array) / width_of_array; will be a division by zero if the user inputs zero as the width of the array. CodePad flags this as a floating point error, but at runtime it results in undefined behavior.

Dylan Kirkby
  • 1,427
  • 11
  • 19
  • Thanks for pointing out. It would have been another possiblity, but it was mentioned that values are greater than zero,so this was not the problem.Anyways, Thanks!! – Vishal_ranjan Dec 20 '15 at 18:33