Thread: swapping array value

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    99

    swapping array value

    I am trying to sort array in increasing order

    my approach for sequence 5 4 3 2 1 is like following

    5 > 4 True 4 5 3 2 1

    5 > 3 True 4 3 5 2 1

    5 > 2 True 4 3 2 5 1

    5 > 2 True 4 3 2 1 5


    4 > 3 True 3 4 2 1 5

    4 > 2 True 3 2 4 1 5

    4 > 1 True 3 2 1 4 5

    and so on ...


    Code:
      #include<stdio.h>
    
    int main()
    {
        int array[5] = {5, 4, 3, 2, 1 };
    	int temp[5];
    	
    	for ( int i = 0; i < 5; i++)
    	{
    		for ( int j = 0; j < 5; j++)
    		{
    			if (array[i] > array[j+1])
    			{
    				temp[i] =  array[i];
    			}			
    			
    			
    		}
    	}
    	
    	for ( int i = 0; i < 5; i++)
    	{
    		printf("%d", temp[i]);
    	}
    	
    	return 0;
    }
    I am struggling in swapping array value

    I know swapping

    x = 1
    y = 100
    temp =x
    x = y = 100
    y = temp = 1

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    > I know swapping
    So why can't you replace those things with array[i] and array[j] ?

    Also, your j loop has the same mistake as your previous thread.
    You don't start at 0, you start at i
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    99
    Quote Originally Posted by Salem View Post
    > I know swapping
    So why can't you replace those things with array[i] and array[j] ?

    Also, your j loop has the same mistake as your previous thread.
    You don't start at 0, you start at i
    I am struggling with swapping inside inner loop

    Code:
       #include<stdio.h>
    
    int main()
    {
        int array[5] = {5, 4, 3, 2, 1 };
    	int temp[5];
    	
    	for ( int i = 0; i < 5; i++)
    	{
    		for ( int j = i+1; j < 5; j++)
    		{
    			if (array[i] > array[j])
    			{
    				temp[i] =  array[j];
    				array[i] = temp[i];  
    			}			
    			
    			
    		}
    	}
    	
    	for ( int i = 0; i < 5; i++)
    	{
    		printf("%d", temp[i]);
    	}
    	
    	return 0;
    }
    11116422280

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    148
    There is no temp[i]. You only ever need one temporary variable, to hold the value that you are swapping right now. So just store and load to temp.

    And why would you print temp[i]? You're not sorting that array, you're sorting the array array. Print that!

  5. #5
    Registered User
    Join Date
    Oct 2022
    Posts
    99
    Quote Originally Posted by aghast View Post
    There is no temp[i]. You only ever need one temporary variable, to hold the value that you are swapping right now. So just store and load to temp.

    And why would you print temp[i]? You're not sorting that array, you're sorting the array array. Print that!
    I tried but it doesn't give correct output

    Code:
      #include<stdio.h>
    
    int main()
    {
        int array[5] = {5, 4, 3, 2, 1 };
    	
    	int temp;
    	
    	for ( int i = 0; i < 5; i++)
    	{
    		for ( int j = i+1; j < 5; j++)
    		{
    			if (array[i] > array[j])
    			{
    				temp =  array[i];
    				array[j] = array[i];  
    				array[j] = temp;
    			}			
    			
    			
    		}
    	}
    	
    	for ( int i = 0; i < 5; i++)
    	{
    		printf("%d", array[i]);
    	}
    	
    	return 0;
    }
    55555

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    Quote Originally Posted by Kittu20 View Post
    I tried but it doesn't give correct output
    Because it is wrong still...
    Pay attention on what you did on swapping...

  7. #7
    Registered User
    Join Date
    Oct 2022
    Posts
    99
    Quote Originally Posted by flp1969 View Post
    Because it is wrong still...
    Pay attention on what you did on swapping...
    Thanks

    Code:
      #include<stdio.h>
    
    int main()
    {
        int array[5] = {5, 4, 3, 2, 1 };
    	
    	int temp;
    	
    	for ( int i = 0; i < 5; i++)
    	{
    		for ( int j = i+1; j < 5; j++)
    		{
    			if (array[i] > array[j])
    			{
    				temp =  array[i];
    				array[i] = array[j];
    				array[j] = temp;
    				
    				
    			}			
    			
    			
    		}
    	}
    	
    		for ( int i = 0; i < 5; i++)
    	{
    		printf("%d", array[i]);
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping Elements in an Array
    By exoruel in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2014, 11:51 PM
  2. Swapping strings around in an array
    By Tripswitch in forum C Programming
    Replies: 19
    Last Post: 08-04-2014, 12:11 PM
  3. Reverse array by swapping
    By ppooii in forum C Programming
    Replies: 3
    Last Post: 04-21-2012, 11:13 AM
  4. need help in swapping puzzle contain of the array...
    By tianwu in forum C Programming
    Replies: 4
    Last Post: 11-28-2011, 09:08 AM
  5. Swapping rows in a 2D array
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 12:04 PM

Tags for this Thread