In this post, we will understand the difference between one dimensional array and two dimensional array.One dimensional arrayIt helps store a single list of elements that are similar data type.The total bytes is calculates as the product of the datatype of variable array and the size of the array.C++ declarationtype variable_name[ size ];Java declarationtype variable_name [ ]; variable_name = new type[size];int [ ] a = new int [10];Two-Dimensional arrayIt helps store 'list of lists' or 'array of arrays' or 'array of one dimensional arrays', i.e nested arrays.The total bytes is equivalent to product of datatype of variable array and size ... Read More
Take two integers from the user for base and exponent and calculate the power as explained below.ExampleConsider the following for writing a C program.Suppose base =3Exponent = 4Power=3*3*3*3AlgorithmFollow the algorithm given below −Step 1: Declare int and long variables. Step 2: Enter base value through console. Step 3: Enter exponent value through console. Step 4: While loop. Exponent !=0 i. Value *=base ii. –exponent Step 5: Print the result.ExampleThe following program explains how to calculate power of given number in C language.#include int main(){ int base, exponent; long value = 1; printf("Enter a base value: ... Read More
An array of characters is called a string.DeclarationFollowing is the declaration for declaring an array is as follows −char stringname [size];For example − char string[50]; string of length 50 charactersInitializationUsing single character constant −char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char string[10] = "Hello":;Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.Finding minimum occurrenceThe logic to find minimum occurrence of a character in a given string is as follows −for(i=0; i
In this post, we will understand the difference between virtual and pure virtual functions.Virtual FunctionIt has its own definition inside the class.The base class can override a virtual function.It doesn’t have a derived class.Declarationvirtual funct_name(parameter_list) {. . . . .};Pure Virtual FunctionIt doesn’t have a definition.If a class has at least one virtual function, it can be declared abstract.The derived class has to override the pure virtual function to use it.A pure virtual function is specified by placing "= 0" in its declarationDeclarationvirtual funct_name(parameter_list)=0;Following is an example −Exampleclass Box { public: // pure virtual function virtual double ... Read More
In this post, we will understand the difference between the ‘while’ loop and the ‘do-while’ loop.while conditionThe controlling condition here appears at the beginning of the loop.The iterations do not occur if the condition at the first iteration results in False.It is also known as an entry-controlled loopThere is no condition at the end of the loop.It doesn’t need to execute at least one.Examplewhile ( condition){ statements; //body of loop }Following is the flowchart of while loop −do-while conditionThe controlling condition is present at the end of the loop.The condition is executed at least ... Read More
In this post, we will understand the difference between the ‘for’ and the ‘while’ loop.For loopThe initialization, condition checking, and the iteration statements are written at the beginning of the loop.It is used only when the number of iterations is known beforehand.If the condition is not mentioned in the 'for' loop, then the loop iterates infinite number of times.The initialization is done only once, and it is never repeated.The iteration statement is written at the beginning.Hence, it executes once all statements in loop have been executed.Examplefor(initialization; condition; iteration){ //body of the 'for' loop }Following is the flowchart ... Read More
Follow the algorithm to write a C program which enables to count the frequency of each character.AlgorithmStep 1: Define MAX size. Step 2: Declare char and integer variables. Step 3: Read the string from console. Step 4: Find length of the string. Step 5: Initialize frequency of each character to 0. Step 6: Find total number of occurrences of each character. for(i=0; i='a' && string[i]='A' && string[i]
Character can be (A-Z(or) a- z), digit (0-9), a white space, or a special symbol in C programming language.DeclarationFollowing is the declaration for character operations in C programming −char a= ‘A’; using a character constant.Character input / output functionsThe character input/output functions are explained below −Example − char a;scanf("%c", &a); printf ("%c", &a); a = getchar ( ); putchar (a); a = getch ( ); putch (a);ExampleFollowing is the C program for line counting using getchar() − Live Demo#include /* count lines in input */ main(){ int count, num; printf("enter multiple statements and Press cntrl+z:"); num = ... Read More
A union is a memory location that is shared by several variables of different data types.SyntaxThe syntax for the pointers to unions in C programming is as follows −union uniontag{ datatype member 1; datatype member 2; ---- ---- datatype member n; };ExampleThe following example shows the usage of union of structure.union sample{ int a; float b; char c; };Declaration of union variableFollowing is the declaration for union variable. It is of three types as follows −Type 1union sample{ int a; float b; char c; }s;Type 2union{ int a; ... Read More
Data structure is collection of data organized in a structured way. It is divided into two types as explained below −Linear data structure − Data is organized in a linear fashion. For example, arrays, structures, stacks, queues, linked lists.Nonlinear data structure − Data is organized in a hierarchical way. For example, Trees, graphs, sets, tables.QueueIt is a linear data structure, where the insertion is done at rear end and the deletion is done at the front end.The order of queue is FIFO – First In First OutOperationsInsert – Inserting an element into a queue.Delete – Deleting an element from the ... Read More