Connect with us

Array in ascending order

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Feb 7, 2018.

Thread Status:
Not open for further replies.
Scroll to continue with content
  1. vead

    vead

    473
    14
    Nov 27, 2011
    Hello

    I am looking help to understand this program.

    array in ascending order in C programming

    Code:
    #include <stdio.h>
     
    int main(void)
    {
        int a[20], i = 0, j = 0, n, t;
     
        printf ("\n Pramot user to Enter Element: ");
        scanf ("%d", &n);
        printf ("\n");
     
        for (i = 0; i < n; i++)
        {
            printf ("\n Enter the %dth element: ", (i+1));
            scanf ("%d", &a[i]);
        }
     
        for (j = 0 ; j <(n-1); j++)
        {
            for ( i = 0 ; i <(n-1) ; i++)
            {
                if (a[i+1] < a[i])
                {
                    t = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = t;
                }
            }
        }
     
        printf ("\n Ascending order: ");
        for (i=0 ; i<n ; i++)
        {
            printf (" %d", a[i]);
        }
     
     
          return 0;
    }

    I don't understand this part of program

    Code:
     
        for (j = 0 ; j <(n-1); j++)
        {
            for ( i = 0 ; i <(n-1) ; i++)
            {
                if (a[i+1] < a[i])
                {
                    t = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = t;
                }
            }
        }
     
  2. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,448
    2,809
    Jan 21, 2010
    Do you understand "for" loops?
     
  3. vead

    vead

    473
    14
    Nov 27, 2011
    Yes I understand use of "for" loop
    Code:
    #include <stdio.h>
    int main (void)
    {
       int N = 10;
       int i; 
       for (i=0; i<N; i++)
       {
         printf ("Number %d \n",i);
       }
      
       return 0;
    } 
    Number 0
    Number 1
    Number 2
    Number 3
    Number 4
    Number 5
    Number 6
    Number 7
    Number 8
    Number 9
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    10,764
    2,424
    Nov 17, 2011
    Look up bubblesort.
     
  5. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,448
    2,809
    Jan 21, 2010
    So, what does this do?

    for (j = 0 ; j <(n-1); j++)
     
  6. vead

    vead

    473
    14
    Nov 27, 2011
    It check the value n-1 value then print the value of j

    Code:
    #include <stdio.h>
    
    int main()
    {
       int j, n;
      
       printf("value of n : ");
       scanf("%d", &n);
      
       for (j = 0 ; j <n-1; j++)
              
        printf(" j : %d \n",j);
              
        return 0;
    }
    value of n : 4
    j : 0
    j : 1
    j : 2
     
  7. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,448
    2,809
    Jan 21, 2010
    No, just the for statement. Do you understand it?

    What do the three parts of the for statement do?
     
  8. vead

    vead

    473
    14
    Nov 27, 2011
    I was showing you that I understand what is "for (j = 0 ; j <(n-1); j++)"

    I am stuck in three parts of the for statement. I don't know what is use of them ?
     
  9. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,448
    2,809
    Jan 21, 2010
    Hi I'm that case you clearly don't understand.

    Research the c for statement and come back when you have learned what they do.

    Trying to claim you understand when you only have the vaguest notion is another example of your penchant for trying to run before you can walk.

    There are no short cuts. Stop trying to take them.
     
    Brian Griffin likes this.
  10. vead

    vead

    473
    14
    Nov 27, 2011
    Where do you find wrong me in my understanding ?

    Here loop run 5 times , first it set to 0 than increase by 1 until it get 4 and then stop
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       int N = 5;
       int i;
       for (i=0; i < N; i++)
       {
         printf ("Number = %d \n",i);
       }
     
       return 0;
    }
    Number = 0
    Number = 1
    Number = 2
    Number = 3
    Number = 4

    N-1 = 5-1 = 4
    So N = 4
    Here loop run 4 times , first it set to 0 than increase by 1 until it get 3 and then stop
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       int N = 5;
       int i;
       for (i=0; i < N-1; i++)
       {
         printf ("Number = %d \n",i);
       }
     
       return 0;
    }
    Number = 0
    Number = 1
    Number = 2
    Number = 3
     
  11. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,448
    2,809
    Jan 21, 2010
    But what do the the parts mean?

    So far all I can see is that you've written some code and explained what it did.

    The fact that you can't understand the original code is evidence you don't understand the c statements it uses.

    If you understand the for statement

    For (a; b; c)
    {
    d;
    }

    Then you can trivially give the mapping of a,b,c, and d to w, x, y, and z below

    z;
    While (y)
    {
    x;
    w;
    }
     
  12. vead

    vead

    473
    14
    Nov 27, 2011
    I have make it simple

    Code:
    #include <stdio.h>
     int main(void)
    {
        int a[10], i, j, n, temp;
     
        printf ("\n Pramot user to Enter size: ");
        scanf ("%d", &n);
       
          for (i = 0; i < n; i++)
          {
            printf ("\n Enter the  element: ");
            scanf ("%d", &a[i]);
          }
       
           for(i=0;i<n;i++)
          {
         
            for( j=i;j<n;j++)
            {
                    if(a[i]>a[j])
                    {
                            temp=a[i];
                            a[i]=a[j];
                            a[j]=temp;
                    }
            }
           
          }
         
          for(i=0;i<n;i++)
                 
          printf("%d ",a[i]);
       
     
          return 0;
    }
    Pramot user to Enter size: 4

    Enter the element: 1

    Enter the element: 9

    Enter the element: 2

    Enter the element: 5

    1 2 5 9
     
    Last edited: Feb 8, 2018
  13. Harald Kapp

    Harald Kapp Moderator Moderator

    10,764
    2,424
    Nov 17, 2011
    Read my post #4 and follow the link.
    Look at your input and output and compare to what is explained in the link.
     
  14. vead

    vead

    473
    14
    Nov 27, 2011
    I followed that link and wrote program in Post #11
     
  15. Harald Kapp

    Harald Kapp Moderator Moderator

    10,764
    2,424
    Nov 17, 2011
    Do you understand how the program works? It is described in the linbk and the descriptin imho answers the question about the nested for loops.
     
  16. vead

    vead

    473
    14
    Nov 27, 2011
    Yes i understand how it works
     
  17. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,448
    2,809
    Jan 21, 2010
    Great, even though I don't necessarily entirely believe you, this thread can now be closed.
     
    vead and Brian Griffin like this.
  18. Brian Griffin

    Brian Griffin

    2
    0
    Jun 27, 2017
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Thread Status:
Not open for further replies.
Electronics Point Logo
Continue to site
Quote of the day

-