lab Assignment 9

  1. Using the Selection Control Structure if…else

1)Design a program that prompts the user to enter three integers separated by spaces. Using if-elsestatements, display the numbers in ascending order. Name the .cpp file to be NumSort.cpp.

The following is a copy of the screen results that might appear after running your program, depending on the data entered. The input entered by the user is shown in bold.

Pleaseenter3 integers:8 11 2

Theascendingorderofthe integers is:

2

8

11

You can follow the program structure below:

#include <iostream>

using namespace std;

int main()

{

//step 1: declare three integer-type variables: a, b, c;

/*step 2: declare three integer-type variables: large, middle, small; (Note: These three variables will store the largest number, the middle number and the smallest number respectively) */

//step 3: prompt user to input three integers and save them in variables a, b, c;

/*step 4: use your own algorithm to compare three integers and assign the largest number to variable large, the middle number to variable middle and the smallest number to variable small. One simple method is:

if (a is greater than b and c)

then

large=a;

if (b is greater than c)

then middle=b,small=c

else middle=c, small=b

if (b is greater than a and c)

then

large=b;

if (a is greater than c)

then middle=a, small=c

else middle=c, small=b

if (c is greater than b and a)

then

large=c;

if (b is greater than a)

then middle=b, small=a

else middle=a, small=b

You may use smarter method to solve this */

cout<small<endl<middle<endl<large<endl;

return 0;

}

2)Test your program with the following three inputs respectively.

Rerun NumSort.cpp with the names entered in this order: 11 2 8

Rerun Num,Sort.cpp with the names entered in this order: 2 11 8

Rerun NumSort.cpp with the names entered in this order: 8 2 11

  1. Using the Selection Control Structure if…else and file I/O

Write a program to read the file named “Grade.txt”, print in good format all the students with pass/fail information (if student’s grade is greater than or equals to 60, then pass; otherwise, fail).

Assume Grade.txt has the contents:

NicholasPaul85

SmithJohn55

CruzTom100

Your output(screen) should be like:

LastnameFirstnameGradePass/Fail

NicholasPaul85Pass

SmithJohn55Fail

CruzTom100Pass

  1. Complete the following code: (This program prompts user input a number, an operator(+,-,*,/) and another number, then output the expression and the calculated result.)

#include<iostream>

using namespace std;

int main()

{

int a, b;

char op;

cin>a>op>b;

switch(op)

{

case '+' :

cout<a<op<b<'='<a+b<endl;

break;

// complete the other cases: op is '-' or '*'or '/',

//do subtraction, multiplication, or division respectively.

//Otherwise, output "Bad input"

}

}