Dsa 01
Dsa 01
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 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.
• 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
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).
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:
int main() {
char str[100];
cin>>str;
cout<<"You entered:"<<str<<endl;
cin>>str;
return 0; }
#include <iostream>
int main() {
char str[100];
cin.get(str, 100);
return 0;
int main() {
char str[100];
cin.get(str, 100);
display(str); return 0;
Declaring a string:
using namespace std;
string my_string;
or
std::string my_string;
You can also specify an initial value for the string in a constructor:
#include <iostream>
}
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>
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>
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";
}
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>
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;
for(i = 0; i < my_string.length(); 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
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.
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.