大家帮忙分析下这段代码
看书发现有一段代码怎么实践都不通过麻烦大家帮忙分析一下。
#include <stdio.h>
#define READINGSPERDAY 24
#define VERYHIGHTEMPERATURE 200
#define VERYLOWTEMPERATURE -200
int todaysReadings[]={62,64,65,68,70,70,71,72,74,75,78,79,79,78,73,70,69,68,64,63,61,59 };
void setup(){
Serial.begin(9600);
}
void loop(){
int lowTemp;
int hiTemp;
int retVal;
retVal = CalculateMinMax(todaysReadings, &lowTemp, &hiTemp);
Serial.println("=== Sfter the function call :");
Serial.print("The lvalue for lowTemp is :");
Serial.print((long) &lowTemp,DEC);
Serial.print("and the rvalue is :");
Serial.println((long) lowTemp,DEC);
Serial.print("The lvalue for hiTemp is:");
Serial.print((long) &hiTemp,DEC);
Serial.print(" and the rvalue is: ");
Serial.println((long) hiTemp,DEC);
Serial.println("\n");
Serial.flush();
exit(0);
}
int CalculateMinMax( int temps[],int *minTemp, int *maxTemp){
int j;
*minTemp = VERYHIGHTEMPERATURE;
*maxTemp = -VERYLOWTEMPERATURE;
for (j = 0; j <READINGSPERDAY; j++ ){
if(temps >= *maxTemp){
*maxTemp = temps;
}
if(temps <= *minTemp){
*minTemp = temps;
}
}
return j;
}
输出如下,能计算出最低气温,但是计算不出最高气温,不理解开始定义VERYLOWTEMPERATURE -200,函数内部为什么又定义一次*maxTemp = -VERYLOWTEMPERATURE; 这样数值就为正了吧?
for循环中判断的函数temps >= *maxTemp 假如为69>=-200?
=== Sfter the function call :
The lvalue for lowTemp is :2296and the rvalue is :61
The lvalue for hiTemp is:2294 and the rvalue is: 24941
(1)Line 33 错了,
应该*maxTemp = VERYLOWTEMPERATURE;
(2)你的 Line 2 这样
#define READINGSPERDAY 24
可是其实 Line 5 的 todaysReadings[] 只有 22 个数据
这样会多比较两项 "乱七八糟" 的未知数,
有可能导致答案不正确 !
本帖最后由 li23108 于 2015-5-5 09:02 编辑
tsaiwn 发表于 2015-5-4 21:53 static/image/common/back.gif
(1)Line 33 错了,
应该*maxTemp = VERYLOWTEMPERATURE;
谢谢你朋友 ,确实是数组内数据不完整造成的。
=== Sfter the function call :
The lvalue for lowTemp is :2296 and the rvalue is :59
The lvalue for hiTemp is:2294 and the rvalue is: 80
*temps *temps地址的数据是不确定的大! 所以 以不确定的大进行比较 所以输出的值就是不确定的大
页:
[1]