CS 101 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

CS 101

Description:

{ ifstream fin('input.txt'); ofstream foute('oute.txt'),fouto('outo.txt'); int x; ... {fin x;if(x%2==1)fouto x ;else foute x ;} fin.close();fouto.close ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 11
Provided by: aa291
Category:
Tags: fin

less

Transcript and Presenter's Notes

Title: CS 101


1
CS 101
  • Lecture 4
  • Repetition

2
pre and post increment and decrement
  • If ii1 we may write i to mean the same
    thing, and is called the postincrement operator.
    i also means ii1 and is called the
    preincrement operator. --i and i-- are called the
    preincrement and postincrement operators and each
    means ii-1.
  • The difference shows up in expressions and
    commands. If i 4 then coutltltxltlt ltltxwill
    print out 5 5 butcoutltltxltlt ltltx will print
    out 4 5.
  • Similarly, if x5 and y3 thencoutltltxltlt ltltylt
    ltltxy--ltlt ltltxltlt ltlty will print5 3 18 6 2
    to the screen

3
Shortcut assignment and arithmetic operators
  • We can write x3 to mean xx3, similarly
    we can write x4 x/5 x-2 x3if x,y are
    integer variables, x/y is truncated, i.e., 7/3 is
    2. If at least the numerator or denominator of
    x/y is a double or a float, then the result is an
    untruncated double or float. 7.0/3 is 2.333 We
    can also writedouble(7)/3 or 7/double(3) to get
    the same result, but not double(7/3), since this
    gives 2.0. If x,y are integer variables, xy
    gives the remainder when x is divided by y. 73
    is 1. 135 is 3. -135 is 3. In math, -135 is
    2.

4
The while loop
  • If i is an integer variable containing 0, then
    the statementwhile(ilt5) coutltltiiltlt
    iwill print 0 1 4 9 to the screen. The
    pair of statements coutltltiiltlt i gets
    repeated 5 times, each time with a different
    value of i. The five values of i are 0,1,2,3,4.
    Before the pair of statements is executed, the
    test ilt5 is performed. If it is true, then both
    statements are executed. If not(as when 5lt5 is
    false), then the next statement after the while
    loop is executed.

5
while loop program
  • While loops are usually used when it is not known
    in advance how many times the loop will be
    executed. The following program
    includeltiostream.hgtmain( )double sum0int
    i1while(1.0/(2ii2i1)gt.0001)sumsum1.0/(2
    ii2i1)icoutltltsumltltendlwill execute
    70 times and print .433617 to the screen. We have
    avoided solving the equation 2i2 2i 1 10000
    to find out in advance how many times we must
    loop.

6
for loops
  • There are three parts to the for loop
    statement.for(int i1ilt5i)coutltltiltlt int
    i1 is called the initialization command, and is
    done first and only once. If there are several
    commands before the first then they must be
    separated by commas. ilt5 is called the test
    expression. If it is true then the body of the
    for loop, in this case, the statement coutltltiltlt
    is performed. If the body is performed then
    the final statement(s) is(are) carried out after
    the body. Then the test expression is evaluated
    again. The above for statement will print 1 2 3
    4 to the screen. The above for statement is
    equivalent tofor(int i1ilt5coutltltiltlt ,i)
    and also tofor(int i1ilt5)coutltltiltlt i
    and also to for(int i1 )if(ilt5)coutltltiltlt
    else break

7
break and continue
  • If a break statement occurs in the body of a
    loop, the loop is exited. If a continue statement
    occurs in the body of a loop, the next iteration
    is performed. If the loop is a for loop then
    after the continue, the update portion of the for
    loop is executed.

8
Quiz 2 and solution
  • Write a program which will ask the user to
    input 50 numbers(doubles), and print the average
    to a file called average.txtincludeltfstream.hgt
    main()coutltltPlease enter 50 numbers.\ndouble
    x, sum0for(int i0ilt50i)cingtgtxsumsum
    xofstream fout(average.txt)foutltltsum/50fo
    ut.close()

9
loops with a conditional
  • Suppose we want to read 50 integers from the
    file input.txt and write the even integers to the
    file oute.txt and the odd integers to the file
    outo.txt.includeltfstream.hgtmain()ifstream
    fin(input.txt)ofstream foute(oute.txt),fouto
    (outo.txt)int xfor(int i0ilt50i)fingtgtx
    if(x21)foutoltltxltlt else fouteltltxltlt
    fin.close()fouto.close()foute.close()

10
Quiz 3 and solution
  • Write a program which will ask the user to
    input 50 numbers(doubles), and print the numbers
    to three different files. If the number is
    divisible by 3, the number is printed to the file
    three0.txt if dividing the number by 3 yields a
    remainder of 1, the number is printed to the file
    three1.txt if dividing the number by 3 yields a
    remainder of 2, the number is printed to the file
    three2.txt
  • includeltfstream.hgt
  • main()int x
  • ofstream out0(three0.txt),fout1(three1.txt),fo
    ut2(three2.txt)
  • coutltltPlease enter 50 integrs.\n
  • for(int i0ilt50i)cingtgtx if(x30)fout0ltltxltlt
  • else if(x31)fout1ltltxltlt else fout2ltltxltlt
  • fout1.close() fout1.close() fout2.close()
Write a Comment
User Comments (0)
About PowerShow.com