0% found this document useful (0 votes)
40 views10 pages

Tutorial 2

This document provides an overview of built-in data types in C++, including integers, constants, and arithmetic operators. It then presents exercises to practice declaring and initializing variables, identifying syntax errors, understanding constant values, evaluating arithmetic expressions, and displaying values and messages that incorporate variable data.

Uploaded by

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

Tutorial 2

This document provides an overview of built-in data types in C++, including integers, constants, and arithmetic operators. It then presents exercises to practice declaring and initializing variables, identifying syntax errors, understanding constant values, evaluating arithmetic expressions, and displaying values and messages that incorporate variable data.

Uploaded by

david
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMP 218 - Fundamentals of Programming

Tutorial 2 – Built in Data Types I


Built in Data Types I - Quick Review.

Integer Type -- int


§ Declaration statement
int k, classSize;
§ Assignment Statement
k = 65; classSize = 50;
§ Initialized Declaration
int k = 65;
int classSize = 50;
Built in Data Types I - Quick Review

§ Ask User to provide value


cout << "Enter class size: ” << endl;
cin >> classSize;
cout << ”The class size is: ” << classSize;
§ Integer with fixed values (Constant)
const int WEEK_DAYS =7;
const int WKS_IN_YR = 52;
Try the following code..
WEEK_DAYS = 8;
Built in Data Types I - Integer Operations

int a =1, b=2, c=3;

§addition: a+4 value is 1+4=5


§subtraction: c-1 value is 3-1 =2
§multiplication: b*c value is 2*3 =6
§negation: –a value is -1
§division: a/b value is 17/3 =5
§remainder: a%b value is 17%3 =2
Built in Data Types I – Exercises 1
Syntax: identify and correct the syntax error in the
following program:
#include <iostream>
using namespace std;
int main()
{
// declaration of variables
double mark1, mark2, mark3;
double average,
mark1 = 90.0
Mark2 = 75;
mark3 = 65;
// compute the average
aver = (mark1 + mark2 + mark3) / 3;
// display the result
cout << "the average is " << "average";
return 0;
}
Built in Data Types I – Exercises 2

Assignment and arithmetic operators:


Given the following declaration statement:
const double MAX = 100;

What type of error will the following statement cause and


why?
MAX = 200;
Built in Data Types I – Exercises 3
Assume the following declaration:
int a, b, c;
Indicate the values of a, b and c after the following
instructions:
instruction value of a value of b value of c
a = 5;
b = 3;
c = a + b;
a = 7 + 3 * 7 / 2 - 1;
c = b - a;
a = a + 2;
b = 2 % 2 + 2 * 2 - 2 / 2;
a = b = c + 10;

!
Built in Data Types I – Exercises 4
Arithmetic Operators:
To compute the sum S of the integers from 1 to n, we can
use the formula:
S = ((1+n)*n)/2
For instance, if n has value 10, then S is 55
(1 + 2 +… + 9 + 10)

Assume the following declarations:


int s, n;

à Write a C++ statement to calculate S using the above


formula
Built in Data Types I – Exercises 5
Assume the following declaration:
char cat = ‘B’;
char art = ‘S’;
int qty = 50;
double price = 19.99;

Write C++ instructions to display the following messages


1. The article is expensive!
2. <value of qty>
3. <value of price> … That’s expensive
4.<value of cat> <value of art>
5.<value of qty> articles of <value of art> $ cost <value of qty*value of price> $
6. Write a C++ instruction to read from the keyboard the value of qty and price

You might also like