I'm writing a program that could store 100 customer ID in an array.
Using foreach loop to check, If same ID exist more than 3 times, it will prompt for error.
Can anyone please help me with this, thanks;
int[] Cid = new int[5];
for (int i = 0; i < 100; i++)
{
Console.Write("Customer ID: ");
Cid[i] = int.Parse(Console.ReadLine());
foreach(int d in Cid)
{
if (??? Cid in array more than 3 times ???)
Console.WriteLine("YOU HAVE EXCEEDED NUMBER OF ID");
}
}
SamPosted Aug 4, 2007, 7:11 AM
Hi Keith,
Are the id[] Array and Cnric[] Array suppost to be the same?
Your program seem to work as is, however what i think Jan meant by loop to j - 1 was that the 2nd for loop should stop before j = i becaues then your if statement condition is equivilent to Cnric[i] == Cnric[i] which is bound to be true, so you program is not as efficient as it could. All you need to amend are the following lines:
count = 0; => count = 1; (there's bound to be at least 1 of each id!);
for ( int j = 0; j <= i; j++) => for ( int j = 0; j < i; j++)
Hope this helps,
Sam.
Edit: the '=>' is used in the sense of 'change to' rather than the C# 3.0 meaning
KeithPosted Aug 4, 2007, 5:28 AM
Hi Jan,
Check this out;
Can you incorporate the j-1 into my program, I want to see your version, thanks.
My assignment is due on Monday :(
int count = 0;
for (int i = 0; i < 10; i++)
{
Console.Write("Enter ID: ");
id[i] = int.Parse(Console.ReadLine());
if (i >= 0)
{
count = 0;
for (int j = 0; j <= i; j++)
{
if (Cnric[i] == Cnric[j])
count++;
Console.WriteLine("Count: " + count);
}
}
if (count > 3)
{
Console.Clear();
Console.WriteLine("YOU HAVE HOLD MORE THAN 3 ID");
break;
}
}
Jan MontanoPosted Jul 31, 2007, 9:26 PM
Assuming your array already contains 100 numbers.
for (int i = 0; i < 100; i++)
{
}
I think I overlooked something. Looking at your code, the user will input 100 number of times right? And for each input, you'll validate... if there exists 3 or more then error, otherwise process.
Clue:
1. you need an outer for loop i=0 to 99 input which you already have.
2. you need an inner for loop j=0 to i - 1. we'll need this to traverse your array which already has input.
3. for your condition, you need to compare cid[i] with cid[j] and check your counter variable for the number of occurence before assigning it to your array
Try to work around it first. Post your new solution then we'll walk you through it, if it doesn't work yet.
I just hope it's not yet deadline for your assignment. =)
KeithPosted Jul 31, 2007, 12:48 PM
Can I combine 'for' loop with 'foreach' loop. Like what you have said, I need 2 loops, inner and outer. Can I combine outer('for' loop) with inner ('foreach' loop)?
Can you help to give me some examples?
Jan MontanoPosted Jul 31, 2007, 12:46 AM
Is this a school assignment? (^_^)
Here's an idea.
You need 2 for loops here, an outer and an inner.
The outer loop will pass through all of it's contents.
Same goes with the inner loop.
The processing will be done in the inner loop, wherein you compare the content of the outer loop with the content of the inner loop.
You need to have a counter variable to track how many times the ID occured.