Thread: datatypes

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    Question 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!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    52
    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...
    MS VC++ 6.0

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    Talking 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
                }
            }
        }  
    }

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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.
    Truth is a malleable commodity - Dick Cheney

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    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.
    Thanks again!

  6. #6
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    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
                }
            }
        }  
    }
    Programming is a high logical enjoyable art for both programer and user !!

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    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.
    Truth is a malleable commodity - Dick Cheney

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    Thumbs up 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing datatypes (typedefs) inside template class
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2008, 02:49 AM
  2. naming iterators and other datatypes
    By l2u in forum C++ Programming
    Replies: 8
    Last Post: 10-13-2008, 09:21 PM
  3. confusion on use of datatypes!
    By Saimadhav in forum C++ Programming
    Replies: 1
    Last Post: 08-23-2008, 12:38 AM
  4. Replies: 2
    Last Post: 01-10-2005, 11:14 PM
  5. C++ Datatypes and OS Datatypes :: C++
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 12-07-2002, 02:06 PM