(solution) The following incomplete program first asks the user to enter the

(solution) The following incomplete program first asks the user to enter the

The following incomplete program first asks the user to enter the number of items he/she has eaten today and then to enter the number of calories for each item. It then calculates the number of calories he/she has eaten for the day and displays the value.

#include <iostream>

using namespace std;

int main()

{

int numberOfItems;

int count; //loop counter for the loop

int caloriesForItem;

int totalCalories;

cout << “How many items did you eat today? “;

cin >> numberOfItems;

cout << “Enter the number of calories in each of the “

<< numberOfItems << ” items eaten: ” << endl;

// ————- Your code ————–

cout << “Total calories eaten today = ” << totalCalories;

return 0;

}

You have to complete the code. First complete the code by using a while loop to read in the calories of all the items. Compile and run your program and submit only the code that you added and the output. Then change you program to use a for loop to read in the calories of all the items. Compile and run the program again and submit only the code that you added and the output. Use the variables that have already been defined in the given program. Test your program by entering 7 for the number of items and the following values for the calories:

7 120 60 150 600 1200 300 200

If your logic is correct, the following will be displayed:

Total calories eaten today = 2631

COS1511/101/3/2016 Tutorial letter 101/3/2016
Introduction to Programming I COS1511
Semesters 1 &amp; 2
School of Computing IMPORTANT INFORMATION:
Please activate your myUnisa and myLife email addresses and ensure you have
regular access to the myUnisa module site COS1511-15-S1 (first semester) or
COS1511-15-S2 (second semester) as well as your e-tutor group site. Note: This is a blended online module, and therefore your module is available on
myUnisa. However, in order to support you in your learning process, you will also
receive some study materials in printed format. COS1511/101 CONTENTS
Page
1 INTRODUCTION AND WELCOME …………………………………………………………………………………. 4 1.1 To get started? ……………………………………………………………………………………………………………. 4 1.2 Tutorial matter………………………………………………………………………………………………………………. 5 2 OVERVIEW OF THE MODULE COS1511 ………………………………………………………………………… 6 2.1 Purpose ………………………………………………………………………………………………………………………. 6 2.2 Outcomes ……………………………………………………………………………………………………………………. 6 3 LECTURER AND CONTACT DETAILS …………………………………………………………………………… 7 3.1 Lecturers……………………………………………………………………………………………………………………… 7 3.2 Department ………………………………………………………………………………………………………………….. 7 3.3 University …………………………………………………………………………………………………………………….. 8 4 MODULE RELATED RESOURCES ………………………………………………………………………………… 8 4.1 Joining myUnisa …………………………………………………………………………………………………………… 8 4.2 Other resources ? Printed support materials ……………………………………………………………………… 8 4.3 Prescribed books ………………………………………………………………………………………………………….. 9 4.4 Recommended books ……………………………………………………………………………………………………. 9 4.5 Study Guide …………………………………………………………………………………………………………………. 9 4.6 Prescribed software ………………………………………………………………………………………………………. 9 4.7 myUnisa Forum ………………………………………………………………………………………………………….. 10 4.8 Tutorial letters …………………………………………………………………………………………………………….. 10 4.9 Drawing Variable Diagrams Tutorial CD………………………………………………………………………….. 10 4.10 Additional Resources …………………………………………………………………………………………………… 10 5 STUDENT SUPPORT SERVICES FOR THE MODULE ……………………………………………………. 11 5.1 E-Tutors …………………………………………………………………………………………………………………….. 11 5.2 Downloading study material and software ……………………………………………………………………….. 11 5.3 Additional Resources on myUnisa …………………………………………………………………………………. 12 5.4 Announcements on myUnisa ………………………………………………………………………………………… 12 5.5 Installation of the software ……………………………………………………………………………………………. 12 5.6 Drawing Variable Diagrams Tutorial ……………………………………………………………………………….. 12 6 MODULE SPECIFIC STUDY PLAN ………………………………………………………………………………. 13 7 MODULE PRACTICAL WORK AND WORK INTEGRATED LEARNING…………………………….. 14 8 ASSESSMENT …………………………………………………………………………………………………………… 15 8.1 Assessment plan ………………………………………………………………………………………………………… 15 8.2 General assignment numbers ……………………………………………………………………………………….. 17 2 COS1511/101
8.3 Submission of assignments ………………………………………………………………………………………….. 17 8.4 Assignments ………………………………………………………………………………………………………………. 18 8.5 Assignment 1: 1st semester …………………………………………………………………………………………… 19 8.6 Assignment 2: 1st semester …………………………………………………………………………………………… 24 8.7 Assignment 3: 1st semester …………………………………………………………………………………………… 29 8.8 Assignment 1: 2nd semester ………………………………………………………………………………………….. 47 8.9 Assignment 2: 2nd semester ………………………………………………………………………………………….. 52 8.10 Assignment 3: 2nd semester ………………………………………………………………………………………….. 57 9 EXAMINATIONS ………………………………………………………………………………………………………… 75 10 FREQUENTLY ASKED QUESTIONS ……………………………………………………………………………. 75 11 CONCLUSION……………………………………………………………………………………………………………. 75 Please note / important note: If you do not receive your study material with your registration,
you have to download it from the Internet so that you are able to start IMMEDIATELY with your
studies. See section 5.2 of this tutorial letter for details about the downloading of study material.
To gain admission to the examination you have to submit at least one assignment on time.
Remember there is NO extension for assignment 3. 3 COS1511/101 1 INTRODUCTION AND WELCOME Dear student,
Welcome to COS1511. We hope that you will find it interesting and stimulating. You will shortly
receive a tutorial letter where the names and contact details of your COS1511 lecturers are
supplied. In the meantime, if you would like to speak to a lecturer, you may contact the
secretary of the School of Computing at 011 670 9200. This is for academic issues only. (See
your [email protected] brochure for information regarding other kinds of queries.)
There are a number of first-year Computer Science and Information Systems modules at Unisa.
Here are some of the modules. COS1511 deals with the basic concepts of programming and the design of algorithms,
using the programming language C++.
COS1512 is a further introductory course in C++ programming where object-oriented
programming is introduced. COS1511 is a pre-requisite for COS1512.
COS1521 provides a general background to computer systems.
INF1511 is an introduction to visual programming, and
COS1501 introduces the mathematics relevant to Computer Science. All the above modules are semester modules and you will need at least 8 hours per week for
each, especially for the programming modules. Programming is extremely time-consuming
and we do not recommend that you enrol for more than one programming module
simultaneously.
We begin this tutorial letter by giving you some general background information about
COS1511. We discuss the tutorial matter, the syllabus, the study programme, examination
admission requirements, semester mark and how to submit assignments. You will find all the
assignments for the first and second semester in this letter. Please make sure that if you are
registered for the first semester that you do the assignments for the first semester; if you are
registered for the second semester, do the assignments for the second semester.
Some facts about the three assignments: You have to at least submit one assignment before its due date. Assignment 1 and 2 each contribute 35% towards your semester mark and is written
assignments. Assignment 3 contributes 30% towards your semester mark and is a multiple choice
assignment. Because this is a blended online module, you need to use myUnisa to study and complete the
learning activities for this course. You need to visit the website on myUnisa for COS1511
frequently. The website for COS1511 for the first semester is COS1511-15-S1 and for the
second semester it is COS1511-15-S2.
1.1 To get started? Because this is a blended online module, you need to go online to see your study materials and
read what to do for the module. Go to the myUnisa website here: https://my.unisa.ac.za and
login with your student number and password. You will see COS1511-16-S1 (for the first
semester) or COS1511-16-S2 (for the second semester) in the row of modules in the orange
4 COS1511/101
blocks across the top of the webpage. Remember to also check in the -more- tab if you cannot
find it in the orange blocks. Click on the module you want to open.
In addition, you will receive this tutorial letter and a printed copy of the online study materials
from your module. While these printed materials may appear to be different from the online
study materials, they are exactly the same and have been copied from the online myUnisa
website.
1.1.1 About myUnisa
myUnisa is the student website that allows you to connect with your lecturers and fellow
students, download your study material, submit assignments, gain access to the Library and
various learning resources and participate in online discussion forums.
We also use myUnisa for announcements, and to deliver additional study material. Please join
myUnisa and visit the COS1511 course website regularly.
1.2 Tutorial matter When you register, you will receive an inventory letter containing information about your tutorial
matter. See also the brochure [email protected], containing contact addresses of the various
administrative departments, information about computer laboratories, the library, myUnisa,
assistance with study skills, et cetera. It also contains telephone numbers of several Unisa
departments, for example Examinations, Assignments, Despatch, Finances and Student
Administration.
The Department of Despatch should supply you with the following tutorial matter after
registration for this module: Tutorial letter 101 Study guide CD (DISK2016). CD (Drawing Variable Diagrams Tutorial) [email protected] brochure Some of this study material may not have been available when you registered. Study
material that was not available when you registered will be posted to you as soon as
possible, but is also available on myUnisa.
Please do not contact the School about missing tutorial matter, cancellation of a module,
payments, enquiries about the registration of assignments, and so on, but rather the
relevant department as indicated in the [email protected] brochure. The School should
only be contacted about academic matters.
If you have not received all of the above mentioned tutorial matter, please contact our
DESPATCH DEPARTMENT, using the contact details as given in the [email protected]
brochure. In the meantime, please download the study material from myUnisa. 5 COS1511/101
2 OVERVIEW OF THE MODULE COS1511 2.1 Purpose
The purpose of the module is to introduce students to programming and to cover the
fundamentals of data and control structures, techniques for problem solving and algorithm
design, input and output of data from and to the standard input/output streams. Data types and
structures which are covered are floating point, integer, character, string, boolean, one- and
two-dimensional arrays. The C++ decision and iteration structures that are covered include if,
while, for, switch and do..while. Furthermore, functions with both reference and value
parameters are covered, as well as structs.
2.2 Outcomes
At the end of the course you should have attained the following outcomes:
Outcome 1:
You should be able to design a logical solution to a simple programming problem, making appropriate
assumptions.
Assessment criteria:
Through assignments, including multiple choice and written assignments and an examination at the end of the
semester, you are assessed on your ability to: Interpret a problem description which specifies the requirements of a program; Identify all steps necessary to solve a problem and order the steps in the correct logical sequence. Outcome 2:
You should be able to write programs in C++, demonstrating the principles of good programming style.
Assessment criteria:
In the form of exercises in your study guide, written assignments (including working computer programs) and
examinations, you are assessed on your ability to: Write functions and use them in a program; Use control structures to implement a solution to a programming problem; Define and use data types and data structures to implement the solution to a programming problem; Recognize / locate errors in the code and correct them; Provide code for a small to medium sized working program (not exceeding 200 lines of code).
Outcome 3:
You should be able to demonstrate an understanding of the theory underlying the basic programming concepts.
Assessment criteria:
In the form of exercises in your study guide, written assignments (including working computer programs) and
examinations, you are assessed on your ability to: Apply good programming principles; Use the different programming constructs appropriately and correctly, in order to implement a solution to a
programming problem; Apply the concepts of C++ required for beginner level computer programming.
Outcome 4:
You should be able to successfully locate errors in a fragment of code demonstrating an understanding of the
syntax of the underlying programming language.
Assessment criteria:
In the form of tasks in study material, written assignments (including computer programs) and examinations, you
will be assessed on your ability to locate syntactic and logical errors in a given beginner level computer program; locate syntactic and logical errors in your own written computer programs, in order to implement a correct
solution to a programming problem. 6 COS1511/101
Outcome 5:
You can successfully construct a correct programming solution to a given problem, demonstrating understanding of
the stated problem and implementing the solution in a structured format.
Assessment criteria:
In the form of tasks in study material, written assignments (including computer programs) and examinations, you
will be assessed on your ability to analyse the problem and identify the necessary steps in order to produce the solution; design a structured solution to the problem, making your own decisions of which programming constructs
should be used to reach the solution; apply the different programming constructs appropriately and correctly to successfully implement a
programming solution to the problem.
Outcome 6:
You can demonstrate logical reasoning and analytical skills through the correct use of supplied computer software
to produce the solution to a stated problem.
Assessment criteria:
In the form of tasks in study material, written assignments (including computer programs) and examinations, you
will be assessed on your ability to install the supplied compiler for the procedural programming language; write, compile and run computer programs using the supplied software; supply solutions to written and compiled programs after successfully running programs. 3 LECTURER AND CONTACT DETAILS 3.1 Lecturers
The names and contact details of your COS1511 lecturers are supplied in Tutorial Letter
COSALLF/301/4/2016. The contact numbers of the lecturers for COS1511 are also available on
the module website for COS1511. In the meantime, if you have any academic query about the
module and would like to speak to a lecturer, you may do so in one of the following ways: send an email to the COS1511 email addresses: [email protected] if you
are registered for the first semester or [email protected] if you are
registered for the second semester. for queries regarding the installation of the CD, first see the FAQs and Additional
Resources for possible solutions. If there is no answer to your problem, contact the etutor allocated to you or one of the lecturers. please note that for general (non-academic) queries, you will find contact details in the
brochure [email protected] that you received with your study material. always have your student number ready when contacting the university. 3.2 Department
Please note that the School of Computing has moved to Florida in 2013. The School of
Computing can be contacted telephonically at 011 670 9200 or via e-mail with the e-mail
address [email protected] Should you be unable to reach any of the lecturers for
COS1511, please leave a message with one of the secretaries, who can be contacted via the
number given above. Remember to include the module code and your student number with the
message. 7 COS1511/101
3.3 University
Please note that for general (non-academic) queries, you will find contact details in the brochure
[email protected] that you received with your study material. It contains important telephone
and fax numbers, addresses and other very useful information. 4 MODULE RELATED RESOURCES 4.1 Joining myUnisa If you have access to a computer that is linked to the internet, you can quickly access resources
and information at the University. The myUnisa learning management system is Unisa's online
campus that will help you to communicate with your lecturers, with other students and with the
administrative departments of Unisa ? all through the computer and the internet.
You can start at the main Unisa website, http://www.unisa.ac.za, and then click on the myUnisa
orange block. This will take you to the myUnisa website. To go to the myUnisa website directly,
go to https://my.unisa.ac.za. When you are on the myUnisa website click on the ?Claim UNISA
Login? at the right-hand side of the screen. You will then be prompted to give your student
number to claim your initial myUnisa as well as myLife login details.
Please consult the publication [email protected] which you received with your study material
for more information on myUnisa.
Other resources ? Printed support materials 4.2 Because we want you to be successful in this blended online module, we also provide you with
some of the study materials in printed format. This will allow you to read the study materials,
even if you are not online. These printed study materials will be sent to you at the beginning of the semester, but
you do not have to wait to receive them to start studying ? You can go online as soon as
you register and all your study materials will be there. Therefore, the printed materials are not something that you need to wait for before you
start with the module. It is only an offline copy of the formal content for the online
module. This will give you the chance to do a lot of the studying of this module WITHOUT having
to go to the internet or to an internet cafe. This will save you money, of course, and you
will be able to take as much time as you need to read — and to re-read — the materials
and do the activities. It is therefore very important that you log into myUnisa regularly. We recommend that you
should do this at least every week or every 10 days to check for the following: Check for new Announcements. You can also set up your myLife email so that you
receive the Announcement emails on your cell phone.
Do the Discussion forum activities. For every unit in this module, we want you to share
with the other people in your group in the activities. You can read the instructions there,
and even prepare your answers but you need to go online to post your messages.
Do other online activities. For some of the unit activities, you need to post something
on the Blog or take a quiz or complete a survey in Self Assessment. Don't skip these
activities because they will help you to complete the assignments and activities for the
module.
8 COS1511/101
We hope that this system will help you to succeed in this blended online module by giving you
extra ways to study the materials and practice with all of the activities and assignments. At the
same time, you MUST go online in order to complete the activities and assignments on time -and to get the most from the online course.
Remember, the printed support materials are a back-up to everything that is found online, on
myUnisa. There are no extra things there. In other words, you should NOT wait for the
Printed support materials to arrive to start studying.
4.3 Prescribed books There is no prescribed book for this module. The Study Guide for COS1511 takes the place of a
text book.
4.4 Recommended books You do not have to consult any textbooks. However, those of you who want to read further may
consider any of the books listed below. (They are not necessarily available in the Unisa library.) 4.5 HM Deitel and PJ Deitel. C++ How to Program, 6th edition. Prentice Hall, 2008. John R Hubbard. Schaum?s Outlines: Programming with C++, 2nd edition. McGraw-Hill,
1996. Walter Savitch. Problem solving with C++, 8th edition. Addison Wesley, 2012. (This
book is prescribed for COS1512 in 2016.)
Study Guide The Study Guide is written in such a way that it guides you through practical activities that you
can do on the computer or sometimes on paper, so that you can experience what programming
actually is like. The Study Guide is in a flexible format. Students who have little previous
knowledge of computers will find it useful to go through all the activities and sub-activities in the
Study Guide. Students who have more computer experience will find that the Study Guide gives
guidance on which parts to do and which parts to skip.
It is important to start with this module immediately after registration. If you do not
receive your Study Guide immediately, you should download it from myUnisa under
Official Study Material.
Lesson 30 of the Study Guide is not examinable but for enrichment. It will also be of help
in the transition from COS1511 (Introduction to Programming 1) to COS1512
(Introduction to Programming 2).
Note that the solutions to the exercises at the end of each lesson are available online on the
COS1511 website.
4.6 Prescribed software The prescribed software for this module is Code::Blocks 10.05. We will refer to the software as
Code::Blocks. Code::Blocks includes the MinGW C++ compiler and an Integrated Development
Environment (IDE), which we use to create program files. The prescribed software is provided
on the CD-ROM that you should have received in your study package when you registered.
The CD-ROM contains instructions on how to install the software, and how to use the IDE to
write, compile and execute your programs.
9 COS1511/101
These instructions can be accessed from the file index.html on the CD-ROM. There are a
number of ways to view this file. After inserting the CD-ROM into the CD-ROM drive of your
computer, do one of the following: Click on Run… on the Start menu. In the dialog box that appears, type d:index.html
and click on the OK button. Double-click on the My Computer icon on your desktop. In the window that appears,
double-click on the CD-ROM icon (D:). In the window that appears, double click on the
file index.html. Load Windows Explorer and locate index.html on the CD-ROM drive. Double click on
this file. After doing any one of the above, the file index.html should be loaded into Internet Explorer
(or whatever web browser is installed on your computer). Click on the link for COS1511 and
follow the instructions.
If you did not receive the CD-ROM upon registration, you should download the software
immediately from myUnisa so that you are able to start with your studies at once. See
Section 5 of this tutorial letter.
4.7 myUnis…