This is the problem I have

Read in 10 numbers, 10<numbers<100. As each number is read, print it only if it is not a duplicate of a number already read.

I did the first part but couldn't work out the second,where the duplicate is used.

I have some idea in my mind but can't think of how i can write a loop that compares the i'th number to all numbers (Except itself)

``````#include<stdio.h>

int main (void)
{
int numbers[10];
int i;
for (i=1;i<=10;i++)
{
printf("enter %d'th value \n",i);
scanf("%d",&numbers[i]);
while ((numbers[i]<10) || (numbers[i]>100))
{
printf("\nInvalid Number!\nEnter again\n");
scanf("%d",&numbers[i]);
}
}
fflush(stdin);
getchar ();
return 0;
}``````

Recommended Answers

You could store the input numbers in an array and use a loop again to check if the current number has been entered before

you can use a boolean to know if the number has been repeated instead of using printf

For each number you read, loop over all of the numbers presently read into the array and compare with the number. If there's not a match, append the number to the array. Repeat until the array contains ten numbers.

All 10 Replies

You could store the input numbers in an array and use a loop again to check if the current number has been entered before

but should this be done in this way :

if
numbers = numbers[1] or
numbers = numbers[2] or
numbers = numbers[3] or
numbers = numbers[4] or
etc.

??

``````for(i=1;i<=10;i++)
{
for (j=1;j<=10;j++) {
if (numbers[i]!=numbers[j]) {
printf("-%d\n",numbers[i]);
}
}
}``````

I added this part, but I can't arrange it to work well. The program will be repeating the printf(numbers) 10 times .. for every j value..

commented: Should it? Or are there other better options? -4

you can use a boolean to know if the number has been repeated instead of using printf

you can use a boolean to know if the number has been repeated instead of using printf

how? .. can you at least help me with the logic ?

``````#include<stdio.h>

int main (void)
{
int numbers[10];
int i;
for (i=1;i<=10;i++)
{
printf("enter %d'th value \n",i);
scanf("%d",&numbers[i]);
while ((numbers[i]<10) || (numbers[i]>100))
{
printf("\nInvalid Number!\nEnter again\n");
scanf("%d",&numbers[i]);
}
}
for (i=1;i<=10;i++)
{
if ((numbers[i]!=numbers[0]) &&(numbers[i]!=numbers[1]) &&(numbers[i]!=numbers[2]) &&(numbers[i]!=numbers[3]) && numbers[i]!=numbers[4]) &&(numbers[i]!=numbers[5]) &&(numbers[i]!=numbers[6]) &&(numbers[i]!=numbers[7]) &&(numbers[i]!=numbers[8]) &&(numbers[i]!=numbers[9]))
{
printf("%d\n",numbers[i]);
}
}
fflush(stdin);
getchar ();
return 0;
}``````

I tried this

For each number you read, loop over all of the numbers presently read into the array and compare with the number. If there's not a match, append the number to the array. Repeat until the array contains ten numbers.

Are you supposed to
A) input 10 numbers and not output duplicates?
B) input numbers until 10 numbers are output?
It's not quite clear.

Another option to each of the above is to set an array of 101 integers (0 to 100).
Zero them all.
As you read in a number, test array[number]. If zero, print it. If not, it's a duplicate.
Add 1 to array[number].
Go back and read another number.

Another option to each of the above is to set an array of 101 integers (0 to 100).
Zero them all.
As you read in a number, test array[number]. If zero, print it. If not, it's a duplicate.
Add 1 to array[number].
Go back and read another number.

This would be a better option if you aim for a faster program, but it would eat up a bit more space. The methods posted above aren't faster but they do not consume space.

You can chose between speed or space.

You can chose between speed or space.

Or you can choose clarity. Given that we're talking about ten numbers in the range of 0 to 100, either method will be fast enough and space thrifty enough.

For each number you read, loop over all of the numbers presently read into the array and compare with the number. If there's not a match, append the number to the array. Repeat until the array contains ten numbers.

That is what I was trying to achieve. Well I did some research on the topic (I'm still learning parts here and there) and managed to achieve this .. even though I would have prefered the program to output the numbers as a batch in the end ..

``````#include<stdio.h>

int main (void)
{
int numbers[10];
int i,j;
for (i=1;i<=10;i++)
{
do{
printf("Enter %d'th value \n",i);
scanf("%d",&numbers[i]);
}
while (numbers[i]<10 || numbers[i]>100);
int found =0 ;
for (j=1;j<i;j++)
{
if(numbers[i]==numbers[j])
{
found=1;
}
}
if (found==0)
{
printf("%d\n",numbers[i]);
}

}
fflush(stdin);
getchar ();
return 0;
}``````

I think .. for results to be outputted as a batch at the end .. the values of found should be stored in an array and then checked in another loop after the input stage. Anyways but this will do for the problem purposes.

how? .. can you at least help me with the logic ?

If you're compiling in C99, you could #include <stdbool.h> and use bool, true and false. bool would be a macro for _Bool, which itself is a standard type guaranteed to hold 0 or 1, and true and false are macros for 1 and 0 respectively.

If not, you could just use 0 and 1 directly.But anyway you already got the idea

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.