IN CLASS LAB ASSIGNMENT

 

Print out this sheet so that you can answer any questions on this sheet.  Hand in this sheet and any program that you write or work on during this lab.

 

1)      Practice while loops:

 

 

a)      Type in the following program, compile and run it:

 

#include<iostream>

using namespace std;

 

int main()

{

 

  int i= 0;

 

  //first example of a while loop

 

  while (i < 15)

  {

       cout << '*';

       i++;

  }

 

return 0;

 

}

 

b)      What is printed by the program above?

 

 

 

c)      What happens if you change your declaration so that it is:

int i;   instead of     int i = 0;  Why?

 

 

 

 

d)     Change the program above so that each * is printed on its own line.  Run your program.  Which line needed to be changed and how?

 


 

e)      Change the program above so that each star is printed on its own line and there is an empty line between each *, as follows.  Write down the changed line of code.

*

 

*

 

*

 

 

 

f)       Change the program above so that the cout statement is: cout << i; What is printed out?

 

 

 

g)      Change the cout so that there is a space between each number.  Write down the new cout statement.

 

 

 

 

 

 

 

 

 

a)      Change the  code from part g so that  instead of printing out the numbers between 0 and 14, your program prints out the numbers that are between 33 and 124.  Write down the changed line of code.

 

 

 

 

 

 

 

 

 

b)      Change your code from part h so that instead of printing out the numbers between 33 and 124, your code prints out the ASCII characters that have values between 33 and 124.  (hint:  use the static_cast )Your output should look as follows:

 

! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H

I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p

q r s t u v w x y z { | Press any key to continue


 

c)      Now that you have completed part i, write code that uses a while loop prints out the above characters in an ASCII table, just like the appendix in the book.  Your output should be:

 

    0 1 2 3 4 5 6 7 8 9

-------------------------------------

3|        ! " # $ % & '

4|  ( ) * + , - . / 0 1

5|  2 3 4 5 6 7 8 9 : ;

6|  < = > ? @ A B C D E

7|  F G H I J K L M N O

8|  P Q R S T U V W X Y

9|  Z [ \ ] ^ _ ` a b c

10| d e f g h i j k l m

11| n o p q r s t u v w

12| x y z { | Press any key to continue

 

 

Notice that the same set of characters are printed out as in part i.  However, there are two lines of column headers (that is just simple cout statements), and the output is placed on multiple  lines.  Hint:  a newline is started each time the ASCII value is a multiple of 10 (think if statement!)  And each time a newline is started, the row label  is printed out.