100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

COS Assignment 2 solutions

Rating
1.0
(1)
Sold
19
Pages
19
Uploaded on
19-04-2023
Written in
2022/2023

This contains correct tested solutions and discussions of the assignment

Institution
Course













Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
April 19, 2023
File latest updated on
May 3, 2023
Number of pages
19
Written in
2022/2023
Type
Other
Person
Unknown

Subjects

Content preview

COS1511 2023 ASSIGNMENT 2
DISCUSSION
(with the source code you need)

Crystal Indigo!
Crystal Indigo!
Providing all solutions you need anytime
+27 76 626 8187




***copy and run the code, then submit what you need to submit***
Or ask for the source code
if there is a program that is not running, please contact


Submit assignment 2 (as a .pdf file) . No assignments in the wrong format can
be accepted. So make sure your source codes for all questions are put in
one .pdf file.

,We are not providing full questions in this pdf because you already have the
questions.... these are only solutions and discussions. What’s highlighted in
yellow are the key points we have to follow.

Question 1a
We only have to submit the completed while loop. So Suppose we want to input and validate the
age of students that qualify for an internship, as well as the final mark obtained for the examination,
in a while loop. To qualify, the student should be younger than 30 with a final mark of more than
65%. Read in values until a suitable candidate is found. Display appropriate messages, whether
successful or not.
Using variable names → age and finalMark
SOURCE CODE
#include <iostream>
using namespace std;

int main(){
//diclare variables
int age;
double finalMark;

//ask the use for input
cout<<"Enter age: ";
cin>>age; //read age from keyboard
cout<<"Enter final mark for exam: "; //ask for marks
cin>>finalMark; //read mark from keyboard
//start validation
while(age >= 30 || finalMark <= 65){
cout<<"Either the age is greater than 30 or final mark is less
65. This student does not qualify"<<endl;
//ask the use for input again
cout<<"Please enter age again: ";
cin>>age; //read age from keyboard
cout<<"Please enter final again: "; //ask for marks
cin>>finalMark; //read mark from keyboard
}
//Display the results
cout<<"Conditions meet, student qualifies!!"<<endl;
cout<<"Age is: "<<age<<endl;
cout<<"Final mark is: "<<finalMark<<endl;
}

,output




Question 1b
Here we see how the codes run this question is straight forward, copy code and run it then paste the
output in the OUTPUT column. Then for submission Copy the first 3 columns.

CODE OUTPUT DESCRIPTION
example for (int i = 0; i < 10; i++) 0123456789 The loop runs 10 times,
cout << i; each iteration has an
cout << endl; increment of 1.
a. for (int i = 1; i <= 1; i++) * The loop executes once,
cout << “*”; increment once.
cout << endl;
b. for (int i = 2; i >= 2; i++) Runs This is an infinity loop
cout << “*”; infinitely starting from 2 to 2 then
cout << endl; increment forever.
c. for (int i = 1; i <= 1; i--) Runs This is an infinity loop, it
cout << “*”; infinitely start at 1 then decrement
cout << endl; forever
d. for (int = 12; i >= 9; i--) Error, Gives an error because we
cout << “*”; identifier don’t have i. If it was there
cout << endl; “i” is not it would execute 4 times,
defined
starting at 12
decrementing to 9. it runs
as long as i is greater than
9
e. for (int i = 0; i <= 5; i++) ***** The loop runs 5 times
cout << “*”;
cout << endl;
f. for (int i = 0; i <= 5; i++) Error, Gives an error because we
cout << “*”; identifier don’t have i. If there was
i = i + 1; “i” is not brackets and i=i+1 was

, cout << endl; defined inside with
cout<<”*” , it would
have printed *****



Question 1c
For this for loop to run 10 times our n should be 9. so in your program declare int n and assign it
to 9. Like int n = 9;
The for loop is commented because you don’t need to submit it. The while loop has an initial
declaration and incremental that is different from the for loop. We need to declare i outside the
loop and increment at the bottom of the loop body.
SOURCE CODE
#include <iostream>
using namespace std;

int main()
{
// a for loop that execute 10 times
// int n = 9;
// for (int i = 0; i <= n; i++)
// if (i < 5 && i != 2)
// cout << 'X';

// changing the for loop into a while loop
int n = 9;
int i = 0;
while (i <= n)
{
if (i < 5 && i != 2){
cout << 'X';
}
i++;
}
return 0;
}


Output

,Question 1d
Here we are correcting errors and make sure the program displays this correct output:
Please enter 10 integers, positive, negative, or zeros.
The numbers you entered are:
2
7
-4
-3
0
7
4
0
-9
-4
There are 6 evens, which includes 2 zeros.
The number of odd numbers is: 4
If we enter the exact numbers.
The comments is what we needed to correct to get the program to work.
SOURCE CODE
#include <iostream>
using namespace std;

const int LIMIT = 10;
int main()
{
float counter;
int number;
int zeros = 0; //for start count at 0
int odds = 0; //for start count at 0
int evens = 0; //for start count at 0

cout <<"Please enter "<< LIMIT << " integers, " //undefined identifier it should be

LIMIT

<< "positive, negative, or zeros." << endl;
cout << "The numbers you entered are:" << endl;
for (counter = 1; counter <= LIMIT; counter++)
{

, cin >> number; // wrong operator used should be >>



switch (number % 2) //to check evens and odds
{
case 0:
evens++;
if (number == 0) // compare number to zero
zeros++;
break; //break so that it does not continue to anothercase
case 1:
case -1:
odds++;
}
}
cout << endl;
cout << "There are " << evens << " evens, "
<< "which includes " << zeros << " zeros."
<< endl;
cout << "The number of odd numbers is: " << odds
<< endl;
return 0;
}




OUTPUT

Reviews from verified buyers

Showing all reviews
2 year ago

Not happy I need a refund

2 year ago

Hie Tshepomonaisa, how can we help

1.0

1 reviews

5
0
4
0
3
0
2
0
1
1
Trustworthy reviews on Stuvia

All reviews are made by real Stuvia users after verified purchases.

Get to know the seller

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.
CrystalIndigo University of South Africa (Unisa)
Follow You need to be logged in order to follow users or courses
Sold
486
Member since
5 year
Number of followers
226
Documents
73
Last sold
1 month ago
CrystalIndigo Solutions

providing all solutions to all computer science modules

4.1

51 reviews

5
27
4
13
3
6
2
1
1
4

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

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

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

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

Student with book image

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

Alisha Student

Frequently asked questions