Thanks a lot for the replies. I've written the code but there are a couple of bits I'm finding it hard to do. Here's the code:
Code:
#include <stdio.h>
#define SIZE 5
void read (float *, float);
void display (float *, float);
void sort (float *, float);
void conv_far (float *, float);
void conv_cel (float *, float);
main ()
{
float values[SIZE]; //declaring variables and initalizing them
int i;
int option = 0;
while (option != 6) //loop
{ //start loop
printf("Please select an option from the list below:\n");
printf("1. Enter values\n");
printf("2. Display values\n"); //displays menu
printf("3. Sort temperatures in increasing order\n");
printf("4. Convert temperatures to Fahrenheit\n");
printf("5. Convert temperatures to Celsius\n");
printf("6. Exit Program\n");
scanf("%d", &option); //allows user to choose option
fflush (stdin);
switch (option) //switch statement, chooses option selected by user and performs task
{ // start switch
case 1:
{
read (values, SIZE);
fflush (stdin);
break;
}
case 2:
{
display (values, SIZE);
break;
}
case 3:
{
sort (values, SIZE);
break;
}
case 4:
{
conv_far (values, SIZE);
break;
}
case 5:
{
conv_cel (values, SIZE);
break;
}
case 6:
{
printf ("program ended\n");
break;
}
default:
{
printf ("\nerror! Please enter a number between 1-6\n\n");
}
} //end switch
} //end loop
}
void read (float *array, float no_of_els) // function for case 1
{
int i = 0;
for (i = 0; i < no_of_els; i++)
{
printf ("enter temperature %d: ",i+1);
scanf ("%f", &(*(array+i)));
}
printf("\n");
}
void display (float *array, float no_of_els) // case 2
{
int i = 0;
for (i = 0; i < no_of_els; i++)
{
printf ("temperature %d is %4.2f\n",i+1,*(array+i));
}
printf("\n");
}
void sort (float *array, float no_of_els) // case 3
{
int i, item, temp;
for (item = 0; item < no_of_els - 1; item++)
{
for (i = item + 1; i < no_of_els; i++)
{
if (*(array+i) < *(array+item))
{
temp = *(array+item);
*(array+item) = *(array+i);
*(array+i) = temp;
}
}
}
for (i = 0; i < SIZE; i++)
{
printf ("%4.2f\n", *(array+i) );
}
printf("\n");
}
void conv_far (float *array, float no_of_els) // case 4
{
int i = 0;
float celcius = 0;
float fahrenheit = 0;
for (i = 0; i < no_of_els; i++)
{
celcius = celcius + *(array+i);
fahrenheit = fahrenheit + (((celcius/5) * 9) + 32);
printf ("temperature %d is %4.2f\n", i+1, fahrenheit);
fahrenheit = 0;
celcius = 0;
}
printf("\n");
}
void conv_cel (float *array, float no_of_els) // case 5
{
int i = 0;
float celcius = 0;
float fahrenheit = 0;
for (i = 0; i < no_of_els; i++)
{
fahrenheit = fahrenheit + *(array+i);
celcius = celcius + (((fahrenheit-32) / 9) * 5);
printf ("temperature %d is %4.2f\n", i+1, celcius);
fahrenheit = 0;
celcius = 0;
}
printf("\n");
}
The bits I'm finding it hard to do are:
5. Provide an option to allow a user to convert the temperatures back to Celsius (only if the temperatures have already been converted to Fahrenheit). NB: You must find a way that will prevent the user selecting this option if the temperatures are already in Celsius.
and
The user should only be allowed to select option 4 (convert to Fahrenheit) if the contents of the 1-D array are in Celsius. Appropriate error messages should handle any potential problem. The same should happen if the user selects option 5 (convert to Celsius) i.e. this should only be allowed if the contents of the 1-D array are in Fahrenheit.
thanks again.