29 Apr 2017

PPS8 - CSE1002

 
 For Copy Pasting Codes: copyable codes are at the end of this post, scroll to the bottom. 
For Input and Output just select and drag-drop the "input format" and "output format" text from the questions. (edit a little if you have time.)

count Words in Tweets

Code: 


Psuedocode:

start
input w
count =0
untill eof
           input s
           count=count + number of w in s
end loop
output count
stop


____________________________________________________________________________________________________________________

Sort Data in File 

Code:


Psuedocode:

start
input numbers into array
sort numbers in array
output elements in array
stop

____________________________________________________________________________________________________________________

Special Pay Increase Problem

Code:





Psuedocode:

start
loop until end of file
       input name1,name2,salary,increase
       output name1,name2,salary*(1+ increase/100)
end loop
stop

____________________________________________________________________________________________________________________

Code Detection Problem  


Code:



Psuedocode:

start
loop until eof:
input n
input n elements in to array1
input z
if n=z
            input n elements in to array2
if n!=z or array1!=array 2
        output Message transmitted is not ok
else output Message transmitted is ok
end loop
stop

____________________________________________________________________________________________________________________

Class Average Problem

Code:



Psuedocode:

start
loop until end of file:
input n,m,t
mt=mt+m; tt=tt+t; at=at + m/t;
output n,m,t,m/t
end loop
output mt,tt,at
stop




-----------------------------------------------------------------------------------------------------------------------------------------
CODES FOR COPY-PASTING (in same order as above. seperated by a line of underscores)

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main(){
    ifstream f("tweets.txt");
    char w[10],s[50]; int c=0;
    cin>>w;f>>s;
    while(!f.eof()){
        c+=strcmp(w,s)?0:1;
        f>>s;       }
    cout<<c;
}
____________________________________________________________________
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int main(){
    char s[20];cin>>s;
    ifstream f(s);
    int v[100],i=0;
    while(!f.eof()) f>>v[i++];
    sort(v,&v[i]);
    for(int z=0;z<i;z++)cout<<v[z]<<"\n";
}

_____________________________________________________________________

#define e employee
void e::get(ifstream& i){i>>lastName>>firstName>>salary>>inc;}
void e::print(){cout<<fixed<<setprecision(2)<<lastName<<"\n"<<firstName<<"\n"<<updated_Salary<<"\n";}
void e::update_Sal(){updated_Salary=salary*(1+inc/100);}
int main(){
    char s[20];cin>>s;
    ifstream f(s);  e x;
    while(!f.eof()){
        x.get(f);   x.update_Sal(); x.print();
    }
}


__________________________________________________________

#include<iostream>
#include<fstream>
using namespace std;
int main(){
    char s[20];cin>>s;
    int n,a[30],z,i,t,g;
    ifstream f(s);
    while(!f.eof()){
        f>>n;
        for(i=0;i<n;i++)f>>a[i];
        g=0;    f>>z;
        for(i=0;i<z;i++){   f>>t;    if(z!=n||t!=a[i])g=1;}//short circuit so np..
        cout<<"Message transmitted"<<(g?" is not ":" ")<<"ok\n";}
}
_____________________________________________________________

#include<iostream>
#include<fstream>
using namespace std;
int main(){
    char s[20]; cin>>s;
    ifstream f(s);
    int n; float m,t,mt=0,tt=0,at=0,i=0;
    while(!f.eof()){
        f>>n>>m>>t;
        mt+=m;tt+=t;at+=m/t;i++;
        printf("%d\t%.2f\t%.2f\t%.2f\n",n,m,t,m/t);}
    printf("%.2f\n%.2f\n%.2f\n%.2f",mt,tt,at,at/i);
    f.close();
}
 



24 Apr 2017

Inlab 10 - CSE1002

 You don't need to tick the "Request desktop site" option in your browser anymore while using a mobile device, but if you don't then the code formatting of the code given as text for copying will be disturbed.

Important Note: (although the code will run now) Neither the question, nor the output format mentioned this anywhere, and it was determined only after reverse calculation from expected output, that before the average of mileages (not average mileage *) they expect the sum of mileages in the output as well. For the sake of those who were unable to do their Inlab report this issue to your faculty since those who have been affected do not deserve to bear the brunt of yet another case of carelessness from those who set these questions.

* = {  average mileage ((m1+ m2+ m3.......mi ) / (g1+g2+g3....gi) ) is not equal to  average of mileages ( (m1/g1 + m2/g2 + m3/g3 + ... mi/gi) / i  )}   [unrelated to above explained issue]

Code (choose one):

[now verified]


[now verified]

 The second one is shorter and is based on the fact that all the cases (even the hidden ones that recently became known to me.) have exactly 5 lines of data so there is no need to check for eof, should have been varied.


 If you know how to copy paste on skillrack then you can copy the code from below
#include<iostream>
#include<fstream>
using namespace std;
int main(){
    float m,g,gt=0,mt=0,at=0;   int a,i;  char s[30];
    cin>>s;     ifstream f(s);
    for(i=0;1;gt+=g,mt+=m,at+=(m/g),i++){
        if(!(f>>a>>m>>g))break;
        printf("%d\t%.2f\t%.2f\t%.2f\n",a,m,g,m/g);}
    printf("%.2f\n%.2f\n%.2f\n%.2f",mt,gt,at,at/i);
    f.close();
}



Input:

Name of the file
car number, miles driven and gallons (separated by a tab)  of car1 in line1
car number, miles driven and gallons (separated by a tab) of car2 in line2
….
car number, miles driven and gallons (separated by a tab) of car-n in line-n

Output:

car number, miles driven, gallons and mileage of car1 (separated by tab)
car number, miles driven, gallons and mileage of car2 (separated by tab)

...
car number, miles driven, gallons and mileage of car-n (separated by tab)
Total miles driven (by all the cars)
Total gallons used (by all the cars)
average miles per gallon for all the cars

Processing:

mileage= distance / fuel consumed  (= miles/gallons)

Psuedocode: 

start
input s
open file s
let ds=0,fs=0
untill end of file:
         input n,d,f
         ds=ds+d,fs=fs+f
         output n,d,f,d/f
end of loop
output ds,fs,ds.fs
stop
   

22 Apr 2017

PPS7 - CSE1002

 
 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:

start
input 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 type
Choice 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:

start
while 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 vector1
String to be stored in vector2

Output:

duplicateVector of vector1
duplicateRevVector 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 library

Psuedocode:

start
input 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.)

Input:

List of even Points
Number 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)^2

Psuedocode:

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

19 Apr 2017

Inlab 9 - CSE1002

 You don't need to tick the "Request desktop site" option in your browser anymore while using a mobile device, but if you don't then the code formatting of the code given as text for copying will be disturbed.

 

Code:

[verified] 
{reportedly working.}

 

  If you know how to copy paste on skillrack then you can copy the code from below:


#define p point
void p::get(){cin>>name>>x>>y;}
void p::print(){cout<<name;}
float p::dist(p o){return pow(x-o.x,2)+pow(y-o.y,2);}
void mobile::get(){int i;cin>>i;p o;
    for(int z=0;z<i;z++,o.get(),tower_Pts.push_back(o));  mobile_Pt.get();}
p mobile::find_Min(){int l=999999; list<p>::iterator f=tower_Pts.begin(),o;
    for(;f!=tower_Pts.end();f++)if((*f).dist(mobile_Pt)<l){
    l=(*f).dist(mobile_Pt); o=f;}
    return *o;
}

 

Input:

n (number of towers)
for n towers : name,x,y
mobile's name,x,y  

Output:


name of tower closest to mobile

Processing:

distance between two points =( (x1-x2)^2 + (y1-y2)^2) ^0.5


Psuedocode: 

start
input 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