0% found this document useful (0 votes)
65 views24 pages

DS in C++ Tim Budd

This chapter reviews fundamental C++ language features including comments, constants, variables, data types, operators, input/output streams, conditional statements, loops, arrays, and pointers. It introduces basic concepts like declaring variables, assigning values, defining constants, built-in data types, and using conditionals, loops, and arrays. The chapter establishes the core building blocks for working with data structures in C++.

Uploaded by

Quân
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)
65 views24 pages

DS in C++ Tim Budd

This chapter reviews fundamental C++ language features including comments, constants, variables, data types, operators, input/output streams, conditional statements, loops, arrays, and pointers. It introduces basic concepts like declaring variables, assigning values, defining constants, built-in data types, and using conditionals, loops, and arrays. The chapter establishes the core building blocks for working with data structures in C++.

Uploaded by

Quân
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
You are on page 1/ 24

Data Structures in C++ 1

Data Structures
in C++
Chapter 1
Tim Budd
Oregon State University
Corvallis, Oregon
USA

Language Fundamentals Chapter 1


Data Structures in C++ 2

Language Features
The purpose of this chapter is to quickly review all those features of
C++ that you should already have encountered and be familiar
with.
Even if you have learned programming in another language (Pascal,
for example) you should be able to quickly get up to speed with the
features described here.

Language Fundamentals Chapter 1


Data Structures in C++ 3

Comments
There are two forms of comments in C++
// from slashes to end of line

/
comments that span
multiple lines
/
Comments should be used extensively for documentation.

Language Fundamentals Chapter 1


Data Structures in C++ 4

Constants
There are various types of constants:
 integer { 1, 12, 37
 octal integers { 014
 hexadecimal integers 0XFF 0XC
 oating point { 3.14159 2.7e14
 character { 'a' '\n'
 string { "abc"
Suxes can be applied to integer constants (U for unsigned, L for
long)
Several other special backslash characters

Language Fundamentals Chapter 1


Data Structures in C++ 5

Variables, Types, Values and


Declarations
A variable is a named location that can hold values of a certain
type.
Variables are created using a declaration statement, which also
describes the associated type.
int a, b, c // declare three integer variables

Declarations can be combined with initialization:


double pi = 3.1415926;

Language Fundamentals Chapter 1


Data Structures in C++ 6

Fundamental Data Types


The fundamental data types:
 integer { int
 oating point { double, float
 character { char
Modi ers that can be used with funamental types
 signed, unsigned { positive and negative, or positive only
 short, long { (possibly) shorter or longer than standard

Language Fundamentals Chapter 1


Data Structures in C++ 7

More Data Types


Boolean (bool) variables are true/false.
Enumerated values are de ned by providing an explicit range
enum months fJanuary, February, March, April, May, June, July,
August, September, October, November, Decemberg;

months workingMonth, vacationMonth;


months summerMonth = August;

Language Fundamentals Chapter 1


Data Structures in C++ 8

Variables and Assignment


Variables are modi ed by assignment statments, which assign an
expression to a variable.
double f, c; // Fahrenheit and Celsius temperature

c = 43;
f = (c  9.0) / 5.0 + 32;

Binary operators can be combined with assignment:


i += 5;
has the same meaning as the statement:
i = i + 5;
Other short-hand notations:
i++

has the e ect of incrementing the variable i by one.

Language Fundamentals Chapter 1


Data Structures in C++ 9

Lots of Operators
Unary Operators
increment, decrement i++, ++i, i , i
negation i
bit-wise inverse i
Arithmetic Operations
addition, subtraction a+b a b
multiplication, division ab a=b
remainder after division a%b
Shift Operations (also stream I/O)
left shift (also stream output) a << b
right shift (also stream input) a >> b
Relational Operations
less than, less than or equal < <=
equal, not equal == ! =
greater than, greater than or equal > >=
Logical Operations
and x && y
or x jj y
logical negation !i
Miscellaneous Operations
function call f(a,b,c)
conditional expression c? a: b

Language Fundamentals Chapter 1


Data Structures in C++ 10

Stream I/O
The left and right shift operators are given di erent meanings when
used with stream values. The most common stream is associated
with \console input and output".
cout  "the Fahrenheit equivalent of "  c
" is "  f "\n";

cin  c; // get a new value of c


cout  "the Fahrenheit equivalent of "  c 
" is "  f  "\n";

Operator >> works by side e ect, changing the right hand


expression. Result can be converted into a boolean, to test if input
was successful.
int sum = 0;
int value;
while (cin  value) f
sum += value;
g
cout  "sum is "  sum  "\n";

Language Fundamentals Chapter 1


Data Structures in C++ 11

Pointers
A pointer is a variable that maintains the address of another
location in memory.
PPP
PqP

Pointers can be used in the following ways:


 Pointers can be subscripted, (works best if they point to an
array, but isn't checked).
 Pointers can be dereferenced, using the  operator.
 Can combine dereference and eld access, using > operator.
 Can use addition, p+i is address of p[i].

Language Fundamentals Chapter 1


Data Structures in C++ 12

Conditional Statements
Normal sequential control can be modi ed using a conditional
statment:
month aMonth;
...
if ((aMonth >= June) && (aMonth <= August))
isSummer = true;
else
isSummer = false;

Else part is optional.

Language Fundamentals Chapter 1


Data Structures in C++ 13

Switch Statements
Switch statements can select one of many alternatives:
switch (aMonth) f
case January:
highTemp = 20;
lowTemp = 0;
break;
case February:
highTemp = 30;
lowTemp = 10;
break;
...
case July:
highTemp = 120;
lowTemp = 50;
break;
default:
highTemp = 60;
lowTemp = 20;
g;

Language Fundamentals Chapter 1


Data Structures in C++ 14

Loops
Loops are used to execute statements repeatedly until a condition is
satis ed.
c = 0;
while (c <= 100) f
cout  "Celsius "  c  " is Fahrenheit " 
((9.0  c) / 5.0 + 32)  "\n";
c += 10;
g

Language Fundamentals Chapter 1


Data Structures in C++ 15

For statements
For statements combine in one statement initialization, termination
test, and update.
for (c = 0; c <= 100; c += 10) f
cout  "Celsius "  c  " is Fahrenheit " 
((9.0  c) / 5.0 + 32)  "\n";
g
Declaration of new variables can be combined with loop.
for (int i = 0; i < 12; i++) f
cout  "i: "  i  " i squared "  ii  "\n";
g

Language Fundamentals Chapter 1


Data Structures in C++ 16

Array
An array is a xed sized collection of similarly-typed values. Array
elements are accessed using subscripts, range is zero to one less
than array size.
// declare an array of twelve integer values
int Temperatures[12];
// now assign all values
Temperatures[0] = 0;
Temperatures[1] = 10;
Temperatures[2] = Temperatures[1] + 15;
...

Arrays can be initialized:


string MonthNames[12] = f"January", "February",
"March", "April", "may", "June",
"July", "August", "September", "October",
"November", "December" g;

Language Fundamentals Chapter 1


Data Structures in C++ 17

Multidimensional Arrays
Arrays of more than one dimension can be created by giving the
extent along each axis.
double matrix[10][20];

Creates a double precision array of ten rows and twenty columns.


Elements accessed by giving subscript for each dimension:
matrix [i][j] = matrix [i 1][j+1] + 1;

Language Fundamentals Chapter 1


Data Structures in C++ 18

Arrays and Pointers


Close relationship between arrays and pointers.
Array name is in fact treated just like a pointer.
Pointers can be subscripted, as if they were arrays (even if they
aren't!)
MonthNames + 3 is legal, means address of MonthNames[3].

Language Fundamentals Chapter 1


Data Structures in C++ 19

Arrays as Arguments
When used as an argument, size need not be speci ed:
int arraySum (int values[ ], int n)
// compute sum of array values[0] .. values[n-1]
f
int result = 0;
for (int i = 0; i < n; i++) f
result += values[i]
g
return result;
g

Language Fundamentals Chapter 1


Data Structures in C++ 20

Structures
A structure is a collection of elds, which need not have the same
type.
struct person f
string name;
int age;
enum fmale, femaleg sex;
g;
Fields are accessed using dot notation.
person employee;
employee.name = "sam smith";
employee.age++;
if (employee.sex == male)
...

We actually won't use structures, will use more general mechanism


called class (Described in chapter 2).

Language Fundamentals Chapter 1


Data Structures in C++ 21

Functions
Functions encapsulate a set of actions, so that later we can refer to
the sequence of actions by name alone:
int Fahrenheit(int cTemp)
f
return (cTemp  9.0) / 5.0 + 32;
g
Parts:
 Header { with return type, name, and arguments
 Body { with statements to execute. Can have return statement
to end execution.
Return type can be void { no value.
A function prototype is a declaration but not a de nition, just gives
name, arguments and return type.
// prototype for Fahrenheit { de nition occurs later
int Fahrenheit (int);

Language Fundamentals Chapter 1


Data Structures in C++ 22

Local Variables
Variables within a function come into existance when the function
is entered, disappear when the function exits. Execute in stack-like
fashion. Assume function A calls function B which calls function C
{ can imagine variables as follows:
local variables for C
local variables for B
local variables for A

Will eventually encounter recursive functions, functions that can


call themselves. Imagine B is recursive, and has called itself once
before calling C, can envision the following:
local variables for C
local variables for B
local variables for B
local variables for A

Language Fundamentals Chapter 1


Data Structures in C++ 23

The Main Event


A program must always include a procedure named main, which is
the starting point for execution.
# include <iostream>

void main() f
// program to write table of squares
cout  "Table of Squares\n";

for (int i = 0; i < 12; i++) f


cout  "i: "  i  " i squared "  ii  "\n";
g
g

Language Fundamentals Chapter 1


Data Structures in C++ 24

Include Files
Many data structures require one to de ne an include le before
they can be processed.
purpose name
stream input/output iostream
math functions math.h
complex numbers complex
Boolean values bool.h
generic algorithms algorithm

Language Fundamentals Chapter 1

You might also like