Tuesday, 16 December 2014

Functions related to C++

The following rules are necessary while implementing functions.
1. Function Prototype
2. Function Definition
3. Function Calling
2.Function Definition:
We need to infirm the compiler that the name of function (identifier) along with arguments, return type.
The compiler checks that weather the identifier is matched or not. And then checks the arguments matching.

After identifying proper function name and function arguments, The corresponding calling function communicate with called function.
 Implicit assignment: when we are going to use function overloading( more than one function with same name but different number of arguments and different data types) method, if we pass float data type instead of int data type, Then the compiler adjusts that float value to int type for real time applications, it gives runtime bugs in ‘C’ language.
    
      C is not allowed to pass float instead of int. if we want to pass such type of arguments, we need to use type casting. The compatible data types need to use in C. But C++ language is flexible towards type casting. Automatically it adjusts data type based on functional arguments
Read More
            Scope resolution is an operator(::) which gives access to control the global variables in-spite of local variables exist.

Syntax:

                          ::variable_name;
Tell me the out of bellow program.


Compile time error. Global variable has not declared.But, You are trying to print undeclared value.







How to Create reference to Variable:


1)For existing variables we can create reference by the following syntax.


2)Variable declaration and initialization need to be at the same place.


       The following syntax is invalid. Because Variable declaration and initialization are not at the same place.
3)We can give reference to any number of variables. At this instant the addresses for all remains same.

 4) we can create reference for references(reference to reference)
5) we can’t refer the constants.
6) we can’t change references from one variable to another variable.

7)we can change address of pointer variable through references

8) Referece to pointers:

9) Array of references and array of pointers possible. But reference of references is not possible.

Example program to perform various arithmetic calculations.

Rules for reference variables:

1)we can’t change the reference.

2)declaration at one place, initialization at some other place is not possible.

        int x=100;
        int *p=&x;
        int &y=*p;        //valid
        int &y=100;      //not valid

3)array of pointers possible, array of references possible, array of addresses possible.

4)references for int,float,char is possible. References for pointers also possible

Benefits of reference variables:

1)It gives simplicity.

2) by using reference operator,we can refer address of one function variable to address of another function(calling function).

3) it is possible to make changes to the original function data by using reference operator instead of passing address of variable.
Read More

Introduction to C++| Practical approach to C++

Programming languages basically classified into two types. Those are

1)Procedural Languages(Fortran, Pascal etc)

2)Structural languages(C,C++,Java etc)


 C++ Positives compared to C:

1)C++ works on any environment

2)C++ Provides Security

3)C++ is User Friendly

4)In C, we need to change data types everywhere if we modified at initialization time. It is more time consumption one.

5)Structured Language (C) will not Support for Real Time applications

6)with the help of C++, Complex programs can be implemented in shorter way. So C++ is called feature rich language.

7)with C++ we can implement
        a) better programs
        b)Secured Programs

8) In C++ Minor changes in data types enough.

9) C++ is a Real world situation Platform. That means, Real time applications can be implemented using C++

10) Characteristics of C++

    a) Encapsulation
                Example: class ( along with structure variables, we can able to use the functions which are using those structure variables)

              Encapsulation means Binding data members and member functions using class      
         Class: Class is a collection of data members(in C variables) and member functions (in C functions).


Object Creation in C++:


 Object creation process is called instantiation 


b) Inheritance:  Creating a new class from existing class is called “inheritance”.

C) polymorphism : Names are same but functionality is different. It is called “polymorphism”.

D) Exception Handling: Runtime errors are called Exceptions. Run time problems can be handled by object oriented programming language is called “Exception Handling”.

            Example: If we try to open non-existing file, it generates an error.

E) Templates: C++ supports generic data types. Hence it is called Templates.

F) Persistence: persistence means storing results. This feature is not supported by C++. But supported by Java and .net.

            Persistence means, the previous outputs will also be stored for viewing them next time.

ADA: ADA is a language which supports encapsulation.

Review Questions:
i)                   What is application?
ii)                  What is program
iii)                What are procedural languages?
iv)                What are structural languages?
v)                  Explain differences between procedural languages and structural languages?

G)Delegation: Answer Soon

H)Abstraction:
1) if we don’t know the functionality(coding) of an application, we can’t resolve the
    problem. This concept is called abstraction.
2)Hiding unnecessary details is called data abstraction. Data/ class/ functions can be possible to abstraction(hide).

I) Message passing: passing parameters to the function is called “message passing”
How to view output of compiled code using C++?


         iostream is an object which is already created header file while implementing compiler standard. The class for cout is available in ostream itself.



Here cin is an object. Which is available in istream.

cin means data insertion or data input.

Flexibility:
What is meant by flexibility in C++?


What ever may be the data member(data type) the syntax remains same. This is called flexibility.

In C++, it is possible to access global variables inspite of local variable with same name exists.


Read More

Thursday, 11 December 2014

Program to print student information
void name(void);
void age(void);
studentdata()
{
printf("Student information\n");
name();
age();
printf("we are in main function\n");
exit(0);
}
void name(void)
{
 char ch;
 printf("enter any character between A-Z\n");
 ch=getchar();
 putchar(ch);
 if((ch>='a')&&(ch<='n'))
 {
  printf("name is MADANREDDY\n");
 }
 else
 {
  printf("name is SURESH\n");
 }
}
void age(void)
{
int n;
printf("enter value for n:\n");
scanf("%d",&n);
if((n>0)&&(n<14))
{
printf("child\n");
}
else if((n>14)&&(n<30))
{
printf("young age\n");
}
else if((n>30)&&(n<60))
{
printf("middle age\n");
}
else if(n>60)
{
printf("old age");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi studentinfo.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles studentinfo.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
Student information
enter any character between A-Z
e
ename is MADANREDDY
enter value for n:
56
middle age
we are in main function

Read More

Tuesday, 9 December 2014

C program to print array elements with references

program to print array elements with references
#include<stdio.h>
main()
{
int a[5]={1,2,3,4,5};
printf("%d\n",a[3]/a[1]);
printf("%d\n",a[6]);
printf("%d\n",a[-2]);
printf("%d\n",a[0]);
printf("%d\n",a[2]);
printf("%d\n",2[a]);
printf("%d\n",2[a]);
printf("%d\n",*(a+1));
printf("%d\n",*a+1);
}
OutPut:
2
0
4195718
1
3
3
3
2
2

Read More

C program to find sum of two arrays

program to find sum of two arrays
#include<stdio.h>
main()
{
int i,n;
int a[5]={12,23,42,11,12},b[5]={65,34,22,1,22},c[5]={0};
n=sizeof(a)/sizeof(a[0]);
//a[5]={12,23,42,11,12}
//b[4]={65,34,22,1,22}
for(i=0;i<n;i++)
{
c[i]=a[i]+b[i];
printf("a[%d]=%d b[%d]=%d c[%d]=%d\n",i,a[i],i,b[i],i,c[i]);
}
printf("\n");
}
OutPut:
adan@madan-Lenovo-G570:~/madan$ ./a.out
a[0]=12 b[0]=65 c[0]=77
a[1]=23 b[1]=34 c[1]=57
a[2]=42 b[2]=22 c[2]=64
a[3]=11 b[3]=1 c[3]=12
a[4]=12 b[4]=22 c[4]=34

Read More
program to find memory locations of array elements
#include<stdio.h>
main()
{
int a[5]={12,32,22,14,54},i,n;
int bit,num;
n=sizeof(a)/sizeof(a[0]);
for(i=0;i<n;i++)
{
printf("a[%d]=%d at %u\n",i,a[i],&a[i]);
num=a[i];
bit=31;
while(bit>=0)
{
num&(1<<bit)?printf("1"):printf("0");
bit--;
}
printf("\n");
//printf("a[%d]=%d at %u\n",i,a[i],&a[i]);
}
}
OutPut:
a[0]=12 at 4080586848
00000000000000000000000000001100
a[1]=32 at 4080586852
00000000000000000000000000100000
a[2]=22 at 4080586856
00000000000000000000000000010110
a[3]=14 at 4080586860
00000000000000000000000000001110
a[4]=54 at 4080586864
00000000000000000000000000110110

Read More

C Program to pass 2D_Array using malloc function

Program to pass 1D_Array using malloc function
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void array1d(int **ptr,int m,int n)
{
int i,j;
printf("The resultant array is..\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",ptr[i][j]);
}
printf("\n");
}
}
main()
{
int **a,rows,cols,i,j;
printf("entyer size of 2d array...\n");
scanf("%d%d",&rows,&cols);
a=malloc(rows*sizeof(int*));
for(i=0;i<rows;i++)
a[i]=malloc(cols*sizeof(int));
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
array1d(a,rows,cols);
}
OutPut:
entyer size of 2d array...
3
3
9
8
7
6
5
4
3
2
1
The resultant array is..
 9  8  7 
 6  5  4 
 3  2  1

Read More

C program to sort a 2D array

Sorting a 2D-array

#include<stdio.h>
#include<string.h>
main()
{
int a[3][3],i,j,k,n,temp;
n=sizeof(a)/sizeof(a[0]);
printf("enter 2D array....\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(" %d",&a[i][j]);
//printf("\n");
printf("array before sorting\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf(" %d ",a[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=j+1;k<n;k++)
{
if(a[i][j]>a[i][k])
{
temp=a[i][j];
a[i][j]=a[i][k];
a[i][k]=temp;
}
}
printf("array after sorting\n");
for(i=0;i<n;i++)
{
for(k=0;k<n;k++)
printf("%d ",a[i][k]);
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi sort_2D_Array.c
madan@madan-Lenovo-G570:~/madan$ cc sort_2D_Array.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter 2D array(3x3)....
9
8
7
6
5
4
3
2
1
array before sorting
 9  8  7 
 6  5  4 
 3  2  1 
array after sorting
7 8 9 
4 5 6 
1 2 3

Read More

C program to Sort an array without using strcmp

Sorting an array without using strcmp

#include<stdio.h>
#include<string.h>
main()
{
int a[5],i,j,n,temp;
n=sizeof(a)/sizeof(a[0]);
printf("enter an array of 5 elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("array before sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("array after sorting\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
OutPut:
enter an array of 5 elements:
43
32
33
12
22
array before sorting
43 32 33 12 22 array after sorting
12 22 32 33 43

Read More

C program to Sort an array by using strcmp

Sorting an array by using strcmp


#include<stdio.h>
#include<string.h>
#include<string.h>
main()
{
char a[4][10],temp[10];
int i,j,k;
printf("enter 4 strings\n");
for(i=0;i<4;i++)
scanf("%s",a[i]);
for(i=0;i<4;i++)
for(j=i+1;j<4;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
printf("sorted array is:\n");
for(i=0;i<4;i++)
printf(".....%s....\n",a[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi arr_sort.c
madan@madan-Lenovo-G570:~/madan$ cc arr_sort.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter 4 strings
welcome
madan
tech
world
sorted array is:
.....madan....
.....tech....
.....welcome....
.....world....

Read More
fseek,ftell importance through program
#include"myHeader.h"
main()
{
int pos;
char str[20]="abcdefgh";
FILE *fp;
fp=fopen("madan","r");
if(fp==NULL)
{
printf("no data\n");
return;
}
fseek(fp,4,0);
pos=ftell(fp);
printf("%c ",str[pos]);
fseek(fp,3,1);
pos=ftell(fp);
printf("%c ",str[pos]);
fseek(fp,-1,1);
pos=ftell(fp);
printf("%c ",str[pos]);
//fseek(fp,0,0);
//pos=ftell(fp);
rewind(fp);
printf("%c ",str[pos]);
fclose(fp);
}
OutPut:
e h g g

Read More

C Program to read a source file by using fgets

Program to read a file by using fgets
#include"myHeader.h"
main(int argc,char **argv)
{
char ch[20];
FILE *fp;
if(argc!=2)
printf("enter correct no of arguments\n");

fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("no file exists\n");
return;
}
while((fgets(ch,20,fp)))
printf("%s",ch);
fclose(fp);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter correct no of arguments
no file exists
madan@madan-Lenovo-G570:~/madan$ ./a.out 1.c
#include<stdio.h>
main()
{
 int ch;
printf("enter any character:");
ch=getchar();
printf("entered character:");
putchar(ch);
}

Read More

C program for Pointers with address references

Pointers with address references example
#include"myHeader.h"
main()
{
int a=10;
char *p=&a;
int *p1=&a;
float *p2=&a;
printf("%d\n",*p);
printf("%d\n",*p1);
printf("%d\n",*p2);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
10
10
2147483645

Read More

C Program to print strings through command line

Program to print strings through command line
#include"myHeader.h"
main(int argc,char **argv)
{
int i;
for(i=0;i<argc;i++)
printf("%s\n",argv[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1223 1223
1223
1223

Read More

C Program to print Binary Equivalent of a number

Program to print Binary Equivalent of a number
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int num,bit=0;
printf("enter a number\n");
scanf("%d",&num);
l1:
num&(1<<bit)?printf("1"):printf("0");
bit++;
if(bit<=31)
goto l1;
printf("\n");
}
OutPut:
enter a number
12
00110000000000000000000000000000
(for big endian machine)

Read More

C Program to print Binary Equivalent of a number

Program to print Binary Equivalent of a number
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int num,bit=0;
printf("enter a number\n");
scanf("%d",&num);
l1:
num&(1<<bit)?printf("1"):printf("0");
bit++;
if(bit<=31)
goto l1;
printf("\n");
}
OutPut:
enter a number
12
00110000000000000000000000000000
(for big endian machine)

Read More

C program for the Implementation of atoi function

Implementation of atoi function
#include<stdio.h>
int myatoi(const char *string);
int main(int argc, char* argv[])
{
int i;
for(i=1;i<argc;i++)
  printf("\n%d\n", myatoi(argv[i]));
//  getch();
 // return(0);
}
int myatoi(const char *string)
{
    int i;
    i=0;
    while(*string)
    {
         i=(i<<3) + (i<<1) + (*string - '0');
         string++;
         // Dont increment i!
    }
    return(i);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1234

1234

Read More
Dynamic Memory allocation concept Related to 1D array
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int a[4]={0},*p,i;
p=malloc(4*sizeof(int));
for(i=0;i<4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
printf(" %d ",a[i]);
}
OutPut:
1
2
3
4
 1  2  3  4

Read More

C Program to print integer equivalent of character

Program to print integer equivalent of character
#include<stdio.h>
main()
{
int n;
printf("enter any integer\n");
scanf("%d",&n);
printf("n=%d n=%c\n",n,n);
}
OutPut:
enter any integer
123
n=123 n={
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
45
n=45 n=-
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
56
n=56 n=8
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
98
n=98 n=b

Read More