10 Feb 2017

inlab 4 - CSE1002

IMPORTANT: Tick option "Request desktop site" while using mobile device, or else code formating may be disturbed.

If there is any problem with the formatting of the blog, code (not likely.) etc. or if you have suggestions to improve any aspect of it contact me (reffer to contact/feedback button at top of page) along with details of the kind of device used(mobile/laptop) etc.


Code:

#include<iostream>
using namespace std;
class ap{
    int f; int d;
public:
    ap()               {cin>>f>>d;}
    int an(int x)      {return f+d*(x-1);}
    int r(int x)       {return (x-f)/d +1;}
    int ad(int a,int b){return abs((a-b)*d);}
};
int main(){
ap a; int z,x,c,v;
cin>>z>>x>>c>>v;
cout<<a.an(z)<<"\n"<<a.r(x)<<"\n"<<a.ad(c,v);
}


(update:) if however there is some fixed code written before and after the code window then write the following code in the box (skillrack seems to have again changed the original question format)



void apr::get(){cin>>first>>diff;}

int apr::compute_term(unsigned int n){ return(first+(n-1)*diff);}
int apr::find_r(int k){ return(((k-first)/diff)+1);}
int apr::abs_diff(unsigned int p,unsigned int q){
    return(abs((p-q)*diff)); }



Input:

First line contains the first term of the AP
Next line contains the common difference d
Next line contains the value of k
Next line contains the value of p
Next line contains the value of q

Output:

First line contains the nth of the arithmetic progression
Next line contains the value of ‘r’
Next line contains the absolute difference between a_p and a_q

Psuedocode:

start
input f,d,k,p,q
let nth= f+d*(n-1)
let r= (k-f)/d +1
let abs_dif= absolute value of (p-q)*d
output nth,r,abs_dif
stop

No comments:

Post a Comment