Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Warm-up Exercises for Chapter 9: Coding Challenges, Study notes of Computer Science

A series of coding exercises designed to help readers prepare for chapter 9 of their programming course. The exercises cover various control structures, including for loops, while loops, and nested loops, as well as functions and conditional statements. Students are asked to write functions to determine month abbreviations and check if a given month number is valid, and to write expressions for the number of times certain loops execute. The document also includes an exercise to print the days of a month in a calendar format.

Typology: Study notes

Pre 2010

Uploaded on 08/13/2009

koofers-user-wr9
koofers-user-wr9 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Warmup for chapter 9
1.Write a value returning string function called MonthAbbrev that takes an int value as a
parameter. The parameter, month, represents the number of a month. The function returns
a string containing a three letter abbreviation for the month if 1 <= month <= 12.
Otherwise it returns the string ###.
2. Write a value returning bool function named OkMonth that takes an int value as a
parameter. The parameter, month, represents the number of a month. The function returns
true if 1<= month <=12 and false otherwise.
3. For each of the following, write an expression for the number of times the statement
X = X+1; is executed.
a. for ( I = 0; I < 10; I++)
X = X + 1;
b. for(I =0; I < 10; I++)
for(j=0; j < 10; j++)
X = X + 1;
c. Contra = N;
while (Contra >= 1) {
X = X + 1;
Contra = Contra / 2;
}
d. Dontra = 0;
while ( Dontra <= 10) {
Contra = 1;
While (Contra <= 10 ){
X = X + 1;
Contra = Contra * 2;
}
Dontra = Dontra + 2;
}
e. for(I =0; I < 10; I++)
for(j=0; j < 10; j++){
K = 1;
WHILE (K <= j) {
X = X + 1;
K = K + 1;
}
}
}
pf2

Partial preview of the text

Download Warm-up Exercises for Chapter 9: Coding Challenges and more Study notes Computer Science in PDF only on Docsity!

Warmup for chapter 9

1.Write a value returning string function called MonthAbbrev that takes an int value as a

parameter. The parameter, month, represents the number of a month. The function returns

a string containing a three letter abbreviation for the month if 1 <= month <= 12.

Otherwise it returns the string ###.

2. Write a value returning bool function named OkMonth that takes an int value as a

parameter. The parameter, month, represents the number of a month. The function returns

true if 1<= month <=12 and false otherwise.

3. For each of the following, write an expression for the number of times the statement

X = X+1; is executed.

a. for ( I = 0; I < 10; I++)

X = X + 1;

b. for(I =0; I < 10; I++)

for(j=0; j < 10; j++) X = X + 1;

c. Contra = N;

while (Contra >= 1) { X = X + 1; Contra = Contra / 2; }

d. Dontra = 0;

while ( Dontra <= 10) { Contra = 1; While (Contra <= 10 ){ X = X + 1; Contra = Contra * 2; } Dontra = Dontra + 2; }

e. for(I =0; I < 10; I++)

for(j=0; j < 10; j++){ K = 1; WHILE (K <= j) { X = X + 1; K = K + 1; } } }

4. The following program is supposed to output the average of the five numbers on each

line, for all of he lines in a file. What's wrong with the code segment?

Change the code so it works correctly.

sum =0;

while(indata)

count = 1;

while( count <= 5 && indata)

cin >> number;

sum = sum + number;

cout << sum / count << endl;

5. Number 6 page 300

Write a code segment that prints the days of a month in calendar format. the day of the

week on which the month starts is represented by an int variable startDay. When startDay

is zero, the month begins on a Sunday. The int variable days holds the number of days in

a month. Print a heading with the days of the week as the first line of output. The day

numbers should neatly align under these column headings.