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

Chapter 1 D Struct

The document provides an overview of fundamental C++ concepts, including arrays, functions, structures, pointers, data structures, abstract data types, and algorithms. It explains the properties and usage of each concept, detailing how they are defined, accessed, and manipulated in C++. Additionally, it outlines the characteristics and properties of algorithms, emphasizing their role in problem-solving within programming.

Uploaded by

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

Chapter 1 D Struct

The document provides an overview of fundamental C++ concepts, including arrays, functions, structures, pointers, data structures, abstract data types, and algorithms. It explains the properties and usage of each concept, detailing how they are defined, accessed, and manipulated in C++. Additionally, it outlines the characteristics and properties of algorithms, emphasizing their role in problem-solving within programming.

Uploaded by

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

Data structure and Algorithms

Chapter one
Overviews of C++ concepts

Prepared by. Samuel T.


Contents to be covered
• Arrays
• Functions
• Structures
• Pointers
• Data structure
• Abstract data type
• Algorithm
• Properties of algorithm
Arrays
Array - consists of a set of objects, all of which are of the same type.
A collection of identical data objects, which are stored in consecutive
memory locations under a common heading or a variable name.
In other words, an array is a group or a table of values referred to by the same
name.
Elements: set of objects in the array. Each element is identified by an index.
Index denotes the position of the element in the array.
Dimension: the number of elements in the array.
Arrays(cont.)
An array variable is defined by specifying its dimension and the type of its
elements.
Example of array declarations:
int heights[10];
A. Dimension
Array Element Type

Properties of arrays
- Arrays in C++ are zero-bounded; that is the index of the first element in
the array is 0 and the last element is N-1, where N is the size of the array.
- It is illegal to refer to an element outside of the array bounds, and your
program will crash or have unexpected results, depending on the compiler.
- Array can only hold values of one type
Arrays(cont.)
Accessing arrays
• In any point of the program in which the array is visible we can access
individually anyone of its elements for reading or modifying it as if it was
a normal variable. To access individual elements, index or subscript is
used.
The format is the following:
name [ index ]
Multidimensional Arrays
Multidimensional arrays can be described as arrays of arrays. For example, a
bi-dimensional array can be imagined as a bi-dimensional table of a uniform
concrete data type.
Example
int matrix[3][5];
Multidimensional arrays are not limited to two indices (two dimensions).
They can contain so many indices as needed, For example:
char century [100][365][24][60][60];
Functions
A function provides a convenient way of packaging a computational recipe, so that
it can be used as often as required. Therefore, a function is a block of code designed
to tackle a specific problem
C++ adhere the following rules on functions
- Every functions must have a name
- Function names are made up and assigned by the programmer
following the same rules that apply to naming variables. They can contain up to
32 characters, they must begin with a letter, and they can consist of letters,
numbers, and the underscore (_) character
- All function names have one set of parenthesis immediately following them.
This helps you (and C++ compiler) differentiate them from variables.
- The body of each function, starting immediately after parenthesis of the function
name, must be enclosed by braces
Functions(cont.)
Declaring function:
The interface of a function (also called its prototype) specifies how it may be used. It
consists of three entities:
The function return type. This specifies the type of value the function returns. A
function which returns nothing should have a return type void
The function name. this is simply a unique identifier
The function parameters (also called its signature). This is a set of zero or more typed
identifiers used for passing values to and from the function
Example
Void fun1();, int fun2(int x, int y);
Defining a function
A function definition consists of two parts: interface (prototype) & body. The brace of a
function contains the computational steps (statements) that computerize the function.
The definition consists of a line called the decelerator
Example
Void func1() – no semi colon decelerator
{
//body implementation
}
Functions(cont.)
If function definition is done before the main function, then there is no need to
put the prototype, otherwise the prototype should be scripted before the main
function starts.
Calling the function
- Calling a function means making the instruction of the function to be
executed.
- A function call consists of the function name followed by the call operator
brackets ‘()’, inside which zero or more comma-separated arguments
appear.
-The number and type of arguments should match the number of function
parameters. Each argument is an expression whose type should match the
type of the corresponding parameter in the function interface.
- The parameters of a function are list of variables used by the function to
perform its task and the arguments passed to the function during calling of
a function are values sent to the function
example func1();
Functions(cont.)
- Inline Functions :- a function that is expanded when it is called and its
definitions are small. Syntax for inline functions
Inline int x()
{
Return 2;
}
Example Suppose that a program frequently requires finding the absolute
value
of an integer quantity
- Overloaded Functions :- Functions with the same name are called
overloaded functions. C++ requires that each overloaded functions differ in
its argument list. Overloaded functions enable you to have similar
functions that work on different types of data.
- Recursion Functions :- A function which calls itself is said to be recursive.
Recursion is a general programming technique applicable to problems
which can be defined in terms of themselves example factorial
Structures
A structure is a collection of one or more variable types grouped together that
can be referred using a single name (group name) and a member name.
- Each element (called a member) in a structure can be of different data type
The General Syntax of structures is:
Struct [structure tag]
{
Member definition;
Member definition;

Member definition;
}[one or more structure variables];
Example
struct Inventory
{
char description[15];
char part_no[6];
int quantity; members of the structure
float cost;
}; // all structures end with semicolon
struct Inventory inv;//structure variable declaration
Structures(cont.)
Referencing members of a structure
To refer to the members of a structure we need to use the dot operator (.)
The General syntax to access members of a structure variable would be:
VarName.Member
Where VarName is the varaibale name of the structure variable And Member is
varaibale name of the members of the structure
inv.cost // from previous example
Initializing Structure Data
You can initialize members when you declare a structure, or you can initialize
a structure in the body of the program
struct cd_collection
{
char title[25];
char artist[20];
int num_songs;
float price;
char date_purchased[9];
} cd1 = {"Red Moon Men","Sams and the Sneeds", 12, 11.95,"08/13/93"};
Structures(cont.)
Arrays of Structures
Arrays of structures are good for storing a complete employee file, inventory
file, or any other set of data that fits in the structure format.
Example
struct Company
{
int employees;
int registers;
double sales;
}store[1000];
Referencing the array structure
The dot operator (.) works the same way for structure array element as it does
for regular variables.
Example
Store[2].employees += 3 // if employees number increase by 3 for store index
2.
Pointers
A pointer is a variable which stores the address of another variable. The only
difference between pointer variable and regular variable is the data they hold.
There are two pointer operators in C++:
& the address of operator
* the dereference operator
Whenever you see the & used with pointers, think of the words “address of.”
The & operator always produces the memory address of whatever it precedes. The *
operator, when used with pointers, either declares a pointer or dereferences the
pointer’s value. The dereference operator can be literally translated to "value
pointed by" .
A pointer is simply the address of an object in memory. Generally, objects can
be accessed in two ways: directly by their symbolic name, or indirectly
through a pointer. The act of getting to an object via a pointer to it, is called
dereferencing the pointer. Pointer variables are defined to point to objects of
a specific type so that when the pointer is dereferenced, a typed object is
obtained.
Pointers(cont.)
Declaring Pointers:
Is reserving a memory location for a pointer variable in the heap.
Syntax: type * pointer_name ;
Example int * x;
Whenever the dereference operator, *, appears in a variable declariation, the
variable being declared is always a pointer variable.
Assigning values to pointers:
int age = 26;
int * p_age;
p_age = &age;
OR
int age = 26;
int * p_age = & age;
both ways are possible
This would assign to variable p_age the address of variable age, since when
preceding the name of the variable age with the ampersand ( & ) character we
are no longer talking about the content of the variable, but about its address
in memory.
Pointers(cont.)
Arrays of Pointers
If you have to reserve many pointers for many different values, you might
want to declare an array of pointers. The following reserves an array of 10
integer pointer variables:
int *iptr[10]; //reserves an array of 10 integer pointers
iptr[4] = &age;// makes iptr[4] point to address of age.
Pointer to pointer
The address of a pointer can also be stored in another pointer. This pointer is
said to be pointer to a pointer. An array of pointer is conceptually same as
pointer to pointer type. The pointer to pointer type is declared as follows:
Data_type ** pointer_name;
Example
char a;
char * b;
char ** c;
a = 'z';
b = &a;
c = &b;
Data structure
Program is a set of instruction which is written in order to solve a
problem.
 A solution to a problem actually consists of two things:
 A way to organize the data
 Sequence of steps to solve the problem
The way data are organized in a computers memory is said to be Data
Structure.
The sequence of computational steps to solve a problem is said to be an
Algorithm.
Therefore, a program is Data structures plus Algorithm.
• The first step to solve the problem is obtaining ones own abstract
view, or model, of the problem.
• This process of modeling is called abstraction.
Data structure(cont.)
Abstraction is a process of classifying characteristics as relevant and irrelevant
for the particular purpose at hand and ignoring the irrelevant ones.

• Example: model students of Wollo university.


• Relevant:
Char Name[15];
Char ID[11];
Char Dept[20];
int Age, year;
• Non relevant
float hieght, weight;
• Using the model, a programmer tries to define the properties of the problem.
• These properties include
 The data which are affected and
 The operations that are involved in the problem
Data structure(cont. )
An entity with the properties just described is called an abstract data type
(ADT).
• Does not specify how to store or how to implement the operation.
• Is independent of any programming language
Example: ADT employees of an organization:
 This ADT stores employees with their relevant attributes and
discarding irrelevant attributes.
Relevant:- Name, ID, Sex, Age, Salary, Dept., Address
Non Relevant :- weight, color, height
 This ADT supports hiring, firing, retiring, … operations.
Algorithm
Is a brief specification of an operation for solving a problem.
is a well-defined computational procedure that takes some value or a set of
values as input and produces some value or a set of values as output.

Inputs Algorithm Outputs


An algorithm is a specification of a behavioral process. It consists of a finite set
of instructions that govern behavior step-by-step.
Data structures model the static part of the world. They are unchanging while
the world is changing. In order to model the dynamic part of the world we need
to work with algorithms. Algorithms are the dynamic part of a program’s world
model. An algorithm transforms data structures from one state to another state
in two ways:
• An algorithm may change the value held by a data structure
• An algorithm may change the data structure itself
Properties of algorithm
Finiteness: Algorithm must complete after a finite number of steps.
Definiteness: Each step must be clearly defined, having one and only one
interpretation. At each point in computation, one should be able to tell exactly what
happens next.
Sequence: Each step must have a unique defined preceding and succeeding step.
The first step (start step) and last step (halt step) must be clearly noted.
Feasibility: It must be possible to perform each instruction.
Correctness: It must compute correct answer for all possible legal inputs.
Language Independence: It must not depend on any one programming language.
Completeness: It must solve the problem completely.
Effectiveness: It must be possible to perform each step exactly and in a finite
amount of time.
Efficiency: It must solve with the least amount of computational resources such as
time and space.
Generality: Algorithm should be valid on all possible inputs.
Input/Output: There must be a specified number of input values, and one or more
result values.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy