Sunteți pe pagina 1din 8

1)

//******************************************************* // Author: D.S. Malik // // Program: Counts zeros, odds, and evens // This program counts the number of odd and even numbers. // The program also counts the number of zeros. // // Note: Program modified by PMB to include 6 errors. //******************************************************** #include <iomanip> using namespace std; const int N = 20; int main () { //Declare variables int number; //variable to store the new number int zeros = 0; int odds = 0; int evens = 0; //Step 1 //Step 1 //Step 1

cout << "Please enter " << N << " integers, " << "positive, negative, or zeros." << endl; //Step 2 cout << "The numbers you entered are:" << endl; for (int counter = 1; counter <= N; counter++) //Step 3 { cin >> number; //Step 3a cout << number << " "; //Step 3b //Step 3c switch (number % 2) {

case 0: evens++; if (number == 0) zeros++; break; case 1: case -1: odds++; } //end switch } //end for loop cout << endl; //Step 4 cout << "There are " << evens << " evens, " << "which includes " << zeros << " zeros" << endl; cout << "The number of odd numbers is: " << odds << endl; char holdscr; cin >> holdscr; return 0; }

2 A)

SELECTION STRUCTURE
The selection structure is governed by a condition and the statements are executed if the governing condition is true. The selection structure includes if condition and switch statements. Both of thses slection statements are prioritized at different conditions based on input given by user. IF CONDITION: the if condition works on one or more conditions when this condition is true the related statements are executed otherwise not. The if condition is basically finds a use when we have range of values to compare. The conditions has different types as discussed below. y SIMPLE IF: simple if have only one governing condition. SYNTAX: if(condition) { Statements; } EXAMPLE: if (x>5) { Statements; } y
IF ELSE CONDITIONS: if else condition is same as that of simple if with a difference that

in case the condition governing if is not true the else part will be executed. SYNTAX: if(condition) { Statements; } Else { Statements; } EXAMPLE: if (x>5) {

Statements; } Else { Statements; } y


IF ELSE IF LADDER: the condition is used when we have multiple conditions to test and

execution is based on them SYNTAX: if(condition) { Statements; } Else if(condition) { Statements; } Else { Statements; }

EXAMPLE: if (x>5) { Statements; } Else if(x==5) { Statements; } Else { Statements; } NESTED IF: nested if is a if nested in another if. The outer if is the governing condition. Inner if will be executed only if the outer is true. SYNTAX:

If(condition) { If(condition) { Statements; } } Else { Statements; }

SWITCH CASE: the switch statement is used when there are different cases exist for a
particular condition. It is a substitute of the if condition but whereas if is used for ranges; switch is used when an integer have different values. Each value is treated as a case. SYNTAX: Switch (case value condition { case value 1: Statements; break; case value 2: Statements; break; . . . . . case value n: Statements; break; default: statements; break; } The case value condition is matched with the case values and the matched case is executed. If neither of case executes, default case will be fired.

2B)
The repetition structure in programming is used when a set of statements are needed to be executed a number of times. The repetition structure id divided as y Entry controlled loops o For o while y Exit controlled loops o Do while Entry controlled loops: the loop checks the condition before executing the statement. If the condition fails the first time, no statements will be executed. For and while are entry controlled loops. FOR LOOP: the loop is used when the user knows how many times the loop will be executed. SYNTAX: For(initialization; condition; increment/decrement) { Statements; } WHILE LOOP: the loop is used when the user do not know the number of times the loop will be executed. Syntax: Initialization; While(condition) { Statements; Increment /decrement; } EXIT CONTROLLED LOOP: the loop is controlled at the exit point. The statements are executed unconditionally for the first time and rest for the iterations will depend upon the condition
DO WHILE:

Syntax: Initialization; Do { Statements; Increment /decrement } while(condition);

3)
#include <iomanip> using namespace std; const float aprice=5,bprice=6,cprice=8; int main() { int option,qty; float budget,left; cout<<"enter your budget"; cin>>budget; while(budget>=5) { lab: cout<<"what you want to purchase\nchoose from list"; cout<<"enter\n 1 for apples\n 2 for cheese\n 3 for bread\n 4 to exit"; cin >>option; cout<<"enter how may items you want to purchase\n"; cin>>qty; if(option==1) { budget-=qty*aprice; } else if(option==2) { budget-=qty*bprice; } else if(option==3) { budget-=qty*cprice; } else { break; } if(budget<0)

{ cout<<"sorry! you do not have that budget\n"; cout<<"please choose fromm options again\n"; goto lab; } else { cout<<"you are left with "<<budget<<endl; } } cout<<"thanks for visting"; char regd; cin>>regd; return 0; }

S-ar putea să vă placă și