How to work with C strings within an Array? - c

So let's say I have an array of C strings. I'm trying to create a function that I will tell which two strings (probably by index path) to remove and then add a new string (merge of the other two) to the array.
I don't need the actual merging code, just the code that removes the 2 strings by index path, then adds an additional string to the array.
(I will probably need another function to do the actual merge and that's out of the scope of this question.)

You cannot remove a string from an array. You would need to mark the index as being deleted and then copy the entire array to another array omitting the marked elements.

Related

getting column of array in C99?

In C if I have:
int grades[100][200];
and want to pass the first row, then I write: grades[0], but what if I want to pass first column? writing this won't help grades[][0]
You can't pass columns in C. You pass pointers to the beginning of some continuous data.
Rows in C are written continuously in memory, so you pass the pointer to the first element of some row (you do it implicitly by using its name: matrix[row]; an explicit version would be &matrix[row][0]), and you can use the row by iterating over the continuous memory.
To use columns, you need to pass the whole array (a pointer to the first element in the 2D array, actually), and pass also the length of the rows, and then the function has to jump that length to jump from an element of the same column to the next one. This is one of many possible solutions, you could develop any other solution, for example copying the column in a temporary array as some comment pointed out; but this one is commonly used in cblas functions for example.
If it helps to visualize, a 2-dimensional array is an array of arrays, it's not formulated as a matrix. Thereby, we can pass a sub-array (i.e., a row), but there's no direct way of passing a column.
One way to achieve this is to loop over the outer array, pick the element at the fixed location (mimicking the "column"), and use the values to create a separate array, or pass to function that needs to process the data.
Matrixes do not exist in C (check by reading the C11 standard n1570). Only arrays, and in your example, it is an array of arrays of int. So columns don't exist neither.
A good approach is to view a matrix like some abstract data type (using flexible array members ....) See this answer for details.
Consider also using (and perhaps looking inside its source code) the GNU scientific library GSL, and other libraries like OpenCV, see the list here.
In some cases, arbitrary precision arithmetic (with gmplib) could be needed.

Hi, I'm trying to create a list with multiples arrays that i get from indexing a huge array that contains them

I have my original array that has 85 arrays, and i can access to them like this
da1[34]
The problem is that I have a list (aa) where I have the specific index for the arrays I need. I want to make a loop to append all that arrays in a new list, so i used this code
aa=[45,76,18,34]
for i in aa:
orden=[]
orden.append(da1[i])
But I only get an array, I don't know what I'm doing wrong.
What I hope is to get the same number of arrays as the number of indexes that I have in aa
You just need to define the list outside the loop, like this:
aa=[45,76,18,34]
orden=[]
for i in aa:
orden.append(da1[i])
The reason is you are creating the list every time you iterate in the for. So at the end you end up only with the last element in the list.

How to compare two string values from an ArrayList?

I have an ArrayList that is populated with strings from a text file, and now I want to sort it alphabetically (I can't use collections.sort or anything like that, it has to be manual).
What is the correct way to compare two elements of an ArrayList (if there is one at all)?

Retrieve multiple values from matlab string

I currently have a large single row array of chars... I also have two arrays, the first array has all the start indexes of data I would like to retrieve from the char array, the second array has all the end indexes for the data. How can I retrieve all these wanted values from my char array without using a loop?
So far I have tried doing
chararray(1,start(:):end(:))
but this will only retrieve the first value I would like!
Cheers!
Try this -
chararray(bsxfun(#plus,start1(:)-start1(1),start1(1):end1(1)))
This would create a 2D char array where each row be the output from each iteration of your loop code.
Also, please note that I am using start1 and end1 to represent your start and end arrays respectively, so as not to create a clash with the reserved terminate scope end used by MATLAB.

Is it possible to create an array from an array in Pascal?

i have a string array called Files, and a boolean function IsGood(Files[i]) in a for loop. How can i create an array of GoodFiles using IF.
Assuming I'm understanding you correctly, if you really must have an array you would first have to iterate over your Files array, calling IsGood on each one, and count how many good ones you have, allocate the array, then loop again, this time storing the good ones into the array.
There's another data structure, TStringList, however, that you can conveniently use for something like this:
GoodList := TStringList.Create;
for i := 0 to length(Files) - 1 do
if IsGood(Files[i]) then
GoodList.Add(Files[i]);

Resources