scanf in bubblesort

This is a discussion on scanf in bubblesort within the C Programming forums, part of the General Programming Boards category; here is my bubblesort code: Code: #include <stdio.h> int N; void swap(int *p, int *q) { int tmp; tmp = ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    scanf in bubblesort

    here is my bubblesort code:
    Code:
    #include <stdio.h>
    
    int N;
    void swap(int *p, int *q)
    {
        int tmp;
    	
        tmp = *p;
        *p = *q;
        *q = tmp;
    }
    
    void bubble (int a[N])
    {
    	int i, j;
    	
    	for (i = 0; i < N; i++)
    		for (j = N - 1; i < j; j--)
    		if (a[j - 1] > a[j])
    			swap(&a[j - 1], &a[j]);
    }
    
    int	main (void)
    {	
    	int i;
    	int a[N];
    	printf ("Enter the amount of numbers to be sorted: ");
    	scanf ("%d\n", &N);
    	printf ("Enter your numbers:\n");
    	for (i = 0; i < N; i++){
    		scanf (" %d", &a[i]);}
    	bubble (a);
    	for (i = 0; i < N; i++)
    	printf("%d ", a[i]);
    	printf("\n");
    	printf("\n");
    	return 0;
    }
    what i can't understand is why main only calls printf ("Enter your numbers:\n") after the for loop and not before it as it is so in the code. any suggestions? also any ideas on how to re-declare N so it is not a global variable?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Like I said in your previous thread, newlines in scanf calls do NOT do what you think they should.

    scanf ("&#37;d\n", &N);

    Also, this isn't the way to declare an N element array either.
    You need to set a maximum size when you declare the array, then decide how many values you want to input.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    For your global var, just define it in main and pass it as argument to any function that needs it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    ok cheers. i removed \n from scanf and it works now. yeah i do know that i didn't declare N properly. it has to be define N etc etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  2. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21