Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what is wrong with my code for bubble sort

0 votes
asked Dec 30, 2022 by mohammad aquib sheikh (120 points)
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void

main ()

{

  int i, j, n, temp;

  int a[20];

  printf ("Enter the size of array\n");

  scanf ("%d", &n);

  printf ("Enter the elements of an array\n");

  for (i = 0; i < n; i++)

    {

      scanf ("%d", &a[i]);

    }

  printf (" the elements of an array before sorting are:\n");

  for (i = 0; i < n; i++)

    {

      scanf ("%d\t", &a[i]);

    }

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

    {

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

{

  if (a[j] > a[j + 1])

    {

      temp = a[j];

      a[j] = a[j + 1];

      a[j + 1] = temp;

    }

}

    }

  printf ("the elements of an array after sorting are:\n");

  for (i = 0; i < n; i++)

    {

      scanf ("%d", &a[i]);

    }

}

3 Answers

+1 vote
answered Jan 1, 2023 by Santosh reddy Maryada (160 points)
Line 19

In the second for loop use printf instead of scanf
commented Jan 10, 2023 by Max Jian (730 points)
gud jub : } :} ::} :} :}: }: }: }: }: }: }: } :}
0 votes
answered Jan 1, 2023 by Peter Minarik (89,230 points)

When you're about to print the numbers, you're supposed to use printf(), not scanf(). Otherwise, things should work fine.

0 votes
answered Jan 4, 2023 by Pratibha yadav (140 points)
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void

main ()

{

  int i, j, n, temp;

  int a[20];

  printf ("Enter the size of array\n");

  scanf ("%d", &n);

  printf ("Enter the elements of an array\n");

  for (i = 0; i < n; i++)

    {

      scanf ("%d", &a[i]);

    }

  printf (" the elements of an array before sorting are:\n");

  for (i = 0; i < n; i++)

    {

      printf ("%d\t", a[i]);

    }

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

    {

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

{

  if (a[j] > a[j + 1])

    {

      temp = a[j];

      a[j] = a[j + 1];

      a[j + 1] = temp;

    }

}

    }

  printf ("the elements of an array after sorting are:\n");

  for (i = 0; i < n; i++)

    {

      printf ("%d", a[i]);

    }

}

why you using scanf

you want print element use printf

scanf for input value for user.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...