0% found this document useful (0 votes)
119 views

C++ OOP Practice Questions 2

The document contains code snippets and questions related to C++ programming concepts like 2D arrays, sorting, structures, character arrays and ASCII values. It asks the reader to write programs that can transpose a matrix, sort arrays, store student data in structures, calculate student grades and marks totals, and sort a character array by ASCII values.

Uploaded by

zainzahid99
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

C++ OOP Practice Questions 2

The document contains code snippets and questions related to C++ programming concepts like 2D arrays, sorting, structures, character arrays and ASCII values. It asks the reader to write programs that can transpose a matrix, sort arrays, store student data in structures, calculate student grades and marks totals, and sort a character array by ASCII values.

Uploaded by

zainzahid99
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

2nd Week Worksheet

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;
}

Q.2. Write a program to read 10 random integer values into an array.


a) Write a user defined function to sort the array using bubble sort.
b) Write a user defined function to sort array using Select sort
NOTE: Array will be passed to these functions by reference from main
function.
//============================================================================
// Name
: f3.cpp
// Author
: Zain Zahid 150900
// Version
:
// Copyright
: Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
void bubblesort(int arr[10])
{
int i,j,temp;
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"\nSorted Array through Bubble:\n";
for(j=0;j<10;j++)
{
cout<<arr[j]<<"\t";
}
}
void selectionsort(int arr[10])
{
int i,j,min,temp;
cout<<"\nSored Array through Selection Sort:\n";\
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{

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;
}

Q.3. Create a data structure to store student information including:


- Student name
- Class Name
- Section
- Status (true/false to determine if student is currently at study or left
university)
- Marks Obtained
- Total Marks
Initialize structure instance and fill values at runtime from user.
Display student information on screen.
//============================================================================
// Name
: f4.cpp
// Author
: Zain Zahid 150900
// Version
:
// Copyright
: Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>

using namespace std;


struct student
{
char name[30],classname[30],section[30],status;
int ObtainedMarks,TotalMarks;
};
int main() {
student s;
cout<<"Enter the Name of Student";
cin>>s.name;
cout<<"Enter the Class Name of Student";
cin>>s.classname;
cout<<"Enter the Section of Student";
cin>>s.section;
cout<<"Enter the Status of Student.enter 't' or enter 'f'.";
cin>>s.status;
cout<<"Enter the Obtained Marks of Student";
cin>>s.ObtainedMarks;
cout<<"Enter the Total Marks of Student";
cin>>s.TotalMarks;
cout<<"\nName of Student\n";
cout<<s.name<<endl;
cout<<"Class Name of Student\n";
cout<<s.classname<<endl;
cout<<"Section of Student\n";
cout<<s.section<<endl;
cout<<"Status of Student\n";
if(s.status=='t')
{
cout<<"Student is Currently Studying\n";
}
if(s.status=='f')
{
cout<<"Student is Not Studying Currently\n";
}
cout<<"Obtained Marks of Student\n";
cout<<s.ObtainedMarks<<endl;
cout<<"Total Marks of Student\n";
cin<<s.TotalMarks<<"\n";
return 0;
}

Q.4. Write a program which creates an array of structure Student (created


in question 3) to read student information at runtime from user (up to 10
students). After filling in all information:
- Calculate & display grand total of marks obtained by all students
- Calculate & display average of marks obtained by all students
- Calculate grade of students (A is given where marks are greater than
80%)
- Display names of students who have secured grade B or above
//============================================================================
// Name
: f4.cpp
// Author
: Zain Zahid 150900
// Version
:

// 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')

cout<<"Grade of Student "<<i+1<<" "<<s[i].name<<" is "<<s[i].grade<<endl;


}
cout<<"\nFollowing Students have grade B\n";
for(i=0;i<10;i++)
{
if(s[i].grade=='B')
cout<<"Grade of Student "<<i+1<<" "<<s[i].name<<" is "<<s[i].grade<<endl;
}
return 0;
}

Q.5. Write a program which perform following tasks


a) Read a string from user at runtime (e.g. student name)
b) Convert read value into array of characters
c) Sort the character array based on ASCII values of array elements
d) Display sorted array
//============================================================================
// Name
: z1.cpp
// Author
: Zain Zahid 150900
// Version
:
// Copyright
: Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include<string.h>
using namespace std;
int main() {
int l;
cout<<"Enter the lenth of string";
cin>>l;
const int lenth=l;
string stringg1;
char string[lenth];
int i,ascii[lenth];
cout<<"Enter the string\n";
cin>>stringg1;
//Convert String to Character//
for(i=0;i<lenth;i++)
{
string[i]=stringg1[i];
}
//Conversion to ASCII//
for(i=0;i<lenth;i++)
{
ascii[i]=string[i];
cout<<"\nASCII code of "<<string[i]<<" is "<<ascii[i]<<endl;
}
//Sorting based on ASCII//
int j,temp;
for(i=0;i<lenth;i++)
{
for(j=0;j<(lenth-1);j++)
{

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;
}

You might also like