bool lock[N];
int turn=0;
int offset=0;
int M = N-1;
int pidToN(int pid); //returns a unique number between (0,N-1) for a given pid; mapping pids
void critical()
{
int pidn = pidToN(getpid());
lock[pidn] = true;
turn = M-pidn;
if(turn == pidn)
{
val=1;
turn+=val%N;
}
else
val=0;
while(lock(M-pidn+val) && turn == (M-pidn+val) && lock(M-pidn-val) && turn == (M-pidn-val));
//critical section
lock[pidn] = false;
}
Does this implemention work? Essentially thread[i] tries to pass to thread[N-1-i] and vice versa. If i = N/2 (the thread at the middle, if it exists, which passes to itself) then I increment it by certain val (1 in this case) which then waits.
Couldn't come up with any race conditions.
Any help would be appreciated.
I don't know how to convert a word into complete lowercase in cs50. I have to convert words into lowercase to check it properly.
Below is my code so far
bool check(const char *word) {
char *lword[strlen(word)];
for (i = 0; i < strlen(word); i++) {
lword[i] = tolower(int
word[i]);
}
node *current;
int hashnum = hash(word);
if (table[hashnum] == NULL)
return false;
current = table[hashnum];
while (current->next != NULL) {
if (strcmp(current->word, word) == 0)
return true;
else
current = current->next;
}
return false;
}
This declaration char *lword[strlen(word)]; is a problem. it declares lword as an array of strings (aka char*). An array of chars would be more appropriate. (Also, program would probably complain when lword is sent as an argument to hash function.) Don't forget to declare the lower case word large enough to accommodate the null-terminator, and null-terminate it. Don't forget to send the lower-case word to hash, instead of the original word.
I've completed about half of my assignment where I have to count the "chickens" in a string, remove the chickens, and return the amount of times I have to remove them.
public static int countChickens(String word)
{
int val = word.indexOf("chicken");
int count = 0;
if(val > -1){
count++;
word = word.substring(val + 1);
//I'm aware the following line doesn't work. It's my best guess.
//word.remove.indexOf("chicken");
val = word.indexOf("chicken");
}
return count;
}
As is, the program counts the correct amount of chickens in the word itself. (Sending it "afunchickenhaschickenfun" returns 2.) However, I need it to be able to return 2 if I send it something like "chichickencken" because it removed the first chicken, and then the second chicken came into play. How do I do the remove part?
Not tested and writen in sudo code, but should give you a better idea on a way to approach this.
int numberOfChickens = 0;
public void CountAndReplaceChicken(string word)
{
int initCheck = word.indexOf("chicken");
if (initCheck > -1)
{
word = word.remove.indexOf("chicken"); // not sure about the syntax in Eclipse but given you figure this part out
numberOfChickens++;
int recursionCheck = word.indexOf("chicken");
if (recursionCheck > -1)
CountAndReplaceChicken(word);
}
}
Okay, the teacher showed us how to do it a few days later. If I understood David Lee's code right, this is just a simplified way of what he did.
public static int countChickens(String word)
{
int val = word.indexOf("chicken");
if(val > -1){
return 1 + countChickens(word.substring(0, val) + word.substring(val + 7));
}
return 0;
}
Question:
Write an efficient algorithm that searches for a value in an m x n matrix.
This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
class Solution {
public:
/**
* #param matrix, a list of lists of integers
* #param target, an integer
* #return a boolean, indicate whether matrix contains target
*/
bool searchMatrix(vector<vector<int> > &matrix, int target) {
bool flag = false;
int mbegin = 0, mend = matrix.size() - 1;
// target in matrix?
while (mbegin <= mend) {
int mmid = (mbegin + mend) / 2;
vector<int> sub = matrix[mmid];
int begin = 0, end = sub.size() - 1;
// target in sub?
if (sub[0] <= target && target <= sub[end]) {
while(begin <= end) {
int mid = (begin + end) / 2;
if (sub[mid] == target) {
flag = true;
return flag;
}
else if (sub[mid] < target) {
begin = mid + 1;
}
else {
end = mid - 1;
}
}
}
else if (sub[end] < target) {
mbegin = mmid + 1;
}
else{
mend = mmid - 1;
}
}
return flag;
}
};
input
[[1,5,9,13,18,25,28,33,39,43,44,51,55,56,63,68,73,77,80],[100,117,141,164,181,199,218,241,265,285,310,326,341,354,370,380,397,408,420],[433,453,475,494,506,518,536,550,568,585,609,626,651,662,676,698,716,729,746],[766,791,809,829,844,869,894,916,930,947,967,981,992,1012,1036,1059,1083,1099,1116],[1137,1150,1163,1177,1199,1215,1231,1243,1268,1283,1304,1326,1344,1362,1387,1408,1428,1438,1461],[1485,1499,1521,1546,1566,1584,1606,1622,1637,1656,1681,1705,1726,1744,1758,1779,1802,1827,1842]]
1084
Expected
false
but there are nothing showed up! why?
Taking into account the two properties, here are the optimizations possible for your algorithm:
You can skip searching rows that begin with a number greater than the one you seek.
In each row, when you reach a number greater than the one you seek, you can stop searching.
You have to look in all rows that can't be excluded by these criteria. The specification doesn't if you must find all occurences. It also doesn't tell if the value can be missing, if it is guaranteed to occur once or if it can occur multiple times.
With int mmid = (mbegin + mend) / 2;, it looks as if you are trying to do a binary search.
My MPI code deadlocks when I run this simple code on 512 processes on a cluster. I am far from the memory limit. If I increase the number of procesess to 2048, which is far too many for this problem, the code runs again. The deadlock occurs in the line containing the MPI_File_write_all.
Any suggestions?
int count = imax*jmax*kmax;
// CREATE THE SUBARRAY
MPI_Datatype subarray;
int totsize [3] = {kmax, jtot, itot};
int subsize [3] = {kmax, jmax, imax};
int substart[3] = {0, mpicoordy*jmax, mpicoordx*imax};
MPI_Type_create_subarray(3, totsize, subsize, substart, MPI_ORDER_C, MPI_DOUBLE, &subarray);
MPI_Type_commit(&subarray);
// SET THE VALUE OF THE GRID EQUAL TO THE PROCESS ID FOR CHECKING
if(mpiid == 0) std::printf("Setting the value of the array\n");
for(int i=0; i<count; i++)
u[i] = (double)mpiid;
// WRITE THE FULL GRID USING MPI-IO
if(mpiid == 0) std::printf("Write the full array to disk\n");
char filename[] = "u.dump";
MPI_File fh;
if(MPI_File_open(commxy, filename, MPI_MODE_CREATE | MPI_MODE_WRONLY | MPI_MODE_EXCL, MPI_INFO_NULL, &fh))
return 1;
// select noncontiguous part of 3d array to store the selected data
MPI_Offset fileoff = 0; // the offset within the file (header size)
char name[] = "native";
if(MPI_File_set_view(fh, fileoff, MPI_DOUBLE, subarray, name, MPI_INFO_NULL))
return 1;
if(MPI_File_write_all(fh, u, count, MPI_DOUBLE, MPI_STATUS_IGNORE))
return 1;
if(MPI_File_close(&fh))
return 1;
Your code looks right upon quick inspection. I would suggest that you let your MPI-IO library help tell you what's wrong: instead of returning from error, why don't you at least display the error? Here's some code that might help:
static void handle_error(int errcode, char *str)
{
char msg[MPI_MAX_ERROR_STRING];
int resultlen;
MPI_Error_string(errcode, msg, &resultlen);
fprintf(stderr, "%s: %s\n", str, msg);
MPI_Abort(MPI_COMM_WORLD, 1);
}
Is MPI_SUCCESS guaranteed to be 0? I'd rather see
errcode = MPI_File_routine();
if (errcode != MPI_SUCCESS) handle_error(errcode, "MPI_File_open(1)");
Put that in and if you are doing something tricky like setting a file view with offsets that are not monotonically non-decreasing, the error string might suggest what's wrong.