|
Simplest? I don't understand this very much. Write a function that loops thru from 3 - sqrt(number) and if the mod results in 0 at any point then that number is not a prime. What??? This is really hard to understand I am only in my 4th week of programming and its moving to fast. I haven't done any programming before. Could you please help me?
|
|
|
|
|
Hey don't shout directly into my ear! Now im hearing a ringing allaround
And please explain why this algorithm should be easier to understand then mine
(its nearly the same except a bit more optimization)
|
|
|
|
|
I know I have to make a loop and check every number if it is a prime number,
But I'm still lost. I'm not sure on how to set it up. I don't know what an array list is either. This is only my fourth week in my programming class and I have never done any programming before, this stuff is really hard to understand.
|
|
|
|
|
Ok, as its Sunday and I have nothing really important to do...
public ArrayList FindPrimes(int maxNumber)
{
ArrayList result = new ArrayList();
for (int i = 3; i <= maxNumber; i += 2)
if (IsPrime(i))
result.Add(i);
return result;
}
public double GetPrimesAverage(int maxNumber)
{
return CalcAverage(FindPrimes(maxNumber));
}
public bool IsPrime(int number)
{
double max = Math.Sqrt(number);
for (int i = 3; i <= max; i += 2)
if (number % i == 0)
return false;
return true;
}
public double CalcAverage(ArrayList list)
{
double sum = 0;
foreach (int number in list)
sum += number;
return sum / list.Count;
}
Generally its the the algorithm I already stated (including some optimizations I initially didnt want you to take care of ).
I tried to split up the functionality. The function names should be descriptive enough to tell you want each part is doing. Note that IsPrime gives only valid results for uneven numbers.
As GetPrimesAverage gets an argument you can calculate the average for as much prime numbers as you want (cpu is the limit - dont test it with more than about 20 millions). To get the result for a maximum of 100 the following call had to be done:
GetPrimesAverage(100)
Do yourself the favor and read it carefully until you understand it. If you already know how to use a debugger it would be good if you just step through the code until you get it.
|
|
|
|
|
Dear Friends
I am developing my website in ASP.NET with C-Sharp I want to include my html file which contains the background image. how i can include the .html file in my index.aspx file.
Your quick response will be appriciated
Thanks
Murtuza.
|
|
|
|
|
hi,
take a look at <a href = "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpresponseclasswritefiletopic.asp" rel="nofollow">Response.WriteFile(string)</a>[<a href = "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpresponseclasswritefiletopic.asp" target = "_blank" rel="nofollow">^</a>]
David
Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy
|
|
|
|
|
to obfuscate any application on dot.net by add-ins i m geting all namespace ,class, memberfuntion,datamember.... but i m not getting the local variables and local funtions and the object derived from class... how to get that..
thanks in advance
|
|
|
|
|
Hi... i am an amateur programmer trying to do a context menu for a listbox in visual studio with C#. In the listbox there are several items and the context menu popups where ever you click in the listbox. I need that the context menu pops up only when the user right clicks a selected item from the listbox.
Please help
thanks
|
|
|
|
|
That's fairly simple. Create a context menu by drag/dropping a ContextMenu from the Toolbox. Subscribe to the MouseDown event of the listbox. Check if there are any items selected (listBox.SelectedIndex != -1). If true, then call contextMenu.Popup().
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
For this to function you will have to first disable the default functionality. For this set the ContextMenu property of the ListBox to null (which is default when adding it to the form).
Then subscribe to the MouseUp event of the ListBox and add the following code to it:
private void listBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
int index = listBox1.IndexFromPoint(e.X, e.Y);
if (listBox1.SelectedIndices.Contains(index))
contextMenu1.Show(listBox1, new Point(e.X, e.Y));
}
}
You can implement every thinkable kind of logic this way. You could even modify the ContextMenu depending on which item was clicked.
|
|
|
|
|
Hello,
I have a send method which sends data to a socket. To send the data first I read it to a byte array, but after the read it consists only of zeros. It's very strange to me because the buffer of the stream, which is a memory stream, contains the right values. I'm very confused, please help !
code:
public static void Send(Stream data)
{
byte[] byteData = new byte[data.Length];
data.Read(byteData,0, (int)data.Length);
Socket handler = state.workSocket;
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
}
|
|
|
|
|
Hmmm... the code seems definitely correct. Recheck if the buffer of the Stream is really correct (you can do this by browisng through its properties in the watch window) and do this right IN THE LINE where Read is called. This is important as the buffer could theoretically be changed from some code outside this block.
|
|
|
|
|
i have a temporary folder which contains DBF files, it might be 1 dbf file up too 40 dbf files
the structure of these DBF file are the same, i wrote a small app with C# to read these files one by one and add it to a main DBF file so i can run a report from one big DBF file. but this is a very slow solution.
this is a small example just adding two fields TYPE,EMPLOYEE
DirectoryInfo TMP_ReportDirectory = new DirectoryInfo(Application.StartupPath+"\\TMP");
FileInfo [] DbfFiles = TMP_ReportDirectory.GetFiles("*.dbf");
if (DbfFiles.Length != 0)
{
string ITMConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath+ "\\TMP;Extended Properties=dBase IV";
OleDbConnection ITMConnection = new OleDbConnection(ITMConnString);
string ITMSelectQuery = "Select TYPE,EMPLOYEE from " + DbfFiles[iloop].ToString();
//MessageBox.Show(ITMSelectQuery);
OleDbCommand ITMCommand = new OleDbCommand(ITMSelectQuery,ITMConnection);
ITMConnection.Open();
OleDbDataReader ITMReader = ITMCommand.ExecuteReader();
string STOCKConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath+ ";Extended Properties=dBase IV";
OleDbConnection STOCKConnection = new OleDbConnection(STOCKConnString);
while (ITMReader.Read())
{
frmwait.statusBar1.Text = " Loading . . . . . . .";
string newID = ITMReader.GetValue(0).ToString();
string newLONGNAME = ITMReader.GetValue(1).ToString();
string STOCKInsertQuery = "INSERT INTO GNDITEM.DBF (TYPE, EMPLOYEE) VALUES (" + newID +",\"" + newLONGNAME + "\")";
//MessageBox.Show(STOCKInsertQuery);
OleDbCommand STOCKInsertCommand = new OleDbCommand(STOCKInsertQuery,STOCKConnection);
STOCKConnection.Open();
STOCKInsertCommand.ExecuteNonQuery();
STOCKConnection.Close();
}
ITMConnection.Close();
ITMReader.Close();
STOCKConnection.Close();
}
|
|
|
|
|
This has to be slow. Dbf files dont have a very good performance and copying all this together for just some queries is probably a bit overhead.
Some hints you might try out:
1. Try out the Visual Fox Pro Ole Db Provider. It can be found somewhere on MSDN. I dont know it but its probably faster (I used it once and never had performance issues).
2. What kind of report are you making? Do you really need one big DBF file? Consider getting all data at once into one DataTable and then calculating whatever you need on this DataTable . You can use the UNION ALL statement to get all tables in one statement:
Select TYPE,EMPLOYEE from MyDbfFile1 UNION ALL Select TYPE,EMPLOYEE from MyDbfFile2 UNION ALL Select TYPE,EMPLOYEE from MyDbfFile3 ...
3. Use some real Database
|
|
|
|
|
thx man
i used UNION ALL STATMENT a lot faster than reading DBF files one by one and adding Records.
this code is with loop
string ITMConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + ";Extended Properties=dBase IV";
OleDbConnection ITMConnection = new OleDbConnection(ITMConnString);
string ITMSelectQuery = "Select * from "+ALOHA_PATH+"\\GNDITEM.DBF UNION ALL Select * from "+ALOHA_PATH+"\\"+datefolders.ToString()+"\\GNDITEM.DBF" ;
MessageBox.Show(ITMSelectQuery);
OleDbDataAdapter da= new OleDbDataAdapter(ITMSelectQuery ,ITMConnection);
da.Fill (dsrefresh,"GNDITEM");
dv = new DataView(dsrefresh.Tables["GNDITEM"]);
dataGrid1.DataSource = dv;
ITMConnection.Close();
i have a small problem to generate a report out of dataset, before i was using the main DBF to Generate Report. how can i solve this problem.
|
|
|
|
|
What do you mean with 'report'? Crystal report? If yes Im sorry to say I dont have much knowledge in making crystal reports
I can only help you if you state how your report creation functions so far. That includes what kind of report you are creating, what data it should present and how you currently bind the datasource to it.
|
|
|
|
|
Yup i want to Create a Crystal Report,
can i create a DBF file from my DATASET , if yes, then i can easily create a Crystal Report.
i tried to create a blank report, then set Report data source to DATASET but it didn't work.
thx Robert
|
|
|
|
|
As I said I have no experience with Crystal Report. But a quick googling showed up this.
It basically says you have to bind your report at design time to a XSD schema file and then bind the DataTable at runtime.
Hope this helps.
|
|
|
|
|
dataset i created with some merges, i want to put that dataset into another empty database that has the same datastruture already.
|
|
|
|
|
Is it possible to automatically set up a default "shortcut key" for a C# application at the time of its installation on a user machine, so that the user could launch the application by directly using the shortcut key combination such as ctrl + alt + S.
|
|
|
|
|
windows has a hot key registration process. Try the link below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/hotkey/hotkey.asp
|
|
|
|
|
g'day folks,
well i am stuck on a problem with regards to obtaining security permissions on folders. At the moment I have created a simple treeview example which shows all sub-directories in a tree form for a particular directory.
Then I wanted to see how the application would handle a sub-directory(test1) if the current user logged on win2k box didnt have the permissions to access test1. The application throws an exception which somethign along the lines of unauthorized access etc. I changed the permissions for test1 by right clicking and accessing the security tab.
I did some googling and came across couple of links on NTFS security permissions in .NET.
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=e6098575-dda0-48b8-9abf-e0705af065d9
http://weblogs.asp.net/cumpsd/archive/2004/02/08/69403.aspx
My problem is that I am having a hard time understanding those links. Does anyone have an alternative tutorial that would explain things a lot clearly interms of say obtaining the current ntfs security permissisons for a selected folder ?
thanks
|
|
|
|
|
Hello!
I am using the HttpWebRequest and HttpWebResponse classes to retrieve data from a web site which is UTF8 encoded. I am using a StreamReader which I have set to UTF8. When I read the stream into a string using ReadToEnd the UTF8 characters do not appear in the string when I debug. Instead it results in \0\0\0, null characters representing the UTF8 characters. As far as I can tell every medium used is Unicode friendly. Does anyone have an idea of what may be wrong?
I have searched the newsgroups as well as codeproject.com, but have found nothing. Thank you.
-Dan
|
|
|
|
|
What is the difference (pros and cons) between retrieving data from the web
using System.Web.WebClient and using HttpWebRequest and Response to get the
data? The WebClient download methods seem to neatly encapsulate the multiple
steps (request.Create; request.GetResponse; response.GetResponseStream ...)
required with the traditional HttpWebRequest/Response approach. I am trying
to crawl urls and download data.
|
|
|
|
|
Hi Jason,
As you already know that the WebClient class in fact uses the WebRequest internally to make a connection to the remote resource, and the HttpWebRequest is just a descendant of the abstract basic WebRequest class. The way I see it is the WebClient class provides the user with an easy and simple way to connect to request data from the remote resource, because it encapsulates the basic steps which you should have to do by yourself if you use the WebRequest. However, with the WebRequest and its descendants you will have more control to handle the way to request data from the remote resource. For more information, you can see Requesting Data[^]
|
|
|
|