INLAB 7-(Tested working 100%)
Question-Price of Books and CD/DVD
Lally publication House publishes two types of materials viz,books and recorded CDs.Both.......i am lazy to type
Solution-that's all u gotta type
void learning_Material::get(){}
void learning_Material::print(){}
class book:public learning_Material
{
public:
void get()
{
cin>>isbn>>title>>author>>year;
}
void print()
{
cout<<isbn<<"\n"<<title<<"\n"<<price;
}
void get()
{
cin>>isbn>>title>>author>>year;
}
void print()
{
cout<<isbn<<"\n"<<title<<"\n"<<price;
}
void calc_Price()
{
int pages;
cin>>pages;
price=pages*1;
}
};
class CD:public learning_Material
{
public:
void get()
{
cin>>isbn>>title>>author>>year;
}
void print()
{
cout<<isbn<<"\n"<<title<<"\n"<<price;
}
cin>>isbn
void get()
{
cin>>isbn>>title>>author>>year;
}
void print()
{
cout<<isbn<<"\n"<<title<<"\n"<<price;
}
cin>>isbn
void calc_Public()
{
int dur:
cin>>dur;
price=dur*2;
}
};
INLAB 5
Question (Electronics Store Problem)
XYZ electronics store has three different products in their store such as Television, Refrigerator and Washing machine. Create a C++ class to process the basic details about the electronic products in the store, such as the name of the product, product code, product price and number of products in stock. Create another C++ class to support the store keeper to maintain the stock details in the store. Write a non member function to help the store keeper to find the products whose stock is less than 10.
Input Format:
The first line will contain the number of products n.
Name of the product
Product code
Price
stock in hand
Output Format:
The complete details of the product whose available stock is less than 10
Boundary Conditions:
n=3
Solution
#include< iostream >
using namespace std;
class store_keeper;
class item
{
char prod_name[30];
char prod_code[10];
float prod_price;
int stock_In_Hand;
public:
void get();
void print()const;
friend class store_keeper;
};
class store
{
int num_Of_Items;
item items[20];
public:
void get_details();
void print_details()const;
friend class store_keeper;
};
class store_keeper
{
char name[30];
char id[10];
public:
void get();
void print();
//Function used to print name and id of products
// with stock_In_Hand less than 10
void stock_mgmt(store &);
};
ANSWER-
void item::get()
{
cin>>prod_code>>prod_item>>prod_price>>stock_in_hand;
}
void item::print(const)
{
cout<<prod_code<<prod_item;
}
void store::get_details()
{
cin>>num_Of_Items;
for(int i=0;i<num_Of_Item;i++)
items[i].get();
}
void store::print_details()const
{}
void store_keeper::get()
{}
void store_keeper::print()
{}
void store_keeper::stock_mgmt(store &s)
{
for(int i=0;i<s.num_Of_Items;i++)
if(s.items[i].stock_In_hand<10)
s.items[i].print();
}
main()
{
store s;
store_keeper sk;
s.get_details();
sk.stock_mgmt(s);
}
INLAB 4
Question (Arithmetic Progression)
An Arithmetic Progression(AP) is described by the first term ‘f’ and the common difference’. The n-th term of an AP is described by a_n= f+(n-1)d. The progression 1,4,9 .. is an AP. Given the f,d n, k, p , q, Develop a program in C++ using the class, to represent an arithmetic progression and provide methods to compute:
(i) n-th term of the arithmetic progression
(ii) r such that a_r=k. If there is no r such that a_r=k, the program should print zero.
(iii) Absolute Difference between a_p and a_q
Input Format:
First line contains the first term of the AP
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 Format:
First line should contain the nth of the arithmetic progression
First line should contain the nth of the arithmetic progression
Next line should contain the value of ‘r’
Next line should contain the absolute difference between a_p and a_q
Boundary Conditions:
a , d are integers
Boundary Conditions:
a , d are integers
n,p,q are all positive integers
c++ code-
#include<iostream>
#include<stdlib.h>
using namespace std;
class apr
{
int first;
int diff;
public:
void get();
int compute_terms(unsigned int n);
int find_r(int k);
int abs_diff(unsigned int p, unsigned int q);
};
void apr::get()
{
cin>>first>>diff;
};
int apr::compute_terms(unsigned int n)
{
n=first+(n-1)*diff;
return((n));
}
int apr::find_r(int k)
{
if (((k-first)%diff)+1==0)
{
k=(abs(((k-first)/diff)+1));
return(abs(k));
}
else
return(0);
}
int apr::abs_diff(unsigned int p, unsigned int q)
{
return(abs((p-q)*diff));
}
int main()
{
apr ap1;
unsigned int n,p,q;
int k;
ap1.get();
cin>>n;
cout<<ap1.compute_terms(n)<<endl;
cin>>k;
cout<<ap1.find_r(k)<<endl;
cin>>p>>q;
cout<<ap1.abs_diff(p,q)<<endl;
}
#include<stdlib.h>
using namespace std;
class apr
{
int first;
int diff;
public:
void get();
int compute_terms(unsigned int n);
int find_r(int k);
int abs_diff(unsigned int p, unsigned int q);
};
void apr::get()
{
cin>>first>>diff;
};
int apr::compute_terms(unsigned int n)
{
n=first+(n-1)*diff;
return((n));
}
int apr::find_r(int k)
{
if (((k-first)%diff)+1==0)
{
k=(abs(((k-first)/diff)+1));
return(abs(k));
}
else
return(0);
}
int apr::abs_diff(unsigned int p, unsigned int q)
{
return(abs((p-q)*diff));
}
int main()
{
apr ap1;
unsigned int n,p,q;
int k;
ap1.get();
cin>>n;
cout<<ap1.compute_terms(n)<<endl;
cin>>k;
cout<<ap1.find_r(k)<<endl;
cin>>p>>q;
cout<<ap1.abs_diff(p,q)<<endl;
}
INLAB -3
Guiding a travelling salesman
Ques-A laptop sales company wants their salesperson to visit some cities every month and promote their sales. A code has to be written to find the sequence of visit so that he travels the minimum. Given ‘n’ cities and a home city, design an algorithm and write a C++ code to determine the number of routes to be evaluated when the problem is to be solved by brute force approach. For example, if there are four cities A, B, C, and D and ‘A’ is the home city then brute force method calculate distance of the following routes:
A B C D A
A B D C A
A C B D A
A C D B A
A B C D A
A B D C A
By default the company assign only five months every month for each of its employee but it may vary.
Input Format
Number of cities
Name of the cities
Home city name
Output Format
Number of routes to be analyzed in brute force approach
C code-
#include<iostream>
#include<conio.h>
using namespace std;
int use(int n)
{
int a=1;
for(int i=n;i > 1;i--)
a=a*i;
return(a);
}
main()
{
int n,i;
char p;
cin>>n;
for(int i=0;i < n;i++)
cin>>p;
cin>>p;
cout<<use(n-1);
return(0);
}
for those who don't know what is brute force method-
example : "If we wanted to guess a person's age, we could brute force by just starting from 1 and increasing by 1 every time, etc" by adding 1 if not found the solution.
It's brute force because you'd eventually reach the person's age, but you didn't do anything but try every possibility until one worked. An algorithm is not brute force if it exploits some advantage or approaches a problem such that you could arrive at a solution without having to try every possibility, ever. For example if a person was an adult, and you knew he was born in the 1970s, your 'guessing' algorithm would limit you to only 10 or so age possibilities, because you have some knowledge you can exploit to limit how many solutions you have to try
It's brute force because you'd eventually reach the person's age, but you didn't do anything but try every possibility until one worked. An algorithm is not brute force if it exploits some advantage or approaches a problem such that you could arrive at a solution without having to try every possibility, ever. For example if a person was an adult, and you knew he was born in the 1970s, your 'guessing' algorithm would limit you to only 10 or so age possibilities, because you have some knowledge you can exploit to limit how many solutions you have to try
Nature of digit in position
Ques-Given a number 'n' and a position ; write an algorithm and the subsequent 'C' program to check if the 'p-th' digit .starting from the left most digit of the number of 'n',is odd or even. For example-if n is 3145782 and p is 4 then you have to check if 5 is odd or even .Since it is odd ,print 'Odd',make your code to accept numbers of larger size.
Input format-
the first line contains the number ,n
The second line contains the position p
Output format-
Print either "Odd" or "Even".
c code-
#include<stdio.h>
main()
{
int b,z,i=0,a,k,j,l=0;
long n,r;
scanf("%d",&n);
r=n;
scanf("%d",&k);
while(n!=0)
{
n=n/10;
l++;
}
b=l-k;
a=pow(10,b);
r=r/a;
z=r%10;
if(z%2==0)
printf("number is even");
else
printf("number is odd");
}
Ques-Given a number 'n' and a position ; write an algorithm and the subsequent 'C' program to check if the 'p-th' digit .starting from the left most digit of the number of 'n',is odd or even. For example-if n is 3145782 and p is 4 then you have to check if 5 is odd or even .Since it is odd ,print 'Odd',make your code to accept numbers of larger size.
Input format-
the first line contains the number ,n
The second line contains the position p
Output format-
Print either "Odd" or "Even".
#include<stdio.h>
main()
{
int b,z,i=0,a,k,j,l=0;
long n,r;
scanf("%d",&n);
r=n;
scanf("%d",&k);
while(n!=0)
{
n=n/10;
l++;
}
b=l-k;
a=pow(10,b);
r=r/a;
z=r%10;
if(z%2==0)
printf("number is even");
else
printf("number is odd");
}
main()
{
int b,z,i=0,a,k,j,l=0;
long n,r;
scanf("%d",&n);
r=n;
scanf("%d",&k);
while(n!=0)
{
n=n/10;
l++;
}
b=l-k;
a=pow(10,b);
r=r/a;
z=r%10;
if(z%2==0)
printf("number is even");
else
printf("number is odd");
}
Complex number
ques-A computer scientist working in image processing is working on discrete Fourier transforms.he need an implementation of complex number to use in his program. develop an algorithm and write a C program to implement addition,subtraction and multiplication in complex numbers. implement each operation as a function and call it in your main.The function call sequence is addition,subtraction and multiplication. for example-when the complex numbers are 3+2i and 1+7i the output should be-
4+9i
2-5i
-11+23i
input format-
real part of complex number 1
imaginary part of complex number 1
real part of complex number 2
imaginary part of complex number 2
output format-
resultant complex number represented as
real part +/- imaginary part followed by an i
C code-
#include<stdio.h>
void add();
void subs();
void mul();
main()
{
int a,b,c,d,e,f,n,i;
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
scanf("%d",&d);
add(a,b,c,d);
printf("\n");
subs(a,b,c,d);
printf("\n");
mul(a,b,c,d);
printf("\n");
}
void add(int a,int b,int c,int d)
{ int e,f;
e=a+c;
f=b+d;
{
printf("%d",e);
}
{
if (f>0)
printf("+%di",f);
else
printf("%di",f);
}
}
void subs(int a,int b,int c,int d)
{
int g,h;
g=a-c;
h=b-d;{
printf("%d",g);
}
{
if (h>0)
printf("+%di",h);
else
printf("%di",h);
}
}
void mul(int a,int b,int c,int d)
{ int i,j;
i=(a*c-b*d);
j=(a*d+b*c);
{
printf("%d",i);
}
{
if (j>0)
printf("+%di",j);
else
printf("%di",j);
}
}
No comments:
Post a Comment