C++ OOP Practice Questions 2
C++ OOP Practice Questions 2
Objective :
This Lab has been designed so that you should have a good grasp on the following
topics after the completion of lab.
1. Two dimensional Arrays
2. Sorting Arrays
2.1 Bubble Sort
2.2 Insertion Sort
3. Structure definition, Initialization and usage
4. ASCII Values
Q.1. Write a program which can store a matrix data and display transpose
of matrix. User should be able to enter matrix values at runtime and
display matrix data and its transpose. Create a separate function
Transpose which accepts matrix as an argument and displays its
transpose. After displaying transpose of matrix, program should give a
choice to use whether to continue and repeat matrix input or terminate.
#include<iostream>
using namespace std;
void matrix(int arr[3][3])
{
int i,j;
cout<<"\nOrignal Matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\nTranspose of Matrix is\n";
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
}
int main()
{
int arr[3][3],i,j;
for( i=0;i<3;i++)
{
for( j=0;j<3;j++)
{
cout<<"Enter the element of row " <<i+1<<" and column "<<i+1;
cin>>arr[i][j];
}
}
matrix(arr);
return 0;
}
min=i;
if(arr[min]>arr[j])
min=j;
if(min!=i)
{
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
}
for(i=0;i<10;i++)
{
cout<<arr[i]<<"\t";
}
}
int main() {
int arr[10],j;
srand(time(0));
for(j=0;j<10;j++)
{
arr[j]=rand()%10+1;
}
cout<<"Original Array\n";
for(j=0;j<10;j++)
{
cout<<arr[j]<<"\t";
}
bubblesort(arr);
selectionsort(arr);
return 0;
}
// Copyright
: Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
struct student
{
char name[30],classname[30],section,grade;
int ObtainedMarks,TotalMarks;
};
int main() {
int i;
float total=0.0;
student s[10];
//Taking Information
for(i=0;i<10;i++)
{
cout<<"Enter the Name of Student"<<i+1;
cin>>s[i].name;
cout<<"Enter the Class Name of Student"<<i+1;
cin>>s[i].classname;
cout<<"Enter the Section of Student"<<i+1;
cin>>s[i].section;
cout<<"Enter the Obtained Marks of Student"<<i+1;
cin>>s[i].ObtainedMarks;
if(s[i].ObtainedMarks>80)
s[i].grade='A';
else
s[i].grade='B';
cout<<"Enter the Total Marks of Student"<<i+1;
cin>>s[i].TotalMarks;
}
//Displaying Information
for(i=0;i<10;i++)
{
cout<<"\nName of Student\n"<<i+1;
cout<<s[i].name<<endl;
cout<<"Class Name of Student\n"<<i+1;
cout<<s[i].classname<<endl;
cout<<"Section of Student\n"<<i+1;
cout<<s[i].section<<endl;
cout<<"Obtained Marks of Student\n"<<i+1;
cout<<s[i].ObtainedMarks<<endl;
cout<<"Total Marks of Student\n"<<i+1;
cout<<s[i].TotalMarks<<"\n";
total=s[i].ObtainedMarks+total;
}
//Displaying Total and Average
cout<<"\nTotal Marks="<<total;
cout<<"\nAverage Marks="<<total/10;
//Displaying Grades
cout<<"\nFollowing Students have grade A\n";
for(i=0;i<10;i++)
{
if(s[i].grade=='A')
if(ascii[j]>ascii[j+1])
{
temp=ascii[j];
ascii[j]=ascii[j+1];
ascii[j+1]=temp;
}
}
}
cout<<"Sorted Array is \n";
for(i=0;i<lenth;i++)
{
cout<<ascii[i]<<"\t";
}
return 0;
}