26.6.20

c program dynamic memory management


DYNAMIC MEMORY MANAGEMENT
void main()
{
    int *a,n,i;

    printf("How many numbers do u want to enter ");
    scanf("%d",&n);

    a=(int*)malloc(n*sizeof(int));
    printf("Enter numbers:");
    for(i=0;i<n;i++)
        scanf("%d",a+i);

    printf("the entered numbers are\n");
    for(i=0;i<n;i++)
        printf("%d\n",*(a+i));
    free(a);
}
OUTPUT
 How many numbers do u want to enter 5
Enter numbers

78
956
324
8562
0159

Entered numbers are
78
956
324
8562
159

c program using arrays


USING ARRAYS:
void main()
{

    int a[100],n,i;

    printf("How many numbers do u want to enter:");
    scanf("%d",&n);

    printf("Enter numbers:");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    printf("The entered numbers are\n");
    for(i=0;i<n;i++)
        printf("%d\n",a[i]);
}
OUTPUT:
How many numbers do u want to enter:4
Enter numbers:23
45
67
21
The entered numbers are
23
45
67
21

call by address example 2


CALL BY ADRESSS EXAMPLE 2
void changeMe(int*a)
{
    *a=72;
}
void main()
{
    int a=145;
    printf("Before :%d\n",a);
    changeMe(&a);
    printf("After : %d",a);
}
OUTPUT
Before:145
After:72

call by address /reference example 1


CALL BY ADDRESS/REFERENCE EXAMPLE 1:
void swap(int*a,int *b)
{

int t;
t=*a;
*a=*b;
*b=t;

}
void main()
{
    int a=10,b=20;
    printf("Before swapping : %d,%d\n",a,b);
    swap(&a,&b);
    printf("After swapping : %d,%d\n",a,b);

}
OUTPUT
Before swapping 10,20
After swapping 20,10

call by value


CALL  BY VALUE:
void swap(int a,int b)
{

int t;
t=a;
a=b;b=t;

}
void main()
{
    int a=10,b=20;
    printf("Before swapping : %d,%d\n",a,b);
    swap(a,b);
    printf("After swapping : %d,%d\n",a,b);

}
OUTPUT
Before swapping : 10,20
After swapping :10,20
Explanation :the a,b in main() and swap() functions are different....
So the changes made to a and b in the swap() function has no affect on a and b of main().

c program on arthmetic example 2


POINTER ARTHMETIC EXAMPLE 2
void main()
{
    int a=10,b=20;
    int*p=&a,*q=&b;
    printf("%u,%u\n",p,q);

    printf("%u\n",p+2);
    printf("%u\n",p-2);
   /* printf("%u\n",p*2);  */

    printf("%u\n",q-p);
    /*printf("%u\n",p+q); illegal */
}
OUTPUT
6356724,6356720
6356732
6356716
4294967295

c program for pointer arthmetic


POINTER ARTHMETIC
void main()
{
    int a=15;
    char ch='x';
    float f=9.7;

    int*p=&a;
    char*q=&ch;
    float*r=&f;

    printf("Before incremention:%d,%c,%f\n",a,ch,f);
    a++;
    ch++;
    f++;
    printf("after incremention:%d,%c,%f\n\n",a,ch,f);

    printf("Before incremention:%d,%c,%f\n",p,q,r);
    p++;
    q++;
    r++;
    printf("after incremention:%d,%c,%f\n",p,q,r);


}
Before incrementation: 15,x,9.7
After incrementation: 16,y,10.7
Before incrementation:100,200,300
After incrementation: 102,201,304

C program for the necessary of different pointers


WHY DIFFERENT TYPES FOR POINTERS
void main()
{

int a=4873;
char ch='p';
float f=2173.964;

int *p;
p=&a;
printf("%u\n",*p);

p=&ch;
printf("u\n",*p);

p=&f;
printf("%u",*p);
}
OUTPUT
4873
garbage
garbage                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

c program for address of and value at operators example 2


ADDRESS OF AND VALUE AT OPERATORS EXAMPLE 2
void main()
{
    int a=35;
    int*p;
    int**q;

    p=&a;
    q=&p;

    printf("%u\n",p);
    printf("%u\n",q);

    printf("u\n",*q);
    printf("u\n",*p);
    /* printf ("%u\n",*a); illegal */
    printf("%u\n",**q);
  /*  printf("%u\n",**p); illegal */
    printf("%u",*&p);

}
OUTPUT
6356728
6356724
u
u
35
6356728

C program for size of a pointer


SIZE OF A POINTER
void main()
{
    int a=10;
    char ch='x';
    float f=9.7;

    int *p=&a;
    char*q=&ch;
    float*r=&f;

    printf("%d,%d,%d\n",sizeof(a),sizeof(ch),sizeof(f));
    printf("%d,%d,%d",sizeof(p),sizeof(q),sizeof(r));

}
OUTPUT
4,1,4
2,2,2

c program for address of and value at operators


ADDRESS OF AND VALUE AT OPERATORS
void main()
{
    int a=35;
    int *p;
    p=&a;
    printf("%u\n",a);
    printf("%u\n",&a);
    printf("%u\n",p);
    printf("%u\n",&p);
    printf("%u\n",*p);
}
OUTPUT
35
6356732
6353732
6357328
35                                                                                         

c program for to print an address


HOW TO PRINT AN  ADDRESS
void main()
{
    int a=10;
    printf("%d\n",a);
    printf("%u",&a);

}
OUTPUT
10
6356732

19.6.20

c global variable


GLOBAL VARIABLE:
int i=78;
void fun1()
{

    printf("%d\n",i);

}
void fun2()
{

    int i= 45;
    printf("%d\n",i);
}
void main()
{

    printf("%d\n",i);
    fun1();
    fun2();
}
OUTPUT:
78
78
45

c register storage class specifier


REGISTER STORAGE CLASS SPECIFIER
void main()
{

    register int i;
    for(i=1;i<=5;i++)
        printf("I LOVE INDIA.\n");

}
OUTPUT
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.
I LOVE INDIA.

c static storage class


STATIC STORAGE CLASS :
void fun()
{

    static int a=10;
    printf("%d\n",a);
    a++;

}
void main()
{

    fun();
    fun();
    fun();
    fun();
}
OUTPUT:
10
11
12
13

c auto storage class


AUTO STORAGE CLASS
void fun()
{

    int a=10;
    printf("%d\n",a);
    a++;

}
void main()
{

    fun();
    fun();
    fun();
}
OUTPUT
10
10
10

c macro: where we can access a variable


WHERE WE CAN ACCESS A VARIABLE...
void main()
{

    int a=10;
    printf("%d\n",a);
    {

        int b=20;
        printf("%d,%d\n",a,b);

    }
 printf("%d,%d",a,b); /*error: b not accesible */
}
EXPALNATION:
A variable defined in a block can be accessed in that block and all its inner blocks (i.r., a variable defined in a block cannot be accessed in its outer block).It is called as the scope of a variable .

c macro :where to define a variable


WHERE TO DEFINE A VARIABLE
void main()
{

    int a=10;
    printf("%d\n",a);
    {

        int b=20;
        printf("%d",b);
    }
}
OUTPUT:
10
20

c program enclosing the variable in the macro expansion


IMPORTANCE OF ENCLOSING THE VARIABLES IN THE MACRO EXPANSION WITHIN PARENTHESIS
#include<math.h>
#define MAC(x,y)sqrt(x*x+y*y)
float fun(int x,int y)
{
    return sqrt(x*x+y*y);

}
void main()
{

    int a=3,b=4;
    float p,q,r,s;
    p=fun(a,b);
    q=fun(a+1,b+1);
    r=MAC(a,b);
    s=MAC(a+1,b+1);
    printf("Using functions:%f,%f\n",p,q);
    printf("Using macros:%f,%f",r,s);
}
OUTPUT
Using functions: 5.0000000,6.403124
Using macros      5.0000000,4.000000

c program on name less blocks


PROGRAM ON NAMELESS BLOCKS
void main()
{

    int a=10;
    printf("%d\n",a);
    {

    printf("Stella Maris school -Marripalem");
    }
}
OUTPUT
10
Stella Maris School-Marripalem                                                                                                                           

c program on macros with arguments


PROGRAM ON MACROS WITH ARGUMENTS
#define PI 3.142857
#define AREA(x)PI*x*x
void main()
{

    float r,a;
    printf("Enter radius of a circle");
    scanf("%f",&r);
    a=AREA(r);
    printf("AREA of the circle=%f",a);
}

OUTPUT
Enter radius  of a circle 45
AREA of the circle6364.285645                                      

program on macros with arguments


PROGRAM ON MACROS WITH ARGUMENTS
#define SUM(a,b) a+b
void main()
{

    int a,b;
    printf("Enter two numbers");
    scanf("%d%d",&a,&b);
    printf("Sum=%d",SUM(a,b));

}
OUTPUT:
Enter two numbers:30 20
Sum=50

program on macros example 3


PROGRAM FOR MACROS EXAMPLE 3
#define PI 3.142857
void main()
{

    float r,a;
    printf("Enter radius of a circle:");
    scanf("%f",&r);
    a=PI*r*r;
    printf("Area of circle:%f",a);

}
OUTPUT:
Enter radius of circle: 3
Area of circle: 285713

program on macros example 2


PROGRAM FOR MACROS 2
 #define TRUE 1
void main()
{

    while(TRUE)
    {

    printf("This loop runs infinite times\n");
    }
}
OUTPUT
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times
This loop runs infinite times.....................

program on macros example 1


PROGRAM FOR MACROS EXAMPLE 1
#define NULL 0
void main()
{

    char st[20];
    int i;
    puts("Enter a string:");
    gets(st);
    for(i=0;st[i]!=NULL;i++);
    printf("length=%d",i);
}
OUTPUT:
Enter a string:
Sirisha
Length=7

9.4.20

string sorting


STRING SORTING
#include<stdio.h>
#include<strings.h>
int main()
{

    char st[10][30],t[30];
    int n,i,j,flag;
    printf("How many strings do u want to enter :");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {

    fflush(stdin);
    gets(st[i]);

    }
for(i=0;i<n-1;i++)
{

    flag=1;
    for(j=0;j<n-1-i;j++)
{
    if(strcmp(st[j],st[j+1])>0)
    {

        strcpy(t,st[j]);
        strcpy(st[j],st[j+1]);
        strcpy(st[j+1],t);
        flag=0;
    }
}
if(flag==1)
    break;
}
printf("In alphabetical order\n");
for(i=0;i<n;i++)
    printf("%s\n",st[i]);
return 0;
}
OUTPUT
How many strings do u want to enter 4
Sirisha
Dali
Deepthi
Rama
In alphabetical order
Dali
Deepthi
Rama
Sirisha

finding the string which occurs first in alphabetical order without modifying the array


FINDING THE STRING WHICH OCCURS FIRST IN ALPHABTICAL ORDER WITHOUT MODIFYING THE ARRAY
#include <stdio.h>
void main()
{

    char st[10][30],t[30];
    int n,i;
    printf("how many strings do u want to enter :");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {

        fflush(stdin);
        gets(st[i]);

    }
for(i=0;i<n;i++)
    if(i==0||strcmp(t,st[i]>0))2

strcpy(t,st[i]);
printf("The string which occurs first in alphabetical order is %s",t);
}
OUPUT
How many strings do u want to enter :1
Enter strings: abcde
The string which occurs first in alphabetical order is abcde

finding the longest string


FINDING THE LONGEST STRING
#include <stdio.h>
void main()
{

    char st[10][30],t[30];
    int n,i,l,max=0;
    printf("How many strings do u want ot enter:");
    scanf("%d",&n);
    printf("enter strings:");
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        gets(st[i]);
        l=strlen(st[i]);
        if(l>max)
        {
            max=1;
            strcpy(t,st[i]);

        }
    }
    printf("Longest string is : %s",t);
    }
OUTPUT
How many strings do u want to enter 2
Enter strings: sirisha
Sanapathi
Longest string sanapathi

reading and printing a set of strings


READING /PRINTING A SET OF STRINGS
#include <stdio.h>
void main()
{

    char st[10][30];
    int n,i;
    printf("how many strings do u want to enter:");
    scanf("%d",&n);
    printf("Enter strings:");
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        gets(st[i]);
    }
printf("The entered strings are\n");
for(i=0;i<n;i++)
printf("%s\n",st[i]);
}
OUTPUT
How mant strings do u want to enter : 5
Enter strings:Dwarakanagar
MVP
Gajuwaka
Scindia
NAD
The entered strings are : Dwarakanagar
MVP
Gajuwaka
Scindia
NAD

transpose matrix


TRANSPOSE MATRIX
#include <stdio.h>
void main()
{

    int m[5][5],t[5][5],i,j,r,c;
    printf("How many rows and cols");
    scanf("%d%d",&r,&c);
    printf("enter the matrix:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m[i][j]);
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        t[j][i]=m[i][j];
    printf("original matrix:\n");
    for(i=0;i<r;i++)
    {

        for(j=0;j<c;j++)
            printf("%5d",m[i][j]);
        printf("\n");

    }
printf("\n its transpose:\n");
for(i=0;i<c;i++)
{

    for(j=0;j<r;j++)
        printf("%5d",t[i][j]);
    printf("\n");
}
}
OUTPUT
How many rows and cols 2
2
Enter the matrix: 1
2
3
4
Orginal matrix:
1               2
2               4
Its transpose
1                 3
2                 4

product of two matrices


PRODUCT OF TWO MATRICES
#include <stdio.h>
void main()
{

    int m1[5][5],m2[5][5],m3[5][5]={0},r1,r2,c1,c2,i,j,k;
    printf("how many rows and cols for matrix 1");
    scanf("%d%d",&r1,&c1);
    printf("how many rows and cols for matrix 2");
    scanf("%d%d",&r2,&c2);
    if(c1!=r2)
    {

        printf("Multiplication rule violated -cannot be multiplied");
        exit(0);
    }

    printf("Enter elements into matrix 1:");
    for(i=0;i<r1;i++)
        for(j=0;j<c1;j++)
        scanf("%d",&m1[i][j]);

printf("Enter elements into matrix 2:");
    for(i=0;i<r2;i++)
        for(j=0;j<c2;j++)
        scanf("%d",&m2[i][j]);
        for(i=0;i<r1;i++)
            {


            for(j=0;j<c2;j++)
            {
                for(k=0;k<c1;k++)
                    m3[i][j]=m3[i][j]+m1[i][j]*m2[i][j];
            }
}
        printf("Product of two matrices:\n");
        for(i=0;i<r1;i++)
        {

            for(j=0;j<c2;j++)
                printf("%5d",m3[i][j]);
            printf("\n");




        }

}
OUTPUT
How many rows and cols for matrix 1 2
2
How may rows and cols for matrix 2  2
2
Enter element into matrix 1:1
2
3
4

Enter element into matrix 2:1
2
3
4

Product of two matrices:
2         8
18    32

sum of two matrices


SUM OF TWO MATRICES
#include <stdio.h>
void main()
{

    int m1[5][5],m2[5][5],m3[5][5],r,c,i,j;
    printf("how many rows and cols ");
    scanf("%d%d",&r,&c);
    printf("Enter elements into matrix 1:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m1[i][j]);

printf("Enter elements into matrix 2:");
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        scanf("%d",&m2[i][j]);
        for(i=0;i<r;i++)
            for(j=0;j<c;j++)
            m3[i][j]=m1[i][j]+m2[i][j];
        printf("sum of two matrices:\n");
        for(i=0;i<r;i++)
        {

            for(j=0;j<c;j++)
                printf("%3d",m3[i][j]);
            printf("\n");



           
        }

}
OUTPUT
How many rows and cols 2
2
Enter elements into matrix 1:1
2
3
4
Enter elements into matrix 2:1
2
3
4
Sum of matrices:
2  4
6   8

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...