Posts

Showing posts from September, 2025

Pointers in c++

              A variable which holds the address of a memory location is called a pointer. It deals with memory. To create a pointer variable, follow the syntax. ‘datatype *variable_name;’ Eg: ‘int *ptr;’   C++ Program: This program includes the built-in header file <iostream>. Next main() is defined. It has following steps: A variable a is declared as integer. It reads the user input from command prompt. A pointer variable *ptr is declared. #include <iostream> using namespace std; int main() {     int a;//Declaration of integer variable     int *ptr; // Declarartion of pointer variable     // getting the input from user     cout<<"Enter the variable:"<<endl;     cin>>a;     // storing address of a in pointer myptr     ptr = &a;     cout << "Value o...

Functions in C++

                “A set of statements that performs a specific task” is called a function. Generally, the code is reusable in anywhere. Features: ·        Easy to read. ·        It has modularity. ·        The maintenance of code is easy. First, consider about the syntax of creating a function. Syntax: Function declaration: ‘return_value function_name(datatype parameter1,…datatype parameter); Eg: int factorial(int n); Function Definition: return_type function_name(parameter1,parameter2,…… parametern) { Code; return(); } Eg: int factorial(int n) {   Code… return(); } The functions can be classified into two types. ·        User defined functions: user can create this function. ·        Built-in functions: these are the system files. Example:...

String Operations in c++ part3

 This post shows the remaining string operations as follows. Substring extraction Searching an element Other String operations using built-in functions Substring extraction:               It extracts the part of string. This program reads a character from the user. Using the ‘substr()’ function,it extracts a substring and display it in the output screen. C++ Program: #include <iostream> #include <string> using namespace std; int main() {     string st1 = "C++ Programming";     string sub_str1 = st1.substr(4, 14);        cout << "Substring 1: " << sub_str1<< endl;     string sub_str2 = st1.substr(0, 3);       cout << "Substring 2: " << sub_str2 << endl;     return 0; } Output: Substring 1: Programming Substring 2: C+...

String Operations in C++ part2

               Part1 includes the basic functionality of strings. This post explains the string operations for the following functionalities. Length of a String Modification of a string Length of a String:               This function finds the number of characters in the String. It uses two method. One is ‘size()’ and second one is ‘length()’ . Program: #include <iostream> #include <string> using namespace std; int main() {     string st1 = "C++ Programming";     //first method (size())     cout << "Length of the string using size(): " << st1.size() << endl;     // Second method (length())     cout << "Length of the string using length(): " << st1.length() << endl;     return 0; } Output: ...

String Operations in C++

               Strings are a special kind of objects in std::string class. It is a collection of characters in a common name. Features: The manipulation is easy. You can access, concatenate strings, extract a substring and compare them quickly. It has many built-in functions. You can add or remove characters according to the user need. It adopt it effectively. How to create and manipulate Strings? First, include header file #include<string> Next, declare a string as follows.. Syntax: ‘string str;’ Where string is the keyword to represent string. ‘str’ is the variable. Eg: ‘string a;’ C++ Program to print string: #include <iostream> #include <string> using namespace std; int main() {     string a="Welcome to C++";     cout<<a;     return 0; } Output: Welcome to C++ The operations of strings are given below. ·    ...

Built-in functions of std::array

             The built-in header file <array> has variety of member functions. The way of using these functions in c++ programs as follows… ‘size()’:               This function finds of the size of an array. C++ Program:   #include <iostream> #include <array> using namespace std; int main() {     std::array<int, 8> x = {1, 2, 3, 4, 5,6,7,8};     size_t size = x.size();     cout<<"the size of array is:"<<size<<endl;     return 0; } Output: the size of array is:8 ‘at()’:               This function finds an element in a position. Sample c++ code: #include <iostream> #include <array> using namespace std; int main() {     int pos,i;    ...

Built in functions in array -part2

 This post contains two built in functions std::accumulate() and std::find(). 3. std::accumulate():               This function is used to find the sum of all the elements in the array. C++ Program: #include <iostream> #include <numeric> using namespace std; int main() { int i,a[10],sum=0,n; cout<<"Enter the n value:"<<endl;     cin>>n;     cout<<"Enter the array elements:"<<endl;     for(i=0;i<n;i++)     {     cin>>a[i];     }     cout<<"The original array is:"<<endl;      for(i=0;i<n;i++)     {     cout<<a[i]<<endl;     }     sum = std::accumulate(a, a + n, 0);     cout<<"the sum of all the eleme...

Built-in functions and Library utilities in arrays:

                Built-in functions and library utilities are predefined in standard files. It mainly manipulates arrays in terms of sorting, searching and so on. The standard functions are given below. Header file : <algorithm> and <numeric> Functions: ‘std::sort(array,size)’ – this function sorts the array in ascending order. ‘std::reverse(array,size)’ – it reverses the array. ‘std:: accumulate(array, array + size, 0)’ – it adds the array elements together. ‘ std::find(array, array + size, value)’- it finds a particular element in the array. ‘ std::binary_search(array, array + size, value)’- it uses the binary search to find an element. ‘ arr.size()’ -it gives you the size of the array. ‘ arr.at(2)’- a particular element is accessed. ‘ arr.fill(1)’- it fills 1 for all elements. ‘ arr.swap(arr2)’ – it swaps the element between two arrays. ‘sizeof(array)’ – it gives the size of the array. ‘memset(array...

Arrays in c++

              Array is a collection of similar data elements. It has a sequential storage starting from location 0 to n. A single dimensional array is explained here. Here is the syntax of array Declaration: Datatype variable_name[size]; Eg : int a[10]; How to get inputs for array? You can directly assign values as input. a[5]={1,2,3,4,5}; using a for loop, you can get it from the user, eg: int a[5]; for(i=0;i<n;i++) { cin>>a[i]; } How to access the elements?   Element starts from 0 th location to n. Eg: a[0] is the first element. a[1] is the next element. likewise, its going on. Let us create a c++ program to display ‘n’ number of elements in an array. C++ Program to display the array elements: #include <iostream> using namespace std; int main() {   int x[10],n,i;   cout<<"Enter the n value:"<<endl;   cin>>n;   cout<<"Enter the elements...

Jump statements in c++

               These are the statements which change the flow of execution. Mainly, it transfers the code flow into another part of the program. For example, in switch case, it uses break. The list of jump statements is given below... 1.       ‘break’ 2.       ‘continue’ 3.       ‘return’ and ‘goto’ Let us create programs using these statements. 1.‘break’ statement:               This statement is used to make the program stop the current flow. C++ Program: #include <iostream> using namespace std; int main() {    int a=2;    switch(a)    {        case 1: cout<<"The value is one";break;        case 2: cout<<"The value is two";break;        ...

Loops in c++ with examples

             Loops make the set of statements executed until a specific condition met. The types of loops in c++ are given below.. ·        ‘for loop’ ·        ‘while’ loop ·        ‘do -while’ loop Each and every loop is illustrated with an example. ‘for loop’:               Most common type of loop is for loop. It has following syntax. ‘for(initialization; condition; increment)’ Eg: ‘for(i=0;i<n;i++)’ C++ Program: #include <iostream> using namespace std; int main() {    int i,n;    cout<<"Enter the number of elements:"<<endl;    cin>>n;    cout<<"The elements are:"<<endl;    for(i=1;i<=n;i++)    {        cout<<i<<endl...

Conditional Statements in c++: Switch

               When you turn on the switch, the appliance started. In  the same way, using your switch case, you can control your variables. First, the syntax of the switch case is given below… switch( expression) {   case value1 : statements;                      break;   case value2 : statements;                      break;   ………………………………..     ………………………………..   default : statements; } Key terms: ‘expression’ : It is a condition or variable which makes the switch as conditional statement. ‘case’ : it is a keyword to identify the choice value. ‘break’ : it is also a keyword which makes a particular block to end after the statement execution. ‘default’ : If none of the value is applicable for ...

Conditional Statements in c++: if-else

              Conditional statements are the decision-making statements which makes the program to flow in a direction based on the inputs. In c++, the list is given below…. 1.       If-else 2.       Switch 3.       Ternary operator Each one is explained with an example. ‘if-else’:               The most commonly used conditional statement is ‘if-else’. It has the following syntax.. ‘if’ statement: If(condition) { ………………………………. } C++ Program: #include <iostream> using namespace std; int main() {     int a;     cout <<"Enter the number"<<endl;     cin >> a;          if (a>1)     {         cout<<"a is greater than 1...