For Copy Pasting: copyable codes are at the end of this post, scroll to the bottom.
Symmetric Matrix
Code:
Input:
dimensions of matrix
elements of matrix
Output:
Symmetric or Not symmetric
Processing:
matrix is symetric if m[i][j]=m[j][i] for all i,j in range [1,n]Psuedocode:
startinput n
for i=1 till i=n
for j=1 till j=n
input m[i][j]
end for
end for
check if m[i][j]=m[j][i] for all i,j in range [1,n]
if true
output "Symmetric"
else
output "Not Symmetric"
end if
stop
____________________________________________________________________________________________________________________
Generic Double Ended Queue
Code:
I know this code is not good because 'delete from front' should have been done by shifting the elements instead which would make the code longer and this code only works because the test cases are not very long/difficult. But then again, it works, and this is just skillrack so who cares? +time constraint.
Input:
Choice of queue and data typeChoice of operation for queue
{1,2,3,4,5,6}
Choice of operation for deque
{1,2,3,4,5,6,7,8}
Output:
the content of queue after each operation from first to last
Processing:
no processing involved, insertion and deletion is to be done as described in the question.
Psuedocode:
startwhile choice != exit:
input choice
implement action as defined
output all elements in queue
end while
stop
____________________________________________________________________________________________________________________
Vector of Characters
Code:
Input:
String to be stored in vector1String to be stored in vector2
Output:
duplicateVector of vector1duplicateRevVector of vector2
Processing:
to initialize:for i=1 till i=length of string
v[i]=s[i]
end for
to print:
for i=first element of v till i=last element of v
output i
end for
to duplicate:
let e= end of v
for i=first element till i=e
pushback i in v
end for
to dupreverse:
for i=last element till i= first element
pushback i in v
end for
Psuedocode:
start
input s1,s2
for i=1 till i=length of s1
v1[i]=s1[i]
end for
for i=1 till i=length of s2
v2[i]=s2[i]
end for
let e= end of v1
for i=first element of v1 till i=e
pushback i in v1
end for
for i=last element in v2 till i= first element in v2
pushback i in v2
end for
for i=first element of v1 till i=last element of v1
output i
end for
for i=first element of v2 till i=last element of v2
output i
end for
stop
____________________________________________________________________________________________________________________
Arrange Items for Solar Vehicle
Code:
Input:
n;details of n bags:
number of items
details of items
Output:
names of bags in descending order
Processing:
no processing involved. sorting has been done using sort algorithm from the algorithm libraryPsuedocode:
startinput n
for i=1 till i=n
input name and number of items in bag
for j=1 till j=number of items
input weight and count of item bagweight=bagweight+weight*count
end for
end for
sort bags by bagweight in desc order
output names of bags (sorted)
stop
____________________________________________________________________________________________________________________
List of even Points
Code:
(In case of odd-numbered locations they expected "Invalid input" followed by program termination. They never mentioned this anywhere in the question so maybe they thought that we were omniscient or something. idk.
anyway, thanks to my man Sai Teja, for bringing it to my notice that a person "Kenayan" has found out from somewhere what was required.)
anyway, thanks to my man Sai Teja, for bringing it to my notice that a person "Kenayan" has found out from somewhere what was required.)
Input:
List of even PointsNumber of towers, 'n'
Name of tower1
X-coordinate of tower1
Y-coordinate of tower1
...
Name of tower-n
X-coordinate of tower-n
Y-coordinate of tower-n
Name of mobile
X-coordinate of mobile phone
Y-coordinate of mobile phone
Output:
Name of the tower
Processing:
squared distance=(x[i]-x)^2 + (y[i]-y)^2Psuedocode:
startinput n
for i=i till n
input nth towers's name,x,y
end for
input mobile's name,x,y
let min=tends to infinity
for i=1 till n
if(min>ithtowers distance to mobile)
min=th tower's distance to mobile
closest=ith tower
end if
end for
output closest's name
stop
-----------------------------------------------------------------------------------------------------------------------------------------
CODES FOR COPY-PASTING
SYMMETRIC MATRIX
#include<iostream>
#include<vector>
#define b begin()
#define v vector
using namespace std;
int main(){
int n;cin>>n; bool flag=true;
v<v<int> > m(n,v<int>(n,0));
for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>*((*(m.b+i)).b+j);
for(int i=0;i<n;i++)for(int j=i;j<n;j++)
if(!(*((*(m.b+i)).b+j)==*((*(m.b+j)).b+i))){
flag=0;break;}
cout<<(flag?"Symmetric":"Not symmetric");
}
Generic Double Ended Queue :
#define q queue
#define d deque
#define t template
t<class T> q<T>::q(){front=-1;rear=-1;capacity=20;ele=new T[100];}
t<class T> bool q<T>::isempty(){return !(rear+1);}
t<class T> bool q<T>::isfull(){return (rear-front)==(capacity-1);}
t<class T> bool q<T>::enqueue(T data){if(isfull())return false;
ele[++rear]=data; if(front==-1)++front; return true;}
t<class T> T q<T>::dequeue(){if(isempty()){ERR_Flag=true;return T();}
int a=front; if(++front>rear)front=rear=-1; ERR_Flag=false; return ele[a];}
t<class T> q<T>::~q(){delete [] ele;}
t<class T> void q<T>::print(){if(!isempty())
for(int i=front;i<=rear;i++)cout<<ele[i]<<"\n";
else cout<<"Queue is empty\n";}
t<class T> bool d<T>::push_Back(T data){return this->enqueue(data);}
t<class T> bool d<T>::push_Front(T data){if(this->isfull())return false;
for(int i=this->rear;i>=this->front;i--)this->ele[i+1]=this->ele[i];
this->ele[this->front]=data;this->rear++; return true;}
t<class T> T d<T>::pop_Front(){return this->dequeue();}
t<class T> T d<T>::pop_Back(){if(this->isempty()){ERR_Flag=true;return T();}
int a=this->rear;if(--(this->rear)<(this->front))
this->front=this->rear=-1;ERR_Flag=false; return this->ele[a];}
vector of Characters:
#define r charVector
void r::initializeVector(string a){
for(string::iterator x=a.begin();x!=a.end();x++)cv.push_back(*x);}
void r::print(){
for(vector<char>::iterator x=cv.begin();x!=cv.end();x++)cout<<*x;}
void r::dupVector(){int s=cv.size();for(int i=0;i<s;i++)cv.push_back(cv[i]);}
void r::dupRevVector(){for(int i=cv.size()-1;i>=0;i--)cv.push_back(cv[i]);}
Arrange Items for Solar Vehicle :
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<<"\n";}
float bag::compute(){float x=0;
for(int i=0;i<num_Of_Items;i++)x+=item_Count[i]*item_Wt[i]; return x;}
bool wayToSort(int i,int j){return i>j;}
void solar::get(){cin>>num_Bags;bag x;for(int i=0;i<num_Bags;i++){
x.get(); m1[x.compute()]=x; v.push_back(x.compute());}}
void solar::sort_Vec(){sort(v.begin(),v.end(),wayToSort);}
void solar::print_In_Order(){
for(vector<float>::iterator i=v.begin();i!=v.end();i++)m1[*i].print();}
List of even Points:(In case of odd-numbered locations they expected "Invalid input" followed by program termination. They never mentioned this anywhere in the question so maybe they thought that we were omniscient or something. idk.
anyway, thanks to my man Sai Teja, for bringing it to my notice that a person "Kenayan" has found out from somewhere what was required.):
#define t point
#include<exception>
void t::get(){cin>>name>>x>>y;if(x%2||y%2){cout<<"Invalid input";exit(1);}}
void t::print(){cout<<name;}
int t::dist(t p){return pow(p.x-x,2)+pow(p.y-y,2);}
void mobile::get(){cin>>num_Tower_Pts;
for(int i=0;i<num_Tower_Pts;i++){t x;x.get();tower_Pts.push_back(x);}
mobile_Pt.get();}
t mobile::find_Max(){ int max=0; list<t>::iterator j,h;h=tower_Pts.begin();
for(;h!=tower_Pts.end();h++)
if(max<h->dist(mobile_Pt)){max=h->dist(mobile_Pt); j=h;}
return *j;}
SYMMETRIC MATRIX
#include<iostream>
#include<vector>
#define b begin()
#define v vector
using namespace std;
int main(){
int n;cin>>n; bool flag=true;
v<v<int> > m(n,v<int>(n,0));
for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>*((*(m.b+i)).b+j);
for(int i=0;i<n;i++)for(int j=i;j<n;j++)
if(!(*((*(m.b+i)).b+j)==*((*(m.b+j)).b+i))){
flag=0;break;}
cout<<(flag?"Symmetric":"Not symmetric");
}
Generic Double Ended Queue :
#define q queue
#define d deque
#define t template
t<class T> q<T>::q(){front=-1;rear=-1;capacity=20;ele=new T[100];}
t<class T> bool q<T>::isempty(){return !(rear+1);}
t<class T> bool q<T>::isfull(){return (rear-front)==(capacity-1);}
t<class T> bool q<T>::enqueue(T data){if(isfull())return false;
ele[++rear]=data; if(front==-1)++front; return true;}
t<class T> T q<T>::dequeue(){if(isempty()){ERR_Flag=true;return T();}
int a=front; if(++front>rear)front=rear=-1; ERR_Flag=false; return ele[a];}
t<class T> q<T>::~q(){delete [] ele;}
t<class T> void q<T>::print(){if(!isempty())
for(int i=front;i<=rear;i++)cout<<ele[i]<<"\n";
else cout<<"Queue is empty\n";}
t<class T> bool d<T>::push_Back(T data){return this->enqueue(data);}
t<class T> bool d<T>::push_Front(T data){if(this->isfull())return false;
for(int i=this->rear;i>=this->front;i--)this->ele[i+1]=this->ele[i];
this->ele[this->front]=data;this->rear++; return true;}
t<class T> T d<T>::pop_Front(){return this->dequeue();}
t<class T> T d<T>::pop_Back(){if(this->isempty()){ERR_Flag=true;return T();}
int a=this->rear;if(--(this->rear)<(this->front))
this->front=this->rear=-1;ERR_Flag=false; return this->ele[a];}
vector of Characters:
#define r charVector
void r::initializeVector(string a){
for(string::iterator x=a.begin();x!=a.end();x++)cv.push_back(*x);}
void r::print(){
for(vector<char>::iterator x=cv.begin();x!=cv.end();x++)cout<<*x;}
void r::dupVector(){int s=cv.size();for(int i=0;i<s;i++)cv.push_back(cv[i]);}
void r::dupRevVector(){for(int i=cv.size()-1;i>=0;i--)cv.push_back(cv[i]);}
Arrange Items for Solar Vehicle :
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<<"\n";}
float bag::compute(){float x=0;
for(int i=0;i<num_Of_Items;i++)x+=item_Count[i]*item_Wt[i]; return x;}
bool wayToSort(int i,int j){return i>j;}
void solar::get(){cin>>num_Bags;bag x;for(int i=0;i<num_Bags;i++){
x.get(); m1[x.compute()]=x; v.push_back(x.compute());}}
void solar::sort_Vec(){sort(v.begin(),v.end(),wayToSort);}
void solar::print_In_Order(){
for(vector<float>::iterator i=v.begin();i!=v.end();i++)m1[*i].print();}
List of even Points:(In case of odd-numbered locations they expected "Invalid input" followed by program termination. They never mentioned this anywhere in the question so maybe they thought that we were omniscient or something. idk.
anyway, thanks to my man Sai Teja, for bringing it to my notice that a person "Kenayan" has found out from somewhere what was required.):
#define t point
#include<exception>
void t::get(){cin>>name>>x>>y;if(x%2||y%2){cout<<"Invalid input";exit(1);}}
void t::print(){cout<<name;}
int t::dist(t p){return pow(p.x-x,2)+pow(p.y-y,2);}
void mobile::get(){cin>>num_Tower_Pts;
for(int i=0;i<num_Tower_Pts;i++){t x;x.get();tower_Pts.push_back(x);}
mobile_Pt.get();}
t mobile::find_Max(){ int max=0; list<t>::iterator j,h;h=tower_Pts.begin();
for(;h!=tower_Pts.end();h++)
if(max<h->dist(mobile_Pt)){max=h->dist(mobile_Pt); j=h;}
return *j;}
No comments:
Post a Comment