|
You can try numbers.Contains(intRnd). Hope this helps.
Good luck!
|
|
|
|
|
I believe my find is working alright, but I have to figure out how to load a number in the array if it is not in there.
WHEELS
|
|
|
|
|
I have an idea of how you can solve you're problem, below are the steps:
Objective: What do we know? We know that you are looking for 2 identical numbers always. If there is only 1 number, then add his duplicate.
Solution:
Supose the number we are looking for his duplicate is X, the first number of the array.
1. sort the array
2. ask for the neighbour of the number X, at right!
if duplicate is at right
increment index to the next X number (skip the duplicate we found!)
else
append the number to the array and repeat step 1
3. repeat until the end of the array.
This is the pseudocode of the solution I've taught.
Hope this helps.
Cheer's,
Alex Manolescu.
|
|
|
|
|
You cannot add to an array. You're going to have to use a list. If you don't know the difference between an array and a list, use google.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
John Simmons / outlaw programmer is right!
If you know the number of elements in you're array, you can create an array of dimension = elements x 2 (for the duplicates).
If you don't know the number of elements then you need to use another data structure, for example: a linked list!
Cheer's,
Alex Manolescu.
|
|
|
|
|
Alex Manolescu wrote: you can create an array of dimension = elements x 2 (for the duplicates).
But you would only do that if you're a retard, or completely new at programming.
Alex Manolescu wrote: If you don't know the number of elements then you need to use another data structure, for example: a linked list!
Just use a List<t> object. It lets you add/remove items, and even sort them. Jeeze!
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: Jeeze!
I've just replay my opinion. I've taught I could help to understand better the idea.
No need to involve Jesus here!
Cheer's,
Alex Manolescu.
|
|
|
|
|
Wheels012 wrote: Array.BinarySearch(numbers, 0, numbers.Length, intRnd);
It looks like you're just throwing random numbers (trying to, anyway) onto the end of the array, so how is this supposed to work if numbers is unsorted?
Like others have said, use a List and when adding to it insert items at their proper index to keep it sorted, or use a hash table.
|
|
|
|
|
I would definitely use a List(dynamic array) for what you want.
List<int> numbers = new List<int>();
private bool RndDuplicate(int intRnd)
{
if (numbers.Contains(intRnd))
return true;
else
{
numbers.Add(intRnd);
return false;
}
}
|
|
|
|
|
way too much code, and too slow for large sets.
HashSet set=new HashSet();
private bool RndDuplicate(int intRnd) {return !set.Add(intRnd);}
|
|
|
|
|
Wow nice one!
Thanks!
|
|
|
|
|
Thank you petercrab.
That is just what I am looking for.
Happy New Year, WHEELS
|
|
|
|
|
Hello everyone.
I want my program to print out the shown table in the DataGridView using a connected printer to the computer.
I've never done such thing.
Formatting is not important, I want to see the table on the printed paper as it is seen.
How is it done? Can anybody help?
|
|
|
|
|
|
oh thank you this is exactly what I'm looking for
|
|
|
|
|
My quest is simple and I have some ideas, but I wonder if you have a better idea
I have a binary file with some data in it, and I want to modify/replace data from position "x".
I've seen that if I want to modify/replace some data (bytes) from a binary file, I need to replace with the same bytes size.
Now I need a solution to do this.
I taught at a simple solution:
>read the binary file with a handler, and write to a new binary file with another handler
>when reading arrive at my "x" position, I write my own binary data.
My code is shown below:
public void MyWriteMethod()
{
int b;
int x = 153;
Stream str = File.Open("dT.man", FileMode.Open);
Stream strIndex = File.Open("dT.index", FileMode.CreateNew);
BinaryReader br = new BinaryReader(str);
BinaryWriter bw = new BinaryWriter(strIndex);
while ((b = br.BaseStream.ReadByte()) != -1)
{
if (br.BaseStream.Position == x)
{
bw.Write(1);
bw.Write("StringData");
bw.Write("StringData");
bw.Flush();
continue;
}
bw.Write(b);
}
bw.Flush();
bw.Close();
br.Close();
str.Close();
strIndex.Close();
}
Hope there is a better solution, something like seek into the binary file and replace the data.
Cheer's,
Alex Manolescu.
|
|
|
|
|
The Stream class has an overridable method seek
from MSDN:
For an example of creating a file and writing text to a file, see How
to: Write Text to a File. For an example of reading text from a file, see
How to: Read Text from a File. For an example of reading from and writing
to a binary file, see How to: Read and Write to a Newly Created Data File.
Use the CanSeek property to determine whether the current instance supports seeking.
If offset is negative, the new position is required to precede the
position specified by origin by the number of bytes specified by
offset. If offset is zero (0), the new position is required to be the
position specified by origin. If offset is positive, the new position
is required to follow the position specified by origin by the number of
bytes specified by offset.
Stream.Seek() method[^]
|
|
|
|
|
Thanks you verry much for answer!
I know about the method Seek, but there is a problem!
I want to modify/replace some binary data starting from the position "x", but when I try to modify data there is a problem with data integrity!
The new data may be bigger (in size) that the replaced data!!
I've seen that if I want to modify/replace some data (bytes) from a binary file, I need to replace with the same bytes size.
To be more concret, supose the binary file is organised like this: 1 integer, and 2 string. (this is called, by me, a record).
When I want to replace data from position "x", I have to replace with the same amount of bytes like the old data to not damage the other records!
Now, what if I don't want to replace with the same amount of bytes? What if the amount of bytes is bigger/smaller than the old data bytes size? Can I do this without damage other data (records) ? What is the best solution to do this?
Cheer's,
Alex Manolescu.
|
|
|
|
|
Hmm, Okay well an easy approach would be to add some padding to your file so it don't not matter, just use a greedy shuffling algorithm to move things around in the original.
If you create a data structure in your app, fill it with data, and then serialize it to a file with padding you can add what ever the data structure will allow. You would have a set size file, once you get bigger then the file size you would have to recreate the file.
Think of it as a loading problem, similar to a Hashtable. You create the Hashtable at approximately the correct working size. If the number falls within 70% of the total allocated space for the hashtable it 're-hashes' and increases to a larger size, usually 2x bigger.
|
|
|
|
|
Thanks you for answer!
I'll give it a try and come back with news.
Happy New Year!
|
|
|
|
|
Alex Manolescu wrote: The new data may be bigger (in size) that the replaced data!!
Then you'll have to read the entire file into memory, change the part that needs changing, and write the entire thing back out to the file again.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
Thanks you for answer!
I think my method, to read and write bytes from file to file without loading the memory, is working great for big files.
Happy New Year!
|
|
|
|
|
Hi,
some coding comments:
1. that continue is ugly; what you want is an if (...) ... else ...
2. please drop the flushes, they don't make any sense just before a close.
3. you can replace the close() by using a using construct.
FYI: BinaryWriter.Write(string) "Writes a length-prefixed string to this stream in the current encoding", which may or may not be what you want. And of course, inserting something in the middle of the file may break its format. Don't try it like that with an EXE file!
|
|
|
|
|
Thanks you verry much for answer!
You're right about the flushes, but I am missing something at that
continue ?
Is more efficient to use a if/else statement or why is ugly?
I am just curious and I want to know
I've wrote a replay above yours.
Hope I've been more specific to what I want!
Cheer's,
Alex Manolescu.
|
|
|
|
|
Hi,
1.
if (br.BaseStream.Position == x) {
bw.Write(1);
bw.Write("StringData");
bw.Write("StringData");
} else {
bw.Write(b);
}
is much cleaner; performance is the same.
2.
damage to file format depends on file type. You could not do that for a Word document, an EXE file, etc.
You could do it for your own data files if your apps accept such changes.
|
|
|
|