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

Built-in Data Types in C++ Programming

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)
8 views10 pages

Built-in Data Types in C++ Programming

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

Common questions

Powered by AI

To structure C++ code to output messages with variable values, use 'cout' with stream insertion operators '<<'. For variables: char cat = 'B'; char art = 'S'; int qty = 50; double price = 19.99;, the instructions would be: 'cout << "The article is expensive!" << endl; cout << qty << endl; cout << price << " ... That's expensive" << endl; cout << cat << " " << art << endl; cout << qty << " articles of " << art << " $ cost " << qty*price << "$" << endl;' .

Using built-in arithmetic operators for large integer values can be efficient due to their typical implementation as direct CPU instructions, which offers optimal performance speed. However, pitfalls exist, such as integer overflow when numbers exceed the storage capacity of the data type, typically 'int' or 'long'. In such cases, results wrap around, leading to erroneous outcomes. To handle large values practically, consider using data types like 'long long' or specialized libraries that support arbitrary precision arithmetic. Efficient error checking and exception handling mechanisms are crucial to identify and manage overflows .

To compute the sum of integers from 1 to n in C++, use 'S = ((1 + n) * n) / 2;'. For instance, initialize and assign 'int S, n;' and then assign 'S = ((1 + n) * n) / 2;' where 'n' is input by the user or defined. This leverages the formula for the sum of an arithmetic series to compute results efficiently .

In C++, common mistakes in variable declaration and initialization include declaring a variable multiple times, improper variable initialization, and attempting operations on uninitialized variables. For example, in the source provided, the error occurs with the variable 'mark2', where it is assigned as 'Mark2,' which leads to a compilation error due to case sensitivity. This can be fixed by uniformly using 'mark2'. Additionally, in an assignment statement like 'double average, mark1 = 90.0 Mark2 = 75; mark3 = 65;', there's a missing semicolon after the initialization, which should be 'double average; mark1 = 90.0; mark2 = 75; mark3 = 65;' .

Arithmetic operators in C++ follow a specific precedence order: parentheses first, followed by multiplication, division, and modulus, and finally addition and subtraction. For example, in the expression 'a = 7 + 3 * 7 / 2 - 1;', the multiplication and division operations are executed before addition and subtraction. Hence, 3 * 7 gives 21, then 21 / 2 results in 10.5, truncated to 10 as it's an integer operation. The expression simplifies to '7 + 10 - 1' giving 16 .

The remainder operator '%' only works with integer types in C++. If used with non-integer or mixed operand types, it won't yield expected outcomes due to implicit type conversion or undefined behavior. In 'b = 2 % 2 + 2 * 2 - 2 / 2;' the remainder calculated is '2 % 2', which results in 0. However, if either operand is not an integer, a compile-time error will occur . To prevent errors, ensure operands are integers when using '%'. Additionally, understand the effect of operator precedence, like multiplication and division before modulus.

Reassigning a value to a constant variable in C++ causes a compile-time error because constant variables are meant to remain unchanged throughout the program. For instance, doing 'MAX = 200;' after declaring 'const double MAX = 100;' will cause an error, as modifying a 'const' violates the immutability rule in C++ . To prevent this error, ensure that constant variables are used for initialization purposes only and do not attempt to modify their values in the program logic.

In C++, simultaneous adjustment of multiple variables can be done using compound assignments and cascading assignments like 'a = b = c + 10;'. This sets both 'a' and 'b' to 'c + 10', though it's evaluated right-to-left, 'c + 10' is computed, assigned to 'b', then to 'a'. Applications include synchronized variable updates, like when adjusting scores or states based on a computed value in game development scenarios . Ensure that logical dependencies or side-effects due to assignment order are handled properly to avoid errors.

A C++ program can read user input using 'cin'. To initialize a variable with user input, the syntax is 'cin >> variable_name;'. A potential pitfall is not handling invalid input types during the 'cin' operation, which can lead to unexpected behavior or errors. For example, using 'cin >> classSize;' after prompting the user with 'cout << "Enter class size: "' will correctly initialize 'classSize' unless the input type doesn't match the expected integer .

'#include <iostream> using namespace std; int main() { 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; }' contains several syntax errors . Firstly, 'Mark2' is improperly capitalized and should be 'mark2'. Secondly, there's a missing semicolon after 'double average'. Additionally, the variable 'aver' used for calculation is not declared, and 'cout << "average"' attempts to print the string 'average' instead of the variable. Correctly, it should be 'cout << "the average is " << average;'. Finally, 'double average, mark1 = 90.0;' wrongly declares 'mark1' with 'average'.

You might also like