ADVERTISEMENT

One of the works done by our Robotics and Machine Learning division,
SELF-LEVELING QUADCOPTER
Arduino based Quadcopter.
Self-leveling is acheived by the aligning the quadcopter using the readings from the gryo as well as the accelerometer.
A four channel RC transmitter is used to control the movement of the quadcopter when in flight. Kindly subscribe to our YouTube Channel and stay tuned.

Friday 29 April 2016

CSE1002:PP8:Car Mileage Calculation Problem

A workshop has a variety of cars. The details of the car such as car number, miles driven, and gallons of gas used in each car is stored in a file.
Car Number          Miles Driven         Gallons used
      54            250            19
      62            525            38
      71            123            6
      85            1,322            86
      97            235            14
Write a  C++ program that reads the data in the file created and displays the car number, miles driven, gallons used, and the miles per gallon for each car.

PSEUDOCODE:
START
READ s;
READ FILE with name s in f
while(!f.eof())
f>>car>>mile>>gal;
SET avg=mile/gal;
SET total+=mile;
SET totalg+=gal;
SET tavg+=avg;
PRINT car,mile,gal,avg
end while
PRINT total,totalg,tavg,tavg/n;
STOP
CODE:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
main()
{int car,n=0;
char s[100];
cin>>s;
float mile,gal,total=0,totalg=0,avg,tavg=0;
fstream f;
f.open(s,ios::in);
while(!f.eof())
{
f>>car>>mile>>gal;
avg=mile/gal;
total+=mile;
totalg+=gal;
tavg+=avg;
cout<<fixed<<setprecision(2)<<car<<" "<<mile<<" "<<gal<<" "<<avg<<endl;
n++;
}
cout<<fixed<<setprecision(2)<<total<<endl<<totalg<<endl<<tavg<<endl<<tavg/n;
}

CSE1002:PP8:Code Detection Problem

Code Detection Problem 

 For security reasons, messages are transmitted as a secret code over a transmission channel. It is usually sent as a sequence of bits, that is, 0s and 1s. Due to noise in the transmission channel, the transmitted message may become corrupted. That is, the message received at the destination may not be the same as the message transmitted. There are several techniques to check the validity of the transmitted message at the destination. One such technique is to transmit the same message twice. At the destination, both copies of the message are compared. Given a file "data.txt" with the messages that is transmitted, write a C++ program to check whether the message received at the destination is error-free. For simplicity, assume that the secret code representing the message is a sequence of digits(0 to 9) and the maximum length of the message is 250 digits. Each original message is followed by a copy message and first number in both the messages indcates the length of the message. Each character in the message is separated by a space. For example, 7 9 2 7 8 3 5 6 7 9 2 7 8 3 5 6 means that the original message is of length 7 and it is 9 2 7 8 3 5 6 and copy message is also of length 7 and it is 9 2 7 8 3 5 6. If orginal and copy message is same then print Message transmitted is ok. Print Message transmitted is not OK when th length of the original or copy message is greater than 250 or when the original message is not same as copy message.

PSEUDOCODE:

START
READ name
OPEN FILE NAME IN l
while(!I.eof())
int a=0,b,c=0,d;
I>>n;
for i->0 to n
 I>>b;
 a=10*a+b;
end for
I>>m;
for i->0 to n
 I>>d;
 c=10*c+d;
end for
if(a==c)
 PRINT OK
else
 PRINT NOTOK
end if
STOP

CODE:

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
main()
{
fstream f;
char name[20];
cin>>name;
f.open(name,ios::in);
int n,m;
while(!f.eof())
{int a=0,b,c=0,d;
f>>n;
for(int i=0;i<n;i++)
 {f>>b;
 a=10*a+b;
 }
f>>m;
for(int i=0;i<m;i++)
 {
 f>>d;
 c=10*c+d;
 }
if(a==c)
 cout<<"Message transmitted ok"<<endl;
else
 cout<<"Message transmitted is not ok"<<endl;

}
}

CSE1002:PP8:Special Pay Increase Problem

CSE1002 Special Pay Increase Problem 

Employees in a company are up for a special pay increase. Given a file, with each line consisting of an employee's last name, first name, current salary, and percent pay increase, write a C++ program to compute the revised pay of each employee.
            Miller Andrew     65875.87  5
            Green Sheila     75892.56 6
            Sethi Amit        74900.50 6.1.
For example, the pay increase for Andrew is 5%.
UML:
 
CODE:
void employee::get(ifstream &f)
{
f>>firstName;
f>>lastName;
f>>salary;
f>>inc;
}
void employee::update_Sal()
{
updated_Salary=salary+salary*inc/100;
}
void employee::print()
{
cout<<firstName<<endl<<lastName<<endl<<fixed<<setprecision(2)<<updated_Salary<<endl;
}
int main()
{
char s[100];
ifstream f;
cin>>s;
f.open(s,ios::in);
employee e[100];
int i=0;
while(!f.eof())
{
e[i].get(f);
e[i].update_Sal();
e[i].print();
i++;
}
return 0;
}

CSE1002:PP8:SORTING DATA IN FILE

PSUDOCODE:

START
READ s;
OPEN FILE s
SET c=0
SET vector<int> v;
while(!f.eof())
f>>c;
v.push_back(c);
end while
sort vector v
print vector v
end for
STOP

CODE:
#include<fstream>
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
ifstream f;
char s[100];
cin>>s;
char d;
f.open(s);
int c=0;
int count=0;
vector<int> v;
while(!f.eof())
{
f>>c;
v.push_back(c);
}
sort(v.begin(),v.end());
for(c=0;c<v.size();c++)
{
cout<<v[c]<<endl;
}
return 0;
}

Thursday 28 April 2016

CSE1002:PP8:Number of words

PSUDOCODE:

START
READ s;
SET char d;
OPEN FILE "tweets.txt" in f
SET int c=0;
SET int count=0;
while(!f.eof())
f.get(d);
if(d==s[c])
c++;
else
c=0;
end if
if(c==strlen(s))
count++;
c=0;
end if
end while
print count
STOP

CODE:
 #include<fstream>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
fstream f;
char s[100];
cin>>s;
char d;
f.open("tweets.txt",ios::in);
int c=0;
int count=0;
while(!f.eof())
{
f.get(d);
if(d==s[c])
{
c++;
}
else
{c=0;}
if(c==strlen(s))
{
count++;
c=0;
}
}
cout<<count;



return 0;
}

CSE1002:BONUS: FIND A ROUTE

FIND A ROUTE
Rahul is fond of travelling and he visits cities and towns in the country whenever possible. All cities in the country are not connected to each other. Given the details of the cities that are connected to each city, source city from where he begins the travel and the destination city of the travel, design an algorithm and write a C++ code to list down the cities in the travel. Rahul must not visit a city more than once. When the destination city name is in the connected cities of the current city, chose it and complete the route. When there is more than one city from the current city, he sorts the city names and include the first minimum city name that is not yet visited. For example, if the connection between the cities are given as follows, source city as A and destination city as F the list of cities in the travel are A, B, D and F.

UML:


CODE:
void travel::get()
{
cin>>num_Of_Cities;
for(int i=0;i<num_Of_Cities;i++)
 {int c;
 string cur;
 cin>>cur>>c;
 for(int j=0;j<c;j++)
 {string temp;
 cin>>temp;
 city_Connection[cur].push_back(temp);
 }
 }
cin>>source>>destn;
}
void travel::find_Route()
{
route.push_back(source);
while(route.back()!=destn)
{
 bool f=true;
 vector<string> s=city_Connection[route.back()];
 sort(s.begin(),s.end());
 if(find(s.begin(),s.end(),destn)!=s.end())
 {route.push_back(destn);break;}
 for(int i=0;i<s.size();i++)
 if (find(route.begin(),route.end(),s[i])==route.end())
 {route.push_back(s[i]);f=false;break;}
}
}
void travel::print_Route()
{
for(int i=0;i<route.size();i++)
 cout<<route[i]<<endl;
}

Tuesday 26 April 2016

CSE1002:BONUS:SORT POINTS

CSE1002 Sort a Given Set of Points

    Design a OOP model to represent a point in a two dimensional space and overload the operators >>,<<, > and ==. Given 'n' points, design an algorithm and write a C++ code to sort them in desceding order. While sorting a point is said to be greater than the other based on their x-coordinate value. If value of x-coordinate is same for both the points then make a decision based on their value of y-coordinate.

==============================================================

 UML
:


CODE:
istream& operator>>(istream& in,point &p)
{
in>>p.x>>p.y;
return in;
}
ostream& operator<<(ostream& out,point& p)
{
out<<p.x<<endl<<p.y<<endl;
return out;
}
bool point::operator==(point& p)
{
return p.x==x&&p.y==y;
}
bool point::operator>(point& p)
{
if(p.x==x)
 return y>p.y;
return x>p.x;
}
void sort_Points(point *p,int s)
{
point temp;
for(int i=0;i<s;i++)
 for(int j=i+1;j<s;j++)
 if(*(p+j)>*(p+i))
 {temp=*(p+i);
 *(p+i)=*(p+j);
 *(p+j)=temp;}
}

CSE1002:BONUS:GUEST

GUEST
Jegan organizes a family function and invite some of his friends. Many of his friends come prior to the function. Jegan has arranged for their accommodation in a hotel. The rooms are booked as the guest arrive, so the room numbers are not continous and not in any order. Jegan has the room number of the friend who had arrived first. And he has requested his friends to have a room number of the person who arrives next to him. That is the friend who arrived third will have room number of the friend who arrived fourth. The last guest will not have any room number. Given the details of each guest such as name, his room number and room number of the guest who arrived next, and room number of that Jegan has, design an algorithm and write a C++ code to print the names and room numbers to serve a coffee.
Hint: Map in STL can be used for representing the input, room number of the guest may be the key and other two details may be stored as value by representing them as a user defined data type.

UML:


CODE:
void guest::get()
 {
cin>>room_No>>name>>friend_Room_No;
}
 void hotel::get()
 {
cin>>num_Of_Guest;
 guest t;
 for (int i=0;i<num_Of_Guest;i++)
 {
t.get();
 stay_Det[t.room_No]=t;
 }
 cin>>first_Room_No;
 }
 void hotel::serve_Coffee()
 {
int i=first_Room_No;
 while (i!=-1)
 {cout<<stay_Det[i].name<<" "<<i<<endl;
 i=stay_Det[i].friend_Room_No;
 }
}


CSE1002:BONUS:POLYGON

POLYGON
Design an class polygon to represent a polygon in a two dimensional space. Provide member functions to get the details of the polygon and compute area

UML:

CODE:

#include <cmath>
point::point()
{x=y=0;}
void point::get()
{cin>>x>>y;}
ostream& operator<<(ostream& Out,point p)
{
Out<<p.x<<" "<<p.y<<endl;
return Out;
}
void outofrange::what()
{
cout<<"Out of range";
}
polygon::polygon(int n)
{
num_Of_Ver=n;
vertices=new point[n];
}
polygon::~polygon()
{
delete(vertices);
}
void polygon::get()
{
vertices=new point[num_Of_Ver];
for(int i=0;i<num_Of_Ver;i++)
 cin>>(vertices+i)->x>>(vertices+i)->y;

}
point& polygon::operator[](int idx)
{
if(idx>num_Of_Ver)
 throw outofrange();
return *(vertices+idx);
}
double polygon::area()
{
double a;
for(int i=0;i<num_Of_Ver;i++)
 {
 a+=(vertices+i)->x*((vertices+(i+1)%num_Of_Ver)->y);
 a-=(vertices+i)->y*((vertices+(i+1)%num_Of_Ver)->x);
 }
 return abs(a/2);

}


CSE1002:BONUS:Generic Right Shift

CSE1002 Generic Right Shift    

Given a set of 'n' elements and 'r', write a generic function to right shift the set of elements by 'r' position. If the elements are to moved to position greater than 'n' then wrap the shift process to the beginning of the collection. For example, if the set of five elements are 1,7,8,9,12 and value of 'r' is 3 then the set of elements would be 8, 9, 12, 1, 7.

PSUEDOCODE:

DEFINE right_Shift(T a[],int n,int r)
for i->0 to r
   T temp = a[n-1];
   for j->n-2 to 0
       a[j+1]=a[j];
   end for
   a[0]=temp;
end for
end definition



CODE:
#include<iostream>
using namespace std;
template <class T>
void right_Shift(T a[],int n,int r)
{
for(int i=0;i<r;i++)
 {
   T temp = a[n-1];
   for(int j=n-2;j>=0;j--)
       a[j+1]=a[j];
   a[0]=temp;
 }
}

CSE1002:PP6

https://drive.google.com/open?id=0B2c8Csd3lUtcaHllRDFNSEVNNDQ

CSE1002:PP7:DOUBLE ENDED QUEUE

UML:




CODE:


template<class T>
queue<T>::~queue()
{}
template<class T>
queue<T>::queue()
{front=0;
rear=-1;
capacity=20;
ele=new T[20];
}
template<class T>
bool queue<T>::isempty()
{return rear<front;}
template<class T>
bool queue<T>::isfull()
{return capacity<rear-front+1;}
template<class T>
bool queue<T>::enqueue(T data)
{*(ele+rear-front+1)=data;
return *(ele+ rear++ +1)==data;}
template<class T>
T queue<T>::dequeue()
{
if(rear<front)
 ERR_Flag=true;
else
 return *(ele+front++);
}
template<class T>
void queue<T>::print()
{
if(rear<front)
 cout<<"Queue is empty\n";
else
for(int i=front;i<=rear;i++)
 cout<<*(ele+i)<<endl;
}
template<class T>
bool deque<T>::push_Back(T data)
{
return this->enqueue(data);
}
template<class T>
bool deque<T>::push_Front(T data)
{
T*A =new T[20];
*A=data;
for(int i=this->front;i<=this->rear;i++)
 *(A+i+1)=*(this->ele+i);
this->ele=A;
this->front=0;
this->rear++;
return this->rear<=20;
}
template<class T>
T deque<T>::pop_Front()
{
return this->dequeue();
}
template<class T>
T deque<T>::pop_Back()
{
if(this->rear<this->front)
 ERR_Flag=true;
else
return *(this->ele+this->rear--);
}

CSE1002:PP7:SOLAR

UML:





CODE:


void bag::get()
{
cin>>name>>num_Of_Items;
for(int i=0;i<num_Of_Items;i++)
 cin>>item_Wt[i]>>item_Count[i];
}
void bag::print()
{cout<<name<<endl;}
float bag::compute()
{float f;
for(int i=0;i<num_Of_Items;i++)
 f+=item_Wt[i]*item_Count[i];
return f;
}
bool wayToSort(int i,int j)
{return true;}
void solar::get()
{
cin>>num_Bags;
bag temp;
for(int i=0;i<num_Bags;i++)
{
 temp.get();
 m1[temp.compute()]=temp;
 v.push_back(temp.compute());
}
}
void solar::sort_Vec()
{sort(v.rbegin(),v.rend());}
void solar::print_In_Order()
{
for(int i=0;i<v.size();i++)
 m1[v[i]].print();
}

CSE1002:PP7:List Of points

LIST OF POINTS
QUESTION:

Design a class point with datamembers name of the point(string), value in x-axis and value in y-axis. Provide functions to get the details of a point, print the details of a point and a function to compute distance between the point and a given point. Design a class mobileSpace, with a list of points representing the latitude and longitude of the mobile towers and a point to represent a mobile phone, provide member functions to get details and determine the tower that shall be connected to the phone. Use STL for implementation.

UML:



CODE:

void point::get()
{
cin>>name;
cin>>x>>y;
}
void point::print()
{
cout<<name<<endl;
}
float point::dist(point p)
{
return sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}
void mobile::get()
{
cin>>num_Tower_Pts;
int i;
for(i=0;i<num_Tower_Pts;i++)
{
point temp;
temp.get();
tower_Pts.push_back(temp);
}
mobile_Pt.get();
}
point mobile::find_Min()
{
int i;
float minv=100000000000000000000000000.0;
point minp;
list<point>::iterator it = tower_Pts.begin();
for(i=0;i<num_Tower_Pts;i++)
{
if(mobile_Pt.dist(*it)<minv)
{
minv=mobile_Pt.dist(*it);
minp=*it;
}it++;
}
return minp;

}

CSE1002:PP7: SYMMETRIC MATRIX

SYMMETRIC MATRIX

Given a square matrix check if it is symmetric or not. Represent a matrix as a vector of vectors. Use vector in STL to represent a matrix.


PSEUDOCODE:


START
Read n
INITIALIZE 2d VECTOR m i.e. vector<vector<int> > m(n,vector<int>(n,0));
SET f=1;
INITIALIZE COLUMN VECTOR i.e. vector<int> col;
FOR j=0 to n-1
CLEAR COL i.e. col.clear();
FOR i=0 to n-1
READ t
PUSH t into col
END FOR
PUSH col into m
END FOR
FOR i=0 to n-1
FOR j=0 to n-1
IF(m[i][j]!=m[j][i] && f==1) THEN
PRINT "Not symmetric";
SET f=0
END IF
END FOR
END FOR
IF(f==1) THEN
PRINT"Symmetric"

STOP


CODE:


#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
cin>>n;
vector<vector<int> > m(n,vector<int>(n,0));
int i;
int j;
int f=1;
vector<int> col;
for(j=0;j<n;j++)
{
col.clear();
int t;
for(i=0;i<n;i++)
{cin>>t;
m[j][i]=t;
col.push_back(t);
}
m.push_back(col);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
//cout<<m[i][j]<<" , "<<m[j][i]<<endl;
if(m[i][j]!=m[j][i] && f==1)
{
cout<<"Not symmetric";
f=0;
break;
}
}
}
if(f==1)
cout<<"Symmetric";
return 0;

}

CSE1002:PP7:VECTOR OF CHARACTERS

VECTOR OF CHARACTERS

QUESTION:
Design a class charVector that has a character vector as datamember. Provide member functions in the class to createVector, duplicateVector, duplicateRevVector and print. Functions shall be defined as follows:
initializeVector – read a string and create a vector of characters
duplicateVector – Add the content of the vector once at the end. For example if the content of charVector is “bat” then after the function is called the content must “batbat”
duplicateRevVector – Add the content of the vector in reverse at the end. For example if the content of charVector is “bat” then after the function is called the content must “battab”
print – Print content of vector, use iterators for traversal
Use the vector class defined in STL for the implementation. Use [] operator in functions duplicateVector, duplicateRevVector and use iterator in print and initializeVector functions.
UML DIAGRAM:

CODE:



void charVector:: initializeVector(string S)
{
vector<char>::iterator it=cv.begin();
int i=0;
while(S[i]!='\0')
{
cv.push_back(S[i]);
it++;
i++;
}

}
void charVector:: dupVector()
{
vector<char> d=cv;
vector<char>::iterator it=d.begin();
while(it!=d.end())
{
cv.push_back(*it);
it++;
}
}
void charVector:: dupRevVector()
{
vector<char> d=cv;
vector<char>::iterator it=d.end();
while(it!=d.begin())
{
it--;
cv.push_back(*it);
}
}
void charVector:: print()
{
vector<char>::iterator it=cv.begin();
while(it!=cv.end())
{
cout<<*it;
it++;
}
cout<<"YES";

}