Posts

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...

6.Ternary operator and 7. Bitwise Operators

 6.Ternary operator:               It is a conditional operator(?:) which minimalize the conditional statements.it is easily inserted between code. Syntax: Symbol: ‘?:’ (condition)? statement: else statement; (x > y)? x: y; C++ program: which is greater one? #include <iostream> using namespace std; int main() {     int a, b;     cout<<"Enter the two numbers" <<endl;     cin>> a >> b;         // Using ternary operator     int g_val = (a > b) ? a : b;     cout << "The biggest of two numbers is: " << g_val;     return 0; } Output: Enter the two numbers 45 56 The biggest of two numbers is: 56 7. Bitwise Operators:               If you want to...