5

I am working with microsoft visual studio 2012 and trying to make a bubble sort. 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 can't make console stay for a key entry. getchar() seems like not working but i don't have any error. Plus when I see console for a second, I can say that numbers are listed like "-310892". I don't know why.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Koray Durudogan
  • 624
  • 4
  • 12
  • 31
  • 4
    The call `scanf_s("%d", array)` will read into the first entry in `array`, *all the time*. – Some programmer dude May 27 '15 at 12:51
  • should be array[c] i think? – Koray Durudogan May 27 '15 at 12:52
  • 5
    No, should be `&array[c]` or `(array + c)`: You need a pointer to `int` here. – M Oehm May 27 '15 at 12:53
  • 2
    As for your problem about `getchar`, I assume you en all your number input with a newline? Then think about what happens when `scanf_s` reads the last number, as it will *not* read the following newline, and that newline is left in the input buffer when you call `getchar`... – Some programmer dude May 27 '15 at 12:53
  • 4
    also `printf("%d\n", array[c]);` – BLUEPIXY May 27 '15 at 12:54
  • 2
    Maybe you shouldn't concern yourself with keeping the console open, but [let Visual Studio do that for you](http://stackoverflow.com/questions/454681/how-to-keep-the-console-window-open-in-visual-c). – M Oehm May 27 '15 at 13:01
  • You can use `scanf("%*[^\n]");scanf("%*c");` just before the `getchar()` to keep the console open. – Spikatrix May 27 '15 at 13:15
  • C or C++? You should *really* pick one. Excluding the headers you've included, you're writing C. So `#include ` instead, and change your file extensions to `.c` so VS compiles them as C code. – Jonathon Reinhart May 27 '15 at 14:09
  • @JonathonReinhart: the `using namespace std;` leaves the rest of the code in the C subset of C++. I removed the C tag since the using statement makes it C++ and not C, but I agree that if the using statement were removed, it would leave C code and then the C++ tag would be the one eliminate. – Jonathan Leffler May 27 '15 at 15:05

3 Answers3

2
#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<n-c-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;}
  1. Please note i'm adding additional variable "flag" which helps to increase the efficiency of your program because the loop will break when your elements are done sorting but in your program the loop might be doing some extra iteration.
0

I fixed the typos that others mentioned and added system("pause"). Worked fine for me on VS 2010. Haven't got access to VS 2012 to test it. Here's 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 fine for you too.

banach-space
  • 1,781
  • 1
  • 12
  • 27
0

Concerning your bubble sort implementation:

  1. your scanf_s in the first for loop always reads the number into the first position of the array.
  2. your printf in the last for loop expects an integer but you supply an address.

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

Correcting these things, the bubble sort 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;
}
SX.
  • 81
  • 1
  • 8