3일동안 머리를 쥐어 짜며 생각했지만.. 도저히 안되네요..
일단 온도 평균값을 구하는 프로그램인데요 (while을 사용해서)
사용자로부터 값을 입력 받아, 받은 값 중에서 최저 와 최고 온도를 구한후 그 평균온도를 산출하고 최저, 최고온도를
보여주어야 하는데요
샘플 결과물이
Enter (ctl-z to stop) > 2
Enter (ctl-z to stop) > 5
Enter (ctl-z to stop) > 1
Enter (ctl-z to stop) > //ctrl-z 키 눌렀음
The average is 3.00 // 최고와 최저 사이만의 평균일경우 3.00, 입력받은 횟수로 구할 경우2.00
The range of values is 1.00 to 5.00
====================================== 그리고 또 다른 결과물
Enter (ctl-z to stop) > -3
Enter (ctl-z to stop) > -2
Enter (ctl-z to stop) > //ctrl-z 키 눌렀음
The average is -2.50
The range of values is -3.00 to -2.00
====================================== 그리고 또 다른 결과물
Enter (ctl-z to stop) >
No input, quitting...
======================================
결과가 이렇게 나오도록 해야하구요.. 사용되는 변수들은 1000 이상의 큰 값도 다룰수 있도록끔 해야합니다..
제가 짠 소스는.. 아래와 같구요.. loop(while문)을 사용해야하는데.. 에고..
도저히 제 머리로써는 이 이상은 한계네요.. 물론 제가 짠 소스도 문제가 있구요..ㅠ.ㅠ.. 도와주세요~~!!
========================================
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int Ending (void)
{
printf("\nHit any key to continue..");
fflush(stdin);
getch();
return 0;
}
int main ()
{
float count, temp, high, low, sum, average;
int n;
clrscr();
/*** Input Stage ***/
printf("This program adds together numbers\n");
low=0;
high=0;
sum=0;
count=0;
do
{
printf("\nEnter (ctl-z to stop) > ");
n=scanf("%f", &temp);
if(temp>=0)
{
if(temp<low)
{
low=temp;
}
if(temp>high)
{
low=high;
high=temp;
}
if(high<low)
{
temp=high;
high=low;
low=temp;
}
}
else
{
if(temp<low)
{
if(high>=low)
{
low=temp;
}
if(high<low)
{
high=low;
low=temp;
}
}
if(temp>low)
{
high=temp;
}
if(temp>high)
{
low=high;
high=temp;
}
if(high<low)
{
temp=high;
high=low;
low=temp;
}
}
count++;
} while(!(n==-1)||(n==0));
sum=low+high;
average = sum / (count-1);
/** Output Stage ***/
printf("\nThe average is %.2f\n", average);
printf("\nThe range of vlues is %.2f to %.2f\n", low, high);
Ending ();
}
|