Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Jun 19, 2020

Strings in C++ | C-style String | String Object | Data Structure | Coding Winds

Strings in C++


Hello people, in this article we are going to study about a data structure Strings in C++ language. String is the only way by which we can set up a connection between english and programming language. Not only english, any other communicable language, for that matter.


A string is a collection of char variables in the form of arrays, this is a generic way of string implementation which is possible in C as well as in C++ language. This type of string is called C-style String. Also there exists another type of string implementation, which is an object of string class (The standard C++ ‘string’ library).


So, two implementations of strings are as follows :

  1. C-Style String

  2. Strings that are objects of string library.


C-Style String 


C-strings are arrays of type “char” terminated with a null character, i.e., ‘\0’. 


Defining a string : 

char str[] = “C++”;


Alternate ways of defining a string :

char str[4] = “C++”;

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

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


It is not necessary to use all the space allocated during the declaration, as we need to do in array : 


        char str[100] = “C++”;


Example of a program which reads a single word from the user :



                            Input :
                                    C++
                                    Programming is life
                            
                           Output :
                                    C++
                                     Programming

Example of a program which reads a line from the user :
                

                            Input :
                                    Programming is life
                            
                           Output :
                                    Programming is life


To read the text containing blank space, “cin.get” function can be used. This function takes two arguments.

First argument is the name of the string (address of first element of string) and second argument is the maximum size of the array.

String Object

In C++, you can also create string objects for holding strings.

Unlike using “char” arrays, string object has no fixed length, and can be extended as per requirement.

An example declaration of string using string data type is given below, here  string(str) is declared using string object. Here we ask user to enter the string.

Instead of using “cin>>” or “cin.get()” function, you can get the entered line of text using “getline()”.

“getline()” function takes the input stream as the first parameter which is “cin” and “str” as the location of line is to be stored.

We are coming with a series of posts regarding various data structures and their implementations. Do subscribe to our daily blog update by clicking here.


We want to acknowledge, Reema Thareja Ma'am, her book Data Structures Using C  helped us alot during our research for this article, also this is the best one, you should go for.


Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via instagram "@coding.winds".


                                


Jun 16, 2020

Passing Arrays to Function | Arrays | Coding Winds | We care about the future of future

Passing Arrays to Function

Hello people in this blog we are going to read about the various ways we can pass an array or it’s elements as an argument in a function :

         1.   Passing an element of an array

Passing a single element of an array as an argument to a function,

   

     2.    Passing address of an element of an array

Passing the address of a single element of an array as an argument to a function,

       

        3.   Passing the entire array

Passing an entire array as an argument to a function;


       4.   Passing the entire address

Passing an entire array with their address as an argument to a function;



We can also create array of variable size with the help of Dynamic Memory Allocation, you can look here how to do that. 

We are coming with a series of posts regarding various data structures and their implementations. Do subscribe to our daily blog update by clicking here.


We want to acknowledge, Reema Thareja Ma'am, her book Data Structures Using C  helped us alot during our research for this article, also this is the best one, you should go for.


Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via instagram "@coding.winds".


We also have an article on space complexity and time complexity , do give them a read.



Array in C/C++ | Data Structures | Merging of two arrays | Coding Winds

    Arrays

Hello people, in this article we are going to study about a data structure.

To study this, we need to consider a situation when we have 10 students in a class and we need to read and write marks of 10 students. So in generic way we need to declare 10 integer  variables, for example.

  int marks1, marks2, marks3, marks4, marks5, marks6, marks7, marks8, marks9, marks10;

And for scaning and printing the values of these variables we need to use “cin>>….” And “cout<<…” for ten ten different times.

But is it possible to do so when we have a large number of students in a class, e.g, 500 or 1000. No, So in that case we use arrays.

An array is a collection of similar data elements. These data elements have the same data type. The elements of the array are stored in consecutive memory locations and are referenced by an index(also known as subscript). The subscript is an ordinal number which is used to identify an element of the array.

DECLARATION OF ARRAYS

Declaraing of array requires following three specifications;

1.    DATA TYPE : The kind of values it can store, e.g., int, char, float, double.

2.    NAME :  to identify the array

3.    SIZE : the maximum number of values that the array can hold.

Syntax for declaring arrays :

                                    type name[size];

the size indicates the number of elements that can be stored in an array.

If the statement is

 int marks[10];

In C/C++ array indexing starts from zero. The first element will be stored in marks[0], second element will be stored in marks[1], and so on. Therefore, the last element, that is the 10th element will be stored in marks[9].

These 0,1,…10 written within the brackets are indexes.

Here, 0 is the lower bound and 9 is the upper bound of this array.

 

Different ways of declaring arrays :

1.    Array declaration by specifying size :

int arr[10];

2.    Array declaration by initializing elements :

int arr[] = {10,67,90,87,12};

3.    Array declaration by specifying size and initializing elements :

int arr[5] = {12,15};

                                                Here, arr[2] = arr[3] = arr[4] =0.

 

Q.) Calculate the address of marks for marks[4] in the

marks[] = {99,87,54,21,36,54}

       if the base address = 1000, in the 64-bit machine.

Ans.  The base address of the marks[4] = 1000 + 4 *(4-0)

                                                                           = 1016

Accessing the elements in an Array

            To access any particular array we usually call them with the name of array and there index as a subscript. E.g., Arr[0], Arr[1], Arr[2],etc.

For accessing each and every item of an array we need to use loops, there is no such function, with the help of which we can print and write for each and every element of an array.

 

            For Example –

For scanning ‘n’ number of elements of an array, we use ‘for’ loop like this

                        for(i=0;i<n;i++)

                                    {

                                                scanf(“%d”,&arr[i]);

                                     }

For printing ‘n’ number of elements of an array, we use ‘for’ loop like this

                        for(j=0;j<n;j++)

                                    {

                                                printf(“%d”,arr[i]);

                                    }

Now have a look at the complete program to understand these two implementations

 

 

MERGING OF TWO ARRAYS

14

23

58

46

- Array 1  


45

56

25

78

12

- Array  


14

23

58

46

45

56

25

78

12

- Array 3   

 



We can also create array of variable size with the help of Dynamic Memory Allocation, you can look here how to do that. 

We are coming with a series of posts regarding various data structures and their implementations. Do subscribe to our daily blog update by clicking here.


We want to acknowledge, Reema Thareja Ma'am, her book Data Structures Using C  helped us alot during our research for this article, also this is the best one, you should go for.


Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via instagram "@coding.winds".


We also have an article on space complexity and time complexity , do give them a read.