943,754 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 785
  • C# RSS
Sep 25th, 2009
0

Distinct number.. help

Expand Post »
hi can anyone help me? i cant even run the program I have made...
This program should show the distinct numbers after you inputted numbers...

I really don't know what to do, hehehe..


C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Distinct
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int ten = 10;
  13. int[] first = new int[10];
  14. int[] final = new int[10];
  15. int counter2,counter3;
  16.  
  17. Console.Write("\nEnter 10 Numbers: ");
  18. for (int counter = 0; counter < ten; counter++)
  19. {
  20. first[counter] = Convert.ToInt32(Console.ReadLine());
  21. }
  22. for (counter2 = 0; counter2 < ten; counter2++)
  23. {
  24. if (final[0] & final[1] & final[2] & final[3] & final[4] & final[5] & final[6] & final[7] & final[8] & final[9] != first[counter2])
  25. {
  26. final[counter2] = first[counter2];
  27. }
  28. }
  29. for(counter3=0; counter3<ten; counter3++)
  30. {
  31. Console.WriteLine(final[counter3]);
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38. Console.ReadKey();
  39.  
  40. }
  41. }
  42. }


there's an error here, saying "Operator '&' cannot be applied to operands of type int and bool.
C# Syntax (Toggle Plain Text)
  1. for (counter2 = 0; counter2 < ten; counter2++)
  2. {
  3. if (final[0] & final[1] & final[2] & final[3] & final[4] & final[5] & final[6] & final[7] & final[8] & final[9] != first[counter2])
  4. {
  5. final[counter2] = first[counter2];
  6. }
  7. }

sorry.. I am extremely noob.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wil0022 is offline Offline
24 posts
since Aug 2009
Sep 25th, 2009
0

Re: Distinct number.. help

Use double ampersands && in your if statement.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Sep 25th, 2009
1

Re: Distinct number.. help

Hello, wil0022.
Indeed, you can't do like this:
c# Syntax (Toggle Plain Text)
  1. if (final[0] & final[1] & final[2] & final[3] & final[4] & final[5] & final[6] & final[7] & final[8] & final[9] != first[counter2])
I suppose this construction means "If first[counter2] isn't in the final array .."

If so - then the problem can be solved, e.g. by using nested loops:
c# Syntax (Toggle Plain Text)
  1. // the number of distinct values found
  2. int numberOfDistinctNumbers = 0;
  3.  
  4. // external loop will go through the entire First array
  5. // to check if the value already exist in the Final array
  6. for (int counter = 0; counter < ten; counter++)
  7. {
  8. // true - if the value of First[counter]
  9. // already exist in array
  10. bool alreayExist = false;
  11.  
  12. // then we go through the Final array
  13. for (counter2 = 0; counter2 < numberOfDistinctNumbers; counter2++)
  14. {
  15. // After the first occurrence of the value of First[counter]
  16. // in the Final array - we setting the alreayExist to
  17. // true and going out of that cycle
  18. if (first[counter] == final[counter2])
  19. {
  20. alreayExist = true;
  21. break;
  22. }
  23. }
  24.  
  25. // If we haven't found any occurrence of that number
  26. // in Final array - then it's distinct and we're adding it
  27. if (!alreayExist)
  28. {
  29. final[numberOfDistinctNumbers] = first[counter];
  30. numberOfDistinctNumbers++;
  31. }
  32. }
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Sep 25th, 2009
1

Re: Distinct number.. help

You can use LINQ too!
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5.  
  6. namespace daniweb.console
  7. {
  8. public static class Main5
  9. {
  10. public static void Main()
  11. {
  12. List<int> lst = new List<int>();
  13. Console.WriteLine("Enter 10 numbers");
  14. while (lst.Count < 10)
  15. {
  16. Console.Write("Enter number #[{0:F0}]: ", lst.Count + 1);
  17. int i;
  18. if (int.TryParse(Console.ReadLine(), out i))
  19. lst.Add(i);
  20. else
  21. Console.WriteLine("Invalid input. Please try again");
  22. }
  23.  
  24.  
  25. lst.Sort();
  26. int[] uniques = lst.Distinct().ToArray();
  27. Console.WriteLine("You entered the unique numbers: {0}", string.Join(", ", uniques.ToList().ConvertAll<string>(Convert.ToString).ToArray()));
  28. Console.ReadLine();
  29. }
  30. }
  31. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 28th, 2009
0

Re: Distinct number.. help

thanks guys..i will try this as soon as I installed the C# in my pc.. i formatted my pc last day... thanks.. thanks.. i'll post feedbacks if i have tried.. thanks a lot..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wil0022 is offline Offline
24 posts
since Aug 2009
Sep 28th, 2009
0

Re: Distinct number.. help

Click to Expand / Collapse  Quote originally posted by sknake ...
You can use LINQ too!
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5.  
  6. namespace daniweb.console
  7. {
  8. public static class Main5
  9. {
  10. public static void Main()
  11. {
  12. List<int> lst = new List<int>();
  13. Console.WriteLine("Enter 10 numbers");
  14. while (lst.Count < 10)
  15. {
  16. Console.Write("Enter number #[{0:F0}]: ", lst.Count + 1);
  17. int i;
  18. if (int.TryParse(Console.ReadLine(), out i))
  19. lst.Add(i);
  20. else
  21. Console.WriteLine("Invalid input. Please try again");
  22. }
  23.  
  24.  
  25. lst.Sort();
  26. int[] uniques = lst.Distinct().ToArray();
  27. Console.WriteLine("You entered the unique numbers: {0}", string.Join(", ", uniques.ToList().ConvertAll<string>(Convert.ToString).ToArray()));
  28. Console.ReadLine();
  29. }
  30. }
  31. }

i get this.. hehehe.. this works.. thanks..

maybe C# has been easier if my teacher is really nice..

but sir.. what is this

C# Syntax (Toggle Plain Text)
  1. List<int> lst = new List<int>();

is this a different way of declaring an variable?

is this a basic sir? hehe.. im sorry, so noob about the C# thing..

thanks a lot guys.. I learned more than when I'm at the lesson with my teacher.. gosh..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wil0022 is offline Offline
24 posts
since Aug 2009
Sep 28th, 2009
0

Re: Distinct number.. help

a List<> is a member of the System.Collections.Generic namespace. It allows you to create a Typed Collection of variables. In this case it is a collection of integers.

A list is a very flexible way of storing collections of variables as it allows sorting, finding, adding/deleting and doesnt require you to specify the size when instantiating it.
Last edited by Ryshad; Sep 28th, 2009 at 1:17 pm.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: map prob
Next Thread in C# Forum Timeline: Filling DataGrid in Windows Form App





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC