Segmentation Error / Bus Error in ANSI

This is a discussion on Segmentation Error / Bus Error in ANSI within the C Programming forums, part of the General Programming Boards category; I keep getting segmentation or bus errors based on this code: It compiles fine and the output is just like ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Question Online Judges

    I keep getting segmentation or bus errors based on this code:
    It compiles fine and the output is just like the ones in the assignment.
    Code:
    /* */
    #include <stdio.h>
    
    int main(void)
    {
      int cases,a;
      scanf("&#37;d",&cases);
      float student_percentage[cases];
      for(a=0; a<cases; a++)
              {
                   int students;
                   scanf("%d",&students);
                   int b,c,d;
                   float total,average;
                   float num_of_students = 0;
                   float grades[students];
                   for(b=0; b<students; b++)
                            {
                                       scanf("%f",&grades[b]);
                            }
                   total = 0;
                   for(c=0; c<students; c++)
                            {
                                        total = total + grades[c];
                            }
                   average = total / students;
                   for(d=0; d<students; d++)
                                                {
                                                            if(grades[d] > average)
                                                                         {
                                                                                   num_of_students++;
                                                                                  }
                                                            else { };
                                                }
                   student_percentage[a] = num_of_students / students;
                   }
      int x;
      for(x=0; x<cases; x++)
      { 
               printf("%.3f%%\n",student_percentage[x]*100); 
               }
      return 0;
    }
    Thanks!
    Last edited by drag0n69; 02-05-2008 at 04:17 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you can get segmentation error for "outside of range arrays" too.

    In this case, you are using:
    Code:
      int cases,a;
      float student_percentage[cases];
      scanf("%d",&cases);
    The red indicates that you are using a variable that has not yet been given a value to create an array.

    Strictly speaking, variable size arrays are not allowed in C, only in C++, and even then, it should be a constant, not a variable - only some compilers extend beyond that and allow fully flexible variable size arrays - but you still have to have a defined value when creating the array, not just some random "whatever it happens to be" value.

    It is highly likely that you don't even get to the scanf() function, because cases has such a value that the code runs out of stack-space before it gets past the array declaration.

    You may want to compile your code with "-Wall -ansi" [those are for gcc, if you use a different compiler, you should be able to find some switches in the documentation/help-files to tell the compiler that you want "ANSI C" and "all warnings"] - that way you would get info from the compiler that you are using non-standard extensions and the variable not being set to a value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Please don't delete your entire post and replace it with a completely different subject - I just spent several minutes writing a reply, explaining what you need to change, and then you remove it - which makes my post look very strange.

    I guess the exactness of an online judge would depend on the actual application used and the exactness of the expected output. But segmentation fault is not part of the expected output.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    matsp:
    sorry for that mistake. i realized my mistake of declaring the array before the "students" variable was inputted and set; therefore I tried to delete this post; but I didnt eve realize there was already an answer.


    As for my program; there is only one printf() in the whole code; but I keep getting wrong answer feedbacks. I escaped the "&#37;" sign to display by using "%%". Could that be a possible reason? I have examined the output and compared it to the website's: http://icpcres.ecs.baylor.edu/online...m&problem=1311
    There is no problems whatsoever.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Could you post the output from your code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Input:
    3
    5 50 50 70 80 100
    7 100 95 90 80 70 60 50
    3 70 90 81
    Output:
    40.000&#37;
    57.143%
    66.667%

    Its just like on the Online Judge website. EXACTLY

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't know then - perhaps you should talk your tutor or whatever it is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Ok thanks for the help anyways.
    Just one more question... how would I printf the percent sign. What are the escape characters to display that "&#37;" sign instead of it being a conversion char?
    Is "%%" correct or?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, double percent is the right way to show a single percent in the output. Just like if you want a backslash in a string, you need "\\" to display that.

    Because the first character is a "special character", the second one is there to say "but I really want one of these".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Aha thanks very much. And also; another question... I am having a bit of trouble finding the logic/algorithm for a program like this:
    Takes multiple cases of 2 inputs each; and also outputs 2 numbers for each case.
    In the loop to collect the input; I did the calculations to get the 2 output; but I do not know how to store these so that I can just printf them at the end of the program...
    I used an array for the program I wrote above because the output was a single value; but I am having trouble thinking about how to store two variables dynamically.
    If what I am explaining is really confusing to you; Im sorry lol. But here is the problem which I am supposed to solve: http://icpcres.ecs.baylor.edu/online...m&problem=1753

    Thanks so much for your help!

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Quote Originally Posted by drag0n69 View Post
    but I am having trouble thinking about how to store two variables
    Hmmm

    Code:
    int a;
    int b;
    or

    Code:
    int two[2];
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 04:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. sigaction() and ANSI C
    By awoodland in forum Linux Programming
    Replies: 4
    Last Post: 04-25-2004, 01:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21