C++ Programming
Slip16
Q.1) Write a C++ program to accept ‘ n’ numbers from user through Command Line
Argument. Store all positive and negative numbers in two different arrays. Display
contents both arrays. [Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
if(argc == 1)
{
cout<<"\n\t supply integers";
getch();
exit(0);
}
int pos[10],neg[10];
int n1=0,n2=0;
for(int i=1;i<argc;i++)
{
int x = atoi(argv[i]); //converts string to integers
if(x > 0)
pos[n1++] = x;
else
neg[n2++] = x;
}
cout<<"\n\t Array of positive nos:";
for(i=0;i<n1;i++)
cout<<" "<<pos[i];
cout<<"\n\t Array of negative nos:";
for(i=0;i<n2;i++)
cout<<" "<<neg[i];
getch();
return 0;
}
Q.2) Define a class Product that contains data member as Prod_no, Prod_Name, Prod_Price.
Derive a class Discount(discount_in_Percentage) from class Product. A Customer buys
‘ n’ products. Accept quantity for each product , calculate Total Discount and
accordingly generate Bill. Display the bill using appropriate Manipulators.
[Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
class product
{
int pno;
char pnm[20];
float price;
public:
void accept();
void display();
};
class discount:public product
{
float dis_per;
public:
void accept();
void display();
};
void product::accept()
{
int i,n;
cout<<"\n Enter no,nm,price";
cin>>pno>>pnm>>price;
}
void product::display()
{
cout<<"\n no:-"<<pno<<"\n name:-"<<pnm<<"\nprice:-"<<price;
}
void discount::accept()
{
cout<<"\n Enter discount in percentage ";
cin>>dis_per;
}
void discount::display()
{
product::accept();
discount::accept();
product::display();
cout<<"\n Discount:-"<<dis_per;
}
void main()
{
int i,n,n1;
product p[20];
discount d[20];
int rate=10;
float price;
clrscr();
cout<<"\n How many produect do u want";
cin>>n;
for(i=0;i<n;i++)
{
d[i].display();
}
for(i=0;i<n;i++)
{
cout<<"\n Enter the qunatity of product"<<i+1;
cin>>n1;
price=n1*rate;
cout<<"\n The total price"<<price;
}
getch();
}
Output
How many produect do u want2
Enter no,nm,price
123
TV
30000
Enter discount in percentage 10
no:-123
name:-TV
price:-30000
Discount:-10
Enter no,nm,price
456
Mobile
15000
Enter discount in percentage 10
no:-456
name:-Mobile
price:-15000
Discount:-10
Enter the qunatity of product1 3
The total price30
Enter the qunatity of product2 3
The total price30
Tags:
c++