Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Overig

COS Assignment 2 solutions

Beoordeling
1.0
(1)
Verkocht
19
Pagina's
19
Geüpload op
19-04-2023
Geschreven in
2022/2023

This contains correct tested solutions and discussions of the assignment

Instelling
Vak

Voorbeeld van de inhoud

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

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
19 april 2023
Bestand laatst geupdate op
3 mei 2023
Aantal pagina's
19
Geschreven in
2022/2023
Type
OVERIG
Persoon
Onbekend

Onderwerpen

$6.52
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Beoordelingen van geverifieerde kopers

Alle reviews worden weergegeven
2 jaar geleden

Not happy I need a refund

2 jaar geleden

Hie Tshepomonaisa, how can we help

1.0

1 beoordelingen

5
0
4
0
3
0
2
0
1
1
Betrouwbare reviews op Stuvia

Alle beoordelingen zijn geschreven door echte Stuvia-gebruikers na geverifieerde aankopen.

Maak kennis met de verkoper

Seller avatar
De reputatie van een verkoper is gebaseerd op het aantal documenten dat iemand tegen betaling verkocht heeft en de beoordelingen die voor die items ontvangen zijn. Er zijn drie niveau’s te onderscheiden: brons, zilver en goud. Hoe beter de reputatie, hoe meer de kwaliteit van zijn of haar werk te vertrouwen is.
CrystalIndigo University of South Africa (Unisa)
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
486
Lid sinds
5 jaar
Aantal volgers
226
Documenten
73
Laatst verkocht
7 maanden geleden
CrystalIndigo Solutions

providing all solutions to all computer science modules

4.1

52 beoordelingen

5
27
4
13
3
6
2
1
1
5

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen