Wednesday, 18 May 2016

Write a program to implement following main() function:

int main() { string str2(“MCKVIE”);string str3=str2;cout<<str3; return 0; }



#include<iostream.h>
#include<conio.h>
#include<string.h>

 class string{
public:
char str[100];
string(char *);
string operator=(const string);
friend ostream &operator<<(ostream &output,const string s){
output<<s.str<<endl;
return output;
}
};

string::string(char *s){
strcpy(str,s);
}
string string::operator=(const string s){
string s1("");
strcpy(s1.str,s.str);

return s1;
}

void main()
{
  //  int a=0;
    string str2("MCKVIE");
    string str3=str2;
    clrscr();
    cout<<str3;
    getch();
}

Write a C++ program to create a „MATRIX‟ class of size m×n. Overload the „+‟ operator to add two Matrix objects. Write a main function to implement it.

#include<iostream.h>
#include<conio.h>

class MATRIX{

public:
int m,n;
int val[10][10];
int i,j;
void print();
MATRIX operator+(const MATRIX);

};

MATRIX MATRIX:: operator+(const MATRIX mat1){
int i,j;
MATRIX rmat;
rmat.m=mat1.m;
rmat.n=mat1.n;
for(i=0;i<mat1.m;i++)
for(j=0;j<mat1.n;j++)
rmat.val[i][j]=mat1.val[i][j]+this->val[i][j];

return rmat;
}
void MATRIX::print(){
int i,j;
for(i=0;i<m;i++){
 for(j=0;j<n;j++)
cout<<val[i][j]<<"\t";
cout<<endl;
}
}
void main(){
int m,n;
int i,j;
MATRIX mat1,mat2,rmat;
clrscr();
cout<<"Enter The value of row and col : ";
cin>>m>>n;
mat1.m=m;
mat1.n=n;
mat2.m=m;
mat2.n=n;
cout<<"Enter the value of Matrix 1 : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>mat1.val[i][j];
cout<<"Enter the value of Matrix 2 : ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>mat2.val[i][j];

rmat=mat1+mat2;
cout<<"\nSum Of Matrix is \n";
rmat.print();
getch();
}

Write a C program which will take student info such as name, roll no., department as input and store in a structure student_details. Store information about the students library details which includes roll no. and book_id in a structure library_details. The program will be able to calculate the total number of books issued to the student of a particular department.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
typedef struct student_detail{
    int roll;
    int department;
    struct student_detail *next;
}student;

typedef struct library_detail {
    int roll;
    int book_id;
    struct library_detail *next;
}library;

void print_info(){

    printf("1 . Enter Student Detail.\n");
    printf("2 . Issue Book.\n");
    printf("3 . Count Book Issued to a department.\n");
    printf("4 . Exit.\n");

}

int main()
{
    int choice=0;
    int roll,dept,book_id,bi;
    student *sstart=NULL,*stmp,*sn;
    library *lstart=NULL,*ltmp,*ln;
  //  int c=0,l=0;
    clrscr();
    print_info();

    while(choice!=4){
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
   /* c=0,l=0;

stmp=sstart;
ltmp=lstart;
while(stmp!=NULL){
   c++;
   stmp=stmp->next;
}
while(ltmp!=NULL){
   l++;
   ltmp=ltmp->next;
}

printf("\n Total Entry of student and libray: %d %d\n",c,l);
    */
switch(choice){
   case 1: printf("Enter Roll and department no : ");
   scanf("%d %d",&roll,&dept);
    stmp=(student *) malloc(sizeof(student));
    stmp->roll=roll;
    stmp->department=dept;
    stmp->next=NULL;

    if(sstart==NULL)
    sstart=stmp;
    else
    {
sn=sstart;
while(sn->next!=NULL)
sn=sn->next;
sn->next=stmp;

    }
   break;
   case 2: printf("Enter Roll and book id : ");
   scanf("%d %d",&roll,&book_id);
    ltmp=(library *) malloc(sizeof(library));
    ltmp->roll=roll;
    ltmp->book_id=book_id;
    ltmp->next=NULL;


   if(lstart==NULL)
    lstart=ltmp;
    else
    {
ln=lstart;
while(ln->next!=NULL)
ln=ln->next;
ln->next=ltmp;

    }
   break;
   case 3:
   printf("Enter Department No: ");
   scanf("%d",&dept);
   bi=0;
   ln=lstart;
   while(ln!=NULL){
      roll=ln->roll;
      sn=sstart;
      while(sn!=NULL){
if(sn->roll==roll && sn->department==dept){
bi++;
break;
}
sn=sn->next;

      }
      ln=ln->next;
   }
   printf("Total Issued Book to dept id %d is %d \n.",dept,bi);
break;
   default: printf("Wrong Input!!\n\n");
    print_info();
     break;
}
}

    return 0;
}



Wednesday, 24 February 2016

C prgram to show that C= A * Transpose(B) where A,B,C are 3x3 matrices

#include <stdio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], transpose[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
      scanf("%d", &first[c][d]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if (n != p)
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");

    for (c = 0; c < p; c++)
      for (d = 0; d < q; d++)
 scanf("%d", &second[c][d]);

  // transpose of second matrix

  for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = second[c][d];

  // multiply A * Transpose(B)

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++) {
        for (k = 0; k < p; k++) {
sum = sum + first[c][k]*transpose[k][d];
 }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

    printf("Product of entered matrices:-\n");

    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++)
        printf("%d\t", multiply[c][d]);

      printf("\n");
    }
  }

  return 0;
}

C prgram to find out nPr and nCr

#include <stdio.h>

long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);

int main()
{
   int n, r;
   long ncr, npr;

   printf("Enter the value of n and r\n");
   scanf("%d%d",&n,&r);

   ncr = find_ncr(n, r);
   npr = find_npr(n, r);

   printf("%dC%d = %ld\n", n, r, ncr);
   printf("%dP%d = %ld\n", n, r, npr);

   return 0;
}

long find_ncr(int n, int r) {
   long result;

   result = factorial(n)/(factorial(r)*factorial(n-r));

   return result;
}

long find_npr(int n, int r) {
   long result;

   result = factorial(n)/factorial(n-r);

   return result;
}

long factorial(int n) {
   int c;
   long result = 1;

   for (c = 1; c <= n; c++)
      result = result*c;

   return result;
}

C program to find the second largest element of an array

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

int sl(int a[], int n){
int max, sec_max,i;
if(a[1]<a[0]) {
max=a[0];
sec_max=a[1];
}else {
max=a[1];
sec_max=a[0];
}
for( i=2;i<n;i++) {
if(a[i]>max) {
sec_max=max;
max=a[i];
}
if(a[i]<max && a[i]>sec_max) {
sec_max=a[i];
}
}
return sec_max;
}


int main()
{
    int p;
int array[]= {1,4,8,10,14,9,7,6,5,3,2};//{1,2,5,4,3,0};   //{2,1,6,5,4,8};
int n = sizeof(array)/sizeof(array[0]);
p=sl(array, n);
printf("Second largest element is %d",p);
return 0;
}

C program to print the sum of each row and each column of a matrix using dynamic array

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

void main()
{
int **a,r,c,i,j,sum=0;
clrscr();
//Accepting size of row and column
printf("Enter size of row n");
scanf("%d",&r);
printf("Enter size of column n");
scanf("%d",&c);

//Allocating memory to array
a=(int **)malloc(r*sizeof(int *));
for(i=0;i<r;i++)
a[i]=(int *)malloc((c+1)*sizeof(int));

printf("Enter array elements n");

for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]); //Accepting array elements from user

 for (i = 0; i < r; i++)
    {
 for (j = 0; j < c; j++)
        {
sum = sum + a[i][j] ;
        }
        printf("Sum of the %d row is = %d\n", i, sum);
        sum = 0;
    }
    sum = 0;
for (j = 0; j < c; j++)
    {
 for (i = 0; i < r; i++)
        {
sum = sum + a[i][j];
        }
        printf("Sum of the %d column is = %d\n", j, sum);
        sum = 0;
}

getch();
 }