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 %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]); }



LinkBack URL
About LinkBacks



