C Program To Insert An Element In An Array, insertion in Array



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define S 7

main()
{
int arr[10];
int i, pos, num, j;
clrscr();
for( i = 0; i < S; i++ )
{
printf("\n Enter arr[%d] element: ", i);
scanf(" %d ", &arr[i] );
}

clrscr();
printf(" \n\nArray is \n\n ");

for( i = 0; i < S; i++ )
printf(" %2d ", arr[i] );

printf(" \nEnter the element which you want to insert\n ");
scanf(" %d ", &num );
printf(" \nEnter the position (first is 0)\n " );
scanf("%d", &pos);

if( pos > = 0 && pos <= 9)
{
for( j = S;j > pos; j-- )
arr[j] = arr[j-1];

arr[j] = num;
}
else
{
printf(" Wrong position selected\n ");
getch();
exit(1);
}

printf(" Array after insertion\n ");
for(i = 0; i < S; i++ )
printf(" %d ", arr[i]);

getch();
}

C Program To Find Transpose a 3 x 2 matrix.


#include<stdio.h> 
#include <conio.h>
#include <stdlib.h>

main()
{

int arr[3] [2];
int i, j;
clrscr();

printf(" Enter the 6 elements of matrix\n ");
for( i = 0; i < 3; i++ )
for( j = 0; j < 2; j++ )
scanf(" %d ", &arr[i] [j]);

printf(" The matrix is\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 2; j++ )
printf(" %d ", arr[i] [j]);
printf(" \n ");
}

printf(" The Transposed Matrix is\n ");

for( i = 0; i < 2; i++ )
{
for( j = 0; j < 3; j++ )
printf(" %d ", arr[j] [i]);
printf("\n");
}

getch();

}

C Program To Multiply Two Matrix, Multiplication of 3×3 Matrix in C



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
int a[3][3], b[3][3], c[3][3];
int i, j, k;
clrscr();
printf(" Enter elements of first matrix\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
printf(" \n Enter a[%d] [%d] element: ", i, j);
scanf(" %d ", &a[i] [j] );
}
}
printf(" Enter elements of second matrix\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
printf(" \n Enter b[%d] [%d] element: ", i, j );
scanf(" %d ", &b[i][j]);
}
}
printf(" \n\n First matrix is\n\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
printf(" %2d ", a[i][j]);
printf(" \n ");
}

printf(" \n\nSecond matrix is\n ");
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
printf(" %2d ", b[i][j]);
printf(" \n ");
}
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
c[i][j] = 0;
for( k = 0; k < 3; k++ )
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
printf(" \n\n Multiplication of two matrices is\n ");
for( i = 0; i < 3; i++ )
{
for(j = 0; j < 3; j++ )
printf(" %2d ", c[i][j]);
printf(" \n ");
}
getch();
}

C Program To Print Size Of Commonly Used Data Types.


#include <stdio.h>


main ()
{

printf ("\n An int is %d bytes", sizeof (int));
printf ("\n A char is %d bytes", sizeof (char));
printf ("\n A short is %d bytes", sizeof (short));

printf ("\n A long is %d bytes", sizeof (long));
printf ("\n A float is %d bytes", sizeof (float));
printf ("\n A double is %d bytes", sizeof (double));

printf ("\n An unsigned char is %d bytes", sizeof (unsigned char));
printf ("\n An unsigned int is %d bytes", sizeof (unsigned int));
return 0;

}

C Program to copy one file contents to another file



#include<stdio.h>

void main()
{
FILE *fp1, *fp2, *fopen();
int c ;
fp1 = fopen( "www.programmingmansion.co.cc", "r" ); /* open for reading */
fp2 = fopen( "prog.old", "w" ) ; /* open for writing */
if ( fp1 == NULL ) /* check does file exist etc */
{
printf("Cannot open www.programmingmansion.co.cc for reading \n" );
        exit(1);    /* terminate program */   
}
else if ( fp2 == NULL )
{
printf("Cannot open prog.old for writing \n");
exit(1); /* terminate program */
}
else /* both files O.K. */
{
c = getc(fp1) ;
while ( c != EOF)
{
putc( c, fp2); /* copy to prog.old */
c = getc( fp1 ) ;
}
fclose ( fp1 ); /* Now close files */
fclose ( fp2 );
printf("Files successfully copied \n");
}
}

adapter design pattern example in C++, adapter design in C plus plus, CSquareShapeAdapter Class in C++, using CSquareShapeAdapter in C++


#include <iostream.h>
  
class CRoundShape
{
public:
virtual ~CRoundShape() { }
virtual void m_doRound() const=0;
};

class CSquareShape
{
public:
virtual ~CSquareShape() { }
virtual void m_doSquare() const=0;
};

class CRound_Hole
{
public:
void m_insert(const CRoundShape & shape) const
{
shape.m_doRound();
}
};

class CSquareShapeAdapter : public CRoundShape
{
private:
CSquareShape & square_shape;
public:
CSquareShapeAdapter(CSquareShape & shape) : square_shape(shape){ }
void m_doRound() const
{
square_shape.m_doSquare();
}
};

class CDisplayMessage : public CSquareShape
{
public:
void m_doSquare() const
{
std::cout << "http://animesh-programmingmania.blogspot.com/" << std::endl;
}
};

void display_message(const CRoundShape & shape)
{
CRound_Hole().m_insert(shape);
}

int main()
{
CDisplayMessage shape;
display_message(CSquareShapeAdapter(shape));
return 0;
}