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..

``````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++)
{
}
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]);
}

}
}
}``````

there's an error here, saying "Operator '&' cannot be applied to operands of type int and bool.

``````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. :(

5
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by Geekitygeek

Use double ampersands && in your if statement.

Hello, wil0022.
Indeed, you can't do like this:

``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:

``````// 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]
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++;
}
}``````
turkish always support ukrainian

You can use LINQ too!

``````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;
else
}

lst.Sort();
int[] uniques = lst.Distinct().ToArray();
Console.WriteLine("You entered the unique numbers: {0}", string.Join(", ", uniques.ToList().ConvertAll<string>(Convert.ToString).ToArray()));
}
}
}``````

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..

You can use LINQ too!

``````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;
else
}

lst.Sort();
int[] uniques = lst.Distinct().ToArray();
Console.WriteLine("You entered the unique numbers: {0}", string.Join(", ", uniques.ToList().ConvertAll<string>(Convert.ToString).ToArray()));
}
}
}``````

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

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

but sir.. what is this

``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.

Edited by Geekitygeek: n/a