I am writing a program where the user has to input 10 temperatures and then select an option on what to do with the 10 temps. I am having problems with the exit part of the program. I want it to exit when the user inputs the letter e. Thank you in advance for your opinions and help. Here is my program:
//Source Code -> Temp.c
//Purpose ->To work with array of MAX temps, to define a constant for the array size, to use sort and search algorithms, and to write a menu driven program.
#include<stdio.h>
#include<math.h>
#define MaxTemp 10
void getTemps(int[],int);
void printTemps(int[],int);
void AvgTemps(int[],int);
void HighTemps(int[],int);
void LowTemps(int[],int);
void SortTemps(int[],int);
int main(void)
{
int option;
int Temps[MaxTemp] = {0};
do
{
printf("\n Temperature Calc:\n\n");
printf("\n 1. Input Temperatures");
printf("\n 2. Print Temperatures");
printf("\n 3. Print Temperature Average");
printf("\n 4. Print the Highest Temperature");
printf("\n 5. Print the Lowest Temperature");
printf("\n 6. Sort Temperature:");
printf("\n 7. Press 'E' in operation to exit\n\n");
printf("Please select a number:");
scanf("%d", &option);
switch(option)
{
case 1:
printf("Input \n");
getTemps(Temps,10);
break;
case 2:
printf("Show Temps\n");
printTemps(Temps,10);
break;
case 3:
printf("Average\n");
AvgTemps(Temps,10);
break;
case 4:
printf("High\n");
HighTemps(Temps,10);
break;
case 5:
printf("Low\n");
LowTemps(Temps,10);
break;
case 6:
printf("Sort\n");
SortTemps(Temps,10);
break;
default:
printf ("Error");
}
}while(option != 'E');
return 0;
}
void getTemps(int temp[], int size)
{
int counter = 0;
for(counter = 0; counter < size; counter++)
{
printf("Enter Temp %d:\n", counter + 1);
scanf("%d", &temp[counter]);
}
}
void printTemps(int temp[], int size)
{
int counter = 0;
for(counter = 0; counter < size; counter++)
{
printf("Temperature %d \n", temp[counter]);
}
}
void AvgTemps(int temp[], int size)
{
int counter = 0;
int AvgTemps = 0;
int average = 0;
for(counter = 0; counter < size; counter++)
{
AvgTemps +=temp[counter];
}
AvgTemps /= size;
printf("average %d \n", AvgTemps);
}
void HighTemps(int temp[], int size)
{
int counter = 0;
int high = -1000;
for(counter = 0; counter < size; counter++)
{
if(high < temp[counter])
high = temp[counter];
}
printf("high %d \n", high);
}
void LowTemps(int temp[], int size)
{
int counter = 0;
int low = 1000;
for(counter = 0; counter < size; counter++)
{
if(low > temp[counter])
low = temp[counter];
}
printf("low %d \n", low);
}
void SortTemps(int temp[], int size)
{
int counter1,counter2;
int tempValue;
for(counter1=0; counter1 < size - 1; counter1++)
for(counter2 = 0; counter2 < size -1-counter1; counter2++)
if(temp[counter2 + 1] < temp[counter2])
{
tempValue = temp[counter2];
temp[counter2] = temp[counter2 + 1];
temp[counter2 + 1] = tempValue;
}
}
Edited by v0id, 01 May 2008 - 09:13 PM.
Remember code-tags!


Sign In
Create Account

Back to top









