Need the program below to loop upon the user entering y(yes) or n(no). Write a p
ID: 3621749 • Letter: N
Question
Need the program below to loop upon the user entering y(yes) or n(no).
Write a program that reads in an integer number and checks whether any of the digits in the number appear more than once. Allow the user to repeat the process as many times as he likes. (allow for y/Y/n/N as responses)
Sample run:
Enter a number: 31345
It has a repeated digit
Do you want to enter another number? (y/n) : y
Enter a number: 12345678
No repeated digit
Do you want to enter another number? (y/n) : n
Plan: Use an array called digits_seen of size 10 whose base type is Boolean. It will be indexed from 0 to 9, which corresponds to the 10 possible digits a number can have.. Initially all the entries in the array should be set to false.
When the user enters his number n, the program reads in the entire integer and then examines each digit in n and sets the appropriate index entry in the array to true to indicate that it has been seen. If when it goes to do this, the entry is already true then you know you have encountered a duplicate!
#include<iostream>
using namespace std;
void initialize(bool digit_seen[]);
const int size=10;
int main()
{
bool digit_seen[size];
int new_value;
long int i;
initialize(digit_seen);
cout<<"Please enter an integer to verify whether there are repeated numbers: ";
cin>>digit_seen[0];
new_value=digit_seen[0];
for(i=0; i<size; i++)
{
if(digit_seen[i]==true)
new_value=digit_seen[i];
cin>>digit_seen[i];
}
if(digit_seen[i]!=new_value)
cout<<"There are no repeated numbers. ";
else
cout<<"There are repeated numbers. ";
return 0;
}
void initialize(bool digit_seen[])
{ int i;
for (i = 0; i <10; i++)
digit_seen[i]=false;
return;
}
HINT: use type long int to store the input number read in…so you can hold a lot of digits. Note that you reading a single integer and then dissecting it into digits.
Remember you can dissect the number read in by using % and integer division.
A quick example
Digit_seen is initially…..[false| false| false |false| false |false| false| false| false| false] remember the index runs from 0 to 9
Suppose the input number is 2131
You peel off the 1 and the array becomes
Digit_seen [false| true| false |false| false |false| false| false| false| false]
the true in position 1 indicates that you have seen a 1
Then peel off the 3
Digit_seen becomes [false| true| false |true| false |false| false| false| false| false]
the true in position 3 indicates that you have seen a 3
Then peel off the 1
When you look at Digit_seen [false|true| false |true| false |false| false| false| false| false]
When you go to pposition 1, you see that there is already a true there….that is you found a duplicate!
Explanation / Answer
Let me first explain where the program is wrong.
1) The digit_seen[10] array is used as a 'marker'.
If the number entered by the user is say 123, then digit_seen[3],digit_seen[2],digit_seen[1] should be set to true. (Note: rest are all false)
The program your wrote is accepting the number (123) and storing at digit_seen[0]. Remember 123 is treated as 'true' and hence digit_seen[0] is set to true. That is not supposed to occur.
I have modified the program and written it even simpler here it is:
#include<iostream>
using namespace std;
void initialize(bool digit_seen[]);
const int size=10;
int main()
{
bool digit_seen[size], repeat;
char ch = 'Y';
long int number; // Number holds the actual number entered by the user.
int digit; // this will hold the digits of the entered number
while(ch=='Y'||ch=='y')
{
initialize(digit_seen);
repeat = false;
cout<<"Enter a number: ";
cin >> number; // Accept numer her. Assume 31342 (for sake of explnation)
while(number>0)
{
digit = number%10; // Here the dissection of the number takes place. On the first run of the loop with input //number as 31342 - digit will hold 2
if(digit_seen[digit] == true)
{
cout << "It has a repeated digit ";
repeat = true;
break;
}
else
digit_seen[digit] = true; // marks the digit obtained in the digit_seen array. Here digit_seen[2] = true
number=number/10;// the enetred number is now divided by 10 and the fraction part is discared. Hence
// 31342/10 = 3134. Hence in the next iteration' digit' will be 4 and then again 3134/10 = 313. The //process goes on until the number becomes 0 (3/10 = 0) and the loop is exited
}
if(!repeat)
cout<<"No repeated digit ";
cout << " Do you want to enter another number? (y/n) : ";
cin >> ch;
}
return 0;
}
void initialize(bool digit_seen[])
{
int i;
for (i = 0; i <10; i++)
digit_seen[i]=false;
}
Hence as you can see the first while loop controls the program until user presses n or N and the while loop which is inside the outer while loop dissects and processes the number entered.
Hope this has helped.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.