Q1: A lady always took a little red counter to the grocery store. The counter wa
ID: 3804427 • Letter: Q
Question
Q1: A lady always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to the store if she bought everything in the basket. The counter had a four-digit display, increment buttons for each digit, and a reset button. An overflow indicator came up red if more money was entered than the $99.99 it would register.
Write and implement the member functions of a class Counter that simulates and slightly generalizes the behavior of this grocery store counter. The constructor should create a Counter object that can count up to the constructor’s argument. That is, Counter (9999) should provide a counter that can count up to 9999. A newly constructed counter displays a reading of 0. The member function void reset ( ); sets the counter’s number to 0. The member function void incr1 ( ); increments the units digits by 1, void incr10 ( ); increments the tens digit by 1, and void incr100 ( );and void incr1000 ( ); increment the next two digits, respectively. Accounting for any carry when you increment should require no further action than adding an appropriate number to the private data member. A member function bool overflow ( ); detects overflow. (Overflow is the result of incrementing the counter’s private data member beyond the maximum entered at counter construction.)
Use this class to provide a simulation of the lady’s little red clicker. Even though the display is an integer, in the simulation, the rightmost (lower order) two digits are always thought of as cents and tens of cents, the next digit is dollars, and the fourth digit is tens of dollars.
Provide keys for cents, dimes, dollars, and tens of dollars. Use the following keys: c, d, u, t, o where c is for cents followed by a digit 1 to 9, d is for dimes followed by a digit 1 to 9, u is for dollars followed by a digit 1 to 9, t is for tens followed by a digit 1 to 9, and o is for overflow. Each entry (one of cdut followed by 1 to 9) is followed by pressing the Return key. Any overflow is reported after each operation. Overflow can be requested by pressing the o key."
Q2: Suppose we can buy a chocolate bar from the vending machine for $1 each. Inside every chocolate bar is a coupon. We can redeem 7 coupons for one chocolate bar from the machine. We would like to know how many chocolate bars can be eaten, including those redeemed via coupon, if we have n dollars.
For example, if we have 20 dollars then we can initially buy 20 chocolate bars. This gives us 20 coupons. We can redeem 14 coupons for 2 additional chocolate bars. These two additional chocolate bars have 2 more coupons, so we now have a total of 8 coupons when added to the six leftover from the original purchase. This gives us enough to redeem for one final chocolate bar. As a result we now have 23 chocolate bars and 2 leftover coupons.
Write a recursive solution to this problem that inputs from the user the number of dollars to spend on chocolate bars and outputs how many chocolate bars you can collect after spending all your money and redeeming as many coupons as possible. Your recursive function will be based upon the number of coupons owned. Also output the number of leftover coupons.
Q3: A complex number is a number with a real part and an imaginary part. For example, the complex number a + bi has a real part a and an imaginary part b, where the symbol i represents the square root of -1.
Use the following structure type for complex numbers:
typedef struct {
double real, imag;
} complexNum;
Following are the function prototypes defined on complex numbers:
int readComplex(complexNum *c);
void printComplex(complexNum c);
complexNum addComp(complexNum c1, complexNum c2);
complexNum subComp(complexNum c1, complexNum c2);
complexNum multiComp(complexNum c1, complexNum c2);
complexNum divComp(complexNum c1, complexNum c2);
complexNum absComp(complexNum c);
A partial implementation of these functions, as well as a demo driver, is given below:
int main(void)
{ complexNum com1, com2;
/* Gets two complex numbers */
printf("Enter the first complex number (real and imaginary) ");
readComplex(&com1);
printf("Enter the second complex number (real and imaginary) ");
readComplex(&com2);
printf(" ");
printComplex(com1);
printf(" + ");
printComplex(com2);
printf(" = ");
printComplex(addComp(com1, com2));
printf(" ");
printComplex(com1);
printf(" - ");
printComplex(com2);
printf(" = ");
printComplex(subComp(com1, com2));
printf(" |");
printComplex(com1);
printf("| = ");
printComplex(absComp(com1));
printf(" ");
return (0);
}
Int readComplex(complexNum *c)
{ int status;
status = scanf("%lf%lf", &c->real, &c->imag);
if (status == 2)
status = 1;
else if (status != EOF)
status = 0;
return (status);
}
void printComplex(complexNum c)
{ //To Do
}
complexNum addComp(complexNum c1, complexNum c2)
{
complexNum csum;
csum.real = c1.real + c2.real;
csum.imag = c1.imag + c2.imag;
return (csum);
}
complexNum subComp(complexNum c1, complexNum c2)
{ complexNum cdiff;
cdiff.real = c1.real - c2.real;
cdiff.imag = c1.imag - c2.imag;
return (cdiff);
}
complexNum multiComp (complexNum c1, complexNum c2)
{ // To Do
}
complexNum divComp (complexNum c1, complexNum c2)
{ //To Do
}
complexNum absComp(complexNum c)
{ complexNum cabs;
cabs.real = sqrt(c.real * c.real + c.imag * c.imag);
cabs.imag = 0;
return (cabs);
}
1- Write the implementation of function printComplex. This function displays value as (a + bi) or (a - bi). This function displays 0 if the absolute value of both real and imaginary parts rounds to 0, displays the value of the real part if the absolute value of the imaginary part rounds to 0 and vice versa.
2- Write the implementation of function multiComp and divComp to perform the multiplication and division of complex numbers defined as follows:
Sample Run:
Enter the first complex number (real and imaginary) 4.3 1.2
Enter the second complex number (real and imaginary) 6.9 1.1
(4.30 + 1.20i) + (6.90 + 1.10i) = (11.20 + 2.30i)
(4.30 + 1.20i) - (6.90 + 1.10i) = (-2.60 + 0.10i)
|(4.30 + 1.20i)| = (4.46)
3- What does the following program segment display if the data entered are 5.6 4.1 3.2 -3.4?
complexNum a1, b1, c1;
readComplex(&a1);
readComplex(&b1);
printComplex(a1);
printf(" + ");
printComplex(b1);
printf(" = “);
printComplex(addComp (a1,b1));
c1 = subtComp (a1, absComp (b1));
printf(“ next result = “):
printComplex(c1);
4- Create a header file named complex containing the interface information for the complex arithmetic operations defined in Q3.
5- Create an implementation file containing the implementation code of the library functions in Q3 that are hidden from the user program.
Q4: Write a C program that takes a command line argument that is the name of a text file and creates a new text file with heading line *************************** file name ******************************* and the contents of the original file with line numbers added. If the file’s name contains a period, use the part of the name before the period concatenated with .lis as the name of the new file. Otherwise, just concatenate .lis with the whole file name.
Explanation / Answer
Ans. 1
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
// Function to check whether valid key is pressed
bool valid_key_pressed(char key_pressed);
// The Counter class
class Counter
{
public:
Counter(int max_num); // Constructor
void reset (); // Resets Counter Value to 0
void incr1 (); // Increment Unit digit by 1
void incr10 (); // Increment Ten digit by 1
void incr100 (); // Increment 100 digit by 1
void incr1000 (); // Increment 1000 digit by 1
bool overflow (); // Checks for Overlimit
int CurrCounterValue ();
private:
int curr_num;
int max_num;
bool overlimit; // Overlimit
};
bool valid_key_pressed(char key_pressed)
{
if ((key_pressed == 'c') || (key_pressed == 'd') ||
(key_pressed == 'u') || (key_pressed == 't') ||
(key_pressed == 'o') )
{
return true;
}
cout << "Invalid key pressed is " << key << endl;
return false;
}
Counter::Counter(int x)
{
curr_num = 0;
max_num = x;
overlimit = false;
return;
}
void Counter::incr1()
{
if (curr_num + 1 >=max_num)
overlimit = true;
else
curr_num = curr_num + 1;
return;
}
void Counter::incr10()
{
if (curr_num + 10 >= max_num)
overlimit = true;
else
curr_num = curr_num + 10;
return;
}
void Counter::incr100()
{
if (curr_num+100 >= max_num)
overlimit = true;
else
curr_num = curr_num + 100;
return;
}
void Counter::incr1000()
{
if (curr_num + 1000 >= max_num)
overlimit = true;
else
curr_num = curr_num + 1000;
return;
}
bool Counter::overflow()
{
return overlimit;
}
void Counter::reset()
{
overlimit = false;
curr_num = 0;
return;
}
int Counter::CurrCounterValue()
{
return curr_num;
}
int main()
{
Counter counter(9999);
char curr_key;
char trial;
cout << "Staring money is : "
<< counter.CurrCounterValue () << endl;
cout << endl;
cout << "Press " << endl;
cout << "c for cents " << endl;
cout << "d for dimes " << endl;
cout << "u for dollars " << endl;
cout << "t for tens of dollars " << endl;
cout << "o for overflow " << endl;
cout << "r for reset " << endl;
cout << endl << endl;
cout << "Enter key : ";
cin >> curr_key;
while (valid_key_pressed(curr_key))
{
if (curr_key == 'c')
{ counter.incr1();
if (counter.overflow())
cout << "Overflow " << endl;
else
cout << "Money spend : "
<< counter.CurrCounterValue () << endl;
}
else
if (key == 'd')
{ counter.incr10();
if (counter.overflow())
cout << "Overflow " << endl;
else
cout << "Money spend : "
<< counter.CurrCounterValue() << endl;
}
else
if (key == 'u')
{ counter.incr100();
if (counter.overflow())
cout << "Overflow " << endl;
else
cout << "Money spend : "
<< counter.CurrCounterValue () << endl;
}
else
if (key == 't')
{ counter.incr1000 () ;
if (counter.overflow () )
cout << "Overflow" << endl;
else
cout << "Money spend : "
<< counter.CurrCounterValue () << endl;
}
else
if (key == 'o')
{ if (counter.overflow())
cout << "Overflow" << endl;
else
cout <<"No flow " << endl;
}
else
if (key == 'r')
{
counter.reset ();
cout << "reset " << endl;
}
cout << "Enter key : ";
cin >> curr_key;
}
cout << endl;
cout << "Money spend till now is ";
cout << counter.CurrCounterValue () << endl;
cin >> trial;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.