Thread: what is going wrong in sorting program?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42

    what is going wrong in sorting program?

    Coding:
    Code:
    //Sorting
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	int arr[5],i,t,count=0;
    	for(i=0;i<=4;i++)
    	{
    	  printf("\n Enter element->");
    	  scanf("%d",&arr[i]);
    	  printf("%d",arr[i]);
    	 }
    
    	for(i=0;i<=4;i++)
    	{
    	  if(arr[i]<arr[i+1])
    	  {
    		  t=arr[i];
    		  arr[i]=arr[i+1];
    		  arr[i+1]=t;
    		}
    	  count++;
    	}
    	printf("\n The no of changes are %d",count);
     }
    while running this program Surprisingly it is taking 5 inputs and hangs....saying something like "General Protection Exception" and "processor Fault"....
    What should i do?is there anything wrong?Please help!!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In your sorting loop, what happens when i = 4 and you are asking for arr[i+1]?

    You should change your first for() loop to run on i < 5 ... but the second one needs to run on i < 4...

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42
    Quote Originally Posted by mistu4u View Post
    Coding:
    Code:
    //Sorting
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	int arr[5],i,t,count=0;
    	for(i=0;i<=4;i++)
    	{
    	  printf("\n Enter element->");
    	  scanf("%d",&arr[i]);
    	  printf("%d",arr[i]);
    	 }
    
    	for(i=0;i<=4;i++)
    	{
    	  if(arr[i]<arr[i+1])
    	  {
    		  t=arr[i];
    		  arr[i]=arr[i+1];
    		  arr[i+1]=t;
    		}
    	  count++;
    	}
    	printf("\n The no of changes are %d",count);
     }
    while running this program Surprisingly it is taking 5 inputs and hangs....saying something like "General Protection Exception" and "processor Fault"....
    What should i do?is there anything wrong?Please help!!
    what do the error messages mean?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    They mean you tried to access memory that is not yours to access. Your sorting algorythm was exceeding the bounds of your array.

    Read my previous message... the fix is there.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    30

    Red face Sorting...

    I did not get an error like yours on Dev-C++. Your program was not a SORTING program! Here is a proper sorting program-
    Ask if you have queries on this program-
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    	int arr[5],i,j,t,count=0;
    	for(i=0;i<=4;i++)
    	{
    	  printf("Enter elements\t");
    	  scanf("%d",&arr[i]);
    	 }
    
    	for(i=1;i<=4;i++)
    	{
    	  for(j=0;j<=3;j++)
    	    {
    	      if(arr[j]<arr[j+1])
    	      {
    		   t=arr[j];
    		   arr[j]=arr[j+1];
    		   arr[j+1]=t;
              }
            }
        }
    	for(i=0;i<=4;i++)
    	{
    	  printf("%d",arr[i]);
    	 }
    
    	getch();
    	return 0;
     }
    If possible, thank me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with sorting program
    By pjr5043 in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2008, 03:47 PM
  2. Sorting out cause of wrong response from class members
    By imCrushedByCode in forum C++ Programming
    Replies: 11
    Last Post: 04-18-2006, 12:30 AM
  3. Sorting Program... :(
    By thynksheraze in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2003, 07:01 AM
  4. Whats wrong with my file streaming/bubble sorting combo?
    By Alphacentric666 in forum C++ Programming
    Replies: 12
    Last Post: 01-04-2003, 09:02 PM
  5. sorting program
    By jk in forum C Programming
    Replies: 1
    Last Post: 03-18-2002, 10:21 PM