Saturday, 18 April 2020

C++ Array


Array:-       
               An Array is a collection of multiple values of same type at a contiguous memory locations. In an array we can collect the data. For example if we need to store a class data then we can use an array. If there are 26 students in mechanical branch and we have to store marks of these students then we can create an array of size 26 of same data type i.e int, float, double, char etc.

Declaring Array:-    An array can be declare as follow  :-
  
return type arrayname [aray size];

Int mech[26];

Here Data type :- int
         Array name:- mech
         Size:-26
Here we declare an array of name mech where data type is of integer (int ) and total size is 26.

Initialization of array:- Array can be initialize during declaration of array (without loop) as follow:-
  
Int mech[5]={20,40,50,70,80};

In an array data can be store through an index ,according this first element start from 0 to n-1 if the total size is n.
mech[0]= 20
mech[1]= 40
mech[2]= 50
mech[3]= 70
mech[4]= 80

Accessing array element:-

As we know array elements can be access through index.
If we need to input the data in an array ( int mech[5] ) then there is a loop  :-

for(i=0;i<=4;i++)
{
cin>>mech[i];
}

If we need to display the data in an array ( int mech[5] ) then there is another loop  :-

for(i=0;i<=4;i++)
{
cout>>mech[i];
}

In an array we can also input and output or display the data individually. For example

cin>>mech[2]

that mean we are going to input the value in 3rd element of array mech.

cout<<mech[4]

that mean we are displaying the data of 5th element.   


Thursday, 17 November 2016

Array with structure


      Structure may consist of one or more arrays as its elements. As we know when we need to store list of elements, then we can use array. In this case the elements of structures of a specified type.

Example of arrays of structure:

struct student
{

int marks[6];

long rollno;

} s;

In above example structure student it contains one arrays as structure member marks. Where it can store the marks of 6 students.

Program:- 





























Output:






Write a program to calculate area of circle with structure and function

Coding:-
                      Structure can be used with the functions in c++ by passing whole structure as an argument to a function.









Output:- 
   




Saturday, 5 November 2016

Operators in C++

Operator:- 
               An Operator is a symbol that specifies an operation to be performed.These operators are used in different combinations to form expressions. For example "+ " is a addition operator that add two digits. The data items on which the operators act upon are called operands.

There are a list of operators in c++ programming language.

- Assignment operator.
- Arithmetic operator.
- Relation operator.
- Logical operator.
-Increment - Decrement operator.


1:- Assignment operator:-

                                                    " =" symbol is known as assignment operator. Assignment operator is used to assign a value to any variable.For example. " a=5". mean 5 is assign to a variable or value of a is now 5.

2:- Arithmetic operator :-   
       
                                            Arithmetic operator are used to perform arithmetic operation. simple mathematical operation comes under this operator like subtraction, addition, multiplication, and division. Various symbol under arithmetic operation are:-


         
Symbol
Meaning
Example
+
Addition
a+b  (2+2)
-
Subtraction
a-b   (2-2)
*
Multiplication
a*b  (2*2)
/
Division
a/b  (2/2)
%
Modulus/ Reminder
a%b (2%2)


  These are five symbol under arithmetic operator in which "+" symbol is used to add to operands like in example  [2+2 =4]. Subtraction "-" used to subtract two operands for example [2-2=0]. Multiplication [*] used to multiply two operands for example [2*2= 4]. Division [/] is to divide two operands for example [2/2=1].Modulus or reminder operator "%" provides reminder. for example [2%2=0].  

3:-Relation Operator:-
                                        The relation operator is used for comparison between two or more operands.It check the relation between two operands.




Symbol
Meaning
Example
< 
Less then
a<b
> 
Greater then
a>b
<=
Less then equal to
a<=b
>=
Greater then equal to
a>=b
==
Equal to
a==b
!=
Not equal to
a!=b




4:- Logical operator:- 
                                        The logical operator are used to compare the one or more Boolean expression and return the result in Boolean. C++ provide following Logical operator:-

                                 &&              (AND)

                                   ||                 (OR)

                                   !                 (NOT)

                 The result of the comparison is either TRUE (1) or FALSE (0).

5:-Increment and Decrement operator:-

                                                                         In c++ , the other unary operator are ++(increment operator) and --(Decrement operator). These operates only one one operands. the ++ and -- operators here have been written after the variable they apply to, in which case they are called the post increment  and post decrement operators. there are also pre increment and pre decrement operator.       
                     Increment operator (++) increase the value of variable by 1.
                     Decrement operator(--) decreases the value of a variable by 1.
                                         






















Friday, 28 October 2016

Write a program in c++ to read and write in structure.

Coding:- 
               
#include<iostream.h>


struct ank
{
long int sal;
long int age;

}  var ;

main()
{

cout<<" enter salary of ank =";
  cin>>var.sal;
cout<<"what is the age of ank =";
  cin>>var.age;

  cout<<" salary of ank = "<<var.sal;

  cout<<"\nage of ank ="<<var.age;


}

Output:-




            

Write a program in c++ to read and write in structure .


Coding:-
#include<iostream.h>


struct ank
{
long int sal;
long int age;

};

main()
{
struct ank var;

cout<<" enter salary of ank =";
  cin>>var.sal;
cout<<"what is the age of ank =";
  cin>>var.age;

  cout<<" salary of ank = "<<var.sal;

  cout<<"\nage of ank ="<<var.age;


}


Output:-

                                         



Structure in c++:-

Structure:- 
                  Structure is a collection variables of different data items under a common name .Some can be  int, some can be float, some can be char and so on. In this case the data item of structure is called member of that structure.
           
                  The difference between array and structure is that array is a collection of same type elements that are referred by a common name but in case of structure element of structure can be of different type.

Declaration of Structure:-
   
     Syntax:- 
                        struct structure name
                   
                                                {

                                       data- type 1 member1;
                                       data- type 1 member2;
                                       data- type 1 member3;
                                       -------------------
                                       -------------------
                                       data-type n member n;
                             };
example:-
                               struct student                  
                                                {

                                       char name[10];
                                       int rollno;
                                       char branch[10];
                                       int sem;  
                             };

Creating structure variable:- 

                                   If you need to access the data member of Structure , so you have to declare a structure variable. That can access data member of structure. There are various ways to access these member functions.We can create more then one structure variable.
 
I)                               We can create structure variable at the time of declaration. Structure variable(s)  can be declared after defining the structure type.

                           struct structure name
                   
                                                {

                                       data- type 1 member1;
                                       data- type 1 member2;
                                       data- type 1 member3;
                                       -------------------
                                       -------------------
                                       data-type n member n;
                             } structure variable(s);

Example:-
                     
                               struct student                  
                                                {

                                       char name[10];
                                       int rollno;
                                       char branch[10];
                                       int sem;  
                             }var ;        

Program:-

Write a program in c++ to read and write in structure 


II) We can also create a structure variable with in a main function.

Example:-
                         
                          struct student                  
                                                {

                                       char name[10];
                                       int rollno;
                                       char branch[10];
                                       int sem;  
                             } ;

                      main()

              {
                       struct student var;
                            ----
                             -------
                      }
Program:-

Write a program in c++ to read and write in structure 



Accessing Structure Elements:-
                         
To access member of structure we have to use the . (dot) operator.

 syntax:-
                     structure_variable_name.member

- If you need to read a statement from keyboard then we have to write down the following statement:-
     cin>>var.rollno;

In which var is a structure variable and rollno is a member of structure as we declare above. Remember the .(dot)operator between structure variable and member of structure.

- If you need to print a statement then write down the following statement :-

   cout<<var.rollno;

 In which you can see that it will print rollno that you entered before.