0% found this document useful (0 votes)
47 views11 pages

Experiment 1: University of Engineering and Technology, Taxila

The document discusses data structures and their implementation in C++. It begins by defining data structures and describing common operations performed on them like traversing, searching, insertion, deletion, sorting, and merging. It then discusses built-in and derived data types in C++, providing examples like integers, strings, arrays, lists, stacks, and queues. The remainder of the document provides details on implementing various data structures in C++, including strings, arrays, passing arrays to functions, and comparing strings. It describes declaring, initializing, accessing elements of, and performing common operations on arrays and strings in C++.

Uploaded by

Afras Ahmad
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)
47 views11 pages

Experiment 1: University of Engineering and Technology, Taxila

The document discusses data structures and their implementation in C++. It begins by defining data structures and describing common operations performed on them like traversing, searching, insertion, deletion, sorting, and merging. It then discusses built-in and derived data types in C++, providing examples like integers, strings, arrays, lists, stacks, and queues. The remainder of the document provides details on implementing various data structures in C++, including strings, arrays, passing arrays to functions, and comparing strings. It describes declaring, initializing, accessing elements of, and performing common operations on arrays and strings in C++.

Uploaded by

Afras Ahmad
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/ 11

University of Engineering and Technology, Taxila

Experiment 1
Introduction to Data Structures : Built-in Data types & Derived Data Types

CLO-3: Use modern tools and languages.


CLO-4: Build the projects of varying complexities.
CLO-5: Demonstrate the implementation of problem solving process.
CLO-6: Work individually as well as in teams.
CLO-7: Propose a unique solution that exhibits originality of idea
University of Engineering and Technology, Taxila 1
Defination:
Data Structures are the programmatic way of storing data so that data can be used efficiently.
Almost every enterprise application uses various types of data structures in one or the other
way.

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 Definition defines a particular data with the following characteristics.

 Atomic − Definition should define a single concept.


 Traceable − Definition should be able to be mapped to some data element.
 Accurate − Definition should be unambiguous.
 Clear and Concise − Definition should be understandable.

Data Object:

Data Object represents an object having a data.

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

 Built-in Data Type e.g. Integers, Boolean (true, false),Floating (Decimal


numbers)Character and Strings.

 Derived Data Type:

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:

char str[] = "C++";

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.

Other ways of defining the string:

char str[4] = "C++";

char str[] = {'C','+','+','\0'};

char str[4] = {'C','+','+','\0'};

Like arrays, it is not necessary to use all the space allocated for the string. For example:

char str[100] = "C++";

Example 1: C++ String


C++ program to display a string entered by user.

#include <iostream>

using namespace std;

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 3

int main() {

char str[100];

cout<<"Enter a string: ";

cin>>str;

cout<<"You entered: "<<str<<endl;

cout<<"\nEnter another string: ";

cin>>str;

cout<<"You entered: "<<str<<endl;

return 0;

Example 2: C++ String

C++ program to read and display an entire line entered by user.

#include <iostream>

using namespace std;

int main() {

char str[100];

cout<<"Enter a string: ";

cin.get(str, 100);

cout<<"You entered: "<<str<<endl;

return 0;

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 4
Passing String to a Function

Strings are passed to a function in a similar way arrays are passed to a function.

#include <iostream>

using namespace std;

void display(char s[]);

int main() {

char str[100];

cout<<"Enter a string: ";

cin.get(str, 100);

display(str);

return 0;

void display(char s[]) {

cout<<"You entered: "<<s;}

Standard C++ String class

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:

using namespace std;

string my_string;

or

std::string my_string;

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 5
You can also specify an initial value for the string in a constructor:

#include <iostream>

using namespace std;

int main() {

string my_string("starting value\n");

cout<<"You entered: "<<my_string;

cout<<"You entered: "<<my_string.length();

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>

using namespace std;

int main() {

string my_string;

cout<<"please enter something : ";

getline(cin, my_string, '\n');

cout<<"You entered: "<<my_string;

String concatenation
Strings can also be assigned to each other or appended together using the + operator:

#include <iostream>

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 6
using namespace std;

void display(char s[]);

int main() {

string my_string1 = "a string";

string my_string2 = " is this";

string my_string3 = my_string1 + my_string2;

// Will ouput "a string is this"

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>

using namespace std;

int main() {

string passwd;

cout<<"enter password: "<<endl;

getline(cin, passwd, '\n');

if(passwd == "xyzzy")

cout<<"Access allowed";

else

cout<<"Access denied";

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 7

String Length and Accessing Individual Elements

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>

using namespace std;

int main() {

string my_string1 = "ten chars.";

int len = my_string1.length(); // or .size();

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>

using namespace std;

int main() {

string my_string = "ten chars.";

int i;

for(i = 0; i < my_string.length(); i++)

{ cout<<my_string[i]<<endl;

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 8

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:

type name [elements];

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:

int foo [5];

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

The initializer can even have no values, just the braces:


Engr. Rabia Arshad
University of Engineering and Technology, Taxila 9
int baz [5] = { };

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 {}:

int foo [] = { 16, 2, 77, 40, 12071 };

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.

 Index starts with 0.


 Array length is 4 which means it can store 5 elements.
 Each element can be accessed via its index. For example, we can fetch an element at
index 3 as 40.

Represent a Linear Array in memory


The elements of linear array are stored in consecutive memory locations. It is shown below:

Engr. Rabia Arshad


University of Engineering and Technology, Taxila 10

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.

Engr. Rabia Arshad

You might also like