How can i recognise an Integer array contains characters in C? - c

Scenario :-
My array contains elements like 56,12,ew34,45 or 56,12,34rt,45 how
can I show that the array is not integer array?
var Array =[56,12,'ew34',45 or 56,12,'34rt',45];
Actually am trying to find a solution with out Inbuilt Functions.

If array is declared as integer type then it won't accept any char type data.
If you are trying to insert 0 instead of char type data, handle the same conveniently at the insertion time.
Also you can make use of compiler extension typeof.

Related

Is there a standard function to Convert a string, to an array of integers and back. like: GetString(bytes) but with integers

So :string "ABCD" to array (65,66,67,68)
I would like it to be one function, like GetString(bytes),
but with integer instead of bytes
At the moment I'm converting the resulting array of bytes one by one
to an array of integers.(and there are other workarounds)(don't really want to do that)
I just want to know if there is a standard Visual Basic Method.

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.

BigQuery Types: How to define a array of any type?

Bigquery SQL-UDFs are quite convenient, but is there a possibility to define array arguments without specifying the type of its elements? At least sometimes it would be nice to define operations on all arrays irrespective of the specific type. For example, one could create a function to get the most frequent elements of an array like this:
CREATE TEMPORARY FUNCTION anyHEAVY(arr Array<ANY TYPE>) AS ((
SELECT APPROX_TOP_COUNT(a, 1)[OFFSET(0)].value
FROM UNNEST(arr) as a
));
However, it seems like BQ expects here a specific type and the generic "ANY TYPE" placeholder does not work anymore. Up to now, I am just using the type "any type" without forcing the argument to be an array. This works, but is IMHO not really clean and would require an additional check. I can imagine, that any type would cause some troubles, especially, in case of nested arrays or structs. However, would be great if one could define functions of arrays containing only "elementary" types (excluding arrays and structs).
Currently there's no support for ARRAYS of ANY TYPE in SQL-UDF. The workaround is, as you mention, to declare the function as expecting ANY TYPE:
CREATE TEMPORARY FUNCTION anyHEAVY(arr ANY TYPE)

How to cast and pass an argument properly in delphi

I have a very simple code snippet which you can check here:
type
TMatrix = array of array of Byte;
PMatrix = ^TMatrix;
const
testarray:array [0..2,0..2] of Byte=(
(1,2,3), (4,5,6), (7,8,9));
function PickValue(input:PMatrix):Byte;
begin
Result:=input^[1,3];
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage(inttostr(PickValue(#testarray)));
end;
end.
How can I cast testarray properly to pass it to the PickValue function? Above code crashes in its current form. I'm using delphi 2007.
Thanks in advance!
You cannot cast that static array to be a dynamic array. These types are simply not compatible. The static array is laid out in one contiguous block of memory. The dynamic array has indirection to variable sized arrays at each dimension. In effect think of it as ^^Byte with extra compiler management and meta data. No amount of casting can help you.
You have, at least, the following options:
Copy the contents of the static array to a dynamic array. Then pass that dynamic array to your function.
Switch your static array to be a dynamic array so that no conversion is needed.
Arrange that your function accepts static arrays rather than dynamic arrays, again to avoid requiring a conversion.
Have the function accept a pointer to the first element and the inner dimension. Then perform the indexing manually. The i,j element is at linear offset i*innerDim + j.
Your pointer type PMatrix is probably not needed. Dynamic arrays are already implemented as pointers. This seems to be a level of indirection too far.
Of course, asking for element 3 when the array indices only go up to 2 isn't right, but that is presently the lesser of your concerns. Remember that dynamic arrays are zero based and you have specified zero based for your static array.
I am struggling to be able to recommend which solution is best since I don't understand your real goals and usage based on the simplified example presented here.

Access array elements from string argument in Modelica

I'm having a task in Modelica, where within a function, I want to read out values of a record (parameters) according to a given string type argument, similar to the dictionary type in Python.
For example I have a record containing coefficicents for different media, I want to read out the coefficients for methane, so my argument is the string "Methane".
Until now I solve this by presenting a second array in my coefficients-record storing the names of the media in strings. This array I parse in a for loop to match the requested media-name and then access the coefficients-array by using the found index.
This is obviously very complicated and leads to a lot of confusing code and nested for loops. Isn't there a more convenient way like the one Python presents with its dictionary type, where a string is directly linked to a value?
Thanks for the help!
There are several different alternatives you can use. I will add the pattern I like most:
model M
function index
input String[:] keys;
input String key;
output Integer i;
algorithm
i := Modelica.Math.BooleanVectors.firstTrueIndex({k == key for k in keys});
end index;
constant String[3] keys = {"A","B","C"};
Real[size(keys,1)] values = {1,2*time,3};
Real c = values[index(keys,"B")] "Coefficient";
annotation(uses(Modelica(version="3.2.1")));
end M;
The reason I like this code is because it can be made efficient by a Modelica compiler. You create a keys vector, and a corresponding data vector. The reason it is not a record is that you want the keys vector to be constant, and the values may vary over time (for a more generic dictionary than you wanted).
The compiler can then create a constant index for any constant names you want to lookup from this. This makes sorting and matching better in the compiler (since there are no unknown indexes). If there is a key you want to lookup at run-time, the code will work for this as well.

Resources