Jump to content

Need conversion of my program into function type! ASAP please!

Hi guys! I need a help ASAP!

This is my C program for arranging an array in ascending and descending order using BUBBLE SORT method.... I need to make use of functions my program... But I'm finding it hard to convert it.... So if anybody is an expert in this please reply me with converted code.... Try my code first to get a better understanding of the output...

 

#include <stdio.h>
int main()
{
 
        int arr[30];
 
        int i, j, a, n;
        printf("ELEMENT BUBBLE SORTER - ASCENDING AND DESCENDING\n");
        printf("Enter the number of elements to be sorted = ");
        scanf("%d", &n);
 
        printf("\nEnter the number values one by one\n");
       
        for (i = 0; i < n; i++)
	        scanf("%d", &arr[i]);
 
        for (i = 0; i < n; i++) 
        {
            for (j = i + 1; j < n; j++) 
            {
                if (arr[i] > arr[j]) 
                {
                    a = arr[i];
                    arr[i] = arr[j];
                    arr[j] = a;
                }
            }
        }
 
        printf("\nThe numbers arranged in ascending order are given below\n");
 
        for (i = 0; i < n; i++) 
        {
            printf("%d\n", arr[i]);
        }
    
        for (i = 0; i < n; i++) 
        {
            for (j = i + 1; j < n; ++j) 
            {
                if (arr[i] < arr[j]) 
                {
                    a = arr[i];
                    arr[i] = arr[j];
                    arr[j] = a;
                }
            }
        }
 
        printf("\nThe numbers arranged in descending order are given below\n");
 
        for (i = 0; i < n; i++) 
        {
            printf("%d\n", arr[i]);
        }
    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just copy and paste it into its own function except the array should be passed to it as a pointer argument

void sort(int *arr){

	//... sort code

}

int main(){
	int arr[30];

	//... fill the array

	sort(arr);
  	for (i = 0; i < n; i++){
		printf("%d\n", arr[i]);
	}
}

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

@Sauron I entered this code and got 'Segmentation fault (core dumped)' 

#include <stdio.h>
void sort(int *arr)
{
    int i,j,n,a;
    for (i = 0; i < n; i++) 
        {
            for (j = i + 1; j < n; j++) 
            {
                if (arr[i] > arr[j]) 
                {
                    a = arr[i];
                    arr[i] = arr[j];
                    arr[j] = a;
                }
            }
        }
 
        printf("\nThe numbers arranged in ascending order are given below\n");
 
        for (i = 0; i < n; i++) 
        {
            printf("%d\n", arr[i]);
        }
}
int main()
{
 
        int arr[30];
 
        int i, j, a, n;
        printf("ELEMENT BUBBLE SORTER - ASCENDING ORDER\n");
        printf("Enter the number of elements to be sorted = ");
        scanf("%d", &n);
 
        printf("\nEnter the number values one by one\n");
       
        sort(arr);
        
        for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);
        
    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, suriya2210 said:

@Sauron I entered this code and got 'Segmentation fault (core dumped)' 

 

You didn't set n to anything in your function

int i,j,n,a;
    for (i = 0; i < n; i++) 

so it could be anything, possibly more than your array's length.

 

You also fill the array after you call the function...

        sort(arr);
        
        for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);

which means the array is going to be full of garbage since you didn't initialize it.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×