Sort bubbles and getchar in c

I am working with Microsoft Visual Studio 2012 and trying to make a bubble look. Here is my code:

#include "stdafx.h" #include "String.h" #include <iostream> #include <string.h> using namespace std; int main() { int array[100], n, c, d, swap; printf("enter numbers of elements\n"); scanf_s("%d",&n); printf("enter %d integers\n", n); for (c = 0; c < n; c++){ scanf_s("%d", array); } for (c = 0; c < (n - 1); c++) { for (d = 0; d < n - c - 1; d++) { if (array[d]>array[d + 1]){ swap = array[d]; array[d] = array[d + 1]; array[d + 1] = swap; } } } printf("sorted list in ascending order:\n"); for (c = 0; c < n; c++){ printf("%d\n", &array[c]); } getchar(); return 0; } 

First of all, I cannot make a console for a key entry. getchar() looks like it is not working, but I have no error. Plus, when I see the console for a second, I can say that the numbers are listed as "-310892". I do not know why.

+5
source share
3 answers
 #include<stdio.h> #include<conio.h> #include<stdlib.h> int main(void) { int array[100],n,c,d,flag,swap; printf("Enter the no. of elements\n"); scanf("%d",&n); for(c=0;c<n;c++) { scanf("%d",&array[c]); // here you have to add & for assigning a address to variable in memory } for(c=0;c<(n-1);c++) { flag=0; for(d=0;d<nc-1;d++) { if(array[d]>array[d+1]) { swap=array[d]; array[d]=array[d+1]; array[d+1]=swap; flag=1; } } if(flag==0) break; } printf("sorted elements in ascending order:\n"); for(c=0;c<n;c++) { printf("%d\t",array[c]);// you want to print the element not its address so no need of & } getch(); return 0;} 
  • Please note: I am adding an additional flag flag, which helps to increase the efficiency of your program, because the loop will break when your elements are sorted, but in your program the loop may perform some additional iteration.
+2
source

I fixed the typos that others were talking about and added system("pause") . Worked fine for me on VS 2010. I didn’t get access to VS 2012 to check it out. Here is your code:

 #include <string.h> #include <stdio.h> int main() { int array[100], n, c, d, swap; printf("enter numbers of elements\n"); scanf("%d",&n); printf("enter %d integers\n", n); for (c = 0; c < n; c++){ scanf("%d", &array[c]); } for (c = 0; c < (n - 1); c++) { for (d = 0; d < n - c - 1; d++) { if (array[d]>array[d + 1]){ swap = array[d]; array[d] = array[d + 1]; array[d + 1] = swap; } } } printf("sorted list in ascending order:\n"); for (c = 0; c < n; c++){ printf("%d\n", array[c]); } system("pause"); // <---- Added this!!! return 0; } 

Hope it works great for you too.

0
source

As for your implementation of bubble sorting:

  • your scanf_s in the first for loop always reads the number into the first position of the array.
  • your printf in the last for loop expects an integer, but you specify the address.

To prevent the console from disappearing, you can replace getchar() with system("pause") , although this is not portable.

By fixing these things, bubble sorting works for me:

 #include "stdafx.h" #include "String.h" #include <iostream> using namespace std; #include <string.h> int main() { int array[100], n, c, d, swap; printf("enter numbers of elements\n"); scanf_s("%d",&n); printf("enter %d integers\n", n); for (c = 0; c < n; c++) { scanf_s("%d", &array[c]); } for (c = 0; c < (n - 1); c++) { for (d = 0; d < n - c - 1; d++) { if (array[d] > array[d + 1]) { swap = array[d]; array[d] = array[d + 1]; array[d + 1] = swap; } } } printf("sorted list in ascending order:\n"); for (c = 0; c < n; c++){ printf("%d\n", array[c]); } system("pause"); return 0; } 
0
source

All Articles