Thread: modifying help plz ( int to float )

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    36

    modifying help plz ( int to float )

    can someone please help me modify this program to read floats instead of ints?

    im getting lost in my code :S

    if this is not a quick fix, i guess i just have to follow each data type being passed into each function always keeping track of the data types being sent/received?


    Code:
    #include <stdio.h>
    
    #define YESNUM              1
    #define NONUM               0
    #define MAX_NUM_PROCESSED   20
    #define MAX_LINE_LEN        127
    
    
    int GetArray(int [], int);
    int GetInt(int *);
    int StoI(const char *, int *);
    void BSort(int [], int);
    void DumpArray(const int [], int);
    
    
    
    int main(void)
    {
    	int intArray[MAX_NUM_PROCESSED];
    	int numRead;
    
    	printf("This program stops reading numbers after &#37;d ", MAX_NUM_PROCESSED);
    	printf("values\nor if EOF is encountered.\n\n");
    
    	numRead=GetArray(intArray, MAX_NUM_PROCESSED);		// Load the integer array
    
    	printf("Before sort:\n");
    	DumpArray(intArray, numRead);						// Print the pre-sorted contents
    	BSort(intArray, numRead);							// Sort them
    
    	printf("After sort:\n");
    	DumpArray(intArray, numRead);						// Print the sorted contents
    
    	return(0);
    }
    
    
    
    /*****************************************************************************
     * FUNCTION:	GetArray
     *
     * DESCRIPTION:	This function loads the integers read into an integer array.
     *				It prints a message informing if an integer can be loaded
     *				into the array.
     * 
     * INPUT:		numArray		- (int []) the address of the integer array
     *				maxNumAllowed	- (int)    the maximum number of integers
     *										   allowed
     *
     * OUTPUT		numArray		- (int []) On completion, this array is 
     *										   modified
     *
     * RETURNS:		an integer indicating the actual number of integers read
     *				and loaded into the arra
     *
     *****************************************************************************/
    int GetArray(int numArray[], int maxNumAllowed)
    {
    	int arrayIndex=0;
    	int status;
    
    
    	while (arrayIndex<maxNumAllowed &&
             (status=GetInt(&numArray[arrayIndex]))!=EOF) {
    
    		if (status==YESNUM)
    			printf("%d has been accepted\n", numArray[arrayIndex++]);
    		else
    			printf("Invalid number detected, re-enter\n\a");
    	}
    
    	if (arrayIndex>=maxNumAllowed)
    		printf("Maximum number of inputs reached\a\n\n");
    
    	return(arrayIndex);
    }
    
    
    
    
    
    /*****************************************************************************
     * FUNCTION:	GetInt
     *
     * DESCRIPTION:	This function reads an integer value from the computer
     *				standard input.
     *
     * INPUT:		pnum - (int *) the address of an integer variable
     *
     * OUTPUT:		pnum - (int *) on completion, the integer pointed by
     *							   this pointer will be modified.
     *
     * RETURNS:		a tri-state status code
     *					EOF    - on end of file
     *					YESNUM - a valid integer read
     *					NONUM  - an invalid data detected
     *		
     *****************************************************************************/
    int GetInt(int *pnum)
    {
    	char buf[MAX_LINE_LEN+1];
    
    	printf("Enter a number:  ");
    	return( gets(buf)==NULL ? EOF : StoI(buf, pnum) );
    }
    
    
    
    
    
    /*****************************************************************************
     * FUNCTION:	StoI
     *
     * DESCRIPTION:	This function takes an ASCII-Z and converts it to an integer.
     *				it is similar to the standard runtime library function atoi
     *
     * INPUT:		but  - (const char *) the address of an ASCII-Z
     *				iPtr - (int *) the address of an integer variable used to
     *							   store the converted value.
     *
     * OUTPUT:		iPtr - (int *) On completion, the integer variable pointed
     *							   by this pointer is modified.
     *
     * RETURNS:		A binary status indicating the successfulness of the conversion
     *					NONUM  - bad conversion
     *					YESNUM - good conversion
     *
     *****************************************************************************/
    int StoI(const char *buf, int *iPtr)
    {
    	int	divisor=10;
    	int signBit=1;
    	int arrayIndex=0;
    	int conversionStatus;
    
    
    	while (buf[arrayIndex]==' ')		// Strips leading spaces
    		arrayIndex++;
    
    	// Remember sign 
    	if (buf[arrayIndex]=='+' || buf[arrayIndex]=='-')
    		signBit= (buf[arrayIndex++]=='-') ? -1 : 1;
    
    
    	*iPtr=0;							// Initialize the integer variable
    
    	// This loop exploits the standard decomposition of an integer
    	// For example, 123 = 1 * 10^2 + 2 * 10^1 + 3 * 10^0, etc.
    	// To convert a single character digit to its numeric value,
    	// simply subtract '0' from it.  This is exploitation of its
    	// ASCII table behaviour.
    	while(buf[arrayIndex]>='0' && buf[arrayIndex]<='9')
    		*iPtr= *iPtr*10 + (buf[arrayIndex++]-'0');
    
    
    	if (buf[arrayIndex]=='\0') {
    		*iPtr *= signBit;
    		conversionStatus=YESNUM;
    	}
    	else
    		conversionStatus=NONUM;
    
    	return(conversionStatus);
    }
    
    
    
    
    
    /*****************************************************************************
     * F........ION:	BSort
     *
     * DESCRIPTION:	This function uses the basic bubble sort algorithm to sort
     *				an array of integers.
     *	
     * INPUT:		numArray  - (int []) base address of the integer array to sort.
     *				arraySize - (int) the actual number of elements in the array.
     *
     * OUTPUT:		numArray - (int []) this array is modified on function 
     *                                  completion
     *
     * RETURNS:		None
     *
     *****************************************************************************/
    void BSort(int numArray[], int arraySize)
    {
    	int i,
    		j;
    	int temp;
    
    	for (i=0; i<arraySize-1; i++)
    		for (j=arraySize-1; j>i; j--)
    			if (numArray[i]>numArray[j]) {
    				temp=numArray[i];
    				numArray[i]=numArray[j];
    				numArray[j]=temp;
    			}
    }
    
    
    
    
    
    /*****************************************************************************
     * FUNCTION:	DumpArray
     *
     * DESCRIPTION:	This function prints the contents of an integer array
     *
     * INPUT:		numArray  - (int []) base address of the array to be dumped
     *				arraySize - (int) the actual number of elements in the array.
     *
     * OUTPUT:		None
     *
     *****************************************************************************/
    void DumpArray(const int numArray[], int arraySize)
    {
    	int arrayIndex;
    
    	printf("The list is:\n\n");
    	for (arrayIndex=0; arrayIndex<arraySize; arrayIndex++)
    		printf("%#3d:  %d\n", arrayIndex+1, numArray[arrayIndex]);
    }
    Last edited by sh4k3; 06-26-2007 at 07:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM