Arrays

This is a discussion on Arrays within the C Programming forums, part of the General Programming Boards category; I'm trying to sort an array of numbers input from the keyboard. I've done some searches on here, but nothing ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    12

    Arrays

    I'm trying to sort an array of numbers input from the keyboard. I've done some searches on here, but nothing can point me in the right direction. Without defining the numbers, I can get the array to work, but when I put in the user-definition of the numbers, everything fails.

    here's what I've got without user-defined numbers:

    Code:
    int main ( void )
    {
       int a[ 5 ] = {3, 1, 6, 25, 92}; /* declare and initialize array a */
       int pass; /* passes counter */
       int i; /* comparisons counter */
       int hold; /* temporary location used to swap arrays elements */
       int j = 0;
       int k;
    
          
       
       printf( "Data items in original order:\n\n" );
    
       for ( i = 0; i < 5; i++ ){ /* display array output */
          printf( "%4d", a[ i ] );
       } /* end of for loop */
    
    
       for (pass = 5 - 1 ; pass > 0 ; pass--) {
          k = 0 ;
          for (i = 0 ; i < pass ; i++) {
             j++ ;
         if (a[i] > a[i + 1]) {
            hold = a[i] ;
            a[i] = a[i + 1] ;
            a[i + 1] = hold ;
         }
          }
          if (k == 0)
             break ;
       }
    
       printf( "\nData items in ascending order:\n\n" );
    
       for ( i = 0; i < 5; i++ ){ /* output array in ascending order */
          printf( "%4d", a[ i ] );
       }
       
       printf( "\n\n" );
    
       
    getch();
       return 0;
    }
    and here's the user-defined portion

    Code:
       printf(“Please enter %d data points\n”, 5);
    
    for (i = 0; i < 5; i++) {	
       printf(“Enter data no. %d : ”, i+1); 
       scanf(“%d”, &a[i]);

    in other words, this code works, but won't when I try to input numbers for the array.

    Any help would be appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean by "won't work"? Explain what you expect to have happen, and what does or doesn't happen.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe just a posting oversight, but you are missing a closing curly brace on the for loop.

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    Why are you breaking when k is zero? Since you never modify k, it will always be zero the first time you check it.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    I mean it won't sort the numbers at all. It just kind of skips around.
    I think all the curly braces are in there, actually.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    apparently this
    Code:
    if (k == 0)
             break ;
    was left over from a bad copy paste.. I took it out and didn't affect anything.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    IT WORKS! I've been messing around with this for 5 hours and it finally works.

    Thank you all very much. Here's what the code is supposed to look like:

    Code:
    // asc array.cpp : Defines the entry point for the console application.
    //
    
    #include <stdio.h>
    #include "stdafx.h"
    #include "conio.h"
    
    
    
    int 
    main ()
    {
       int a[ 5 ]; /* declare and initialize array a */
       int pass; /* passes counter */
       int i; /* comparisons counter */
       int hold; /* temporary location used to swap arrays elements */
       int j = 0;
       int k;
    
       
       printf("Please enter %d data points\n", 5);
    
       for (i = 0; i < 5; i++) {	
       printf("Enter data no. %d : ", i+1); 
       scanf("%d", &a[i]);}
    
    
       for (pass = 5 - 1 ; pass > 0 ; pass--) {
          k = 0 ;
          for (i = 0 ; i < pass ; i++) {
             j++ ;
         if (a[i] > a[i + 1]) {
            hold = a[i] ;
            a[i] = a[i + 1] ;
            a[i + 1] = hold ;
         }
          }      
       }
    
       printf( "\nData items in ascending order:\n\n" );
    
       for ( i = 0; i < 5; i++ ){ /* output array in ascending order */
          printf( "%4d", a[ i ] );
       }
       
       printf( "\n\n" );
    
       
    getch();
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    1
    In this code snippet, the stack is defined as global variable, because it needs to be accessible to all other functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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