Skip to main content

Posts

Showing posts from July, 2017

Pointers

Pointers: A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Syntax: type * variable ; Interpretation: The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type. Dynamic Memory Management: C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type. The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management. Syntax: int *num = (int *)malloc(sizeof (int)*numCount); or int *ptr = (int *)calloc(numCount, sizeof (int)); /* returns a pointer to a section of memory just large enough to hold the integers, whose q...

Strings

STRINGS: A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, / and $. String literals or string constants in C are written in double quotation marks. STRING DECLARATION : A string in C is implemented as an array, so declaring a string variable is the same as declaring an array of type char. Syntax: char var[9] = {'S','T','R','I','N','G','S','!'}; OR char string_var[9] = "STRINGS!"; INTERPRETATION: The variable var will hold strings from 0 to 8 characters long. The variable string_var will hold strings from 0 to 8 characters long NULL CHARACTER (‘\0’) : Null character marks the end of a string. All of C’s string handling functions simply ignore whatever is stored in the cells following the null character. When defining a character array to contain a string, the array must be large enough to store the stri...

Structure

STRUCTURE: A structure is a collection of variables of different types under a single name or in other words structure is a user defined data type. Structures can be used like any other variable. We can make a struct array or used as pointers. SYNTAX: struct structure_name{ type1 id_list1 ; type2 id_list2 ; . . . typen id_listn ; } struct_type ; EXAMPLE: struct number{ int num; char name[10]; }; STRUCTURES WITH FUNCTIONS: • Stucture variables are passed by value by default. • To pass a structure by reference, pass its address. Arrays of structures—like all other arrays—are automatically passed by reference. • To pass an array by value, create a structure with the array as a member. Structures are passed by value, so the array is passed by value. Implementation of Structure in C: #include<stdio.h> #include<string.h> //structure declaration struct employee { char name[25]; int id_number; int age; float salary; } emp1 = {"Dummy1",1,24,2...

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Fundamentals of Programming

Fundamentals of C and C++: Every Programming Languages have some basic rules that should be followed to become a good programmer. Some of them are as follows. Ru les for Declaring Variable Names: Variable name can be any combination of 1 to 31 alphabets, digits or underscore. The first character of variable name should be an alphabet. Commas, blanks or any other special symbols are not allowed in variable name.   C/C++ Keywords: int    double    auto    char    if    else    long    switch    case    enum    register    typedef    struct    extern    return    union    const    float    short    unsigned    continue    for    signed    void    default    goto    sizeof    volatile    do ...

Conditional Statement

Conditional Statement: A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch. if-else Statement: Syntax: if (condition 1) { } else if (condition 2) { } else { }   Implementation of If-else statement in C: Q: Write a program to find the greatest of three numbers. #include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { pr...

Loops

LOOP : A loop is a sequence that gets executed several times. The ability to specify repetition of a group of instructions is an important problem-solving tool. TYPES OF LOOPS: For loop Statement. While loop Statement. Do-while Statement. for Loop : The for loop consist of three parts 1-  The initialization: Set the loop control variable assignment. 2-  The condition: A relational expression that determines when the loop exits. 3- The increment: Defines how the loop control variable changes each time the loop is repeated. Syntax: for (initialization expression ; loop repetition condition ; update expression ) { Statement_Block; } INTERPRETATION: First, the initialization expression is executed. Then, the loop condition is tested. If it's true, the statement is executed, and the update expression is evaluated. Then the loop repetition condition is retested. The statement is repeated as long as the loop condition is true. When this condition is...

Functions

Functions: A function definition as follows: return_type function_name( parameter ) { body of the function } Return Type:  A function may return a value(int or float or char). The return_type is the data type of the value the function returns. Some functions does not returning a value. In this case, the return_type is void. Parameters:  A parameter is like a placeholder. When a function is called, you pass a value to the parameter. This value is referred to as parameter or argument. Parameters are optional; that is, a function may contain no parameters. Function Body:  The function body contains a collection of statements that define what the function does.   Calling of a Function : To use a function, it must be called in main. When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it...

Inheritance (OOP)

Inheritance: In object-oriented programming,  inheritance  means an object or class has inherited behaviors from another object  or class. A child class is inherited from parent class. A child inherits characteristics of its parents, besides  inherited characteristics, a child may have its own unique characteristic. . In inheritance, every object of a child class is also an object of parent class, but not vice-versa. Base class: It is the class from which features are to be inherited into another class. Derived class: It is the class in which the base class features are inherited. A derived class can have additional properties and methods not present in the parent class that distinguishes it and provides additional functionality. REAL WORLD EXAMPLE: Let’s consider the Windows operating system. Windows 98 had certain properties and methods that were used in Windows XP. Windows XP was derived from Windows 98, but it was sti...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step

Object Oriented Programming (OOP)

Object Oriented Programming: Object Oriented Programming (OOP) is a programming concept which used in the modern programming world. Languages like Java, C++,  and Python support Object Oriented Programming (OOP). It works on the principle that objects are the most important part of a program. In OOP we think in terms of objects and every object has its attributes (properties) and a state (behavior/functions). Object Oriented Programming (OOP) is a technique of system modeling and its main purpose is to understand the product before developing it and manipulating these objects to achieve a specific task.   Pillars of Object Oriented Language (OOP): There are four basic principles of Object Oriented Language (OOP). Inheritance Polymorphism Data Encapsulation Abstraction WHY is Object Oriented Language (OOP) NEEDED? Problems with Procedural Languages: Functions have unrestricted access to global data Unrelated Functions and data. Before Object Oriente...

2D Array in Cpp (c++)

2D-Array: 2D array is similar to a matrix of mxn (row into column). where every element has a specific index which is usually denoted by i and j. i represent the number of rows and j represent the number of columns.   Declaration of 2D Array in C++ int A[5][4];                                               // 5 rows and 4 column Initialization Of Array: An array initializer for a 2D array contains the rows of A, separated by commas and enclosed in braces. Each row, in turn, is a list of values separated by commas and enclosed in braces. int A[5][4] = { { 1, 2, 3, 4 }, { -44, 65, 4, 23}, { -45, -1, 3, 34} { -45, -1, 3, 34} { -45, -1, 3, 34} }; There are three-dimensional, four-dimensional, and even higher-...

Stack Data Structure in Cpp (c++)

  What is Stack? Stack refers to an abstract data types (ADTs) that is a collection of elements, with two principal operations that are addition and removal of elements from the collection. It works on the principle of LIFO (last In first out). It is also considered as a linear data structure.   Example: A set of books. It easy to take an item off the top rather from the middle. Basic operations of Stack: Push: Add item to the list. Pop: Removes item from the list.   Advantages: Stack is faster than heap memory allocation (also known as Dynamic Memory Allocation). Stack memory is automatically reclaimed when the function exits, which can be convenient for the programmer if the data is no longer required. Therefore, it is suitable for temporary data, where memory is not needed after the execution of a function. Implementation Of Stack in C++: #include<iostream> using namespace std; class StackMemory...