before we get started…. first, a few things… weighted grading system programming style...

49
Before we get started…

Upload: alfred-watkins

Post on 18-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Before we get started…

Page 2: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

First, a few things…

Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Page 3: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Weighted Grading System (WGS)

Provides greater flexibility Does not require knowing exactly how many

points each assignment will be. I use the weighted system because the

portion of your grade that each type of assignment affects remains the same.

Page 4: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Illustration…

Programming Projects 400 pts. 40%

Homework 200 pts. 20%

Quizzes 100 pts. 10%

Exams 300 pts. 30%

At the end of the semester, if the homework total ends up being 130 pointsand the quiz total ends up being 80 points, how does this change the effectthat programming projects and exams have on your grade?

Page 5: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Illustration…

Programming Projects 400 pts. 44%

Homework 130 pts. 14%

Quizzes 80 pts. 9%

Exams 300 pts. 33%

The programming projects and exams end up having a larger effect onyour grade—I don’t like when that happens.

Page 6: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Estimating Your Grade

A = totalHWPointsEarned / totalHWPointsPossible B = totalQuizPointsEarned / totalQuizPointsPossible C = totalProgProjPointsEarned / totalProgProjPointsPossible D = midtermPointsEarned / midtermPointsPossible E = finalPointsEarned / finalPointsPossible

If you do not have any assignments for a given category then you have to substitute it with a 1.

So far we do not have any points in the categories C, D and E so substitute a 1 for these three.

Grade = A * 0.25 + B * 0.15 + C * 0.40 + D * 0.10 + E * 0.10

Page 7: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Programming Style

A style sheet will not be distributed The purpose of the class is to introduce you to

programming, and programming style The style sheet will be built during the

semester as we learn new concepts The main thing we need to worry about for

now in regards to style is the proper use of tabs.

Page 8: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Programming Style (continued)

For the most part, Microsoft Visual Studio 2005 (and many other development environments) will take care of the tabbing issue for you.

A general rule of thumb, all blocks should be tabbed

Page 9: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Submitting Assignments

I ask that assignments are submitted in a clasp envelope for several reasons It keeps the printed part of the assignment and

the media (diskette or CD) together. If there are materials that I need to distribute I

can put them inside of the envelope and hand each person one thing.

Requirements for submitting assignments are stated in the syllabus in the homework and programming project sections.

Page 10: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

char Variable Type

char ch;

ch = ‘8’; // line 1ch = 8; // line 2

These two statements are very different! Line 1 sets the value of the variable ch to the

character 8 Line 2 sets the value of the variable ch to the

backspace character

Page 11: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

string Variable Type

To set the value of a string variable to a string use double quotes.

string myString;

myString = “This is my string.”;

Page 12: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

string Variable Type (contd.)

To access a character in the string at a certain position use [ ].

The position of a character in the string variable begins at zero

myString[0] is the character ‘T’myString[3] is the character ‘s’

Page 13: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

string Variable Type (contd.) Using the string variable type as a filename to use with input and output

streams.

ofstream outputStream;string myOutputFileName;

cout << “Please enter the output file name: ”;cin.ignore( 10000, ‘\n’ ); // skip extraneous data

// in input stream

getline( cin, myOutputFileName );

outputStream.open( myOutputFileName.c_str() );

// do what you need to do with the file

outputStream.close();

Page 14: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Chapter 5

Control Structures II (Repetition)

Page 15: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Types of Looping in C++

while Looping

do...while Looping

for Looping

Page 16: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

The while Loop

General form of a while loop:

while ( expression )statement

expression - usually a logical expression - decision maker

- provides entry into loop

statement - may be simple or compound- called the body of the loop

Page 17: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

The while Loop (continued)

statement continues to execute until expression evaluates to false

An infinite loop occurs when expression never evaluates to false

Page 18: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

while Loop Diagram

Page 19: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Types of Exit Conditions

Counter-Controlled while Loop

Sentinal-Controlled while Loop

Flag-Controlled while Loop

EOF-Controlled while Loop

Page 20: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Counter-Controlled while Loop

Number of iterations is known Must initialize loop control variable Test loop control variable each iteration Update the counter in the body of the loop

int counter = 0;

while ( counter < 10 )

{

cout << “Counter Value is: ” << counter << endl;

counter ++;

}

Page 21: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Sentinel-Controlled while Loop Have a known special value, or sentinel value

loop ends when value encountered. Read first value before loop

Known as the priming read

const char SENTINEL = ‘!’;char newChar;ofstream outStream;outStream.open(“output.txt”);

cin >> newChar;

while ( newChar != SENTINEL ){

cin >> newChar;outStream << newChar;

}

Page 22: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Flag-Controlled while Loop

Uses a bool variable as exit condition.

found = false;

while ( !found )

{

if ( expression )

found = true;

}

Page 23: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

EOF-Controlled while Loop

Uses the end of file marker to control the loop The logical value returned by cin can be used as an exit

condition in an EOF controlled while loop.

int x = 0;cin >> x;while ( cin ) {

cout << “x = ” << x << endl;cin >> x;

}

Input: 1 21 3 0 7 9 11 c 56 7 9

Page 24: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Input Streams and the eof Function

The eof function determines the end of file status.

Syntax:

istreamVar.eof();

where istreamVar is an input stream variable.

Page 25: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

The for Loop

General form of a for loop

for ( initial statement; loop condition; update statement )

statement

The initial statement, loop condition and update statement are called for loop control statements

Page 26: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

The for Loop

How it works…1. The initial statement executes

2. The loop condition is evaluated If loop condition evaluates to true,

The statement is executed The update statement is executed

3. Repeat steps 1 and 2 until the loop condition evaluates to false.

Page 27: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

for Loop Diagram

Page 28: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

More On for Loops

If loop condition is initially false, the loop body does not execute.

When the update expression is executed it actually changes the value of the loop control variable.

Loop control variable is initialized in initial expression

In the for statement, if the loop condition is omitted it is assumed to be true

All three statements may be omitted

Page 29: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

The do…while Loop

General for of a do…while Loop

do

statement

while(expression); // must have semicolon

If statement is compound you must use braces Statement executes first then expression is checked,

if true the statement is executed again Guaranteed to execute at least once

Page 30: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

do...while Loop Diagram

Page 31: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

The break Statement

The break statement alters the flow of control

The break statement when used in a repetition structure, immediately exits loop

After the break statement exits the loop, the first statement outside of the loop body is executed.

Page 32: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

The continue Statement

The continue statement alters the flow of control

The continue statement when used in a repetition structure, immediately proceeds to the next iteration of the loop skipping any statements that follow

Page 33: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Nested Control Structures

Suppose we want to create the following pattern

*

**

***

****

***** In the first line, we want to print one star, in

the second line two stars and so on

Page 34: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Nested Control Structures (contd.)

Since five lines are to be printed, we start with the following for statement

for (i = 1; i <= 5 ; i++)

The value of i in the first iteration is 1, in the second iteration it is 2, and so on

Can use the value of i as limit condition in another for loop nested within this loop to control the number of starts in a line

Page 35: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Nested Control Structures (contd.)

for (i = 1; i <= 5 ; i++)

{

for (j = 1; j <= i; j++)

cout << "*";

cout << endl;

}

Page 36: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Example Program

Create a program that calculates bn. If the base, b, is equal to zero then the result is always

zero If the power, n, is equal to zero then the result is always

one

Have user input values for the base and the power. Output the result Ask the user if they would like to execute the program again.

‘Y’ or ‘y’ for yes ‘N’ or ‘n’ for no Repeat this question until a valid response is entered.

Page 37: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Example Program (continued)

Steps− Declare the variables− Initialize the variables− Get the value of the base− Get the value of the power− Calculate the result (base raised to power)− Print the result− Ask user if they would like to execute again

Page 38: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Example Program (continued)

Variables (declare & initialize)

bool executeAgain = false;

double result = 1.0;

double base = 0.0;

int power = 0;

int absPower = 0;

char response = ‘ ‘;

bool responseValid = false;

Page 39: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Example Program (continued)

Get the values required

cout << “Please enter the base (b): ”;

cin >> base;

cout << “Please enter the power (n): ”;

cin >> power;

Page 40: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Example Program (continued) Calculate the result

if ( base == 0 )result = 0;

else if ( power == 0 )result = 1;

else{

absPower = abs(power);

for ( int counter = 0; counter < absPower; counter++) result *= base;

if ( power < 0 ) result = 1.0 / result;

}

Page 41: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Example Program (continued) Ask the user if he/she would like to execute the program again and

continue until valid response.

while ( !responseValid ){

cout << “Would you like to repeat this program” << “( yes = Y, no = N )? ”;cin >> response;

if ( response == ‘Y’ || response == ‘y’ || response == ‘N’ || response == ‘n’) responseValid = true;else{ cout << “\nInvalid response!” << endl; responseValid = false;}

}

Page 42: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Example Program (continued)

Wrap steps 3-6 in a do…while loop that continues only if response is ‘y’ or ‘Y’

Use input base = 5 and power = a to demonsrate input failure

Page 43: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

#include < iostream >#include < string >#include < conio.h >

using namespace std;

int main ( ){

double result = 1;int i = 0;double base = 0;int power = 0;int absPower = 0;bool responseValid = false;char response = ' ';

do { cout << "This program will calculate b^n" << endl << endl

<< "Please enter base (b): "; cin >> base;

Page 44: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

cout << "Please enter the power (n): "; cin >> power;

if ( base == 0 ) result = 0;

else if ( power == 0 ) result = 1;

else {

absPower = abs(power);

for ( int counter = 0; counter < absPower; counter++ ) result *= base;

if ( power < 0 ) result = 1 / result;

}

Page 45: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

if ( power < 0 ) cout << endl << "b^n = " << base << "^(" << power << ") = " << result << endl <<

endl; else cout << endl << "b^n = " << base << "^" << power << " = " << result << endl << endl;

while ( !responseValid ) { cout << "Would you like to repeat this program ( yes = Y, no = N )? "; cin >> response;

if ( response == 'Y' || response == 'y' || response == 'N' || response == 'n' ) responseValid = true; else { cout << "\nInvalid response!" << endl; responseValid = false; } }

Page 46: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

}while ( response == 'y' || response == 'Y' );

cout << "\nPlease press any key to exit." << endl;_getch();

return 0;}

Page 47: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Lab

Write a program to estimate your grade in this course.

Prompt user for input file name Output total points earned / total points possible for

each assignment type Assignment types are:

H or h : homework Q or q : quizzes P or p : programming projects M or m : midterm exam F or f : final exam

Page 48: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Input file should have the form

Q 7 / 10

H 17.5 / 20

H 19 / 20

P 82 / 100

Page 49: Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types

Homework Questions 1. Exercise #1, Pg. 291 2. Write a program that writes the ASCII table to a file named ascii_table.txt. Start with ASCII 33 and end with ASCII 126. The format should resemble the following:

33 !34 “35 #36 $…

Separate the integer value from the character using the tab character ‘\t’

3. Write a program that prompts the user to enter a string and one character. Search the string provided and display a count of the number of times the character is present in the string. 4. Exercise #17, Pg. 305 (a data file will be provided)