Experiment 1: University of Engineering and Technology, Taxila
Experiment 1: University of Engineering and Technology, Taxila
Experiment 1
Introduction to Data Structures : Built-in Data types & Derived Data Types
The data in the data structures are processed by certain operations. The particular data
structure chosen largely depends on the frequency of the operation that needs to be performed
on the data structure.
Traversing
Searching
Insertion
Deletion
Sorting
Merging
Data Object:
Data Type:
Data type is a way to classify various types of data such as integer, string, etc. which
determines the values that can be used with the corresponding type of data, the type of
operations that can be performed on the corresponding type of data. There are two data types
Those data types which are implementation independent as they can be implemented in one
or the other way ,are known as derived data types. These data types are normally built by the
combination of primary or built-in data types and associated operations on them.
For example
List
Array
Stack
Queue
Engr. Rabia Arshad
University of Engineering and Technology, Taxila 2
Strings:
There are two types of strings commonly use in C++ programming language:
Strings that are objects of string class (The Standard C++ Library string class)
C-strings (C-style Strings)
C-strings:
In C programming, only one type of string is available and this is also supported in C++
programming. Hence it's called C-strings. C-strings are the arrays of type char terminated with null
character, that is, \0 (ASCII value of null character is 0). Consider this example:
In the above code, str is a string and it holds 4 character. Although, "C++" has 3 character, the null
character \0 is added to the end of the string automatically.
Like arrays, it is not necessary to use all the space allocated for the string. For example:
#include <iostream>
int main() {
char str[100];
cin>>str;
cin>>str;
return 0;
#include <iostream>
int main() {
char str[100];
cin.get(str, 100);
return 0;
Strings are passed to a function in a similar way arrays are passed to a function.
#include <iostream>
int main() {
char str[100];
cin.get(str, 100);
display(str);
return 0;
Strings are objects that represent sequences of characters. The standard string class provides support
for such objects with an interface. The string class is an instantiation of the basic string class template
that uses char (i.e., bytes) as its character type, with its default char traits and allocator types.
Declaring a string:
string my_string;
or
std::string my_string;
#include <iostream>
int main() {
If you need to read an entire line at a time, you can use the getline function and pass in an input
stream object (such as cin, to read from standard input, or a stream associated with a file, to read
from a file), the string, and a character on which to terminate input.
The following code reads a line from standard input (e.g., the keyboard) until a newline is entered.
#include <iostream>
int main() {
string my_string;
String concatenation
Strings can also be assigned to each other or appended together using the + operator:
#include <iostream>
int main() {
cout<<my_string3<<endl;
String Comparisons
One of the most confusing parts of using char*s as strings is that comparisons are tricky, requiring a
special comparison function, and using tests such as == or < don't mean what you'd expect.
Fortunately, for C++ strings, all of the typical relational operators work as expected to compare either
C++ strings or a C++ string and either a C string or a static string (i.e., "one in quotes").
For instance, the following code does exactly what you would expect, namely, it determines whether
an input string is equal to a fixed string:
#include <iostream>
int main() {
string passwd;
if(passwd == "xyzzy")
cout<<"Access allowed";
else
cout<<"Access denied";
To take the length of a string, you can use either the length or size function, which are members of the
string class, and which return the number of characters in a string:
#include <iostream>
int main() {
cout<<endl<<len;
Strings, like C strings (char*s), can be indexed numerically. For instance, you could
iterate over all of the characters in a string indexing them by number, as though the
string were an array.
Note that the use of the length () or size () function is important here because C++ strings are not
guaranteed to be null-terminated (by a '\0'). (In fact, you should be able to store bytes with a value of
0 inside of a C++ string with no adverse effects. In a C string, this would terminate the string!)
#include <iostream>
int main() {
int i;
{ cout<<my_string[i]<<endl;
ARRAY
An array is a series of elements of the same type placed in contiguous memory locations
that can be individually referenced by adding an index to a unique identifier. As
discussed in the class/lecture room, there are different algorithms related to an array
(Traversing, Insertion, Deletion, Modify, Sorting, Searching (Linear, Binary). Following
algorithms helps you to understand and write programs of these algos.
Declaration:
Like a regular variable, an array must be declared before it is used. A typical declaration for
an array in C++ is:
where type is a valid type (such as int, float...), name is a valid identifier and the elements
field (which is always enclosed in square brackets []), specifies the length of the array in
terms of the number of elements.
Therefore, the foo array, with five elements of type int, can be declared as:
NOTE: The elements field within square brackets [], representing the number of elements
in the array, must be a constant expression, since arrays are blocks of static memory whose
size must be determined at compile time, before the program runs.
Array Representation
This creates an array of five int values, each initialized with a value of zero:
When an initialization of values is provided for an array, C++ allows the possibility of
leaving the square brackets empty []. In this case, the compiler will assume automatically a
size for the array that matches the number of values included between the braces {}:
After this declaration, array foo would be 5 int long, since we have provided 5 initialization
values.
As per the above illustration, following are the important points to be considered.
Lab Task
Task 1:
Write A c++ Program That checks if a given number is a Palindrome.
Palindrome is a number that remains the same when its digits are reversed. Like
121,313, 14641 etc.
Task 2:
Write a program that computes the Summation of Array Values.Input the array
values from user.