|
|
看书发现有一段代码怎么实践都不通过
麻烦大家帮忙分析一下。
- #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[j] >= *maxTemp){
- *maxTemp = temps[j];
- }
- if(temps[j] <= *minTemp){
- *minTemp = temps[j];
- }
- }
- return j;
- }
复制代码
输出如下,能计算出最低气温,但是计算不出最高气温,不理解开始定义VERYLOWTEMPERATURE -200,函数内部为什么又定义一次*maxTemp = -VERYLOWTEMPERATURE; 这样数值就为正了吧?
for循环中判断的函数temps[j] >= *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
复制代码
|
|