C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-04-2002, 11:54 AM   #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;
}
cazil is offline   Reply With Quote
Old 02-04-2002, 12:02 PM   #2
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,864
One problem is your declaration of the "coor" array.
Quote:
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.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 02-04-2002, 01:36 PM   #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)
cazil is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting structs in an array and sorting the array of structs[ascending].. Huskar C Programming 3 03-31-2009 12:34 PM
Dynamic array of structures containing yet another dynamic array of structures innqubus C Programming 2 07-11-2008 07:39 AM
Problem(s) with an array!! Leojeen C Programming 6 05-02-2008 07:26 PM
Merge sort please vasanth C Programming 2 11-09-2003 12:09 PM
Struct *** initialization Saravanan C Programming 20 10-09-2003 12:04 PM


All times are GMT -6. The time now is 12:17 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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