Array in ascending order

Status
Not open for further replies.

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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;
            }
        }
    }
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Do you understand "for" loops?
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
Do you understand "for" loops?
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
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
So, what does this do?

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

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
So, what does this do?

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

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
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
No, just the for statement. Do you understand it?

What do the three parts of the for statement do?
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
No, just the for statement. Do you understand it?

What do the three parts of the for statement do?
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 ?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
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.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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..

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
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
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;
}
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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:

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
Read my post #4 and follow the link.
Look at your input and output and compare to what is explained in the link.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
14,271
Joined
Nov 17, 2011
Messages
14,271
I followed that link and wrote program in Post #11
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.
 
Status
Not open for further replies.
Top