It must be determined whether the beach is to be closed or not. Right after the average has been output, you will need an if statement to decide whether the beach is to be closed or not. If the average is below 3500 print out the beach number and a short message that the beach is safe; if greater than or equal to 3500 print out the beach number and a short message that the beach is closed. , add a report to count the total number of beaches in the file and how many are open, and how many are closed.
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
. It must be determined whether the beach is to be closed or not. Right after the average has been output, you will need an if statement to decide whether the beach is to be closed or not. If the average is below 3500 print out the beach number and a short message that the beach is safe; if greater than or equal to 3500 print out the beach number and a short message that the beach is closed.
- , add a report to count the total number of beaches in the file and how many are open, and how many are closed.
//include the required header file.
#include "stdio.h"
//Define the main function.
int main(void)
{
//Declare the variable.
int b_num, num_samples, num_orgs_per_100;
char c = 'a';
//Create a file pointer.
FILE *in_file;
//Open the file in read mode.
in_file = fopen("inp.txt", "r");
//Check if the file exists.
if (in_file == NULL)
printf("Error opening the file.\n");
//Read the data from the file.
else
{
//Get the beach number and number of
//samples from the file.
fscanf(in_file, "%d", &b_num);
fscanf(in_file, "%d", &num_samples);
//Check for end of file and beach
//number value and dispaly the samples.
while(!feof(in_file))
{
printf("\nb_num = %d, num_samples = %d",
b_num,num_samples);
for(int i =0;i< num_samples;i++)
{
fscanf(in_file, "%d", &num_orgs_per_100);
//Display the input number.
printf("\nSample value %d: %d",i+1,
num_orgs_per_100);
}
//Read the newline, number of samples and b_num for
//next record
fscanf(in_file, "%c", &c);
printf("\n");
fscanf(in_file, "%d", &b_num);
fscanf(in_file, "%d", &num_samples);
}}
//Close the file.
fclose(in_file);
return 0;
}
Step by step
Solved in 4 steps with 2 images