-
datatypes
Im writing a small sorting program (works so far) but for my next David Blaine stunt/magic trick, I would like it to accept decimal points. Like the typical ignorant newbie I tried to change my array from an int to float and I got quite a few invalid type errors.
Could someone with a lot of patience please break it down for me and simply explain what I need to convert it?
If you need to code, just ask.
Thnx!
-
If it is possible to get away with....simply make the array a float array. It will accept both int and float variables. Either way you're going to have to truncate the variables to be sorted. However, if you're truncating a float value to an integer value then your results will lose accuracy.
Why not display the code? It would be easier to help that way...
-
per your request
Here ya go
Code:
#include <iostream>
using std::cout;
using std::cin;
#include <stdlib.h>
void number_sort(int array[], int maxnum);
int main(void)
{
// DECLARE VARIABLES
int maxnum; // This is how many numbers we will be using
int ToBeSorted; // Values which will eventually be in ascending order
int Array[100]; // Declare Array with no more than 100 elements
cout << "How many numbers do you want to sort? ";
cin >> maxnum;
cout << "\nPlease begin entering " << maxnum << " numbers.\n";
ToBeSorted = 1;
// GATHER DATA
for(ToBeSorted = 1; ToBeSorted < maxnum + 1; ToBeSorted++){
/*
Use ToBeSorted as the array subscript and loop until we have all numbers needed (maxnum).
"ToBeSorted" is set to 1 for the first number entered and since we increased ToBeSorted by
one, the same must be done to "maxnum"
*/
cout << "\nEnter number " << ToBeSorted << "\n";
cin >> Array[ToBeSorted]; // Initialize array with values
cout << "\n";
}
// DISPLAY
cout << "\n***BEFORE***\n";
for(ToBeSorted = 1; ToBeSorted < maxnum + 1; ToBeSorted++){
cout << Array[ToBeSorted] << "\n";
}
// SORTING
number_sort(Array, maxnum);
// DISPLAY
cout << "\n****AFTER****\n";
for(ToBeSorted = 1; ToBeSorted < maxnum + 1; ToBeSorted++){
cout << Array[ToBeSorted] << "\n";
}
system("PAUSE");
return 0;
}
void number_sort(int Array[], int maxnum)
{
int ToBeSorted; // Values which will eventually be in ascending order
int Copied; // If value is < than ToBeSorted Value then copy value to Storage
int Storage; // This holds values temp until copy is complete
for(ToBeSorted = 1; ToBeSorted < maxnum + 1; ToBeSorted++){ // Pass through each element in the array
for(Copied = ToBeSorted+1; Copied < maxnum + 1; Copied++){ // Loop again and move values if needed
if(Array[ToBeSorted] > Array[Copied]){ // if ToBeSorted(1) is greater than Copied(2) then do next 3 lines
Storage = Array[ToBeSorted]; // Move ToBeSorted into temp Storage
Array[ToBeSorted] = Array[Copied]; // Move the smaller number from Copied to the ToBeSorted array
Array[Copied] = Storage; // Now copy the original value we got from ToBeStored (now in Storage) to the Copied array
}
}
}
}
-
Use a double.
Floats need to be prefixed with an 'f' when declared, although if you don't, some compilers just convert it to double.
-
2 errors
Ok I changed the ints to double except for maxnum which will always be a whole number. All others which could possibly have a decimal was changed to double. Now Im getting errors similar to when I tried float, which are:
invalid types `double[100][double]' for array subscript
invalid types `double *[double]' for array subscript
any suggestions are appreciated. :confused:
Thanks again!
-
Here we go :)
hints :
- I changed the variable ToBeStored into the variable counter
- ur program didnt work coz u tried to do this : Array[ToBeStored]---->while ToBeStored is double
- I prefered to initilze counter to 0 as array sort begin through 0,1,2,,..... 9this wasnt neccessary
- this program accept decimals and int and compined list of noumbers
hope it ll help u :)
Code:
#include <iostream>
using std::cout;
using std::cin;
#include <stdlib.h>
void number_sort(double array[], int maxnum);
int main(void)
{
// DECLARE VARIABLES
int maxnum; // This is how many numbers we will be using
double ToBeSorted; // Values which will eventually be in ascending order
double Array[100]; // Declare Array with no more than 100 elements
cout << "How many numbers do you want to sort? ";
cin >> maxnum;
cout << "\nPlease begin entering " << maxnum << " numbers.\n";
//ToBeSorted = 1; useless line !!!!!
// GATHER DATA
for(int counter = 0; counter < maxnum ; counter++){
/*
Use ToBeSorted as the array subscript and loop until we have all numbers needed (maxnum).
"ToBeSorted" is set to 1 for the first number entered and since we increased ToBeSorted by
one, the same must be done to "maxnum"
*/
cout << "\nEnter number " <<(counter+1) << "\n";
cin >> Array[counter]; // Initialize array with values
cout << "\n";
}
// DISPLAY
cout << "\n***BEFORE***\n";
for(int counter = 0; counter < maxnum ; counter++){
cout << Array[counter] << "\n";
}
// SORTING
number_sort(Array, maxnum);
// DISPLAY
cout << "\n****AFTER****\n";
for(int counter = 0; counter < maxnum ; counter++){
cout << Array[counter] << "\n";
}
system("PAUSE");
return 0;
}
void number_sort(double Array[], int maxnum)
{
double ToBeSorted; // Values which will eventually be in ascending order
int Copied; // If value is < than ToBeSorted Value then copy value to Storage
double Storage; // This holds values temp until copy is complete
for(int counter = 0; counter < maxnum; counter++){ // Pass through each element in the array
for(Copied = counter+1; Copied < maxnum ; Copied++){ // Loop again and move values if needed
if(Array[counter] > Array[Copied]){ // if ToBeSorted(1) is greater than Copied(2) then do next 3 lines
Storage = Array[counter]; // Move ToBeSorted into temp Storage
Array[counter] = Array[Copied]; // Move the smaller number from Copied to the ToBeSorted array
Array[Copied] = Storage; // Now copy the original value we got from ToBeStored (now in Storage) to the Copied array
}
}
}
}
-
Uh, sorry, I didn't read your code, just looked at the question. Array subscripts have to be integral values, i.e. an int. I believe char could be used as well, but have never seen that in C++, only Delphi. But you wouldn't use a float for a subscript, either.
-
thanks
hey, thanks for helping me out.. I dont fully understand everything thats happening yet but thats how I learn. Now its time for me to pull it apart, and figure it out for myself!
YDM!
Rye