Initialize the size of global array - c

I need to put the output of arr in global array because I need to use the array in main function. How can I initialize the array in global and I don't know the size of it ? any idea please?
globarr[]; // how to set the size here?
int *suma(int *output, int *arr)
{
*output = 0;
for (int i = 0; i < 100; i++) {
arr[i] = i;
*output += arr[i];
}
return arr;
}
void prose(){
int *by;
int output;
int arr[100];
by = suma(&output, arr);
for (ont i=0; i<output; i++) {
globarr[i] = n[i];
}
}
void main()
{
prose();
// here I need to use the values of globarr
}

I need to put the output of arr in global array
The only reason you might need to do that would be that some external authority you are bound to obey placed such an arbitrary, unnatural requirement.
It is possible to write conforming C code for any task, using only the C standard library, that does not rely on any global variables other than those few provided by the standard library itself.
because I need to use the array in main function. How can I initialize the array in global and I don't know the size of it ?
You cannot. The size of an array declared at file scope must be an integer constant expression, which means, among other things, that it must be computable at compile time.
any idea please?
You have two main alternatives:
Choose an upper bound on the size your program will support, and declare an array large enough for that. Make the program abort if otherwise it would try to use more elements of the array than are available.
Use a dynamically allocated space instead of a (declared) array.

Use function parameters and return values not global variables
long long suma(int **arr, size_t size)
{
long long output = 0;
*arr = malloc(size * sizeof(**arr));
if(*arr)
{
for (size_t i = 0; i < size; i++)
{
(*arr)[i] = i;
output += (*arr)[i];
}
}
return output;
}
#define SIZE 100
void main(void)
{
int *array;
long long output = suma(&array, SIZE);
printf("Sum of elements = %lld\n", output);
/* you can use the array */
if(array)
{
for(size_t i = 0; i < SIZE; i++)
{
printf("[%3zu]=%d%s", i, array[i], ((i + 1) % 10) ? ",\t" : "\n");
}
}
free(array);
}
https://godbolt.org/z/EPdxfPzoq
Output:
Sum of elements = 4950
[ 0]=0, [ 1]=1, [ 2]=2, [ 3]=3, [ 4]=4, [ 5]=5, [ 6]=6, [ 7]=7, [ 8]=8, [ 9]=9
[ 10]=10, [ 11]=11, [ 12]=12, [ 13]=13, [ 14]=14, [ 15]=15, [ 16]=16, [ 17]=17, [ 18]=18, [ 19]=19
[ 20]=20, [ 21]=21, [ 22]=22, [ 23]=23, [ 24]=24, [ 25]=25, [ 26]=26, [ 27]=27, [ 28]=28, [ 29]=29
[ 30]=30, [ 31]=31, [ 32]=32, [ 33]=33, [ 34]=34, [ 35]=35, [ 36]=36, [ 37]=37, [ 38]=38, [ 39]=39
[ 40]=40, [ 41]=41, [ 42]=42, [ 43]=43, [ 44]=44, [ 45]=45, [ 46]=46, [ 47]=47, [ 48]=48, [ 49]=49
[ 50]=50, [ 51]=51, [ 52]=52, [ 53]=53, [ 54]=54, [ 55]=55, [ 56]=56, [ 57]=57, [ 58]=58, [ 59]=59
[ 60]=60, [ 61]=61, [ 62]=62, [ 63]=63, [ 64]=64, [ 65]=65, [ 66]=66, [ 67]=67, [ 68]=68, [ 69]=69
[ 70]=70, [ 71]=71, [ 72]=72, [ 73]=73, [ 74]=74, [ 75]=75, [ 76]=76, [ 77]=77, [ 78]=78, [ 79]=79
[ 80]=80, [ 81]=81, [ 82]=82, [ 83]=83, [ 84]=84, [ 85]=85, [ 86]=86, [ 87]=87, [ 88]=88, [ 89]=89
[ 90]=90, [ 91]=91, [ 92]=92, [ 93]=93, [ 94]=94, [ 95]=95, [ 96]=96, [ 97]=97, [ 98]=98, [ 99]=99

Related

Array Multiplication & Sum with JQ?

{
"pricexquant": [
[
"20233.54000000",
"0.00877000"
],
[
"20233.39000000",
"0.00999000"
]
]
}
for an array of arrays with 2 elements as strings, how would jq multiply element 1 * element 2 in each array? and from that generate a new array...
eg.
{
[
"177.4481458"
],
[
"200.310561"
]
}
and for that sum the total across all?
.pricexquant|map(map(tonumber)|.[0] * .[1])|add
yields
379.5797119

Reverse Array of coordinates in swift 4

I am trying to reverse an array of coordinates. I have this array:
[
[
9.211615025997162,
44.30419567985762
],
[
9.21164184808731,
44.30412081929745
],
[
9.21165257692337,
44.304053636662175
],
[
9.211606979370117,
44.3038789614507
],
[
9.211572110652924,
44.30381945658962
],
[
9.211539924144745,
44.30378874437967
],
[
9.211507737636566,
44.30376187118276
],
[
9.211564064025879,
44.30377530778277
],
[
9.211574792861937,
44.30374075651943
],
[
9.211574792861937,
44.30371388330059
],
[
9.211574792861937,
44.30369852717
],
[
9.21154797077179,
44.303665895379176
],
[
9.211513102054596,
44.303650539236024
],
[
9.211429953575134,
44.30358911462331
],
[
9.211349487304688,
44.30355840229286
],
[
9.211271703243256,
44.303537287556324
],
[
9.21117514371872,
44.30349505806051
],
[
9.211151003837585,
44.303473943301235
],
[
9.211126863956451,
44.30342979423452
],
[
9.211145639419556,
44.30339908182071
],
[
9.211183190345764,
44.303368369390846
],
[
9.211223423480988,
44.30331846165803
],
[
9.211242198944092,
44.30327623200479
],
[
9.211177825927734,
44.30325895622883
],
[
9.211030304431915,
44.303228243725634
],
[
9.210925698280334,
44.30321096793552
],
[
9.210874736309052,
44.303197531206386
],
[
9.210831820964813,
44.303197531206386
],
[
9.21075403690338,
44.303197531206386
],
[
9.21068161725998,
44.3031994507393
],
[
9.210577011108398,
44.3031994507393
],
[
9.210509955883026,
44.303207128870355
],
[
9.210440218448639,
44.303207128870355
],
[
9.210370481014252,
44.30322632419366
],
[
9.2102712392807,
44.30324743904202
],
[
9.210190773010254,
44.30328199059568
],
[
9.210112988948822,
44.30328391012582
],
[
9.210059344768524,
44.303297346835215
],
[
9.209954738616943,
44.30332422024474
],
[
9.209901094436646,
44.303339576473235
],
[
9.209868907928467,
44.303349174114
],
[
9.209858179092407,
44.30338372560779
],
[
9.209791123867035,
44.30340292087333
],
[
9.209697246551514,
44.30342595518367
],
[
9.209648966789246,
44.3034509090097
],
[
9.209606051445007,
44.30351809233474
],
[
9.209579229354858,
44.30355840229286
],
[
9.209581911563873,
44.3035852755829
],
[
9.209622144699097,
44.30355840229286
],
[
9.20967847108841,
44.30355648277167
],
[
9.209697246551514,
44.30354112659989
]
]
And I would like to reverse the coordinates, which are right now in Long-Lat to Lat-Long thus reversing the whole array or each item in the array would do the job here. I have been stuck trying to make it work, trying things like arrayOfCoords.reversed() and .reverse() to no avail. So how can I do this?
There may be a more elegant/efficient way to do this, but I'd look at using compactMap:
let testArray = [[1.123,2.22], [3.11, 4.0], [5.21, 6.19]]
print(testArray)
[[1.123, 2.22], [3.11, 4.0], [5.21, 6.19]]
let flippedElementArray = testArray.compactMap { $0.count > 1 ? [$0[1], $0[0]] : nil }
print(flippedElementArray)
[[2.22, 1.123], [4.0, 3.11], [6.19, 5.21]]
The $0.count > 1 check just makes sure you have at least 2 elements so you don't get a bounds error. If not, the closure returns nil for that element, which will be filtered out by compactMap. Otherwise, it returns a new array element consisting of the second element followed by the first element. All of those elements are stored in the final mapped array (flippedElementArray).
arrayOfCoords.map{ Array($0.reversed()) }
I'm assuming you want to reverse each coordinate pair, but keep the pairs themselves in the original order. Since you have an array of arrays, you have to do the operation on each sub-array separately. And reversed() returns a ReversedCollection instead of an Array so you have to re-initialize it into an array to use it properly.

How to parse JSON array in array Swift?

I'm trying to parse JSON which is like below:
"price": [
[
1539283140000,
6288.07
],
[
1539283440000,
6285.82
],
[
1539283740000,
6285.81
],
[
1539284041000,
6280.37
],
[
1539284340000,
6280.19
]
Please help me deal with this. And is there a possibility to decode the timestamp value to a date.
Correct json
{
"price": [
[
1539283140000,
6288.07
],
[
1539283440000,
6285.82
],
[
1539283740000,
6285.81
],
[
1539284041000,
6280.37
],
[
1539284340000,
6280.19
]
]
}
struct Root: Codable {
let price: [[Double]]
}
let res = try? JSONDecodable().decode(Root.self,from:jsonData)

Format JSON Array format to another JSON Array format

Here is the my JSON Array format
[
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
]
The "1234","4567" and "7890" are my Ids and "Name11","Name12", "Name13", "Name21", "Name22" and "Name23" are the values for each corresponding Ids.
The JSON array contains two records as shown above. Now i need to convert the following JSON Format....
[
{
"1234":"Name11",
"4567":"Name12",
"7890":"Name13"
},
{
"1234":"Name21",
"4567":"Name22",
"7890":"Name23"
}
]
Please help me out, how can i construct the above mentioned JSON array format.
With pure javascript:
var src = [
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
]
var newObj = []
for(var i = 0; i< src.length; i++){
var item = {}
for(var j = 0; j<src[i].length; j++){
if(j%2 === 0){
item[src[i][j]] = ""
}else{
item[src[i][j-1]] = src[i][j][0]
}
}
newObj.push(item)
}
You can do it with the following code (see the snippet below):
var res = src.map(function(a){
var arr = [];
for(var i=0;i<a.length/2;i++) {
var item = {};
item[a[i*2]] = a[i*2+1][0];
arr.push(item);
};
return arr;
});
var src = [
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
];
var res = src.map(function(a){
var arr = [];
for(var i=0;i<a.length/2;i++) {
var item = {};
item[a[i*2]] = a[i*2+1][0];
arr.push(item);
};
return arr;
});
document.getElementById("res").textContent = JSON.stringify(res);
<div id="res"></div>

PANIC=runtime error: index out of range

I have the following code, but i can't figure out why the error:PANIC=runtime error: index out of range is happening
type Board struct {
Tboard [9]string
Player1 Player
Player2 Player
}
func makeBoard() *Board {
b := &Board{Tboard: [9]string{}}
for x := 0; x < len(b.Tboard); x++ {
b.Tboard[x] = E // E = "[ ]"
fmt.Println(b.Tboard[x])
}
fmt.Println(len(b.Tboard)) // => 9
fmt.Print("Error: ")
fmt.Println(b) // => Error: %!v(PANIC=runtime error: index out of range)
fmt.Println(b.Tboard) // => [[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]]
return b
}

Resources