Thread: Segmentation Error / Bus Error in ANSI

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 05:17 AM.

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, 05: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, 01: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