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
Paysera Knowledge Base - 9.27 Is it safe to pay with the Visa payment card?
26 CFR § 25.2512-2 - Stocks and bonds.
Frases para un bendecido domingo: llena tu día con palabras de gratitud y esperanza - Blogfrases
Celebrity Extra
Hertz Car Rental Partnership | Uber
Paula Deen Italian Cream Cake
Vocabulario A Level 2 Pp 36 40 Answers Key
Umn Biology
Www.paystubportal.com/7-11 Login
Pollen Count Central Islip
今月のSpotify Japanese Hip Hopベスト作品 -2024/08-|K.EG
George The Animal Steele Gif
Labor Gigs On Craigslist
Illinois Gun Shows 2022
Cashtapp Atm Near Me
Craigslist Southern Oregon Coast
Dragger Games For The Brain
Craigslist Battle Ground Washington
Imouto Wa Gal Kawaii - Episode 2
Kimoriiii Fansly
Craigslist Pasco Kennewick Richland Washington
When His Eyes Opened Chapter 3123
Sams Gas Price Sanford Fl
Rural King Credit Card Minimum Credit Score
Bfsfcu Truecar
Renfield Showtimes Near Marquee Cinemas - Wakefield 12
Edward Walk In Clinic Plainfield Il
W B Crumel Funeral Home Obituaries
Best Weapons For Psyker Darktide
Pillowtalk Podcast Interview Turns Into 3Some
Solemn Behavior Antonym
New Gold Lee
Frcp 47
1v1.LOL Game [Unblocked] | Play Online
Craigslist Tulsa Ok Farm And Garden
Discover Wisconsin Season 16
Oppenheimer Showtimes Near B&B Theatres Liberty Cinema 12
Www.craigslist.com Waco
Myrtle Beach Craigs List
Az Unblocked Games: Complete with ease | airSlate SignNow
CrossFit 101
RubberDucks Front Office
Server Jobs Near
Sc Pick 3 Past 30 Days Midday
Mejores páginas para ver deportes gratis y online - VidaBytes
Stephen Dilbeck, The First Hicks Baby: 5 Fast Facts You Need to Know
Wrentham Outlets Hours Sunday
Osrs Vorkath Combat Achievements
Metra Union Pacific West Schedule
Bellin Employee Portal
login.microsoftonline.com Reviews | scam or legit check
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.