Thread: Help with an Array

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    7

    Help with an Array

    my program below currently sorts the records in the array in ascending order prior to searching, I am having trouble changing it so that the it sorts the array into descending order. if you have help it would be appreciated.



    /* The PHONE program reads NUMB_RECS records from
    the file students.dat into the two-dimensional
    array students. The records occur one to a line,
    with a maximum length of MAX_REC_SIZE and fields
    formatted as follows:

    25 chars 25 chars 12 chars
    <last name> <first name> <phone number>

    Column 1 Column 26 Column 51

    MAX_REC_SIZE is thus LNAME_SIZE + FNAME_SIZE + PNUM_SIZE.
    One record occurs per line.

    PHONE sorts the records—as character strings—in ascending
    order and then does lookups. It prompts for a last name,
    padding it if necessary with blanks to LNAME_SIZE, and then
    searches the array. If the name is found, the entire record
    is written to the terminal in their file column positions. If the
    name is not found, a message is written.

    PHONE uses selection sort for the sorting and binary search
    for the searching. The program consists of the functions:
    Main, selection_sort, binary_search, and swap. */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define NUMB_RECS 30 /* number of records in students.dat */
    #define LNAME_SIZE 25 /* first 25 chars */
    #define FNAME_SIZE 25 /* next 25 chars */
    #define PNUM_SIZE 12 /* last 12 chars */
    #define MAX_REC_SIZE (LNAME_SIZE + FNAME_SIZE + PNUM_SIZE)
    #define NO_FIND (-999) /* flag to signal lookup failure */
    #define BLANK ‘ ‘ /* used to pad candidate name so
    it, to has LNAME_SIZE chars */
    void selection_sort ( char array [ ] [ MAX_REC_SIZE + 2], int size1 );
    void swap (char array [ ] [ MAX_REC_SIZE + 2 ], int current, int low );

    int binary_search(char candidate [ ],
    char array [ ][ MAX_REC_SIZE + 2],
    int sixe );
    main ()
    {
    /* student records (+2 is for the newline and the
    null terminator) */
    char students [ NUMB_RECS ] [ MAX_REC_SIZE + 2];

    /* candidate in search (+2 is for the newline and the
    null terminator) */
    char candidate [ LNAME_SIZE + 2];

    int cand_len; /* length of candidate’s name */
    int index; /* index into students */
    int i;

    FILE* fp;

    /* read the names of NUMB_RECS records into array students */
    fp = open(”students.dat”, “r”);

    FOR (i = 0; i < NUMB_RECS; ++1)
    fgets( students [i], MAX_REC_SIZE + 2, fp);

    /* sort the records into ascending order, by name */
    selection_sort( students, NUMB_RECS);

    /* Prompt the user for a student’s name and look for it in the
    array students. Halt when the user signals EOF at the
    prompt. */

    printf( “\n\n Enter a student’s name,”
    “or signal EOF to halt: “);
    while ( fgets(candidate, LNAME_SIZE + 2, stdin) !=NULL) {

    /* Pad candidate with blanks to LNAME_SIZE chars. */
    cand_len = strlen( candidate ) – 1; /* -1 for newline stored by fgets */

    for ( i = cand_len; i < LNAME_SIZE; i++ )
    candidate [ i ] = BLANK;
    candidate [ LNAME_SIZE ] = ‘\0’; /*null terminate */

    index = binary_search ( candidate, students, NUMB_RECS );
    if ( index ! = NO_FIND)
    printf( “\nRecord: %s”, students [ index] );
    else {
    candidate [ cand_len ] = ‘\0’; /* knock off blanks */
    printf ( “\n\t%s is not in our directory.”,
    candidate);

    }
    return EXIT_SUCCESS;
    }
    /* selection_sort ( array, size1)

    The function expects an array and its size. It sorts the
    elements into ascending order. it does not return a value.

    The algorithm can be sketched as follows:
    1. Repeat steps 2 and 3 for ind equals 0, 1,..., size1 –2.
    2. Select the smallest item among
    array [ind], array [ind + 1],..., array[ size1 – 1 ].
    3. Swap the smallest with array[ ind].
    */

    void selection_sort (char array [ ] [ MAX_REC_SIZE + 2 ], int size1 )
    {
    int smallest_index; /* index of smallest string seen */
    int i, j;

    /* loop size1; /* index of smallest string seen */
    for ( i = 0; i < size1 – 1; ++i ) {
    /* assume ith string is smallest */
    smallest_index = i;
    /* compare smallest against remaining strings */
    for ( j = i + 1; j < size1; ++j )
    /* look for smaller string */
    if (strcmp ( array [ j ], array[ smallest_index] ) < 0 )
    smallest_index = j;
    /* put smallest at index i */
    if ( i != smallest_index )
    swap( array, i, smallest_index );
    }
    }

    /* Swap array [current ] and array [ low ]. */

    void swap9 char array [ ] [ MAX_REC_SIZE + 2]. int current, int low)
    {
    char temp[ MAX_REC_SIZE + 2];

    strcpy ( temp, array [current ] );
    strcpy (array [current ], array[low] );
    strcpy (array [ low], temp );
    }
    /* binary_search ( candidate, array, size )
    the function searches the sorted array
    array [0], array [ 1 ],..., array [ size – 1 ]

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Perhaps the easiest way would be a simple reversing function, ie:


    Code:
    void Reverse(char *array[], int count, int maxlen)
    {
      int x, z;
      char temp[count][maxlen];  
    
      for( x = 0; x < count, x++ )   //...copy original to temp
         strcpy(temp[x], array[x]);
    
      x = 0;
    
      for( z = count; z != 0; x++, z-- )   //...copy to original in reverse...
          strcpy(array[x], temp[z]);
    
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM