Akshay Prog.
Akshay Prog.
<<"\n\n\t
<<"\n\n\t
<<"\n\n\t
<<"\n\n\n
<<"\n\n\n
Chemistry : "<<marks[2]
English : "<<marks[3]
C++ : "<<marks[4]
Total marks : "<<total
Average marks : "<<avg;
}
void main()
{
clrscr();
STUDENT s;
s.init();
s.disp();
getch();
}
/****************** OUTPUT
1*******************
Name : S.P.Sharma
Address : AA,Pitam Pura
Pin code : 110035
Phone No. : (011)-7311786
************************************************************************/
obj2.getdata();
}
void third :: putdata()
{
obj1.putdata();
obj2.putdata();
}
void main()
{
class third obj[10];
int n, i;
clrscr();
cout<<"How many records to enter : ";
cin>>n;
cout<<"***Enter the data***";
for(i=0; i<=n-1; i++);
{
cout<<"Record No. "<<i+1<<endl;
obj[i].getdata();
}
cout<<endl<<"***The entered data is ***";
for(i=0; i<=n-1; i++);
{
cout<<"Record No. "<<i+1<<endl;
obj[i].putdata();
}
getch();
}
OUTPUT
How many records to enter :1
***Enter the data***
Record No. 1
Name of the car :creta
Model of the car :n077
Enter car number :6674
Year of manufacturer :2015
***The entered data is***
Record no. 1
Name of the car :creta
fout.open("LAUGHTER.DAT");
do
{
l.newentry();
fout.write((char *) &l, sizeof(l));
cout<<"Do you want to continue ?(Y/N) : ";
cin>>ans;
} while ((ans == 'y')||(ans == 'Y'));
fout.close();
}
void display()
{
laughter l;
ifstream fin;
fin.open("LAUGHTER.DAT");
cout<<"The details are :-\n";
while (!(fin.eof()))
{
fin.read((char *)&l, sizeof(l));
l.showentry();
}
fin.close();
}
void modify()
{
ifstream fin;
fin.open("LAUGHTER.DAT");
ofstream fout;
fout.open("TEMP.DAT");
laughter l;
int x, t;
cout<<"Enter the id no to be changed : ";
cin>>x;
while (!(fin.eof()))
{
fin.read((char *) &l, sizeof(l));
t = l.retidno();
if (t == x)
{
l.showentry();
l.modientry();
}
fout.write((char *)&l, sizeof(l));
}
fin.close();
fout.close();
remove("LAUGHTER.DAT");
rename("TEMP.DAT", "LAUGHTER.DAT");
}
void main()
{
clrscr();
create();
display();
cout<<"\nMODIFICATION \n";
modify();
cout<<"The new file is :\n";
display();
getch();
}
Output
Enter the id number : 1
Enter the type : 1
Enter the description :
Do you want to continue
Enter the id number : 2
Enter the type : 2
Enter the description :
Do you want to continue
The details are :ID NO : 1 TYPE
ID NO : 2 TYPE
ID NO : 2 TYPE
MODIFICATION
Enter the id no
1
?(Y/N) : y
2
?(Y/N) : n
: 1 DESCRIPTION : 1
: 2 DESCRIPTION : 2
: 2 DESCRIPTION : 2
to be changed : 1
NO
NO
NO
NO
:
:
:
:
1
2
2
2
TYPE
TYPE
TYPE
TYPE
:
:
:
:
11 DESCRIPTION : 11
2 DESCRIPTION : 2
2 DESCRIPTION : 2
2 DESCRIPTION : 2
# include <fstream.h>
# include <conio.h>
# include <stdio.h>
class tour
{
int tcode;
int noofpassengers;
int fare;
public :
tour()
{
tcode = 0;
noofpassengers = -1;
fare = -1;
}
void input()
{
cout<<"\nEnter the details :-\n";
cout<<"TCODE = ";
cin>>tcode;
cout<<"NO OF PASSENGERS = ";
cin>>noofpassengers;
cout<<"FARE = ";
cin>>fare;
}
void output()
{
cout<<"\n TCODE = "<<tcode;
cout<<" NO OF PASSENGERS = "<<noofpassengers;
cout<<" FARE = "<<fare<<endl;
}
int retcode()
{
return tcode;
}
~tour()
{
}
};
void create()
{
ofstream fout;
fout.open("TOUR.DAT");
tour t1;
char ans;
do
{
t1.input();
fout.write((char *)&t1, sizeof(t1));
cout<<"Do you want to continue ?(Y/N) : ";
cin>>ans;
} while ((ans == 'y') || (ans == 'Y'));
fout.close();
}
void delete1()
{
ifstream fin;
fin.open("TOUR.DAT");
ofstream fout;
fout.open("TEMP.DAT");
tour t1;
int code, c;
cout<<"\nEnter the code to be deleted :\n";
cin>>code;
while (!(fin.eof()))
{
fin.read((char *)&t1, sizeof(t1));
c = t1.retcode();
if (code != c)
fout.write((char *)&t1, sizeof(t1));
}
fin.close();
fout.close();
remove("TOUR.DAT");
rename("TEMP.DAT", "TOUR.DAT");
}
void display()
{
ifstream fin;
fin.open("TOUR.DAT");
tour t1;
cout<<"The file is :-\n";
while (!(fin.eof()))
{
fin.read((char *)&t1, sizeof(t1));
t1.output();
}
fin.close();
}
void main()
{
clrscr();
create();
display();
cout<<"\nDELETING\n";
delete1();
display(); getch();}
grade = 'C';
else
grade = 'f';
}
void student::display()
{
cout<<"\n The details are :-\n";
cout<<"\nRoll No : "<<rollno;
cout<<"\nName : "<<name;
cout<<"\nTotal Marks : "<<total;
cout<<"\nGrade : "<<grade;
}
void main()
{
student s[10];
int n;
clrscr();
cout<<"\n Enter the number of stduents : ";
cin>>n;
for (int i=0 ; i<n ; i++)
{
s[i].input();
s[i].calcgrade();
s[i].display();
}
getch();
}
OutputEnter the number of stduents : 2
Enter the details of the student:Roll No : 12
Name : Rajni
Marks in 5 subjects:34 56 78 21 98
The details are :Roll No : 12
Name : Rajni
Total Marks : 57.400002
Grade : CEnter the details of the student:Roll No : 16
Name : Rahul
Marks in 5 subjects:76
12 98 56 34
The details are :Roll No : 16
Name : Rahul
Total Marks : 55.200001
Grade : C
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<string.h>
class book
{int acc_no;
//access number of the book
char auth_name[50];
//name of the auther of the book
char title[50];
char pub_name[50];
//name of the publisher of the book
float price;
int no_copy;
//no of copies of the book in the library
public:
void read()
//to read the details of the book
{cout<<"\n enter the access no of the book:\n";
cin>>acc_no;
cout<<"\n enter auther name of the book:\n";
gets(auth_name);
cout<<"\n enter the title of the book:\n";
gets(title);
cout<<"\n enter publisher's name:\n";
gets(pub_name);
cout<<"\n price of the book:\n";
cin>>price;
cout<<"\n enter no of copies of book:\n";
cin>>no_copy;
};
void display()
//to display the details of the book
{cout<<"\n access no of the book:\n";
cout<<acc_no;
cout<<"\n name of th eauther:\n";
puts(auth_name);
cout<<"\n title of the book:\n";
puts(title);
cout<<"\n name of the publisher:\n";
puts(pub_name);
cout<<"\n price of the book:\n";
cout<<price;
cout<<"\n no of copies of the book:\n";
cout<<no_copy;
};
int ret_price()
//to return the price of the book
{ return price;};
book()
{acc_no=0;
strcpy(auth_name,"a");
strcpy(title,"b");
strcpy(pub_name,"c");
price=330;
no_copy=12;
};
~book()
{cout<<"destroying";
};
};
void main()
{clrscr();
book b[100];int i,n,p;
cout<<"\n enter the no of books present in the library:\n";
cin>>n;
for(i=0;i<n;i++)
b[i].read();
cout<<"\n enter the price limit:\n";
cin>>p;
for(i=0;i<n;i++)
if(b[i].ret_price()>=p)
b[i].display();
getch();
};
OUTPUT
Enter the number of books in the library : 3
Enter the details of the book
Access Number : 1
Author Name : a
Publisher : a
Title : a
Price : 12
Copies : 3
Enter the details of the book
Access Number : 2
Author Name : b
Publisher : b
Title : b
Price : 45
Copies : 5
Enter the details of the book
Access Number : 3
Author Name : c
Publisher : c
Title : c
Price : 10
Copies : 2
Enter the price limit : 15
The details of the book are
Access Number : 2
Author Name : b
Publisher : b
Title : b
Price : 45
Copies : 5
Output:
Sample Output
Im Constructor
Values :10
20
{
private : char sname[20];
char grade;
public : char saccept();
void sdisplay();
};
char sports::saccept()
{
cout<<"\n\n Enter Sports Name : ";
gets(sname);
cout<<"\n\n Enter Grade : ";
cin>>grade;
return grade;
}
void sports::sdisplay()
{
cout<<"\n\n Sports Name : "<<sname
<<"\n\n Grade : "<<grade;
}
class education:public sports
{
private : char sub[30];
float marks;
public : float eaccept();
void edisplay();
};
float education::eaccept()
{
cout<<"\n\n Enter Subject : ";
gets(sub);
cout<<"\n\n Enter Marks : ";
cin>>marks;
return marks;
}
void education::edisplay()
{
cout<<"\n\n Subject : "<<sub
<<"\n\n Marks : "<<marks;
}
class result:public education
{
private : char grade;
float marks;
public
: result();
char calculate();
void rdisplay();
};
result::result()
{
personal::paccept();
grade=sports::saccept();
marks=education::eaccept();
}
char result::calculate()
{
if(grade=='a'&&marks>60)
return 's';
else
if(grade=='b'&&marks>75)
return 's';
else
if(grade=='c'&&marks>90)
return 's';
else
return 'n';
}
void result::rdisplay()
{
char r;
r=calculate();
personal::pdisplay();
sports::sdisplay();
education::edisplay();
if(r=='s')
cout<<"\n\n Status : Selected";
else
cout<<"\n\n Status : Rejected";
}
void main()
{
clrscr();
result r;
cout<<"\n\n\n\t\t\t\t RESULT";
r.rdisplay();
getch();
}
Name : akshay
Age : 25
Sports Name : Basket Ball
Grade : b
Subject : Physical Education
Marks : 80
RESULT
Roll No. : 5
Name : akshay
Age : 25
Sports Name : Basket Ball
Grade : b
Subject : Physical Education
Marks : 80
Status : Selected
************************************************************************/
return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';
else
return 'E';
}
void enter()
{
indata();
cout<<"\n\nEnter the roll number: "; cin>>rollno;
input();
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{ clrscr();
student A;
A.enter();
A.display();
getch();
}
OUTPUT
Enter
Enter
Enter
Enter
Enter
/*******************************QUESTION 12 *****************************
Ques-Display the information of a student and calculate his grade using the class
inheritance
***********************************************************************/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
Class person{ char name[21]; int age;
public:
void indata()
{
cout<<"\n\nEnter the name of Student: " ;
gets(name);
cout<<"\n\nEnter the age : ";
cin>>age;
}
void outdata()
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;
}
};
class student: public person
{ float Tmarks;
int rollno;
public:
char calgrade()
{if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';
else
return 'E';
}
void enter()
{
cout<<"\n\nEnter the roll number: "; cin>>rollno;
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{ cout<<"\n\nRoll number : "<<rollno;
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{ clrscr();
student A;
A.indata(); A.enter(); A.outdata(); A.display();getch();}
OUTPUT
else
if(isupper(ch))
upper(ch);
else
lower(ch);
display();
}
/******************** OUTPUT 36(Sample for file ABC.CPP)*****************
Total number of numeric data is : 37
No. of character A : 2
No. of character B : 0
No. of character C : 0
No. of character D : 1
No. of character E : 2
No. of character F : 1
No. of character G : 0
No. of character H : 0
No. of character I : 1
No. of character J : 0
No. of character K : 0
No. of character L : 1
No. of character M : 1
No. of character N : 1
No. of character O : 0
No. of character P : 0
No. of character Q : 0
No. of character R : 1
No. of character S : 0
No. of character T : 0
No. of character U : 0
No. of character V : 0
No. of character W : 0
No. of character X : 1
No. of character Y : 0
No. of character Z : 0
No. of character a : 17
No. of character b : 11
No. of character c : 12
No. of character d : 10
No. of character e : 27
No. of character f : 32
No. of character g : 3
No. of character h : 8
No. of character i : 60
No. of character j : 4
No. of character k : 7
No. of character l : 26
No. of character m : 4
No. of character n : 27
No. of character o : 21
No. of character p : 14
No. of character q : 0
No. of character r : 15
No. of character s : 21
No. of character t : 30
No. of character u : 8
No. of character v : 5
No. of character w : 7
No. of character x : 1 No. of character y : 4 No. of character z : 4
int a=1;
if(fin.eof()==1)
{
cout<<"\n\n No Data in the file";
return;
}
while(fin.read((char *)&s,sizeof(student)))
{
cout<<"\n\n Record : "<<a;
s.display();
cout<<"\n\n\n";
++a;
getch();
}
}
void student::modify()
{
fstream f("add.dat",ios::binary|ios::in|ios::out);
int r,n;
char s[20];
float m;
long int pos=0;
cout<<"\n\n Enter the Roll No. whose name is to be modified : ";
cin>>r;
while(!f.eof())
{
pos=f.tellg();
f.read((char *)this,sizeof(student));
if(r==rno)
{
cout<<"\n\n Enter the subject in which marks are to be changed : ";
gets(s);
for(int i=0;i<5;i++)
{
if(strcmpi(s,sub[i])==0)
{
n=i;
break;
}
}
cout<<"\n\n Enter the new Marks : ";
cin>>m;
marks[n]=m;
f.seekp(pos);
f.write((char *)this,sizeof(student));
break;
}
else
{
cout<<"\n\n The record is not found ";
}
}
disp();
}
void add()
{
ofstream fout("add.dat",ios::binary|ios::out|ios::app);
student s;
char ch='y';
while(ch=='y'||ch=='Y')
{
s.accept();
fout.write((char *)&s,sizeof(student));
void deleter()
{
ifstream fin("add.dat",ios::binary);
ofstream fout("temp.dat",ios::binary|ios::out|ios::app);
student s;
int no;
cout<<"\n\n Enter the record's Roll No. which is to be deleted : ";
cin>>no;
while(fin.read((char *)&s,sizeof(student)))
{
if(no==s.retrno())
continue;
else
fout.write((char *)&s,sizeof(student));
}
remove("add.dat");
rename("temp.dat","add.dat");
disp();
}
void main()
{
student s;
char ch='y';
int p;
do
{
clrscr();
cout<<"\n\n 1. Add record"
<<"\n\n 2. Insert record"
<<"\n\n 3. Delete record"
<<"\n\n 4. Modify record"
<<"\n\n 5. Search record"
<<"\n\n 6. View records"
<<"\n\n 7. Exit(0)";
cout<<"\n\n Enter your choice (1 - 7) : ";
cin>>p;
clrscr();
switch(p)
{
case 1 : add();
break;
case 2 : insert();
break;
case 3 : deleter();
break;
case 4 : s.modify();
break;
case 5 : search();
break;
case 6 : disp();
break;
case 7 : exit(0);
}
cout<<"\n\n Do you want to continue : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
# include <fstream.h>
# include <stdio.h>
# include <conio.h>
# include <string.h>
void create()
{
char x;
ofstream fout;
fout.open("story.txt");
cout<<"Enter the contents of text file. Press * to end.\n";
while (x != '*')
{
x = getche();
fout<<x;
}
fout.close();
}
void display()
{
char x;
ifstream fin;
fin.open("story.txt");
cout<<"The file is as follows :-\n";
while (!(fin.eof()))
{
fin.get(x);
cout<<x;
}
fin.close();
}
void count_this()
{
char x[25];
int count = 0;
ifstream fin;
fin.open("story.txt");
while (!(fin.eof()))
{
fin>>x;
if (strcmp(x,"this") == 0)
count++;
}
cout<<"The total no of this are : "<<count;
fin.close();
}
void count_R()
{
char x[25];
int count = 0;
ifstream fin;
fin.open("story.txt");
while (!(fin.eof()))
{
fin>>x;
if (x[0] == 'R')
count++;
}
cout<<"The total no of words of R are : "<<count;
fin.close();
}
void main()
{
int choice;
clrscr();
create();
do
{
cout<<"\n\n\nMENU OPTION FOR TEXT FILE\n";
cout<<"1. Display the text file STORY.TXT\n";
cout<<"2. To count no. of this.\n";
cout<<"3. To count no. of words of R.\n";
cout<<"4. Exit.\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1 : display();
break;
case 2 : count_this();
break;
case 3 : count_R();
break;
case 4 : cout<<"End of program.\n";
break;
default : cout<<"Wrong choice!!!\n";
}
}while (choice != 4);
getch();
}
Output
Enter the contents of text file. Press * to end.
MENU OPTION FOR TEXT FILE
1. Display the text file STORY.TXT
2. To count no. of this.
3. To count no. of words of R.
4. Exit.
Enter your choice : 1
MENU OPTION FOR TEXT FILE
1. Display the text file STORY.TXT
2. To count no. of this.
3. To count no. of words of R.
4. Exit.
Enter your choice : 2
The total no of this are : 0
/*********************************QUESTION 20 **************************
C++ Program To Add Two Matrix Using Arrays
#include <iostream.h>
int main(){
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
/* Storing elements of first matrix entered by user. */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element a" << i+1 << j+1 << " : ";
cin >> a[i][j];
}
/* Storing elements of second matrix entered by user. */
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element b" << i+1 << j+1 << " : ";
cin >> b[i][j];
}
/*Adding Two matrices */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
/* Displaying the resultant sum matrix. */
cout << endl << "Sum of two matrix is: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << sum[i][j] << " ";
if(j==c-1)
cout << endl;
}
return 0;
}
Output
Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 2
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
/****************************Question 21 **************************
Create a program using function to accept an integer array and its size as argument
and replace elements having even values with its half and odd Values with twice its
values ?
************************************************************************/
#include<iostream.h>
#include<conio.h>
void accept(int a[],int n);
void convert(int a[],int n);
void display(int a[],int n);
void main()
{
clrscr();
int a[50],n;
cout<<"\n\n Enter the elements you want to enter : ";
cin>>n;
accept(a,n);
convert(a,n);
cout<<"\n\nThe new array is : \n\n";
display(a,n);
getch();
}
void accept(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"\n\n Enter the element "<<i+1<<" : ";
cin>>a[i];
}
}
void convert(int a[],int n)
{
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
a[i]=a[i]/2;
else
a[i]=a[i]*2;
}
}
void display(int a[],int n)
{
for(int i=0;i<n;i++)
cout<<" "<<a[i];
}
/************ OUTPUT 21 (Taking a sample array of 3) ********************
Enter the number of elements you want to enter : 3
Enter element 1 : 11
Enter element 2 : 24
Enter element 3 : 4
The new array is :
22 12 2
**********************************************************************/
/************************* QUESTION 26 **********************************
Write a program to search an element in a 1D array input by the user and passed as an
argument to the function using the concept of binary search
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int bsearch(int a[],int size,int val)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(val==a[mid])
return mid;
else
if(val>a[mid])
beg=mid+1;
else
last=mid-1;
}
return -1;
}
void accept(int a[],int size)
{
cout<<"\n\n Enter the elements of the array (In ascending order) : ";
for(int i=0;i<size;i++)
{
cout<<"\n Element"<<i+1<<" : ";
cin>>a[i];
}
}
void display(int a[],int size)
{
for(int i=0;i<size;i++)
{
cout<<"\n"<<a[i];
} }
void main()
{
clrscr();
int a[50],size,ele,pos;
cout<<"\n\n Enter the size of the array : ";
cin>>size;
accept(a,size);
display(a,size);
cout<<"\n\n Enter the element you want to search : ";
cin>>ele;
pos=bsearch(a,size,ele);
if(pos==-1)
cout<<"\n\n Element not found";
else
cout<<"\n\n Element found at "<<pos+1<<" position";
getch();
}
1
2
3
4
:
:
:
:
1
2
3
4
1
2
3
4
Enter the element to be searched : 2
The element found at 2 position
**********************************************************************/
void main()
{
clrscr();
int a[50],b[50],c[100],m,n;
cout<<"\n\n Enter the size of array A : ";
cin>>m;
cout<<"\n\n Enter the elements of array A in ascending order : ";
accept(a,m);
display(a,m);
getch();
clrscr();
cout<<"\n\n Enter the size of array B : ";
cin>>n;
cout<<"\n\n Enter the elements of array B in descending order : ";
accept(b,n);
display(b,n);
getch();
clrscr();
mergsort(a,b,c,m,n);
cout<<"\n\n The array after merging is : \n";
int p=m+n;
display(c,p);
getch();
}
OUTPUT
Enter the
Enter the
Enter the
1
2
3
4
5
Enter the
Enter the
Enter the
6
7
8
9
10
first array:length : 5
elements :-
second array:length : 5
elements :-
second array is :7
8
9
10
merged array is :3
5
7
9
4
10
OUTPUT
Enter the number of elements you want to enter : 3
Enter element 1 : 11
Enter element 2 : 24
Enter element 3 : 4
The new array is :
22 12 2
OUTPUT
enter array : 1 3 6
3 4 6
3 6 8
alternate positions are : 16438
OUTPUT
enter string :
2 3 4
2 3 4
sum of column is 18
Output
Enter 5 numbers: 4
-3
5
2
0
First number: 4
Last number: 0
return 0;
Output3
enter the array
1 22 34
the number of integers greater or equal to 10 is:2
#include<iostream.h>
int main()
{
int mat[3][3], trans_mat[3][3];
/* Initializing Mat1 and Mat2 */
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> mat[i][j];
}
}
/* Transposing elements of the matrix */
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
trans_mat[j][i] = mat[i][j];
}
}
cout << "Transpose of the Given 3x3 Matrix : " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << trans_mat[i][j] << "\t";
}
cout << endl;
}
}
Output
Enter elements of a 3x3 Matrix :
2
3
4
3
4
5
4
5
6
Transpose of the Given 3x3 Matrix :
2
3
4
3
4
5
4
5
6
#include<conio.h>
#include<iostream.h>
int main()
{
int a[10][10], b[10][10], c[10][10];
int x, y, i, j, m, n;
cout << "\nEnter the number of rows and columns for Matrix A:::\n\n";
cin >> x >> y;
// x denotes number rows in matrix A
// y denotes number columns in matrix A
cout << "\n\nEnter elements for Matrix A :::\n\n";
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
cin >> a[i][j];
}
cout << "\n";
}
cout << "\n\nMatrix A :\n\n";
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
cout << "\t" << a[i][j];
}
cout << "\n\n";
}
cout << "\n-----------------------------------------------------------\n";
cout << "\nEnter the number of rows and columns for Matrix B:::\n\n";
cin >> m >> n;
// m denotes number rows in matrix B
// n denotes number columns in matrix B
cout << "\n\nEnter elements for Matrix B :::\n\n";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> b[i][j];
}
cout << "\n";
}
cout << "\n\nMatrix B :\n\n";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cout << "\t" << b[i][j];
}
cout << "\n\n";
}
if (y == m)
{
for (i = 0; i < x; i++)
{
for (j = 0; j < n; j++)
{
c[i][j] = 0;
for (int k = 0; k < m; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
cout
<< "\n-----------------------------------------------------------\n";
cout << "\n\nMultiplication of Matrix A and Matrix B :\n\n";
for (i = 0; i < x; i++)
{
for (j = 0; j < n; j++)
{
cout << "\t" << c[i][j];
}
cout << "\n\n";
}
}
else
{
cout << "\n\nMultiplication is not possible";
}
getch();
return 0;
}
Output
Enter the number of rows and columns for Matrix A:::
2 2
Enter elements for Matrix A :::
1 2
3 4
Matrix A :
1
2
3
4
----------------------------------------------------------Enter the number of rows and columns for Matrix B:::
2 2
Enter elements for Matrix B :::
4 5
6 7
Matrix B :
4
5
6
7
----------------------------------------------------------Multiplication of Matrix A and Matrix B :
16
19
36
4
/************************ QUESTION 39 ******************************
Ques -Write a user-defined function named Lower_half() which takes 2D array A, with
size N rows and N columns as argument and prints the lower half of the array.
#include<conio.h>
#include<iostream.h>
int main()
{
void lower_half(int a[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{ cout<<endl;
for(j=0;j<n;j++)
if(i>=j)
cout<<a[i][j]<<" ";
}
}return 0;
}
Output 2
7
2
0
3
3
1
5
1
4
1
5
7
5
9
5
3
8
0
1
0
1
1
1
5
2
7
2
0
3
1
5 7
1 5 0
4 9 1 5
CLASSES
INHERITANCE
FILE
HANDLING
ARRAYS