Array containing only 0 and 1 values [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need an array which will only contain the values 0 and 1. Will bool[] be good enough for me? Or is there something lighter weight?
EDIT:
I dont have memory constraints but that array is made and passes online All The Time with big files passes concurrently with that array. I want the maximun optimization so the big files wont be delayed

A bool is probably not the best way to do it. Depends how many numbers you have got.
It is important to realise that even though a bool is a single bit, it requires a full byte in memory.
A BitArray on the other hand takes care of this for you and is more space efficient, although ever so slightly less time efficient.
http://msdn.microsoft.com/en-us/library/system.collections.bitarray.aspx
Depends on your constraints, if it is not for a constrained environment a bool array will work just fine.

Bool Array is good enough. You can consider "false" as 0 and "true" as 1.

May be you need BitArray, the sequence of 1 and 0.

bool[] would do the trick... If your 0 & 1 numbers are in fact just "Flags" and not real numbers.
Using an Enum with the Flags attribute is another option. This would allow you to have an Intention Revealing Name for both boolean values.
[Flags()]
public enum TheFlags
{
NoneSet = 0,
FirstSet = 1,
SecondSet = 2
}
Then you can check if "First" is set like so:
TheFlags flags = TheFlags.FirstSet;
if (flags.HasFlag(TheFlags.FirstSet))
Console.WriteLine("First flag is set!");

Related

Meaning of -1 in Programming [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I hate to ask this question on here, but I have searched both SO and Google to no success. I have seen in many places statements such as while(var != -1) and other statements, often loops, containing some sort of reference to -1. Is there a certain meaning to the use of -1, or is it just used as giving an integer representation of a boolean, or something like that? I have mainly seen this in C# programming if that is any help.
in C# -1 is just negative one. They're comparing a number against a number, seeing if it is indeed equal to negative one.
It's not uncommon to have an integer field that should only have positive values (for example, when representing an index in a list) and in such cases -1 is sometimes used to represent "not a valid value", for example, there is no item, and hence no index. They use -1 because an int is not nullable; they cannot assign null.
In theory this is probably a bad practice; it's using a "magic value" to mean something more than it really should. Ideally if "there is not valid" is a valid thing for the variable to represent it should be a nullable integer (int? or Nullable<int>) but this is an old convention (carried over from other languages without a feature for nullable ints) so it's hard to eliminate entirely.
Nothing special about it. It's just that in most frameworks and libraries, functions or methods that return an index of an element in a collection will return -1 when whatever you're looking for isn't in the collection.
For example, the index of the character b in the string foo would be -1 in JavaScript, .NET and, as far as I remember, Java as well.
So many devs have burned a rom in their minds saying that -1 is the index for not found items. Now you know why.
If you know that an int should always contain positive value (for instance an item count or an index in a list, -1 can be a kind of "reserved value", so you would for instance assign the count to -1 and as long as it's -1, you know no real value has been put in there, a bit like a "null"
other than that I don't think there's any special meaning to -1

Function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Could someone do this for me please.
Write a function that returns an array of integers with 1000 elements containing the values 1 to 1000 in random order. No number can be repeated or ommitted.
Here is the hint to solve the above question: use Random rnd=new Random() to create a new instance of the random number generator and rnd.Next(1,1000) to get a random integer value between 1 and 1000.
Many Thanks.
I suspect your examiner is probably looking for a Fisher-Yates Shuffle, so the following answer is probably too concise.
Random r = new Random();
public int[] GetArr()
{
return Enumerable.Range(1, 1000).OrderBy(_ => r.Next()).ToArray();
}
did you try :
-generate a random number
-check if he is allready in the tab
-put it in your array,in first free position
the only problem will be that it will drastycally slow down on the end
You might want to try something where you create a class which contains an int and a bool, the bool can be "isused", then the int can be the index, then use a random number generator, get the number associated with the output, then check if that number is in use by an array of your class, if not then push it onto a list of ints, then once completed return the list as an array?

Character Shuffling and Re-Arranging in c# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
In my one application i want to shuffle string contents and in another application if i pass this shuffled string as input then that application has to return original string value.Is there any method is available to do this in dotnet plateform?
you can use Fisher–Yates shuffle algorithm.
it is an algorithm for generating a random permutation of a finite
set—in plain terms, for randomly shuffling the set.
The basic method given for generating a random permutation of the numbers 1–N goes as follows:
Write down the numbers from 1 to N.
Pick a random number k between one and the number of unstruck
numbers remaining (inclusive).
Counting from the low end, strike out the kth number not yet struck
out, and write it down elsewhere.
Repeat from step 2 until all the numbers have been struck out.
The sequence of numbers written down in step 3 is now a random
permutation of the original numbers.
there is an example : shuffle algorithm
and good one here
Definitely not out of the box, but create yourself a struct
struct ShuffleChar{
char c;
int index;
}
and when you shuffle your letters, assign each letter an index so you can put them together again. Remember, a string only makes sense if the letters are in the correct order, changing that order will destroy that information and with high probability you cannot restore it...

Distribute integers for tag cloud weighting [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a list of words that I want to display in a web page as a tag cloud. Each word has a corresponding 'weight' which determines how big the word should appear in the tag cloud.
Let's say that this array contains the number of times each word has been used to tag a document and I want to use these values as the weighting for each word:
int [] ints = new int[] { 1, 2, 4, 3, 2, 1, 4, 2, 1000};
I want the range of weightings to be within a specified range such that the rendering code has a predictable set of numbers to deal with however I don't want to simply normalize these integers because then all but the last will be essentially zero.
If this were to happen then there'd be a single item in the tag cloud that would be very big and all the other tags would be tiny. I'm looking for a way of squeezing all the of the integers into a limited range whilst preserving a degree of diversity.
So how can I transform the set such that the final large value doesn't make the others insignificant?
Use logarithms to pre-process the data and bring down the scale of the numbers. Choose the base to the logarithm based on the data values. For the purpose of a tag cloud, using log to base 2 would be most ideal in my opinion. Once you have found the log, normalize the resulting numbers.

What is the lightest collection for store these values? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have many objects (such as 10.000, more or less). Every object has3 values :
Index (decimal, such as 0,0 <= X <= 100.000,9);
A Latitude value;
A Longitude value;
and I need to perform some search due to the Index value. Which will be the light approch to this? List<MyObject>? I know there are hashtable, but only for 2 values...
I read these values from a .csv file and I'll store it on application. WebForm, .NET 4.5.
The very lightest approach in terms of memory use is to put these into a struct, and hold them in an array of such structs. From what you say, you can't really pack data any tighter than that: two doubles and a decimal will occupy 32 bytes per entry, and the array of structs does not add any per-item overhead on top of this.
Having said that, this will slow down your coding and might save too little to matter in practice.
Why don't you use a Dictionary like this:
public class Position
{
public Latitude Latitude { get ; set ; }
public Longitude Longitude { get ; set ; }
}
public Dictionary<decimal,Position> Positions ;
Or use a Tuple in the dictionary:
public Dictionary<decimal,Tuple<Latitude,Longitude>> Positions ;
I believe the absolutely lightest approach would be bitmasking your values into an unsigned long, though it is slightly cumbersome.
To really get the grasp on which is the most efficient approach, i recommend trying them all with test values and looking at the output of sizeof() on them. that way you'd be really sure what's their runtime memory size.
I'd suggest a custom struct to hold your values, a tuple could work as well.

Categories