0% found this document useful (0 votes)
32 views6 pages

CO1401 Week 1 Lab

The document provides instructions for setting up a C++ development environment in Visual Studio and writing basic C++ programs. It includes tasks for writing code to perform calculations, accept user input, use loops and functions.

Uploaded by

Edward Lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

CO1401 Week 1 Lab

The document provides instructions for setting up a C++ development environment in Visual Studio and writing basic C++ programs. It includes tasks for writing code to perform calculations, accept user input, use loops and functions.

Uploaded by

Edward Lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

CO1401 - Programming

Week 1 – Introduction to C++

Worksheet Key
A diamond bullet point is an action or a task. Other bullets are information:
 A task to perform
- A point of information

Important In-depth detail Advanced optional task

Always refer to the lecture notes for examples and guidance.


Also look at your previous work, many exercises are similar.

Setting up the C++ development environment

 You are going to use C++ in this module instead of C#. It is important to
change the development environment in Visual Studio. This can be done simply
by using a Visual Studio wizard.
 Run Visual Studio (The University version should be 2017, in practice anything
from 2010 onward should be OK).
 Bring up the “Tools” menu from the menu bar.
 Click on “Import and Export Settings”.
 Click on the “Reset all settings” radio button.
 Press “Next”
 Click on the “No, just reset settings....” radio button.
 Press “Next”.
 Select the “Visual C++ Development Settings” from the list of settings.
 Press “Finish”.
 Press “Close” to close the wizard.

Getting started with C++

 The next set of instructions explains how to set up a C++ project in Visual
Studio. There are some important differences from the steps you are used to
with C#.
 On the Start Page choose “New Project…”. Alternatively, in the main menu
choose “File, New Project…”
 The Visual C++ project templates should automatically appear. If they do not
then you almost certainly have not set the environment to C++. See the
instructions above or ask the practical tutor for help.
 From the left choose Visual C++, then from the list on the right choose
"Empty Project".
 At the bottom give a name to your project. Call this project Week 1.
 Browse to a suitable location. I suggest you make a new folder on the N:
drive. (Don’t use the default C: drive location)
 Also make sure “Create directory for solution” is unchecked. The remaining
settings should be fine, so now press OK.
 Unlike C# you do not get any starting code, or even a default file in which to
write...
 The first thing to do is create a file in which to write your code.
 "Solution Explorer" should be open. Typically it will be on the left-hand side of
the Visual Studio pane. If it is not there then click on the "View" menu to bring it
up.
 In "Solution Explorer" right click on the "Source Files" folder. A menu will
appear. Navigate to "Add" and then "New Item". Select "C++ File (.cpp)". Enter
the name in the field at the bottom of the window. Call it "exercise 1" An
extension of ".cpp" will automatically be added to the file.

 The file will open. You write your source code into this file.
A first Visual C++ Program
 Copy and paste the following code into your file.
#include <iostream>
using namespace std;

int main()
{
int a;
int b;
int result;
a = 4;
b = 6;
result = a * b;
cout << "The result is ";
cout << result << endl;

system( "pause" );
}

 Now compile and run the program.


 Compilation and debugging are the same in C++ as in the C# environment.
 Notice that "main" has a lower case 'm'. It's return type is also different from what
you're used to in C#.

We should really return a value from "main". It's not actually


necessary in this program, or indeed many of the programs you'll write in
this module. However, main should return a value of 1 for success.
 Notice the way that you don't have the "namespace" and "class" with surrounding
curly braces around main.
 The most important change from C# is the treatment of input and output in C++.
Output is obtained using the "cout" class. Notice the use of the chevrons: "<<".
Access to keyboard input and monitor output in C++ is given through the use of
the line of code:
#include <iostream>

 In other respects, the code should look similar to what you're familiar with.

Programming Exercise 1
Use a for loop to write out the 2 times table. Your program needs to output the
following to the screen:

1 times 2 = 2
2 times 2 = 4
3 times 2 = 6
4 times 2 = 8
5 times 2 = 10
6 times 2 = 12
7 times 2 = 14
8 times 2 = 16
9 times 2 = 18
10 times 2 = 20
11 times 2 = 22
12 times 2 = 24

You must use a for loop. You are not allowed to use any other type of loop construct.

Keyboard input
 In C++ you get input from the keyboard using the "cin" command.
 Notice that the chevrons are the opposite way around from when you used "cout".
 In the following code you can also see the how C++ implements a string.

#include <iostream>
#include <string>
using namespace std;

int main()
{
string name;

cout << "What is your name?" << endl;


cin >> name;
cout << "Hello " << name << endl;

system( "pause" );
}

More Programming Exercises


 Write a program which prompts the user a number and then outputs the square of
the number (the square of a number is the number mulitplied by itself).
 The length of a string can be found by using the "length" method, e.g. if we had a
string variable called "word" then the length of the string would be given by the
following code:
int result;
result = name.length();
 Write a program which prompts the user for their name and then tells them how
many letters there are in their name.

 Write a program that prompts the user for an integer value. Using this value draw
a square made out of asterisks the size of the value given, e.g. a value of 1 would
give:

*
 A value of 4 would give:
****
****
****
****

 Write a while loop which prints out the message "Type 'no' to end", and then only
ends if the user does indeed enter the word "no". Make sure you carefully test
your program with both "no" and some other words.

 This repeats a question from one of the worksheets in the previous module. Use it
to test your understanding of setting up a C++ project and writing C++ code. Start
a new project. Write a program which asks the user for their age. Display a
different message depending on whether they are a child, adult or pensioner. You
can decide the age ranges.
 Set up a while loop so that program reads in character values from the keyboard
until an asterix is entered. You will need to count the number of characters
entered. Do not include the asterix in your count.
 Output the number of characters entered.
 Write a program that initialises a "secret" number to a value between 1 and 10.
Ask the user to enter a number between 1 and 10 in order to guess what the
number is. If the number entered is correct, output "Well done you guessed it" or
words to that effect. If the guess is incorrect, output "Wrong, try again" or similar.
The program should terminate when they guess the number.
 Now modify the program so that it gives the user a clue as to guessing the
number by outputting "No it is higher than that" or "No it is lower than that".
 Use a nested loop to create and display the following 3 by 3 matrix array
123
456
789

Advanced Tasks

 Here are some tasks for those who want to stretch themselves.
 Functions in C++ look much the same as in C#. The big differences are that they
are declared outside main() and that they do no use the keyword "static". Examine
the following code to see this:
#include <iostream>
#include <string>
using namespace std;

int main()
{
string name;

cout << "What is your name?" << endl;


cin >> name;
cout << "Hello " << name << endl;

system( "pause" );
}

 Obtaining the cube of a number. Write a function called cubed which takes a
single integer as a parameter and then returns the cube (n * n * n) of the number.
 Write a function which, given two integers, discovers the smaller of the two
integers and then returns this value.
 Write a function which will return the maximum of three given integer values.
 Calculating the area of a circle. The area of a circle is calculated using pi (3.14)
times the square of the radius:
area = 3.14 * radius * radius
 Write a function which calculates the area of a circle. The function takes one
parameter of type float, and returns a value of type flat.
 Include some error checking in your code. If the value of the variable passed
over to the function is negative then produce an error message and return a
value of -1.

You might also like