|
how to make a program multithread to find a max number in array??
sample code please..
|
|
|
|
|
How about showing us what you've tried so far?
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|

#include <stdio.h>
#include <windows.h>
DWORD Sum;
DWORD WINAPI Summation (LPVOID param);
typedef struct{
int start;
int end;
} SUM_INFO;
const int numOfThreads = 5;
SUM_INFO *sumInfo[numOfThreads];
DWORD sumThreads[numOfThreads];
int main(int argc,char *argv[])
{
DWORD ThreadsId;
HANDLE *ThreadHandle;
int Param ,i = 0,j = 0;
DWORD totalSum = 0;
if (argc != 2 ){
fprintf( stderr,"An integer parameter is required\n");
return -1;
}
Param = atoi (argv [1]);
printf ("param = %d \n",Param);
if (Param < 0){
fprintf (stderr," an integer >= 0 is required \n" );
return -1 ;
}
ThreadHandle = (HANDLE *)malloc (5 * sizeof(HANDLE));
for (j = 0; j < numOfThreads ; j++){
sumInfo[j] = (SUM_INFO *) malloc (sizeof (SUM_INFO));
sumInfo[j] -> start = ( j * (Param/numOfThreads)) + 1 ;
sumInfo[j] -> end = (j+1)*(Param/numOfThreads);
WaitForSingleObject(ThreadHandle[j-1],INFINITE);
ThreadHandle[j] = CreateThread (NULL,0,Summation,sumInfo[j],0,&ThreadsId);
if (ThreadHandle == NULL)
GetLastError();
}
WaitForMultipleObjects(numOfThreads,ThreadHandle,TRUE,INFINITE);
CloseHandle(ThreadHandle);
for (i=0;i< numOfThreads;i++)
totalSum +=sumThreads[i];
printf("total sum = %u\n",totalSum);
}
DWORD WINAPI Summation (LPVOID Param)
{
static int threadCount = 0;
printf("Thread %d\n is started\n",threadCount);
SUM_INFO *temp = (SUM_INFO * )Param;
DWORD start = (DWORD ) temp -> start;
DWORD end = (DWORD ) temp -> end;
printf("start = %u \n ",start);
printf("end = %u \n",end);
DWORD i,sum= 0;
for (i = start ; i<= end;i++)
sum+= (DWORD)i;
sumThreads[threadCount] = sum;
printf("sum Threads [%d]= %u\n",threadCount,sumThreads[threadCount]);
threadCount++;
return 0;
}
like this,actually the code is to count number 1-5000000 with multi threads,but i don't know,how to changes this code to find the max number.
|
|
|
|
|
Somehow I don't think that that code is Visual Basic - try posting in the C++ forum
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
|
hello guys I'm having a problem with a project that I build
I'm trying to import excel files to sql server 2016 using bulkcopy and I got this error
The Microsoft Office Access database engine could not find the object 'C:\Users\WilliamNick\Desktop\DAFTAR1.xlsx'. Make sure the object exists and that you spell its name and the path name correctly."
I think there is a problem with this code
Dim query_excel As String = "SELECT * from [" & file & "$]"
i tried all of this code on another pc and it works perfectly but on my pc it's getting that error, I don't know what did I wrong, I'm using visual basic 2010 professional edition, sql management server 2016 and microsoft office 2016
can anyone help me to figure what is wrong with the code ?
<pre>Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
file = System.IO.Path.GetFileNameWithoutExtension(TextBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim koneksi_excel As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ace.OLEDB.12.0;Data Source='" & TextBox1.Text & "';Extended Properties=""Excel 12.0 Xml;HDR=YES;""")
koneksi_excel.Open()
Dim query_excel As String = "SELECT * from [" & file & "$]"
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(query_excel, koneksi_excel)
Dim rd As OleDb.OleDbDataReader
rd = cmd.ExecuteReader()
Dim koneksi As New SqlClient.SqlConnection()
Dim koneksidatabase As String = "server=DESKTOP-KJQ8PNO\SQLEXPRESS;database=otto;Integrated Security=True"
koneksi.ConnectionString = koneksidatabase
koneksi.Open()
Dim da As New OleDb.OleDbDataAdapter
Dim ds As New DataSet()
Dim dt As New DataTable
ds.Tables.Add(dt)
da = New OleDb.OleDbDataAdapter(query_excel, koneksi_excel)
da.Fill(dt)
Using bulkcopy As SqlClient.SqlBulkCopy = New SqlClient.SqlBulkCopy(koneksi)
bulkcopy.DestinationTableName = file
bulkcopy.BulkCopyTimeout = 600
bulkcopy.WriteToServer(rd)
rd.Close()
MsgBox("Data uploaded to database", MsgBoxStyle.Information, "Uploaded")
TextBox1.Text = ""
End Using
End Sub
|
|
|
|
|
Try renaming the file and remove any special characters in the file or path.
|
|
|
|
|
I need a macro that will hide and unhide rows and columns that contain all zero values. I strated with the following code that hide and unhide columns with zero but it does not work. Does anyone know how to fix it.
Sub HideEmptyColumns()
' This macro hides empty or all-zero columns in the active worksheet
' It ignores columns outside the used range (UsedRange)
Dim Col As Range
For Each Col In ActiveSheet.UsedRange.Columns
Col.EntireColumn.Hidden = AllZero(Col)
Next Col
End Sub
Sub UnhideColumns()
' This macro unhides all columns on the active worksheet
Columns.Hidden = False
End Sub
Function AllZero(R As Range) As Boolean
' This function returns TRUE if the range is entirely empty or all zero
Dim C As Range
AllZero = True
For Each C In R.Cells
If C.Value <> 0 Then
AllZero = False
Exit For
End If
Next C
End Function
|
|
|
|
|
"It does not work" is not helpful. What happens? Have you tried debugging it?
|
|
|
|
|
When I run the macro in Excel the columns with zero values are not hidden.
|
|
|
|
|
Change the if-statement to
If Not IsEmpty(C.Value2) And C.Value2 <> 0 Then
|
|
|
|
|
Thank you. I will change the If statement
|
|
|
|
|
Member 13089786 wrote: the columns with zero values are not hidden. All you have to do is debug the code. As I recall, F8 will step you through it and you can see exactly what is happening. Very simple.
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
DES Coding on visual studio 2010
|
|
|
|
|
And now ... do you have a question ...?
|
|
|
|
|
|
I am currently writing a business application which is a collaborative tools (e.g. messaging, file sharing, reminders, etc.) which uses a client/server model. One thing I'm currently stuck on is how the client application can locate the server without requiring each user to enter the IP address in their own application. From research I've done, it appears that there are two ways this can be accomplished.
1. I can require that the system admin (who installs the server application) to create an A record for the server and then create an SRV record which would list the port and the hostname (from the A record). However this would require the network to have a DNS server.
2. I can use IP multicasting. On a predefined port, the server can send out a message at regular intervals which contains its IP address and the port to connect on. When the client application starts up, and during login, the client application can listen for this message to determine where the server is located.
If the second option is viable, any suggestion on how often the multicast message should be sent out? It should be often enough that by the time the client application has finished loading, and the user has begun the login process, it has already identified the local server. However, I'm worried about sending it too often and causing network congestion.
I am leaning towards option two as it doesn't require any DNS records to be updated and seems to be easier for whoever installs the server application. I was wondering if anyone else has had to do something similar and had any suggestions on either of these two methods, or any other method that may work better.
Please note that I am using .NET 4.6, server application is a windows forms application, and the client application is being designed with WPF; WCF is not being used for this project. Any comments or suggestions would be greatly appreciated. Thank you in advance for any help in this matter.
-Dominick
A black hole is where God tried to divide by zero.
There are 10 kinds of people in the world; those who understand binary and those who don't.
|
|
|
|
|
Why make life more complicated than it needs to be? Just provide the IP address to each client in a configuration file.
|
|
|
|
|
Dominick Marciano wrote: server application is a windows forms application Are you sure? Really sure? That means that someone has to log in to that server and start that application in his user session. It cannot be run as a Windows Service anymore. And just imagine the server being re-started (e.g. because of updates): your server application won't be restarted. Or: the user in whose session the application is running logs off (instead off disconnects the RDP session), or ....
|
|
|
|
|
Why go with only one method?
No matter what, you're going to have to do some setup on the server side. There are going to be clients who don't want your "here I am!" packets polluting their network every 10 seconds or so. These can also be spoofed by the way which is a security risk.
There are also clients who are going to want that method for ease of configuration.
You can also support the DNS approach but you're still going to have to support manual entry of the address the user wants to use, such as in a situation where they have more than one server up and running, possible one for production use and another for testing.
Now you've got to let the users choose which server. Possibly limiting that the admin setting only, ...
There's a LOT more to this than what you're thinking about in your question.
|
|
|
|
|
Hi guys,
facing a strange problem where a device, normally seen by the system as a serial port and mapped to a COM port, does not behave in same way when connected to a USB3.0 port.
Them COM port is activated, same way as when using USB2.0 and it is initialized exactly in same way (115200, 8N1) but data coming trough are not readable, corrupted or at a different speed (?).
Haven't went deeper for the moment.
Does anyone know something basic remarkable rule about such behaviour/application?
Should a USB3.0 port act exactly same as a USB2.0 port when a serial device is attached (and correctly recognized) to it?
Or is there something extra to be done/known when working through USB3.0?
Any hint is welcome
SepPax
|
|
|
|
|
|
Hello,
How can i sort the outputs in ascending order (Bubble sort)
Console.Write("Enter x: ")
x = Console.ReadLine
Console.Write("Enter y: ")
y = Console.ReadLine
For count = 1 To 5
NumGen = rnd.Next(x, (y + 1))
Console.WriteLine(NumGen)
Next
Console.ReadLine()
|
|
|
|
|
We are not here to do your homework for you!
It is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!
Start by reading the values into an array, then write a method to do the sort. That's not difficult, the algorithm is pretty basic and freely available: Bubble sort - Wikipedia[^]
If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
First save the values in an array, then sort.
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|