473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

please help, using array C++ printing "it has duplicate"if repeats

Hi,
I am a newbee in C++
Please review this is what I wrote , what so ever I understood so far,
My question is that I have to " Write a program C++ array that reads in an integer number and checks whether any of the digits in the number appear more than once"
Hint: 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 and initially all the entries should be set to false. When the user enters a number n, the program examines the digits and sets the appropriate index entry in the array to true. If when it goes to do this, the entry is already true then you have encountered a duplicate!

HINT2: use type long int to store the input number number read in…so you get a lot of digits.


I have just started few weeks ago so this what I know so far.. Please help
#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<<"Enter integers\n";
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<<"no repeat\n";

else

cout<<"repeat\n";


return 0;
}

void initialize(bool digit_seen[])

{

int i;

for (i = 0; i <10; i++)

digit_seen[i]=false;

return;

}
Nov 14 '06 #1
2 2953
You have been asked to use type long int to store the input number number read in…so you get a lot of digits.

The input for your program is a number that should be declared as long int so that you can have long number containing more digits.

The Logic I can explain you then you try to put it in program
Use one more array of size 10 corresponding to digits 0 to 9 which stores the count of the corresponding digits....... initially all counts ahould be zero
% operator gives you remainder
store the input number in temporary variable
use %10 to find the remainder that will be last digit of the number
check the number and increment corresponing count
then use /10 you will get the number in which last digit will be eliminated

repeat above till you get number after /10 as 0

then check in the count array if value is >1 then the corresponding digit appeared more than once
Nov 14 '06 #2
horace1
1,510 Expert 1GB
a simple way to solve this is to have a char arry which holds the digits of the number, e.g.
Expand|Select|Wrap|Line Numbers
  1. char digits[20];
  2.  
you would then have a loop which uses each element of digits as an index into your array digit_seen to see if it is already true, if so it prints "repeat" and exits the program e.g.
Expand|Select|Wrap|Line Numbers
  1. if(digit_seen[digits[i]]) { cout<<"repeat\n"; return; }
  2.  
if digit_sen is not true you set the element to true and then tests the next char in digits, etc.
Nov 14 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: LRW | last post by:
I'm not exactly sure how to even ask the question, and I know my terminology is not good as I'm a SQL beginner, but, here goes. I need to find a way to make an if statement within an array...or,...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
12
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
32
by: Stephen | last post by:
Is there a standard way to remove the warning that a C compiler might produce from the statement: if (a = b) {} I don't want to do: if ((a = b) != 0) {} Because my "a = b" is actually...
5
by: Stan | last post by:
When I create a Web project and then try to add the files to it (Add Existing Item), I get this error message "The folder http://localhost/FinWeb is no longer availabe" In fact the folder and...
13
by: Jim in Arizona | last post by:
I made a page with a gridview that has rows show a different color if a number in a column is greater than or equal to 45. I also did this conditional formatting for the column next to it. Here's...
3
by: Daniel Mark | last post by:
Hello all: I am looking the sample code posted on PIL website http://www.pythonware.com/library/pil/handbook/imagedraw.htm ################################################ <<Draw a Grey Cross...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.