ok... i did most of the dirty work, but now im getting some undesired output.
when i enter in an integer, the program converts it to a float...
but the problem is now, when i enter in a float, for ex. 2.1... the program converts it to 1.2
and if i enter in a float with more than one digit before the decimal, then i get a whole bunch of undesired outputs.
here is my code:
Code:
#include <stdio.h>
#define YESNUM 1
#define NONUM 0
#define MAX_NUM_PROCESSED 20
#define MAX_LINE_LEN 127
int GetArray(float [], int);
int GetInt(float *);
int StoI(const char *, float *);
void BSort(float [], int);
void DumpArray(const float [], int);
int main(void)
{
float 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 - (float []) the address of the integer array
* maxNumAllowed - (int) the maximum number of integers
* allowed
*
* OUTPUT numArray - (float []) On completion, this array is
* modified
*
* RETURNS: an integer indicating the actual number of integers read
* and loaded into the arra
*
*****************************************************************************/
int GetArray(float numArray[], int maxNumAllowed)
{
int arrayIndex=0;
int status;
while (arrayIndex<maxNumAllowed &&
(status=GetInt(&numArray[arrayIndex]))!=EOF) {
if (status==YESNUM)
printf("%f 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 - (float *) the address of an integer variable
*
* OUTPUT: pnum - (float *) 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(float *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 - (float *) 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, float *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.
//int isDecimal = 0;
while(buf[arrayIndex]>='0' && buf[arrayIndex]<='9' && buf[arrayIndex] != '\0')
{
if( buf[arrayIndex]>='0' && buf[arrayIndex]<='9' && buf[arrayIndex]!='.')
{
*iPtr= *iPtr*10 + (buf[arrayIndex++]-'0'); // turning char into int... taking ascii value and subtracting char 0, to get a numeric value
}
if(buf[arrayIndex]=='.')
{
arrayIndex++;
while(buf[arrayIndex]>='0' && buf[arrayIndex]<='9' && buf[arrayIndex] !='\0')
{
*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 - (float []) base address of the integer array to sort.
* arraySize - (int) the actual number of elements in the array.
*
* OUTPUT: numArray - (float []) this array is modified on function
* completion
*
* RETURNS: None
*
*****************************************************************************/
void BSort(float numArray[], int arraySize)
{
int i,j;
float 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 float array
*
* INPUT: numArray - (float []) base address of the array to be dumped
* arraySize - (int) the actual number of elements in the array.
*
* OUTPUT: None
*
*****************************************************************************/
void DumpArray(const float numArray[], int arraySize)
{
int arrayIndex;
printf("The list is:\n\n");
for (arrayIndex=0; arrayIndex<arraySize; arrayIndex++)
printf("%#3d: %f\n", arrayIndex+1, numArray[arrayIndex]);
}