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; }

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

} . cout<<"Chips = R"<< totChips << endl. cout<<"*************************************************"<<endl<<endl. cin>>userAnswer. // update total spent on all sweets so far totSweets += totChoc + totChips + totIce + totOther. break. return 0. answer = (userAnswer != 'n' && userAnswer != 'N').Ngobeni PN COS111-S 4131-999-0 Assignment 02 Semester 02 case'o': case'O': totOther += price. } //prompting user whether there is more input cout<<"Is there more input for shop "<< i <<" (N= no / Y= yes)" <<endl. cout<<"Ice-Cream = R"<< totIce << endl. } //display total spent on sweets at all shops cout<<"Total spent on Sweets at all shops is R" << totSweets <<endl. } //display total spent for each kind of sweet cout<<"Chocolate = R"<< totChoc << endl. // reset totals of sweets totChoc = totChips = totIce = totOther = 0. default: cout<<"Wrong Code for sweets"<<endl. cout<<"Other = R"<< totOther << endl. cout<<endl<<endl. break.

p=chips. 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.8 *********************************** Total spent on Sweets at all shops is R95. . i=ice-cream. o=other) as well as price i 12. 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.10 Is there more input for shop 3 (N= no / Y= yes) y Enter the kind of sweets bought (c=chocolate. o=other) as well as price c 6.4 Chips = R5. o=other) as well as price o 1.6 *************************************** Chocolate = R10. p=chips. p=chips. i=ice-cream.40 Is there more input for shop 2 (N= no / Y= yes) n Enter the kind of sweets bought (c=chocolate.2 Is there more input for shop 2 (N= no / Y= yes) y Enter the kind of sweets bought (c=chocolate. i=ice-cream. p=chips. i=ice-cream.50 Is there more input for shop 3 (N= no / Y= yes) y Enter the kind of sweets bought (c=chocolate. 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. i=ice-cream.5 Chips = R0 Ice-Cream = R10 Other = R13 ******************************** Chocolate = R7 Chips = R11 Ice-Cream = R0 Other = R5. p=chips. p=chips. o=other) as well as price o 3.5 Ice-Cream = R12. p=chips.5 Other = R8. i=ice-cream. i=ice-cream. p=chips. o=other) as well as price p 5. i=ice-cream.5 Is there more input for shop 2 (N= no / Y= yes) y Enter the kind of sweets bought (c=chocolate. .30 Is there more input for shop 3 (N= no / Y= yes) y Enter the kind of sweets bought (c=chocolate. i=ice-cream.3 Press any key to continue . o=other) as well as price c 5. p=chips.20 Is there more input for shop 1 (N= no / Y= yes) y Enter the kind of sweets bought (c=chocolate.8 Is there more input for shop 1 (N= no / Y= yes) n Enter the kind of sweets bought (c=chocolate. p=chips. o=other) as well as price o 9.80 Is there more input for shop 3 (N= no / Y= yes) n Chocolate = R11. o=other) as well as price o 8. p=chips. p=chips. o=other) as well as price c 4. p=chips. p=chips. i=ice-cream.Ngobeni PN COS111-S Output Enter the sweets bought at Shop 1 *************************************** Enter the kind of sweets bought (c=chocolate. o=other) as well as price p 5. o=other) as well as price o 4. i=ice-cream. . o=other) as well as price p 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.50 Is there more input for shop 3 (N= no / Y= yes) y Enter the kind of sweets bought (c=chocolate. i=ice-cream. i=ice-cream. p=chips.50 Is there more input for shop 2 (N= no / Y= yes) y Enter the kind of sweets bought (c=chocolate.

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

50) :21.g.20 A Container of 200 crickets will last for : 16 feeds The cost per feed is : R1. 25.setf(ios::fixed). priceContainer).Ngobeni PN COS111-S 4131-999-0 Assignment 02 Semester 02 cout << endl << "Please enter the price of the container " << " (e.42 Please enter the number of crickets per feed : 12 Please enter the price of the container (e. 25.25 Please enter the number of crickets per feed : 13 Please enter the price of the container (e.50) :23. cin >> priceContainer.41 The total cost for one year is : R147. } Output Please enter the number of crickets per feed : 12 Please enter the price of the container (e. cout << "The cost per feed is : R" << costFeed << endl. 25.51 The total cost for one year is : R157.03 Please enter the number of crickets per feed : 11 Please enter the price of the container (e. } return 0. cout.50) :26. cout. priceContainer).29 The total cost for one year is : R134. cout << "The total cost for one year is : R" << costYear << endl << endl.50 A Container of 200 crickets will last for : 18 feeds The cost per feed is : R1.75 A Container of 200 crickets will last for : 15 feeds The cost per feed is : R1.precision(2).g. 25.g. cout << endl << "A Container of 200 crickets will last for : " << feeds << " feeds" << endl. costFeed = costPerFeed(CricketsPerFeed.g. 25.00 A Container of 200 crickets will last for : 16 feeds .g.50) :25.50) :" . costYear = costPerYear(CricketsPerFeed.

25.00 A Container of 200 crickets will last for : 15 feeds The cost per feed is : R1. . .g. char ch. for(int r = 1. } else .76 Please enter the number of crickets per feed : 14 Please enter the price of the container (e.Ngobeni PN COS111-S The cost per feed is : R1.44 Press any key to continue .69 The total cost for one year is : R175.50) :23.50) :26. 25. 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.61 The total cost for one year is : R167. c <= size * 3. // The function drawBlock must be inserted here void drawBlock(int size. r <= size.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. char ch2) { cout<<endl<<endl.00 A Container of 200 crickets will last for : 14 feeds The cost per feed is : R1. Question 4 //Assignment 2 question 4a #include <iostream> using namespace std. r++) { for(int c = 1.56 The total cost for one year is : R162.g.

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

Ngobeni PN COS111-S 4131-999-0 Assignment 02 Semester 02 .

distance. float dist. float &pricePerLitre. i++) { inputAndValidate(litres. } int main() { float litres. totAmnt += (litres * pricePerLitre). totalAmount.Ngobeni PN COS111-S Question 5 //Assignment 2 Question 5a #include <iostream> using namespace std. float &dist) { cin>>litres. cin>>dist. totalAmount. distance. . totalAmount= 0. Totdist += dist. float &totl. int NR_FILL_UPS = 6. float &Totdist ) { totl += litres. price. price. price. do { cin>>pricePerLitre. distance). totalDistance = 0. totalDistance. float pricePerLitre. 4131-999-0 Assignment 02 Semester 02 //the required function inputAndValidate should be inserted here void inputAndValidate(float &litres. updateTotals(litres. for(int i = 1. totalLitres. totalDistance). } while(pricePerLitre < 4). } void updateTotals(float litres. i <= NR_FILL_UPS. float &totAmnt. // initialise totals totalLitres = 0. totalLitres.

Ngobeni PN COS111-S 4131-999-0 Assignment 02 Semester 02 } cout. } .precision(2). cout. cout<<"Average Fuel Consumption : " << totalLitres / NR_FILL_UPS << " litres" << endl <<"Average Price Per Litre : R" << totalAmount / NR_FILL_UPS << endl << endl.setf(ios::fixed). return 0.

. int & nr50centsP.syst . //Assuming variables get value from input answer = critical(temp. int pulseP. Question 7 #include<iostream> using namespace std.Ngobeni PN COS111-S Question 6 4131-999-0 Assignment 02 Semester 02 6a bool critical(float tempP. calcChange(amt. int nrR. int age. B. age). int & nrRandsP. pulse. int & ageP) string name. nr20c. B). diast. int pulse . nr50c. cin>>temp>>pulse>>syst>>diast. int & nr20centsP. int systolicP. int diastolicP) bool answer. nr50c. int Value = GCD(A. int b) int A. getData(name. syst. nrR. 6d void calcChange(double amountP. if(Num < 0) cout<<"Number must not be less than zero (0)"<<endl. diast). nr5c. 6b int GCD(int a. int & nr10centsP. nr10c. nr20c. nr5c). do { cin>>Num. nr10c. float temp = 0. 6c void getData(string & nameP. int getNumAccidents() { int Num =0. int & nr5centsP) double amt.

int e. int s. } 4131-999-0 Assignment 02 Semester 02 void findLeast(int n. int &min. reg = "North". } else if( w < s && w < e && w < n && w < c) { min = w. return Num. string &reg) { if( n > s && n > e && n > w && n > c) . int w. int c. } else if( c < s && c < e && c < w && c < n) { min = c. } else if( s < n && s < e && s < w && s < c) { min = s. int e. string &reg) { if( n < s && n < e && n < w && n < c) { min = n. int &Max. reg = "East". int s. } else if( e < s && e < n && e < w && e < c) { min = e. int w. reg = "Central". reg = "South". } } void findMost(int n. int c. } else { min = 0.Ngobeni PN COS111-S } while(Num < 0). reg = "-". reg = "West".

} else if( w > s && w > e && w > n && w > c) { Max = w. int e. cout<<"----------------------------------------------------" <<endl. } else { Max = 0. 4131-999-0 Assignment 02 Semester 02 } else if( s > n && s > e && s > w && s > c) { Max = s. int s.Ngobeni PN COS111-S { Max = n. int c) { return (n + s + e + w + c). string regL. string regH. } else if( e > s && e > n && e > w && e > c) { Max = e. int w. int Max. int Tot) { cout<< endl. reg = "Central". reg = "West". reg = "-". else . reg = "South". } else if( c > s && c > e && c > w && c > n) { Max = c. reg = "North". } } int findTotal(int n. if(Min == 0) cout<<"No Region has Minimum Value"<<endl. reg = "East". } void printStats(int Min.

for(int i = 0. regionNameL.Ngobeni PN COS111-S 4131-999-0 Assignment 02 Semester 02 cout<< regL << " is the Region with a Minimum of " << Min << " Accidents"<<endl. nrCentral=0. month. nrSouth. nrEast. string regionNameH. else cout<< regH << " is the Region with a Maximun of " << Max << " Accidents"<<endl. maxNr. if(Max == 0) cout<<"No Region has Maximum Value"<<endl. nrCentral. cout<<"Enter Number of Acciddents South :" . nrWest. nrCentral. nrEast. nrWest. } return 0. cout<< "Total Number of Accidents this month is " << Tot << endl. i++) { cout<<"Enter Number of Acciddents North :" . findLeast(nrNorth. cout<<"Enter Number of Acciddents East nrEast = getNumAccidents(). regionNameH). regionNameH. } . maxNr=0. regionNameL. nrSouth. cout<<"Enter Number of Acciddents Central :" . nrNorth = getNumAccidents(). :" . nrEast=0. totalAccidents). nrSouth. nrSouth=0. nrEast. nrCentral). cout<< "----------------------------------------------------" <<endl. nrCentral = getNumAccidents(). totalAccidents=0. i < 12. int minNr=0. maxNr. minNr. findMost(nrNorth. nrWest=0. :" . nrSouth = getNumAccidents(). printStats(minNr. nrWest. } int main() { int nrNorth=0. regionNameL). cout<<"Enter Number of Acciddents West nrWest = getNumAccidents(). totalAccidents = findTotal(nrNorth.

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 .