Ask Experts » Computers » Programming »

Write a program for the following problem


Date: 27 Dec 2015    Group: Computers    Category: Programming   

A Bookshop has 3000 CDs, DVDs and books in stock. Each item has a unique 4-digit code with the first digit identifying the type of item (whether it is a CD, DVD or Book) as follows:
1 = CD
2 = DVD
3 = book
For example: the 4 digit code 1789 clearly indicates that this is a CD because the code starts
with the digit 1. Similarly, the 4 digit code 3789 clearly indicates that this is a book because
the code starts with the digit 3.
Write a program in C++ that does the following
- Inputs the codes for all 3000 items
- Validates the input code
- Calculates the number of CDs in stock, the number of DVDs in stock and the number of
books in stock
- Output the number of CDs in stock, the number of DVDs in stock and the number of
book in stock
In the program, please ensure you put in comments, you have clear and meaningful names for your variables and you have a clear structure for your coding including the use of indentation.


Searching for C program solution? Check out programs coded by experts on this page.


Author: Nilay    28 Dec 2015      Member Level: Bronze     Points : 3  (Rs 3)    Voting Score: 0

If you want to extract first digit from number you can perform following statement:

type = no % 1000;

i am take 1000 because all numbers are consist of 4 digits. and then use simple for loop as bellow

for(i = 0; i < 3000; i++)
{
if(type == 1)
// Show & Count CD
else if(type == 2)
// Show & Count DVD
else if(type == 3)
// Show & Count Book
}

that's it.


Author: [Anonymous]    31 Dec 2015      Member Level: Gold     Points : 4  (Rs 4)    Voting Score: 0

In the above program following code

If you want to extract first digit from number you can perform following statement:

type = no % 1000;

gives you the last three digits of the four digit code and not the first digit. Because in C++ %(Modulus) operator gives remainder and / (Division) operator gives quotient.
Let us see one example code.
#include
#include
void main(void)
{
int type1,type2,code=3789;
clrscr();
type1=code%1000;
cout< type2=code/1000;
cout<<"\n"< getch();
}

Here in above program code type1 gives value 789, because that is the remainder and type2 gives value 3 because that is the quotient.
As per requirement of the user, type is based on the first digit of the code and to get the first digit we have to use division(/) operator instead of modulus(%) operator.







Author: [Anonymous]    31 Dec 2015      Member Level: Gold     Points : 4  (Rs 4)    Voting Score: 0

The program below may satisfy your requirement.
Please do include iostream.h and conio.h header files in the beginning of the program.
For easy testing I have taken array of 5 elements. By making proper changes in the program below we may also test this program for 3000 codes also.

As program accepts first code, it checks whether entered code is of 4 digits. If it is then it accepts the code otherwise it will continue for the next entry. In this program I did not check whether accepted code is less than four digits. If needed I will make the code available for you. Variable c is used for holding the number of digits of the code.

class decision
{
long int type, code[5],c,i,k,cd,dvd,book;
public:
void input(void)
{
c=0; //initially value of c is zero.
cout<<"Input 5 codes one by one\n";
cout<<"Please input only four digit codes\n";
cout<<"Code begins with 1 indicates CD\n";
cout<<"Code begins with 2 indicates DVD\n";
cout<<"Code begins with 3 indicates BOOK\n";
for(i=0;i<5;i++)
{
cin>>code[i];
for(k=code[i];k>0;k=k/10)
{
c++;
}
if(c>4)
{
cout<<"Wrong choice so input again\n";
c=0; //value of c is assigned to value zero for testing next code
i--;
continue;
}
c=0; //value of c is assigned to value zero for testing next code
}
}
void display(void)
{
cd=0;
dvd=0;
book=0;
for(i=0;i<5;i++)
{
type=code[i]/1000;
if(type==1)
{
cd++;
}
if(type==2)
{
dvd++;
}
if(type==3)
{
book++;
}
}
cout<<"\nTotal CD's available are "< cout<<"\nTotal DVD's available are "< cout<<"\nTotal BOOK's available are "< }
};

void main(void)
{
class decision d;
clrscr();
d.input();
d.display();
getch();
}



Author: Bhushan    15 Jan 2016      Member Level: Gold     Points : 4  (Rs 4)    Voting Score: 0

#include
#include
int n // Input the Number
std::string s = std::to_string(n); // IT WILL CHANGE YOUR NUMBER TO STRING
while(i<=3000)
{
if(s.length() > 0)
{
if(s.At(1) == "1") IT WILL FINDS FIRST CHAR OF THE STRING (FOUR DIGIT)
{
count the CDS...
}
if(s.At(1) == "2" )
{
Count the D.V.DS
}
if(s.AT(2) == "3")
{
COUNT THE BOOKS;
}
}
i++;
}


Author: Madhuri    28 Apr 2016      Member Level: Bronze     Points : 2  (Rs 2)    Voting Score: 0

#include
#include

void main
{
int codes[3000]; //For storing 3000 codes
int cd_count=0,dvd_count=0,book_count=0;
int temp_code; //the code of item will be stored here temporarily and if valid, will go to record of items i.e. to codes array
int counter; //loop counter

clrscr();

//Input 3000 records
for (counter=0;counter<3000;counter++)
{
cin>>temp_code;

if(temp_code>=1000 && temp_code<=3999) //validating the input
codes[counter]=temp_code;
else
counter--; //code invalid, retry with another
}
//counting no. of cds, dvds and books
for(counter=0;counter<3000;counter++)
{
int first_digit=codes[counter]/1000; //Separating first digit
if(first_digit==1)
cd_count++;
else if(first_digit==2)
dvd_count++;
else
book_count++;

//Alternate method
/* if(codes[counter]>=1000 && codes[counter]<=1999)
cd_count++;
else if(codes[counter]>=2000 && codes[counter]<=2999)
dvd_count++;
else //Since in validation, no code greater than 3999 is stored, remaining codes belong to book and no need to explicitly check for that
book_count++;*/
}
cout<<"\nNo. of CDs : "< cout<<"\nNo. of DVDs : "< cout<<"\nNo. of Books : "< getch();
}

//Alternately you can use this to input codes to save yourself from entering 3000 codes but it is not recommended as it will increase the execution time. If you are using it, remember to include stdlib.h
//temp_code=rand(4000);


Author: Krishna Teja Yeluripati    03 Jun 2016      Member Level: Gold     Points : 4  (Rs 4)    Voting Score: 0

In the question, you mentioned "Inputs the codes for all 3000 items". From the format of the 4-digit code,
CDs have codes from 1000 to 1999 (1000 items)
DVDs have codes from 2000 to 2999 (1000 items)
Books have codes from 3000 to 3999 (1000 items)
and hence there are only 3000 items possible. If all codes are inputs to the program, then the output will obviously be 1000, 1000 and 1000. So, I think your question is to find the number of CDs, DVDs and Books corresponding to some of the 3000 codes given as input to the program.

#include<iostream.h>
#include<conio.h>
int main(void)
{
int a[100]; // Integer Array to store input codes
int n; // Integer to store number of input codes
int i=0, j; // Loop Variables
int cd; // Integer to store number of CDs
int dvd; // Integer to store number of DVDs
int book; // Integer to store number of Books

clrscr();

// This loop scans the input codes and places them in the integer array 'a' until a 4000 is given as input.
do
{
i++;
cin>>a[i];
}
while(a[i]!=4000);

// This loop calculates the type of object from the input code value and increases the value of 'cd' or 'dvd' or 'book' accordingly. Not offending anybody but I saw in some of the previous answers that 'type=code%1000' (last digit) is used instead of 'type=code/1000' (first digit) to calculate the type of an item from its code.
for(j=0; j<i; j++)
{
int type=a[j]/1000;
if(type==1)
cd++;
else if(type==2)
dvd++;
else
book++;
}

cout<<"\n Number of CDs is "<<cd;
cout<<"\n Number of DVDs is "<<dvd;
cout<<"\n Number of Books is "<<book;

getch();
}

Hope this program helps.


Author: saket kumar    18 Jun 2016      Member Level: Silver     Points : 2  (Rs 2)    Voting Score: 0

Hello Friends,
the Examples are given below:-

#include
#include

using namespace std;

class Example {
// Variable Declaration
int a,b;
public:

//Constructor
Example() {
// Assign Values In Constructor
a=10;
b=20;
cout<<"Im Constructor\n";
}

void Display() {
cout<<"Values :"< }
};

int main() {
Example Object;
// Constructor invoked.
Object.Display();

// Wait For Output Screen
getch();
return 0;
}





Post Answer



You must Sign In to post a response.


Post Answer        





Related Questions
  • Return to Question Index
  • Ask Question to Experts


  • Awards & Gifts
    Top Contributors
    TodayLast 7 Daysmore...

    Online Members

    Adarsh kumar
    More...
     
    ISC Technologies, Kochi - India. © All Rights Reserved.