Serie: C++
cpp
103 líneas
· Actualizado 2026-07-21
lab8.cpp
C++/Mavis_Homework/Lab8/lab8.cpp
/*
program: lab8.c
author: firstName lastName
date: July 21 2026
purpose: using for loop
*/
#include <stdio.h>
#include "deviceAlpha.h"
int main(void)
{
int numberOfReadings = 0; /* how many readings the user wants */
int readingNumber; /* for loop counter (1, 2, 3, ...) */
int inputStatus; /* return value of scanf */
double currentTemperature; /* one temperature reading */
double totalTemperature = 0.0; /* running sum of all readings */
double averageTemperature; /* totalTemperature / numberOfReadings */
/* step 1: turn on the BLUE led and ask for the number of readings */
SetLEDState(BLUE_LED, ON);
printf("How many temperature readings will be taken? ");
inputStatus = scanf("%d", &numberOfReadings);
/* step 2: validate the input - repeat until a positive number is entered */
while (inputStatus != 1 || numberOfReadings <= 0)
{
/* remove the invalid characters left in the input buffer */
while (getchar() != '\n')
;
printf("Not a valid amount!\n");
/* alarm: BUZZER + RED led on, wait for press AND release of any button */
SetBuzzerState(BUZZER, ON);
SetLEDState(RED_LED, ON);
while (GetPushButtonState(ANY_PUSHBUTTON) == RELEASED)
; /* wait until a button is pressed */
while (GetPushButtonState(ANY_PUSHBUTTON) == PRESSED)
; /* wait until the button is released */
SetBuzzerState(BUZZER, OFF);
SetLEDState(RED_LED, OFF);
/* ask again */
printf("How many temperature readings will be taken? ");
inputStatus = scanf("%d", &numberOfReadings);
}
/* remove the newline left behind by the valid scanf */
while (getchar() != '\n')
;
/* step 3: BLUE off, HEATER on, GREEN on */
SetLEDState(BLUE_LED, OFF);
SetHeaterState(HEATER, ON);
SetLEDState(GREEN_LED, ON);
/* step 4: take the readings with a for loop */
for (readingNumber = 1; readingNumber <= numberOfReadings; readingNumber++)
{
/* show the reading number (1, 2, 3, ...) on the 7-Segment display */
DisplayIntValue(readingNumber);
/* short pause so the display updates and the sensor stabilizes */
Wait(100);
currentTemperature = GetTemperature(TEMPERATURE_SENSOR);
totalTemperature = totalTemperature + currentTemperature;
/* 1 second pause BETWEEN readings: GREEN off, YELLOW on */
if (readingNumber < numberOfReadings)
{
SetLEDState(GREEN_LED, OFF);
SetLEDState(YELLOW_LED, ON);
Wait(1000);
SetLEDState(YELLOW_LED, OFF);
SetLEDState(GREEN_LED, ON);
}
}
/* step 5: heater off and report the average temperature */
SetHeaterState(HEATER, OFF);
SetLEDState(GREEN_LED, OFF);
averageTemperature = totalTemperature / numberOfReadings;
printf("The average temperature from %d readings is %.2f\n",
numberOfReadings, averageTemperature);
DisplayDoubleValue(averageTemperature, 2);
/* step 6: wait for the user to press and release ANY button, then end */
while (GetPushButtonState(ANY_PUSHBUTTON) == RELEASED)
; /* wait for the press */
while (GetPushButtonState(ANY_PUSHBUTTON) == PRESSED)
; /* wait for the release */
return 0;
}
Artículos relacionados
C++
c
Actualizado 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/deviceAlpha.h).
Leer artículo →
C++
c
Actualizado 2026-07-21
finalproject.c
finalproject.c — c source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.c).
Leer artículo →
C++
cpp
Actualizado 2026-07-21
finalproject.cpp
finalproject.cpp — cpp source code from the C++ learning materials (C++/Mavis_Homework/FinalProject/finalproject.cpp).
Leer artículo →
C++
c
Actualizado 2026-07-21
deviceAlpha.h
deviceAlpha.h — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/deviceAlpha.h).
Leer artículo →
C++
c
Actualizado 2026-07-21
lab8.c
lab8.c — c source code from the C++ learning materials (C++/Mavis_Homework/Lab8/lab8.c).
Leer artículo →
C++
cpp
Actualizado 2026-04-03
basic_output.cpp
basic_output.cpp — cpp source code from the C++ learning materials (C++/Part1_基礎入門/Ch01_認識CPP與開發環境/basic_output.cpp).
Leer artículo →