You are on page 1of 18

Ngobeni PN COS111-S Question 01A

4131-999-0 Assignment 02 Semester 02

while(!(i > 8) || !(j >= 1 || j <= 4)) { cout<<"Not Qualified:\nEnter Score & Apparator : "; cin>>i>>j; }

Question 1B for(int i = 0; i < 50; i++) { Cout<< Count is << count << endl; } Question 1C 10 even 9 odd 8 even 7 odd 6 even 5 odd 4 even 3 odd 2 even 1 odd Question 1D
for( int i = 1; i < 7; i++ ) { cin>>food; totFood += food; }

Ngobeni PN COS111-S
Question 2

4131-999-0 Assignment 02 Semester 02

Program #include <iostream> using namespace std; int main() { const int NrOfShops = 3; float price; char sweets, userAnswer; bool answer; //declare and initialize totals float totChoc = 0.0, totChips = 0.0, totIce = 0.0, totOther = 0.0, totSweets = 0.0; //loop over the number of shops for ( int i = 1; i <= NrOfShops; i++ ) { answer = true; cout << "Enter the sweets bought at Shop " << i << endl; cout<<"************************************************"<<endl <<endl; //sentinel while loop prompting for kind of sweets bought while ( answer ) { cout<<"Enter the kind of sweets bought (c=chocolate,"<<endl <<"p=chips, i=ice-cream, o=other) as well as price"<<endl; cin>>sweets>>price; //Updating the total cost per kind of sweet switch ( sweets) { case'c': case'C': totChoc += price; break; case'p': case'P': totChips += price; break; case'i': case'I': totIce += price; break;

Ngobeni PN COS111-S

4131-999-0 Assignment 02 Semester 02

case'o': case'O': totOther += price; break; default: cout<<"Wrong Code for sweets"<<endl; break; } //prompting user whether there is more input cout<<"Is there more input for shop "<< i <<" (N= no / Y= yes)" <<endl; cin>>userAnswer; cout<<endl<<endl; answer = (userAnswer != 'n' && userAnswer != 'N');

} //display total spent for each kind of sweet cout<<"Chocolate = R"<< totChoc << endl; cout<<"Chips = R"<< totChips << endl; cout<<"Ice-Cream = R"<< totIce << endl; cout<<"Other = R"<< totOther << endl; cout<<"*************************************************"<<endl<<endl; // update total spent on all sweets so far totSweets += totChoc + totChips + totIce + totOther; // reset totals of sweets totChoc = totChips = totIce = totOther = 0; } //display total spent on sweets at all shops cout<<"Total spent on Sweets at all shops is R" << totSweets <<endl;

return 0; }

Ngobeni PN COS111-S
Output
Enter the sweets bought at Shop 1 *************************************** Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price c 5.5 Is there more input for shop 1 (N= no / Y= yes) y

4131-999-0 Assignment 02 Semester 02

Enter the sweets bought at Shop 2 ************************************* Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price c7 Is there more input for shop 2 (N= no / Y= yes) y

Enter the sweets bought at Shop 3 ************************************ Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price c 6.30 Is there more input for shop 3 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price c6 Is there more input for shop 1 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price p 5.5 Is there more input for shop 2 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price c 4.10 Is there more input for shop 3 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price i 10 Is there more input for shop 1 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price p 5.50 Is there more input for shop 2 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price p 5.50 Is there more input for shop 3 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price o 3.20 Is there more input for shop 1 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price o 1.2 Is there more input for shop 2 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price i 12.50 Is there more input for shop 3 (N= no / Y= yes) y

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price o 9.8 Is there more input for shop 1 (N= no / Y= yes) n

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price o 4.40 Is there more input for shop 2 (N= no / Y= yes) n

Enter the kind of sweets bought (c=chocolate, p=chips, i=ice-cream, o=other) as well as price o 8.80 Is there more input for shop 3 (N= no / Y= yes) n

Chocolate = R11.5 Chips = R0 Ice-Cream = R10 Other = R13 ********************************

Chocolate = R7 Chips = R11 Ice-Cream = R0 Other = R5.6 ***************************************

Chocolate = R10.4 Chips = R5.5 Ice-Cream = R12.5 Other = R8.8 *********************************** Total spent on Sweets at all shops is R95.3 Press any key to continue . . .

Ngobeni PN COS111-S Question 3 Question 3c

4131-999-0 Assignment 02 Semester 02

Please enter the number of crickets per feed : 11 Please enter the price of the container (e.g. 25.50) :25.50 A Container of 200 crickets will last for : 18 feeds The cost per feed is : R1.40 The total cost for one year is : R145.86 Press any key to continue . . . Question 3d #include <iostream> using namespace std; const int CRICKETS_PER_CONTAINER = 200; const int WEEKS_PER_YEAR = 52; int nrOfFeeds(int NoPerFeed) { return (int)(CRICKETS_PER_CONTAINER / NoPerFeed ); } float costPerFeed(int NoPerFeed, float contPrice) { return (NoPerFeed * (contPrice / (float)CRICKETS_PER_CONTAINER)); } float costPerYear(int NoPerFeed, float contPrice) { return ((costPerFeed(NoPerFeed, contPrice) * 2) * (WEEKS_PER_YEAR)); } int main() { int CricketsPerFeed, feeds; float priceContainer = 0.0; float costFeed; float costYear; for(int i = 0; i < 6; i++) { cout << endl <<"Please enter the number of crickets per feed : "; cin >> CricketsPerFeed; feeds = nrOfFeeds(CricketsPerFeed);

Ngobeni PN COS111-S

4131-999-0 Assignment 02 Semester 02

cout << endl << "Please enter the price of the container " << " (e.g. 25.50) :" ; cin >> priceContainer; costFeed = costPerFeed(CricketsPerFeed, priceContainer); costYear = costPerYear(CricketsPerFeed, priceContainer); cout.setf(ios::fixed); cout.precision(2); cout << endl << "A Container of 200 crickets will last for : " << feeds << " feeds" << endl; cout << "The cost per feed is : R" << costFeed << endl; cout << "The total cost for one year is : R" << costYear << endl << endl; } return 0; } Output Please enter the number of crickets per feed : 12 Please enter the price of the container (e.g. 25.50) :25.20 A Container of 200 crickets will last for : 16 feeds The cost per feed is : R1.51 The total cost for one year is : R157.25 Please enter the number of crickets per feed : 13 Please enter the price of the container (e.g. 25.50) :21.75 A Container of 200 crickets will last for : 15 feeds The cost per feed is : R1.41 The total cost for one year is : R147.03 Please enter the number of crickets per feed : 11 Please enter the price of the container (e.g. 25.50) :23.50 A Container of 200 crickets will last for : 18 feeds The cost per feed is : R1.29 The total cost for one year is : R134.42 Please enter the number of crickets per feed : 12 Please enter the price of the container (e.g. 25.50) :26.00 A Container of 200 crickets will last for : 16 feeds

Ngobeni PN COS111-S The cost per feed is : R1.56 The total cost for one year is : R162.24

4131-999-0 Assignment 02 Semester 02

Please enter the number of crickets per feed : 13 Please enter the price of the container (e.g. 25.50) :26.00 A Container of 200 crickets will last for : 15 feeds The cost per feed is : R1.69 The total cost for one year is : R175.76

Please enter the number of crickets per feed : 14 Please enter the price of the container (e.g. 25.50) :23.00 A Container of 200 crickets will last for : 14 feeds The cost per feed is : R1.61 The total cost for one year is : R167.44 Press any key to continue . . .

Question 4 //Assignment 2 question 4a #include <iostream> using namespace std; // The function drawBlock must be inserted here void drawBlock(int size, char ch, char ch2) { cout<<endl<<endl; for(int r = 1; r <= size; r++) { for(int c = 1; c <= size * 3; c++) { if( size % 2 > 0 && (size * 3) % 2 > 0) { if(((r == 1 || r == size || r == (int)(size / 2) + 1) || (c == 1 || c == size * 3)) || c == (size * 3)/2) { cout<<ch; } else

Ngobeni PN COS111-S cout<<ch2;

4131-999-0 Assignment 02 Semester 02

} else { if((r == 1 || r == size) || (c == 1 || c == size * 3)) cout<<ch; else cout<<ch2; } } cout<<endl; } cout<<endl<<endl; } int main() { int size = 0; char frame; char inside; do { cout << endl<<"Please enter the size of the block that you " << " want displayed. "<<endl; cout << "The size must be at least 4 : " ; cin >> size; }while (size < 4); cout << endl << "Please enter the character to be used for " << " the frame : " ; cin >> frame; cout << endl << "Please enter the character to be used to fill the inside " << " the frame : " ; cin >> ; drawBlock(size, frame, inside); return 0; }

Ngobeni PN COS111-S

4131-999-0 Assignment 02 Semester 02

Ngobeni PN COS111-S Question 5 //Assignment 2 Question 5a #include <iostream> using namespace std; int NR_FILL_UPS = 6;

4131-999-0 Assignment 02 Semester 02

//the required function inputAndValidate should be inserted here void inputAndValidate(float &litres, float &pricePerLitre, float &dist) { cin>>litres; do { cin>>pricePerLitre; } while(pricePerLitre < 4); cin>>dist; } void updateTotals(float litres, float pricePerLitre, float dist, float &totl, float &totAmnt, float &Totdist ) { totl += litres; totAmnt += (litres * pricePerLitre); Totdist += dist; } int main() { float litres, price, distance, totalLitres, totalAmount, totalDistance; // initialise totals totalLitres = 0; totalAmount= 0; totalDistance = 0; for(int i = 1; i <= NR_FILL_UPS; i++) { inputAndValidate(litres, price, distance); updateTotals(litres, price, distance, totalLitres, totalAmount, totalDistance);

Ngobeni PN COS111-S

4131-999-0 Assignment 02 Semester 02

} cout.setf(ios::fixed); cout.precision(2); cout<<"Average Fuel Consumption : " << totalLitres / NR_FILL_UPS << " litres" << endl <<"Average Price Per Litre : R" << totalAmount / NR_FILL_UPS << endl << endl; return 0; }

Ngobeni PN COS111-S Question 6

4131-999-0 Assignment 02 Semester 02

6a bool critical(float tempP, int pulseP, int systolicP, int diastolicP)


bool answer; float temp = 0; int pulse ,syst , diast; cin>>temp>>pulse>>syst>>diast; //Assuming variables get value from input answer = critical(temp, pulse, syst, diast);

6b int GCD(int a, int b)


int A, B; int Value = GCD(A, B);

6c void getData(string & nameP, int & ageP)


string name; int age; getData(name, age);

6d void calcChange(double amountP, int & nrRandsP, int & nr50centsP, int & nr20centsP, int & nr10centsP, int & nr5centsP) double amt; int nrR, nr50c, nr20c, nr10c, nr5c; calcChange(amt, nrR, nr50c, nr20c, nr10c, nr5c);

Question 7 #include<iostream> using namespace std; int getNumAccidents() { int Num =0; do { cin>>Num; if(Num < 0) cout<<"Number must not be less than zero (0)"<<endl;

Ngobeni PN COS111-S } while(Num < 0); return Num; }

4131-999-0 Assignment 02 Semester 02

void findLeast(int n, int s, int e, int w, int c, int &min, string &reg) { if( n < s && n < e && n < w && n < c) { min = n; reg = "North"; } else if( s < n && s < e && s < w && s < c) { min = s; reg = "South"; } else if( e < s && e < n && e < w && e < c) { min = e; reg = "East"; } else if( w < s && w < e && w < n && w < c) { min = w; reg = "West"; } else if( c < s && c < e && c < w && c < n) { min = c; reg = "Central"; } else { min = 0; reg = "-"; } } void findMost(int n, int s, int e, int w, int c, int &Max, string &reg) { if( n > s && n > e && n > w && n > c)

Ngobeni PN COS111-S { Max = n; reg = "North";

4131-999-0 Assignment 02 Semester 02

} else if( s > n && s > e && s > w && s > c) { Max = s; reg = "South"; } else if( e > s && e > n && e > w && e > c) { Max = e; reg = "East"; } else if( w > s && w > e && w > n && w > c) { Max = w; reg = "West"; } else if( c > s && c > e && c > w && c > n) { Max = c; reg = "Central"; } else { Max = 0; reg = "-"; } } int findTotal(int n, int s, int e, int w, int c) { return (n + s + e + w + c); } void printStats(int Min, string regL, int Max, string regH, int Tot) { cout<< endl; cout<<"----------------------------------------------------" <<endl; if(Min == 0) cout<<"No Region has Minimum Value"<<endl; else

Ngobeni PN COS111-S

4131-999-0 Assignment 02 Semester 02

cout<< regL << " is the Region with a Minimum of " << Min << " Accidents"<<endl; if(Max == 0) cout<<"No Region has Maximum Value"<<endl; else cout<< regH << " is the Region with a Maximun of " << Max << " Accidents"<<endl; cout<< "Total Number of Accidents this month is " << Tot << endl; cout<< "----------------------------------------------------" <<endl; } int main() { int nrNorth=0, nrSouth=0, nrEast=0, nrWest=0, nrCentral=0; int minNr=0, maxNr=0, totalAccidents=0; string regionNameH, regionNameL, month; for(int i = 0; i < 12; i++) { cout<<"Enter Number of Acciddents North :" ; nrNorth = getNumAccidents(); cout<<"Enter Number of Acciddents South :" ; nrSouth = getNumAccidents(); cout<<"Enter Number of Acciddents East nrEast = getNumAccidents(); cout<<"Enter Number of Acciddents West nrWest = getNumAccidents(); :" ;

:" ;

cout<<"Enter Number of Acciddents Central :" ; nrCentral = getNumAccidents(); findLeast(nrNorth, nrSouth, nrEast, nrWest, nrCentral, minNr, regionNameL); findMost(nrNorth, nrSouth, nrEast, nrWest, nrCentral, maxNr, regionNameH); totalAccidents = findTotal(nrNorth, nrSouth, nrEast, nrWest, nrCentral); printStats(minNr, regionNameL, maxNr, regionNameH, totalAccidents); } return 0; }

Ngobeni PN COS111-S

4131-999-0 Assignment 02 Semester 02

Ngobeni PN COS111-S

4131-999-0 Assignment 02 Semester 02

Ngobeni PN COS111-S

4131-999-0 Assignment 02 Semester 02

You might also like