Sentinel loops: (2024)

A very common usage of loops is the repetitive reading ofdata . This reading may be from thekeyboard or from an existing data file which contains a long list ofnumbers. In either case we need amethod for signaling the end of the process.A simple way is to use an unnatural value for the data entered, whichwill then be picked up by our loop condition ( or another if statement) . For example if we are entering scores ofexams, it is safe to choose -1 as this unnatural score, the sentinel value,which will terminate the loop. Hencethe user of the program may be instructed to enter the value -1 after all ofthe actual scores have been entered via the keyboard. Or, the sentinel value -1 may be placed at the end of our datafile.

The following is an annotated example for scores entered viathe keyboard. It uses the concept ofsentinel value.

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

Initial comments

/* Loops while( condition ) { statements }

<![if !supportEmptyParas]><![endif]>

file: 3ex1.cpp (or used as 3ex1.appended)

FALL 1998

___________________________________

Jacob Y. Kazakia jyk0

September 12, 1998

Programming example 1 of unit #3

Recitation Instructor: J.Y.Kazakia

Recitation Section 01

___________________________________

<![if !supportEmptyParas]><![endif]>

Purpose: This program reads a sequence of test scores a, b, c, etc....

and calculates the average of them. The sequence is terminated

when -1 is inserted as a test score.

The scores entered as well as the average are outputted to a file

called 3ex1report.txt with an appropriate heading.

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

Algorithm: The score entered is stored in the variable float score.

Then the variable sum ( which is float and initialized at

the beginning to zero ) is increased by the score:

<![if !supportEmptyParas]><![endif]>

sum = sum + score or equivalently sum += score

<![if !supportEmptyParas]><![endif]>

As the scores are entered an integer named count is increased

so that the number of entries is registered.

<![if !supportEmptyParas]><![endif]>

Finally we calculate average = sum / count and it is outputted.

*/

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

The fstream.h header can support both file and default I/O

<![if !supportEmptyParas]><![endif]>

The stream report is created

#include<iostream.h> // header which contains the functions

// needed for default input and output

<![if !supportEmptyParas]><![endif]>

#include<fstream.h> // header needed for file input output

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

void main()

{

ofstream report ( "3ex1report.txt", ios :: out);

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

Variables are declared and initialized

<![if !supportEmptyParas]><![endif]>

The first score is entered

The heading of the list is created

float score ; // the test score entered

float sum = 0;

float average; // the average score

<![if !supportEmptyParas]><![endif]>

int count = 0; // counter of scores entered . It starts as zero,

// ends as the total number of scores.

<![if !supportEmptyParas]><![endif]>

cout<<" \n\n Enter the first score (-1 to terminate) " ;

cin >> score;

report << "\n\n This is the list of scores entered \n\n";

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

The while loop. It will run as long as the scores entered are nonnegative.

<![if !supportEmptyParas]><![endif]>

Note that the reading of the next score is done as the last statement

while ( score >= 0) // read the scores and add them up

{

count = count + 1;

report << " score # " << count << " is " << score << endl;

sum = sum + score;

cout << " \n Enter the next score ( -1 terminates) " ;

cin >> score;

<![if !supportEmptyParas]><![endif]>

}

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

The if structure in this part makes sure that the count and average are not outputted to the file if the user terminates the loop at the very start ( avoids division by zero as well).

if ( count > 0 )

{

average = sum / count;

report << " \n\n number of scores entered : "<< count ;

report << ". The average score is "<< average ;

}

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

The results are sent to the report file. We output the Done! Statement to the screen so that the user is informed. We could have given the name of the report file (better ?)

cout << " \n \n Done! " ;

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

The output to the default window and the contents of the report file are illustrated

<![if !supportEmptyParas]><![endif]>

cout << " \n\n Enter e(exit) to exit ...";

char hold;

cin >> hold;

<![if !supportEmptyParas]><![endif]>

}

<![if !supportEmptyParas]><![endif]>

/*

<![if !supportEmptyParas]><![endif]>

THIS IS THE OUTPUT TO THE DEFAULT WINDOW:

<![if !supportEmptyParas]><![endif]>

Enter the first score (-1 to terminate) 97

<![if !supportEmptyParas]><![endif]>

Enter the next score ( -1 terminates) 83

<![if !supportEmptyParas]><![endif]>

Enter the next score ( -1 terminates) 76

<![if !supportEmptyParas]><![endif]>

Enter the next score ( -1 terminates) 95

<![if !supportEmptyParas]><![endif]>

Enter the next score ( -1 terminates) 88

<![if !supportEmptyParas]><![endif]>

Enter the next score ( -1 terminates) 34

<![if !supportEmptyParas]><![endif]>

Enter the next score ( -1 terminates) 56

<![if !supportEmptyParas]><![endif]>

Enter the next score ( -1 terminates) -1

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

Done!

<![if !supportEmptyParas]><![endif]>

Enter e(exit) to exit ...

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

THE CORRESPONDING FILE IS :

<![if !supportEmptyParas]><![endif]>

<![if !supportEmptyParas]><![endif]>

This is the list of scores entered

<![if !supportEmptyParas]><![endif]>

score # 1 is 97

score # 2 is 83

score # 3 is 76

score # 4 is 95

score # 5 is 88

score # 6 is 34

score # 7 is 56

number of scores entered : 7. The average score is 75.5714

<![if !supportEmptyParas]><![endif]>

*/

Sentinel loops: (2024)
Top Articles
The six (and a half) steps to an effective apology
New Wealth Equation Reveals 3 Ways To Grow Richer Without Working Harder
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Umn Biology
Obituaries, 2001 | El Paso County, TXGenWeb
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Colin Donnell Lpsg
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Electric Toothbrush Feature Crossword
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Used Curio Cabinets For Sale Near Me
San Pedro Sula To Miami Google Flights
Selly Medaline
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 6293

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.