0% found this document useful (0 votes)
6 views5 pages

Lab 1

Uploaded by

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

Lab 1

Uploaded by

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

ACSC182: Programming Principles I

Lab 1: Introduction to C++ Programming (I)


Exercise 1:
1. Create a new C++ project in Visual Studio (choose Console App – Empty Project), click Next and
save it as Lab1Ex1 in your directory.

2. Press Ctrl + Shift + A or Right click on the option Source Files (found under the Solution Explorer
 Source Files) and select Add  New Item and then select C++ File (.cpp) and name the file to
be created using the same name with the project, that is Lab1Ex1.cpp, and press Add.
3. Enter the following code within your editor.

#include <iostream>
using namespace std;

int main()

{
float test = 85;
float homework = 90;
float lab = 80;
float final_exam = 75;
float coursework;
float final_grade;

coursework = 0.3 * test + 0.2 * homework + 0.5 * lab;


final_grade = 0.6 * coursework + 0.4 * final_exam;

cout << "Final Grade is " << final_grade << endl;

system("pause");
return 0;
}

4. Press the F10 function key to trace out the variables’ values after the execution of each
statement, at the position shown below, and try to understand the concept of the assignments.

Page 1 of 5
5. Save your project and close it. Exit Visual Studio.

Exercise 2:
1. Create a new C++ project in Visual Studio (choose Console App) and save it as Lab1Ex2 in your
directory.
2. Press Ctrl + Shift + A or Right click on the option Source Files (found under the Solution Explorer
 Source Files) and select Add  New Item and then select C++ File (.cpp) and name the file to
be created using the same name with the project, that is Lab1Ex2.cpp, and press Add.
3. Enter the following code within your editor.

#include <iostream>
using namespace std;
//program to calculate and print the area of a triangle
int main()

{
float base = 4.2;
float height = 5.25;
area = (base * height) / 2;

cout << "Area is " << area << endl;

system("pause");
return 0;
}

4. Compile the program (press F5). What is the error produced? What type of error is it?
Correct it, re-compile & run.
5. Save your project and close it. Exit Visual Studio.

Page 2 of 5
Exercise 3:
1. Create a new C++ project in Visual Studio (choose Console App) and save it as Lab1Ex3 in your
directory.
2. Press Ctrl + Shift + A or Right click on the option Source Files (found under the Solution Explorer
 Source Files) and select Add  New Item and then select C++ File (.cpp) and name the file to
be created using the same name with the project, that is Lab1Ex3.cpp, and press Add.
3. Enter the following code within your editor.

#include <iostream>
using namespace std;
//program to convert miles to kilometres
int main()

{
float miles = 10;
float kilometres;

kilometres = 1.609 * miles;

//Output the conversion


cout << miles << " mile(s) -> " << kilometres << " km. " << endl;

system("pause");
return 0;
}

4. Compile and Run the program (press F5).


5. Press the F10 function key to trace out the variables’ values after the execution of each
statement.
6. Save your project and close it. Exit Visual Studio.
7. Create a new C++ project in Visual Studio (choose Console App) and save it as Lab1Ex3b in your
directory.
8. Modify the Lab1Ex3.cpp code as given above to prompt the user to input the number of miles to
be converted in kilometers.
9. Compile and Run the program (press F5).
10. Press the F10 function key to trace out the variables’ values after the execution of each
statement.
11. Save your project and close it. Exit Visual Studio.

Page 3 of 5
Exercise 4:
1. Create a new C++ project in Visual Studio (choose Console App) and save it as Lab1Ex4 in your
directory.
2. Press Ctrl + Shift + A or Right click on the option Source Files (found under the Solution Explorer
 Source Files) and select Add  New Item and then select C++ File (.cpp) and name the file to
be created using the same name with the project, that is Lab1Ex4.cpp, and press Add.
3. Enter the following code within your editor.

#include <iostream>
using namespace std;
//program to swap two numbers
int main()

{
int number1 = 5;
int number2 = 10;
int temp;

cout << "Before swapping : " << endl;


cout << "Number 1 is: " << number1 << endl;
cout << "Number 2 is: " << number2 << endl << endl;

temp = number1;
number1 = number2;
number2 = temp;

cout << "After swapping : " << endl;


cout << "Number 1 is: " << number1 << endl;
cout << "Number 2 is: " << number2 << endl;

system("pause");
return 0;
}

4. Complete the following table by tracing out the variables’ values after the execution of each
statement.

Statement number1 number2 temp


int number1 = 5;
int number2 = 10;
int temp;
temp = number1;
number1 = number2;
number2 = temp;
5. Compile and Run the program (press F5).
6. Press the F10 function key to trace out the variables’ values after the execution of each
statement. Compare the results you get with the contents of your completed table.
7. Save your project and close it. Exit Visual Studio.
8. Create a new C++ project in Visual Studio (choose Console App) and save it as Lab1Ex4b in your
directory.
9. Modify the Lab1Ex4.cpp code as given above to prompt the user to input the two numbers to be
swapped.
10. Compile and Run the program (press F5).
11. Press the F10 function key to trace out the variables’ values after the execution of each
statement.
Page 4 of 5
12. Save your project and close it. Exit Visual Studio.

Page 5 of 5

You might also like