Distinct number.. help

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 24
Reputation: wil0022 is an unknown quantity at this point 
Solved Threads: 0
wil0022 wil0022 is offline Offline
Newbie Poster

Distinct number.. help

 
0
  #1
Sep 25th, 2009
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..


  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 123
Reputation: cgyrob is on a distinguished road 
Solved Threads: 18
cgyrob's Avatar
cgyrob cgyrob is offline Offline
Junior Poster

Re: Distinct number.. help

 
0
  #2
Sep 25th, 2009
Use double ampersands && in your if statement.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 254
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Distinct number.. help

 
1
  #3
Sep 25th, 2009
Hello, wil0022.
Indeed, you can't do like this:
  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:
  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. }
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,442
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 626
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Distinct number.. help

 
1
  #4
Sep 25th, 2009
You can use LINQ too!
  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. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 24
Reputation: wil0022 is an unknown quantity at this point 
Solved Threads: 0
wil0022 wil0022 is offline Offline
Newbie Poster

Re: Distinct number.. help

 
0
  #5
Sep 28th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 24
Reputation: wil0022 is an unknown quantity at this point 
Solved Threads: 0
wil0022 wil0022 is offline Offline
Newbie Poster

Re: Distinct number.. help

 
0
  #6
Sep 28th, 2009
Originally Posted by sknake View Post
You can use LINQ too!
  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

  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..
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 465
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 92
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training

Re: Distinct number.. help

 
0
  #7
Sep 28th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 381 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC