Celmomether
A thermometer measures the temperature at regular intervals and stores the measured temperatures (may be also negative) in the temperatures
array of integers (the int
type). Unfortunately, the device is somewhat unreliable, so sometimes it is not able to measure the temperature. In that case, the value stored in the array is equal to no_value
constant.
Display the measured values in the array on the standard output as a horizontal bar thermometer. Each measured value will be displayed on one line. The first column of the output represents the most negative measured value (ignore no_value
constant). Each additional column of the output represents a temperature 1 degree higher. The temperature will be displayed as a continuous segment of the characters *
from the column representing the measured temperature to the column representing the temperature 0. Space characters are used for padding of the unused columns on the left side. If a value corresponding to the constant no_value
appears in the measured values, then the last actually measured value is used instead (use 0 if there is no such value).
To simplify the input, insert the following two lines into your source code and use them as input array for measured values
constexpr int no_value = -999;
int temperatures[] { 10, 12, no_value, no_value, 20, 14, 6, -1, -5, 0, no_value, 1, -3 };
Demo output for values -1, no_value, -3, 0, 2
:
**
**
****
*
***
#include<stdio.h>
int main(void){
constexpr int no_value = -999;
int temperatures[] { 10, 12, no_value, no_value, 20, 14, 6, -1, -5, 0, no_value, 1, -3 };
int templen = sizeof(temperatures)/sizeof(*temperatures);
int tempMax,tempStart, temperMin = 999;
for(int i = 0 ; i < templen; i++)
{
if(no_value == temperatures[i])
{
if(0 == i)
{
temperatures[i] = 0;
}
else
{
temperatures[i] = temperatures[i - 1];
}
}
temperMin = temperatures[i] < temperMin ? temperatures[i] : temperMin;
}
for(int i = 0 ; i < templen; i++)
{
if(temperatures[i] < 0)
{
tempMax = 0 ;
tempStart = temperatures[i];
}
else
{
tempMax = temperatures[i] ;
tempStart = 0;
}
for(int j = temperMin ; j <= tempMax ; j ++)
{
printf(j < tempStart?" ":"*");
}
printf("\n");
}
}
欢迎使用 CSDN 问答。 请说明你自己能做到多少, 碰到了哪些具体技术问题,我们的专家会来帮助你。 直接留下源代码你可能也不理解,不利于你的学习和成长。
你好,我是问答小助手,非常抱歉,本次您提出的有问必答问题,超出我们目前的服务范围,暂时无法为您解答。
问答VIP目前服务范围为 :Python、Java、MySQL、Redis、网络、Linux、大数据、云计算、云原生、中间件、MongoDB、HBase、Zookeeper、Kafka等领域专业问题解答,为您提供解决问题的思路和指导。
不提供源码代写、项目文档代写、论文代写、安装包资源发送或安装指导等服务。
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。