• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 18 pages
Other

COS1511- assignment 2 solutions with output 2023

Document preview thumbnail
Preview 3 out of 18 pages

This document contains the solutions to assignment 2 for COS With all the codes and comments in them, functions and outputs to every question

Content preview

ASSIGNMENT 02 COS1511 2023
QUESTION 1a.
while(age>30||finalMark<65)

{

cout<<”Candidate not suitable.”<<endl;

cout<<”Enter age: “;

cin>>age;

cout<<”Enter final mark: “;

cin>>finalMark;

}



QUESTION 1b.
CODE OUTPUT
a. for(int i=1;i<=1;i++) *
cout<<”*”;
cout<<endl;
b. for(int i=2;i>=2;i++) Endless loop of *
cout<<”*”;
cout<<endl;
c. for(int i=1;i<=1;i--) Endless loop of *
cout<<”*”;
cout<<endl;
d. for(int i=12;i>=9;1--) ****
cout<<”*”;
cout<<endl;
e. for(int i=0;i<=5;i++) ******
cout<<”*”;
cout<<endl;
f. for(int i=1;i<=5;i++) Syntax error.
cout<<”*”; “i” is inaccessible after the for loop.
i=i+1;
cout<<endl;


QUESTION 1c.

,PROGRAM.
#include <iostream>

using namespace std;

int main()

{

int i=0;

while(i<=10)

{

if (i<5 && i!=2)

cout<<"X";

i++;

}

return 0;

}

Output.




QUESTION 1d.

, PROGRAM.
#include <iostream>

using namespace std;

const int LIMIT =10;

int main()

{



float counter;

//The declaration of variables can be on the same line separated by a comma.

int number, zeros, odds, evens;

//The counter variables are inirialised to assure that they don’t acquire an unknown initial value.

zeros=0;

odds=0;

evens=0;



cout<<"Please enter "<<LIMIT<<" integers, positive,negative or zeros."<<endl;

cout<<"The number you entered are "<<endl;

for(counter=1;counter<=LIMIT; counter++)

{

cin>>number;



switch(number%2) //The switch statement is working on a remainder and (%) is used.

{

case 0:

evens++;

if (number==0) //correcting the boolean operator for number is equal to zero…(==) not (=)

zeros++;

break;

Document information

Uploaded on
May 11, 2023
Number of pages
18
Written in
2022/2023
Type
Other
Person
Unknown
R250,00

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
ThinkLab
4,0
(21)
Sold
171
Followers
118
Items
107
Last sold
11 months ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions