I have a buffer with IDs which looks like this
InBuffer={ID1,ID2,ID3,...}
I need to iterate through, every time using the ID in a function that returns pointers to the Data assigned to this ID and the size of the Data. I then need to fill in another buffer with the result which is of the form
OutBuffer={ID1,SIZE1,DATA1.WORD1,...,DATA1.WORDSIZE1,
ID2,SIZE2,DATA2.WORD1,...,DATA2.WORDSIZE2,
...,
IDN,SIZEN,DATAN.WORD1,...,DATAN.WORDSIZEN
}
I am having problems with forming the whole for-loop for this and the indexing of it, mainly because each SIZE variable can be different. It should be simple but I can't seem to make it work.
Thanks in advance for any help.
// For example
// Iterate through the remaining of the Request Buffer (m=0,1 already set)
for (m = 2; m < InBuffer; m++)`
{
OutBuffer[m] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m], &SIZE, &DATA);
OutBuffer[m + 1] = SIZE; // e.g. SIZE = 2, therefore DATA has 2 fields
OutBuffer[m + 2] = DATA.1; // first field
OutBuffer[m + 3] = DATA.2; // second field
// and so on
}
The first thing I notice is that you're using m to index both buffers:
for (m = 2; m < InBuffer; m++)
{
OutBuffer[m]=InBuffer[m];
but then you're using offsets from m for the additional data in OutBuffer:
OutBuffer[m+1]=SIZE;
OutBuffer[m+2]=DATA;
So, what you do think is going to happen in the next iteration of the loop? Say you go through the loop the first time, so that m is 2. The next time, it's m++, i.e. 3, and you make this assignment again:
OutBuffer[m]=InBuffer[m];
But you already assigned something at m[3], and that's the SIZE value from the previous iteration. You also assigned DATA at m[4], and that's going to be overwritten by the SIZE value in this iteration. Eventually, you'll end up with OutBuffer containing exactly what's in InBuffer, plus the SIZE and DATA values for the very last ID.
You need to use a different variable to index OutBuffer, something like:
for (m = 2, n = m; m < InBuffer; m++) {
OutBuffer[n++] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m],&SIZE,&DATA);
OutBuffer[n++] = SIZE;
OutBuffer[n++] = DATA;
}
There are some other problems as well. For example, the condition in the for loop shouldn't compare m to InBuffer, but should instead compare m and the number of entries in InBuffer. But just straightening out your indexing should be a big step forward.
Update: I just noticed that the data for each ID is larger than just one field. You'll need another loop inside the first one, then, so that you end up with something like this:
for (m = 2, n = m; m < InBuffer; m++) {
OutBuffer[n++] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m],&SIZE,&DATA);
OutBuffer[n++] = SIZE;
for (i = 0; i < SIZE; i++) {
OutBuffer[n++] = DATA[i];
}
}
If DATA is a structure with fields rather than an array, then you may need a series of if statements to check whether each field should be included or not. You can't use the value of a variable like i as the name of a field, i.e. you can't say DATA.i where i is a variable. I don't think a C structure can have field names that are numbers -- identifiers generally have to start with a letter or underscore, so trying to do that won't make much sense anyway. If you have control over the type of DATA, you should make it an array instead of a structure. So your loop would look more like this:
for (m = 2, n = m; m < InBuffer; m++) {
OutBuffer[n++] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m],&SIZE,&DATA);
OutBuffer[n++] = SIZE;
i = 0;
if (i++ < SIZE) { OutBuffer[n++] = DATA.field1; }
if (i++ < SIZE) { OutBuffer[n++] = DATA.field2; }
// and so on for each field in DATA's type
}
As Caleb pointed out you should use one variable for each array.
If you're saying that DATA can contain more than one element then you should increase the variable for outBuffer by SIZE each iteration. Also use a loop to asign DATAs fields to OutBuffer
int n = XXX; // set n to the first element you need to assign an ID to
for (m = 2; m < ElementsInBuffer; m++)
{
OutBuffer[n] = InBuffer[m];
returnPointersToDataAndSizeFunction(InBuffer[m],&SIZE,&DATA);
OutBuffer[n + 1] = SIZE;
for (int i = 0; i < SIZE; i++)
{
OutBuffer[n + 2 + i] = DATA[i]; // works for array only see Calebs answer to see how it works for structs
}
n += SIZE + 1; // +1 to also skip the field for SIZE
}
Related
I have 2 arrays, one called 'edges' which contains a list of city names and another called cityNames which is initialised as an empty string.
What I would like to do is move through the edges array element by element and see if it is included in the cityNames array. If it is, move onto the next element in edges, if it isn't, append the value to the cityNames array.
The code below adds the edges[i].startCity to the cityNames array but it does not check for duplicates and I can't figure out why.
for (int i = 1; i < noEdges; i++) {
for (int j = 0; j < noCities; j++) {
if(strcmp(edges[i].startCity, cityNames[j].cityName) != 0) {
strcpy(cityNames[i].cityName, edges[i].startCity);
}
}
noCities += 1;
}
Thanks in advance
I will assume that:
edges is an array of structures of a known length noEdges, each structure containing a string (either a char pointer or a char array)
cityNames is an array of structures for which the size is at least the number of distinct name (it could be noEdges or the size of the edges array)
the cityNames structure contain a char array element for which the size is at least the longest name + 1 (+1 for the terminating null)
Then the following code could give the unique names:
noCity = 0;
for (int i = 0; i < noEdges; i++) {
int dup = 0; // expect edges[i].startCity not to be a duplicate
for (int j = 0; j < noCities; j++) {
if(strcmp(edges[i].startCity, cityNames[j].cityName) == 0) {
dup = 1; // got a duplicate
break; // no need to go further ...
}
}
if (dup == 0) { // not a duplicate: add it to cityNames
strcpy(cityNames[noCities].cityName, edges[i].startCity);
noCities += 1; // we now have one more city
}
}
}
A good idea to start with would be to ditch working with strings if you can (or at least manipulate strings when actually needed).
You could start off by assigning each city name a number, that way you have an array of ints which is quicker and easier to work with.
Scanning for duplicates becomes trivial as you would now only be comparing numbers.
When you need to display the actual text on screen or write the city names to file, you could use the indexes associated with the city names to retrieve the appropriate textual representation of the index. You could then replace the data type of your cityNames[] to ints. This makes each 'node' which the 'edges' connect a number instead of text.
char* actualCityNames[n]; //array holding all city names with duplicates, could be a file also
char* indexedCityNames[n];//array with indexed cities (in order of appearance in actualCityNames, i.e. not alphabetical order)
//indexedCityNames will most likely not use up N slots if duplicates occur
//this is why there is a second counter for the size of indexed cities
int indexedCount = 0;//number of unique city names
int duplicates = 0;
//loop for actualCityNames slots
for(int i=0; i<n; i++){
//loop for indexedCityNames
for(int j=0; j<indexedCount; j++){
//strcmp returns 0 if both strings are the same
if(strcmp(actualCityNames[i],indexedCityNames[j]) == 0){
//duplicate found, mark flag
duplicates = 1;
}
}
if(!duplicates){
strcpy(indexedCityNames[indexedCount],actualCityNames[I]);
indexedCount++;
}
duplicates = 0;
}
Your code snippet does not check for duplicates because in the inner loop the if statement appends startCity as soon as a first cityName is encountered that is not equal to the current startCity.
Moreover in this statement
strcpy(cityNames[i].cityName, edges[i].startCity);
^^^
there is used an incorrect index.
And the variable noCities shall be incremented only when a new startCity is appended.
Also the outer loop should start from the index equal to 0.
Rewrite the loops the following way
int noCities = 0;
for ( int i = 0; i < noEdges; i++ ) {
int j = 0;
while ( j < noCities && strcmp(edges[i].startCity, cityNames[j].cityName) != 0 ) {
++j;
}
if ( j == noCities ) strcpy(cityNames[noCities++].cityName, edges[i].startCity);
}
Im stuck on this one part and I was hoping to get some help. I have a project that is basically a word search. The program reads in a file that contains the Rows and columns followed by the word search puzzle itself. You are required to create possible combinations of strings from the word search and check those combinations with a dictionary that is provided as another text document.
Here's an example of the file read in 1st is Rows and 2nd is Cols followed by the word search puzzle:
4 4
syrt
gtrp
faaq
pmrc
So I have been able to get most of the code to work except for the function that creates strings for the above file. Basically It needs to search the wordsearch and create strings, each created string gets passed on to another function to check if it's in the dictionary. However my code keeps going out of bounds when creating the strings, and it's continuing to cause Seg faults which is really frustrating.
Theses are the constants that are declared, its every possible direction to go while searching the word search puzzle for possible string combinations
const int DX_SIZE = 8;
const int DX[] = {-1,-1,-1,0,0,1,1,1};
const int DY[] = {-1,0,1,-1,1,-1,0,1};
This is the function I have to create the strings:
int strCreate(char** puzzle, char** dictionary, int n, int rows, int col){
int x, y;
int nextX, nextY, i;
char str[20] = {0};
int length = 1;
for(x = 0; x < rows; x++)
{
for(y = 0; y < col; y++)
{
//Grabs the base letter
str[0] = puzzle[x][y];
length = 1;
for(i = 0; i < DX_SIZE; i++)
{
while(length < MAX_WORD_SIZE)
{
nextX = x + DX[i]*length;
nextY = y + DY[i]*length;
// Checking bounds of next array
//This is where I'm having trouble.
if((x + nextX) < 0 || (nextX + x) > (col-1)){
printf("Out of bounds\n");
break;
}
if((y + nextY) < 0 || (nextY + y) > (rows-1)){
printf("Out of bounds\n");
break;
}
str[length] = puzzle[nextX][nextY];
//search for str in dictionary
checkStr(str, dictionary, n);
length++;
}
memset(&str[1], '\0', 19);
}
}
}
return 0;
}
I know i'm not checking the bounds properly I just can't figure out how to. When X = 1 and nextX = -1, that passes the bounds check, however say the array is at puzzle[0][0] nextX would put puzzle[-1][0] which is out of bounds causing the seg fault.
Thank you for taking the time to read, and I appreciate any help at all.
nextX and nextY are the indices used to access the array puzzle. Then the array bound check should also include the same. But the array bound check includes for example x+nextX.
// Checking bounds of next array
//This is where I'm having trouble.
if((x + nextX) < 0 || (nextX + x) > (col-1)){
printf("Out of bounds\n");
break;
}
Example:
if( nextX < 0)
printf("Out of bounds...\n");
If I have an int[3] array like such:
score_list[3] = [ 1, 2, 0]
Where each location in the array corresponds to a specific Document Number:
score_list[0] = D1
score_list[1] = D2
score_list[2] = D3
What would be the easiest way to sort the array in Descending order, keeping track of the place of each moved int
Where (after sort):
score_list[3] = [ 2, 1, 0]
And
score_list[0] = D2
score_list[1] = D1
score_list[2] = D3
I only need to print in descending order, not actually rearrange the int array, so:
for (int i=0; i<3; i++)
{
if (score_list[0] > score_list[1] && score_list[2])
printf("D%d-%d",i, score_list[0]);
if (score_list[1] > score_list[0] && score_list[2])
printf("D%d-%d", i, score_list[1]);
if (score_list[2] > score_list[0] && score_list[1])
printf("D%d-%d", i, score_list[2]);
}
Would print the highest number first, then I would compare the last 2, I just feel like this takes too long and there must be a more efficient way
You can solve this using marker (interface) design pattern.
By keeping a record of the visited index in your array each time you get the max, let me define the solution structure first then I will walk through it:
Make a new array of the same size of the score_list, let's call that array marker
In the for loop, you will make another loop to check for max score and in the same time check to see if the location of the max score in the marker array is 0.
Sample run:
On i=0
Loop on the score_list, and find the max score with marker == 0, print it and then put marker for that score = 1.
On i=1
You will do the same but now there is one location excluded from the list
Here you are a sample code how to do it:
Note that: this code is not a runnable one (and it is not optimized too O(n^2)), I wrote it for explanation purposes only.
int max = -1;
int doc = -1;
int marker[3] = {0, 0, 0};
for (i=0; i<3; i++)
{
max = -1;
for (j=0; j<3; j++)
{
// skip this location if it is already printed
if (marker[j] == 1)
continue;
if (score_list[j] > max)
{
doc = j;
max = score_list[j];
}
}
// this doc will not appear again in the inner for loop (j-loop)
marker[doc] = 1;
// print the document with the score
printf("D%d-%d",doc, max);
}
I'm trying to make this function create X number of variables using an array. I know that this is technically wrong because I need a constant as my array's value (currently 'x'), but excluding that, what am I missing? Looked at so many code samples and can't figure it out, but I know it's got to be simple...
void variables()
{
int i;
int bars = 10;
int x = 1;
for (i = 1; i <= bars+1; i++)
{
int variables[bars] = { x };
x++;
if (i >= bars+1)
{
break;
}
}
void variables()
{
int bars = 10;
if(bars >= Bars) bars = Bars - 1;
// to be able to set array size based on variable,
// make a dynamically sized array
double highvalues[];
ArrayResize(highvalues, bars);
for (int i = 0 /*Note: Array index is zero-based, 0 is first*/; i <= bars; i++)
{
highvalues[i] = iHigh(NULL, 0, i);
// or
highvalues[i] = High[i];
}
}
It is hard to tell what do you want to achieve.
If you want to fill an array with a value ArrayFill() fill help you.
suppose I have a arrays from each i want to produce b these are just examples
a=[4]=> b=[0,4]
a=[3,1]=>b=[0,3,3,4]
a=[2,2]=>b=[0,2,2,4]
a=[2,1,1]=>b=[0,2,2,3,3,4]
a=[3,4,2,5]=>b=[0,3,3,7,7,9,9,14]
I mean when getting 4 it should produce from 0 and then add it to it's content for example 4
or in a[2,1,1] first it will produce 0 and then it see that the next one in a is 1 so after again producing it it will compute 2+1 and assign it.so the output always will be twice size of the input.
i want a pseudo code for it my problem is that when it will repeat I can not write it.
I used JavaScript like syntax.
var a = new Array(3,4,2,5);
var b = new Array();
var bArrayIndex = 0;
b[bArrayIndex] = 0;
bArrayIndex++;
for(i = 0; i < a.length; i++) {
b[bArrayIndex] = b[bArrayIndex-1] + a[i];
if(i < a.length - 1) {
b[bArrayIndex+1] = b[bArrayIndex];
}
bArrayIndex+=2;
}
for(i = 0; i < b.length; i++) {
document.write(b[i] + " ");
}