Professional Documents
Culture Documents
Lab 1 - 4 CP 2
Lab 1 - 4 CP 2
COMPUTER PROGRAMMING-II
LAB REPORT
Submitted to:
Engr. Abdullah Tahir
Submitted by:
M.Zohaib Farooq
2020_MC_309
Lab report 1
Array :
Arrays are fixed-size sequence containers: they hold a specific number of
elements ordered in a strict linear sequence.
Internally, an array does not keep any data other than the elements it contains
(not even its size, which is a template parameter, fixed on compile time). It is as
efficient in terms of storage size as an ordinary array declared with the
language's bracket syntax ([]). This class merely adds a layer of member and
global functions to it, so that arrays can be used as standard containers.
Program :
Program 1:
In this program I made an array having size of 5 element and show the odd and
even number that was input by user.
Code :
#include<conio.h>
#include<iostream>
using namespace std;
int main (){
int a[5]; // i take 5 number from the user using array
cout<<"enter the number :";
for (int i=0;i<5;i++){
cin>>a[i];
}
for (int j=0;j<5;j++){
if (a[j]%2==0){
Computer programming II
return 0;
}
Output :
Figure :1
Program 2:
Code :
#include<conio.h>
#include<iostream>
using namespace std;
int main (){
int a[5]; // i take 5 number from the user using array
int max,min; // create the two int for finding maximum and minimum
cout<<"enter the number :";
Computer programming II
Output :
Figure :2
Program 3:
Code :
#include<conio.h>
Computer programming II
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
int main (){
int fi[10],q1[10],q2[10],ass[10],mid[10];
string name[10];
for (int i=0;i<10;i++){
cout<<"enter the "<<i+1<<" student name\n";
getline(cin,name[i]);
cout<<"enter the "<<i+1<<" student midterm marks\n";
cin>>mid[i];
cout<<"enter the "<<i+1<<" student final term marks\n";
cin>>fi[i];
cout<<"enter the "<<i+1<<" student quiz 1 marks\n";
cin>>q1[i];
cout<<"enter the "<<i+1<<" student quiz 2 marks\n";
cin>>q2[i];
cout<<"enter the "<<i+1<<" student assignment marks\n";
cin>>ass[i];
cout<<"=============================\n";
cin.ignore();
}
ofstream file1 ("data.csv");
file1<<"name"<<','<<"mid term marks"<<","<<"quiz
1"<<','<<"quiz 2"<<','<<"assignment"<<','<<"final term"<<','<<endl;
for (int i=0;i<10;i++){
file1<<name[i]<<','<<mid[i]<<','<<q1[i]<<','<<q2[i]<<','<<ass[i]<<','<<fi
[i];
file1<<endl;
}
return 0;
}
Output :
Computer programming II
Figure :3
Program 4:
Firstly give the area of array (rows and colums) and enter the value that user
enter consequently
Code :
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
void input(int k[],int m) // here m is the multiple of row and column
{
for(int i=0;i<m;i++)
{
cout<<"enter the element of array conseqitive row wise\n"<<endl;
cin>>k[i];
}
Computer programming II
}
void output(int k[ ],int a,int b)
{
int m=a*b;
for(int i=0;i<m;i++)
{
cout<<k[i]<<"\t";
if(i==b-1) // this was done for generating new row
{
cout<<endl;
b=b+b;
}
}
}
int main(){
int row,col;
cout<<"enter row fo the array\n";
cin>>row;
cout<<"enter column of the array\n";
cin>>col;
int a[row*col]; // here i find the array area which it was occupied
input(a,row*col);
cout<<"\n#############\n";
output(a,row,col);
}
Output :
Figure :4
Computer programming II
Program 5:
Taking the value of the2D array from user and display it .
Code :
#include <iostream>
using namespace std;
int main() {
int numbers[2][3];
cout << "Enter 6 numbers: " << endl; // storing elements in the array
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
cin >> numbers[i][j];
}
}
return 0;
}
Computer programming II
Output :
Figure :5
Computer programming II
Lab report 2
Vector :
Vectors are sequence containers representing arrays that can change in size.
Just like arrays, vectors use contiguous storage locations for their elements,
which means that their elements can also be accessed using offsets on regular
pointers to its elements, and just as efficiently as in arrays. But unlike arrays,
their size can change dynamically, with their storage being handled
automatically by the container.
Internally, vectors use a dynamically allocated array to store their elements.
This array may need to be reallocated in order to grow in size when new
elements are inserted, which implies allocating a new array and moving all
elements to it. This is a relatively expensive task in terms of processing time,
and thus, vectors do not reallocate each time an element is added to the
container.
Program :
Program 1:
Using vector display the value of the vector.
Code :
#include<conio.h>
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v(3,0); /* Here Left number decides the size of the
vector and right number is the
initial values of all elements of vector
so here it will display zero 3 times.*/
for(int i=0;i<v.size();i++){
cout<<v[i]<<"\n";
}
return 0;}
Output :
Computer programming II
Figure :1
Program 2:
Take the value from the user and display them.
Input :
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> num {7, 3, 2, 8, 4};
return 0;
}
Output :
Computer programming II
Figure :2
Program 3:
Allocate the memory at specific position in vector and display ,take value from
the user
Input :
#include <iostream>
#include<conio.h>
#include <vector>
using namespace std;
int main() {
vector<int> vec;
int a;
for(int i=0;i<5;i++){
cout<<"enter the "<<i+1<<" element\n";
cin>>a; // taken value from user
vec.push_back(a); // store it in vector
}
cout << "Element at Index 0: " << vec.at(0) << "\n"; /* display store value at
specific position close by brackets*/
cout << "Element at Index 2: " << vec.at(2) << "\n";
Computer programming II
Figure :3
Program 4:
Using the remove command to remove the last element in the vector .
Input :
#include <iostream>
#include<conio.h>
#include <vector>
using namespace std;
int main() {
vector<int> vec;
int a;
for(int i=0;i<3;i++){
Computer programming II
return 0;
}
Output :
Computer programming II
Figure :5
Program 5:
Take the value from user using vector and clear all the identities.
Input :
#include <iostream>
#include<conio.h>
#include <vector>
#include <windows.h>
using namespace std;
int main() {
vector<int> vec;
int a;
for(int i=0;i<3;i++){
cout<<"enter the "<<i+1<<" element\n";
cin>>a; // taken value from user
vec.push_back(a); } // store it in vector
cout<<"clearing the vector element...";
Sleep(500);
vec.clear(); // clearing the vector element
for(int i=0;i<vec.size();i++){
cout<<"enter the "<<i+1<<" element\n";
cout<<vec[i]<<"\n"; // displaying the value
of the user that they enter
}
return 0;
}
Output :
Computer programming II
Figure :5
List :
Lists are sequence containers that allow constant time insert and erase
operations anywhere within the sequence, and iteration in both directions.
List containers are implemented as doubly-linked lists; Doubly linked lists can
store each of the elements they contain in different and unrelated storage
locations. The ordering is kept internally by the association to each element of a
link to the element preceding it and a link to the element following it.
Program :
Program 1:
Take the value from the user and display it using list
Code :
#include<conio.h>
#include<iostream>
#include<list>
using namespace std;
int main(){
list<float>p;
float c;
for(int i=0;i<5;i++){
cout<<"enter "<<i+1<<" element of list :";
Computer programming II
Output :
Figure :1
Program 2 :
Take the value from the user and display it using list.and usig push back and
push front command.
Code :
#include<conio.h>
#include<iostream>
#include<list>
using namespace std;
int main(){
list<float>p;
Computer programming II
float c;
for(int i=0;i<5;i++){
cout<<"enter "<<i+1<<" element of list :";
cin>>c; // taken value from user
p.push_back(c); //transfer value to list
}
cout<<"\n entered elements are : ";
p.push_front(6); //enter value in the start of list
p.push_back(3); //enter value in the last of list
list<float> :: iterator it;
for(it=p.begin();it!=p.end();it++){
cout<<*it<<"\n";
}
return 0;
}
Output :
Figure :2
Program 3 :
Swaping the two list element .
Code :
#include<conio.h>
Computer programming II
#include<iostream>
#include<list>
using namespace std;
int main(){
list<int> o(2,3);
list <int> e(4,6);
list <int> :: iterator it;
cout<<"the first value...\n";
for (it=o.begin();it!=o.end();it++){
cout<<*it<<"\t";
}
cout<<"\n";
cout<<"the second value...\n";
for (it=e.begin();it!=e.end();it++){
cout<<*it<<"\t";
}
cout<<"\n";
o.swap(e);
cout<<"the first swap value...\n";
for (it=o.begin();it!=o.end();it++){
cout<<*it<<"\t";
}
cout<<"\n";
cout<<"the second swap value...\n";
for (it=e.begin();it!=e.end();it++){
cout<<*it<<"\t";
}
return 0;
}
Output :
Computer programming II
Figure :3
Program 4 :
Sorting the list using sort.list command
Code ;
#include<conio.h>
#include<iostream>
#include<list>
using namespace std;
int main (){
list <int> l;
int a;
for (int i=0;i<4;i++){
cout<<"enter the "<<i+1<<" element\n";
cin>>a;
l.push_back(a);
}
l.sort();
list <int> :: iterator it;
cout<<"the sorted list.....\n";
for (it=l.begin();it!=l.end();it++){
cout<<*it<<"\n";
}
return 0;
}
Computer programming II
Output;
Figure :4
Computer programming II
Lab report 3
Class :
C++ is an object-oriented programming language.Everything in C++ is
associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car
has attributes, such as weight and color, and methods, such as drive
and brake.Attributes and methods are
basically variables and functions that belongs to the class. These are
often referred to as "class members".A class is a user-defined data type
that we can use in our program, and it works as an object constructor, or
a "blueprint" for creating objects
Program :
Program 1 :
Calculate the area and the volume of the room using class
Code :
#include <iostream>
using namespace std;
class Room {
public:
double length;
double breadth;
long double height;
long double calculateArea() {
return length * breadth;
}
Computer programming II
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
Room room1; // here i create the object of the class
// assign values to data members
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;
return 0;
}
Output :
Figure :1
Program 2 :
Store the detail of car using class
Code :
#include<conio.h>
#include<iostream>
Computer programming II
Figure :2
Program 3 :
Use class and store student data keep attendance private
Code :
#include<conio.h>
#include<iostream>
using namespace std;
class Student{
public:
string name;
string regd;
void INPUTdata();
void studentDATA();
private:
string attendance;
};
void Student::INPUTdata(){
cout<<"enter name of student : ";
getline(cin,name);
cout<<"enter registration No. of student : ";
getline(cin,regd);
cout<<"enter attendance of student : ";
getline(cin,attendance);
Computer programming II
}
void Student:: studentDATA(){
cout<<"Name of student is "<<name<<endl;
cout<<" Registration No. : "<<regd<<endl;
cout<<" Attenndance: "<<attendance<<endl;
}
int main(){
Student s1;
Student s2;
s1.INPUTdata();
cout<<endl;
s2.INPUTdata();
cout<<endl;
s1.studentDATA();
cout<<endl;
s2.studentDATA();
return 0;
}
Output :
Figure :3
Computer programming II