| | |
Distinct number.. help
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 24
Reputation:
Solved Threads: 0
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..
there's an error here, saying "Operator '&' cannot be applied to operands of type int and bool.
sorry.. I am extremely noob.
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)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Distinct { class Program { static void Main(string[] args) { int ten = 10; int[] first = new int[10]; int[] final = new int[10]; int counter2,counter3; Console.Write("\nEnter 10 Numbers: "); for (int counter = 0; counter < ten; counter++) { first[counter] = Convert.ToInt32(Console.ReadLine()); } for (counter2 = 0; counter2 < ten; counter2++) { if (final[0] & final[1] & final[2] & final[3] & final[4] & final[5] & final[6] & final[7] & final[8] & final[9] != first[counter2]) { final[counter2] = first[counter2]; } } for(counter3=0; counter3<ten; counter3++) { Console.WriteLine(final[counter3]); } Console.ReadKey(); } } }
there's an error here, saying "Operator '&' cannot be applied to operands of type int and bool.
C# Syntax (Toggle Plain Text)
for (counter2 = 0; counter2 < ten; counter2++) { if (final[0] & final[1] & final[2] & final[3] & final[4] & final[5] & final[6] & final[7] & final[8] & final[9] != first[counter2]) { final[counter2] = first[counter2]; } }
sorry.. I am extremely noob.
Hello, wil0022.
Indeed, you can't do like this:
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:
Indeed, you can't do like this:
c# Syntax (Toggle Plain Text)
if (final[0] & final[1] & final[2] & final[3] & final[4] & final[5] & final[6] & final[7] & final[8] & final[9] != first[counter2])
If so - then the problem can be solved, e.g. by using nested loops:
c# Syntax (Toggle Plain Text)
// the number of distinct values found int numberOfDistinctNumbers = 0; // external loop will go through the entire First array // to check if the value already exist in the Final array for (int counter = 0; counter < ten; counter++) { // true - if the value of First[counter] // already exist in array bool alreayExist = false; // then we go through the Final array for (counter2 = 0; counter2 < numberOfDistinctNumbers; counter2++) { // After the first occurrence of the value of First[counter] // in the Final array - we setting the alreayExist to // true and going out of that cycle if (first[counter] == final[counter2]) { alreayExist = true; break; } } // If we haven't found any occurrence of that number // in Final array - then it's distinct and we're adding it if (!alreayExist) { final[numberOfDistinctNumbers] = first[counter]; numberOfDistinctNumbers++; } }
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
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
You can use LINQ too!
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace daniweb.console { public static class Main5 { public static void Main() { List<int> lst = new List<int>(); Console.WriteLine("Enter 10 numbers"); while (lst.Count < 10) { Console.Write("Enter number #[{0:F0}]: ", lst.Count + 1); int i; if (int.TryParse(Console.ReadLine(), out i)) lst.Add(i); else Console.WriteLine("Invalid input. Please try again"); } lst.Sort(); int[] uniques = lst.Distinct().ToArray(); Console.WriteLine("You entered the unique numbers: {0}", string.Join(", ", uniques.ToList().ConvertAll<string>(Convert.ToString).ToArray())); Console.ReadLine(); } } }
•
•
Join Date: Aug 2009
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
You can use LINQ too!
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace daniweb.console { public static class Main5 { public static void Main() { List<int> lst = new List<int>(); Console.WriteLine("Enter 10 numbers"); while (lst.Count < 10) { Console.Write("Enter number #[{0:F0}]: ", lst.Count + 1); int i; if (int.TryParse(Console.ReadLine(), out i)) lst.Add(i); else Console.WriteLine("Invalid input. Please try again"); } lst.Sort(); int[] uniques = lst.Distinct().ToArray(); Console.WriteLine("You entered the unique numbers: {0}", string.Join(", ", uniques.ToList().ConvertAll<string>(Convert.ToString).ToArray())); Console.ReadLine(); } } }
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)
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..
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.
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.
![]() |
Similar Threads
- Count/Sum distinct problem (MS SQL)
- Least Time for Each Unique Number (MS SQL)
- Design: Distinct Datasets (Database Design)
- How to generate a list of permutations for a given binary number (Java)
- that number 666 (Geeks' Lounge)
- MSSQL and ASP Query (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: map prob
- Next Thread: Filling DataGrid in Windows Form App
Views: 381 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client code color control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file form format forms ftp function gdi+ http httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox save saving serialization server sleep socket sql statistics string table textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






