Thread: Arranging an array ascendingly =x

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Arranging an array ascendingly =x

    I know its prolly an easy question but i cant find what i did wrong in my code.
    Its 1d array that i wanted it to rearrange
    i/p numbers from user
    o/p numbers in ascending order
    The compiler gives me memory violation error.

    Code:
    #include <stdio.h>
    #include <conio.h>
    main()
    {int x[100];
     int temp=0;
     for(int i=0;i<5;i++)
     {scanf("%d",&x[i]);}
     
      for(int i=1;i<5;i++)
       {
    
        if (x[i]<x[i-1])
        {
         temp=x[i];
         x[i]=x[i-1];
         x[i-1]=temp;
        }
    }
    for(int i=0;i<5;i++)
    printf("%d",x[i]);
    getch();
    
    }
    Last edited by Ulti; 11-12-2009 at 11:31 AM.

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    scanf("%d",x[i])
    scanf("%d",&x[i])

    Also, you're redeclaring i as an int several times, only declare it once. Set it back to 0 when needed.

    main needs a type, preferably int, which means it also needs a return statement at the end.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    6
    ty for replying..well disregarding the &x[i] i still did it and still same issue ..i guess there is a problem in the code itself, bah i am too tired xD...cant find the problem..it should work okay =x

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    6
    oh i solved it..but my algorithm to da program isnt correct =<..any hints?=x

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {  int x[100], i, j;
       int temp=0;
       for(i = 0; i < 5; i++)
         scanf("%d", &x[i]);
     
    //  for(int i=1;i<5;i++)  
       for(i = 0; i < 4; i++)  //for this sort, we stop the outer loop index counter at max - 1
       {
          for(j = i + 1; j < 5; j++) //stop at max index
          {
              if (x[i] < x[j])
              {
                  temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
        }
      }
      for(i = 0; i < 5; i++)  //removed duplicate declaration for i
         printf("%3d",x[i]);
      printf("\n\n\t\t\t     press enter when ready");
      getch();
      return 0;
    }
    Although it's now standard to allow variables to be declared anywhere in the function, it's still a great idea, to declare them all at the top of the function. You won't double declare any, and you won't be hunting around trying to find out all the variable names you've declared. Because they're all together, already.

    It wasn't sorting because you left out the second for loop.
    Last edited by Adak; 11-12-2009 at 12:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM