100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
COS Assignment 2 solutions $5.99   Add to cart

Other

COS Assignment 2 solutions

1 review
 374 views  19 purchases
  • Course
  • Institution

This contains correct tested solutions and discussions of the assignment

Last document update: 1 year ago

Preview 6 out of 19  pages

  • April 19, 2023
  • May 3, 2023
  • 19
  • 2022/2023
  • Other
  • Unknown

1  review

review-writer-avatar

By: tshepomonaisa • 1 year ago

Not happy I need a refund

reply-writer-avatar

By: CrystalIndigo • 1 year ago

Hie Tshepomonaisa, how can we help

avatar-seller
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

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller CrystalIndigo. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $5.99. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

80461 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$5.99  19x  sold
  • (1)
  Add to cart