Array sorting problems

This is a discussion on Array sorting problems within the C++ Programming forums, part of the General Programming Boards category; OS: Win 2K Compiler MSVC++ 6.0 This program was supposed to work. I got the example from a C++ book, ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Unhappy Array sorting problems

    OS: Win 2K Compiler MSVC++ 6.0
    This program was supposed to work. I got the example from a C++ book, all I did is changing the wording in the char array the program keep aborting with errors I simply cannot fix. And all those warnings what do they mean? Anyway here is the program and the errors.
    =============================================
    /* This program uses the selection sort algorithm to sort an
    array in ascending order.
    this will sort the names and their corresponding values

    Error messages:
    -------------Configuration: selectSortVendorPrices - Win32 Debug--------------------
    Compiling...
    selectSortVendorPrices.cpp
    C:\Desktop\selectSortVendorPrices.cpp(14) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
    C:\Desktop\selectSortVendorPrices.cpp(14) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
    C:\Desktop\selectSortVendorPrices.cpp(15) : error C2117: 'F_Coop' : array bounds overflow
    C:\Desktop\selectSortVendorPrices.cpp(15) : error C2078: too many initializers
    C:\Desktop\selectSortVendorPrices.cpp(37) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
    C:\Desktop\selectSortVendorPrices.cpp(42) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
    C:\Desktop\selectSortVendorPrices.cpp(49) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
    Error executing cl.exe.

    selectSortVendorPrices.obj - 2 error(s), 5 warning(s)
    */
    ---- - - - - - - - -
    My codes:
    #include <iostream.h>

    // Function prototypes
    void selectionSort(float values[], char corr[], int);
    void showArray(float values[], char corr[], int);

    void main(void)
    {
    float values[3] = {1.50, 1.79, 2.29};
    char corr[3] = {"HP", "FCoop", "YN"}; // corresponding array

    cout << "The unsorted values are\n";
    showArray(values, corr, 3);
    selectionSort(values, corr, 3);
    cout << "The sorted values are\n";
    showArray(values, corr, 3);
    }

    //************************************************** ************
    // Definition of function selectionSort. *
    // This function performs an ascending order selection sort on *
    // array. elems is the number of elements in the array. *
    //************************************************** ************

    void selectionSort(float values[], char corres[], int elems)
    {
    int startScan, minIndex, minValue, minimum;

    for (startScan = 0; startScan < (elems - 1); startScan++)
    {
    minIndex = startScan;
    minValue = values[startScan];
    for(int index = startScan + 1; index < elems; index++)
    {
    if (values[index] < minValue)
    {
    minValue = values[index];
    minimum = corres[index];
    minIndex = index;
    }
    }
    values[minIndex] = values[startScan];
    corres[minIndex] = corres[startScan];
    values[startScan] = minValue;
    corres[startScan] = minimum;
    }
    }

    //************************************************** ************
    // Definition of function showArray. *
    // This function displays the contents of array. elems is the *
    // number of elements. *
    //************************************************** ************

    void showArray(float values[], char corres[], int elems)
    {
    for (int count = 0; count < elems; count++)
    cout << values[count] << " " " - " << corres[count] << endl;
    cout << endl;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    One problem is your declaration of the "coor" array.
    char corr[3] = {"HP", "FCoop", "YN"}; // corresponding array
    Should be:
    Code:
    char *corr[3] = {"HP", "FCoop", "YN"}; // corresponding array
    Try fixing that and see how it goes.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Unhappy Array sorting problems

    When the following codes are use I get these result
    which aren't good and 11 warnings:

    --------Configuration: selectSortVendorPrices - Win32 Debug------
    Compiling...
    selectSortVendorPrices.cpp
    c:\documents\desktop\selectsortvendorprices.cpp(31 ) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
    c:\documents\desktop\selectsortvendorprices.cpp(31 ) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
    c:\documents\desktop\selectsortvendorprices.cpp(32 ) : warning C4305: 'initializing' : truncation from 'const int' to 'char'
    c:\documents\desktop\selectsortvendorprices.cpp(32 ) : warning C4309: 'initializing' : truncation of constant value
    c:\documents\desktop\selectsortvendorprices.cpp(32 ) : warning C4305: 'initializing' : truncation from 'const int' to 'char'
    c:\documents\desktop\selectsortvendorprices.cpp(32 ) : warning C4309: 'initializing' : truncation of constant value
    c:\documents\desktop\selectsortvendorprices.cpp(32 ) : warning C4305: 'initializing' : truncation from 'const int' to 'char'
    documents\desktop\selectsortvendorprices.cpp(32) : warning C4309: 'initializing' : truncation of constant value
    documents\desktop\selectsortvendorprices.cpp(55) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
    documents\desktop\selectsortvendorprices.cpp(60) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
    documents\desktop\selectsortvendorprices.cpp(67) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data

    selectSortVendorPrices.obj - 0 error(s), 11 warning(s)


    For example, the first element was FC but ony C get
    displayed and the same forthe other twos. Any suggestions?
    the same
    char corres[3] = {'HP', 'FC', 'YN'}; // corresponding array
    //error C2078: too many initializers - Why?
    // in char corres......above code


    The unsorted values are
    1.5 - P
    1.79 - C
    2.29 - N

    The sorted values are
    1 - Ęd
    1 - Ęd
    2.29 - N

    Press any key to continue
    =====
    if I use quotes "" in the corres array for the elements
    I get 3 errors and warnings:
    -------Configuration: selectSortVendorPrices - Win32 Debug--------------------
    Compiling...
    selectSortVendorPrices.cpp
    C:\Documents\Desktop\selectSortVendorPrices.cpp(31 ) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
    C:\Documents\Desktop\selectSortVendorPrices.cpp(31 ) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
    C:\Documents\Desktop\selectSortVendorPrices.cpp(36 ) : error C2664: 'showArray' : cannot convert parameter 2 from 'char *[3]' to 'char []'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\Documents\Desktop\selectSortVendorPrices.cpp(37 ) : error C2664: 'selectionSort' : cannot convert parameter 2 from 'char *[3]' to 'char []'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\Documents\Desktop\selectSortVendorPrices.cpp(39 ) : error C2664: 'showArray' : cannot convert parameter 2 from 'char *[3]' to 'char []'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    C:\Documents\Desktop\selectSortVendorPrices.cpp(55 ) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
    C:\Documents\Desktop\selectSortVendorPrices.cpp(60 ) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
    C:\Documents\Desktop\selectSortVendorPrices.cpp(67 ) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
    Error executing cl.exe.

    selectSortVendorPrices.obj - 3 error(s), 5 warning(s)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Problem(s) with an array!!
    By Leojeen in forum C Programming
    Replies: 6
    Last Post: 05-02-2008, 07:26 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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