<?
#include <stdio.h>
#include <stdlib.h>
float numberArray[5]; // to hold the values
// A funtion to ask for a float
void readFloat(int num)
{
float tnum;
char line[256];
printf("\nEnter Value %i : ",num); // Ask
getline(line, 256); // read
tnum = (float)strtod(line); // convert (0 if invalid)
if (tnum == 0)
{
printf("\nInvalid Input... try again);
readFloat(num); // loop
}
else
numberArray[num] = tnum; // store one value
}
// Display the array
void displayAll()
{
for(int i = 0; i < 5; i++)
printf("\nTemp %i = %f",i,numberArray[i]);
}
// Selection Sort
void sortArray()
{
int i,j,n,min_pos;
float tmp;
for (i=1; i < n; i++)
{
min_pos = i;
for (j = i + 1; j < n; j++)
if (numberArray[j] < numberArray[min_pos]) min_pos = j;
tmp = numberArray[i];
numberArray[i] = numberArray[min_pos];
numberArray[min_pos] = tmp;
}
}
void convToCelcius()
{
for(int i = 0; i < 5; i++)
numberArray[i] = ( ( (numberArray[i] - 32) / 9) * 5 );
}
void convToFahr() {
for(int i = 0; i < 5; i++)
numberArray[i] = ( (numberArray[i]/5) * 9) + 32;
}
void main()
{
for(int i = 0; i < 5; i++)
readFloat(i);
printf("\nHere are the numbers entered : ")
sortArray();
displayAll();
char c;
printf("Convert to Farheniet?");
scanf("%c",&c);
if(c = 'Y')
{
convToFahr();
printf("\nHere are the converted numbers");
displayAll();
char c2;
printf("Convert back?");
scanf("%c",&c2);
if(c2 = 'Y')
{
convToCelcius();
printf("\nHere are your numbers");
displayAll();
}
}
}
?>
<?
printf("\nEnter Value %i : ",num); // Ask
getline(line, 256); // read
tnum = (float)strtod(line); // convert (0 if invalid)
?>
| |
![]() ![]()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by: Trance Music & vBulletin Forums
Copyright ©2000-2026, Jelsoft Enterprises Ltd.
Privacy Statement / DMCA