Unable to Initialize 2D Array - c

I have declared a 2D global array variable like so:
int grid_2d_array[ROWS][COLUMNS];
then in main I've to initialize it with hard-coded values:
grid_2d_array[ROWS][COLUMNS] = {{5, 7, 2, 8, 3, 6, 1, 4},
....
{1, 6, 3, 2, 4, 8, 9, 5}
};
Example:
#include <stdio.h>
#define ROWS 9
#define COLUMNS 9
/* Global variable. */
int grid_2d_array[ROWS][COLUMNS];
int main()
{
/* Initialze the 2D array. */
grid_2d_array[ROWS][COLUMNS] = {{5, 7, 2, 8, 3, 6, 1, 4},
....
{1, 6, 3, 2, 4, 8, 9, 5}
};
return 0;
}
But when I try compiling the source code, GCC gives the following error:
source_file.c: In function ‘main’:
source_file.c:45:34: error: expected expression before ‘{’ token
grid_2d_array[ROWS][COLUMNS] = {{5, 7, 2, 8, 3, 6, 1, 4},
^
I'm not sure why GCC is not recognizing grid_2d_array as a global variable.
The problem goes away if I redeclare the aforementioned variable in main.
I'm running GCC version: gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)

C and C++ arrays can be initialized only as a part of the definition statement:
int grid_2d_array[ROWS][COLUMNS] = {{5, 7, 2, 8, 3, 6, 1, 4},
....
{1, 6, 3, 2, 4, 8, 9, 5}
};
Assignment of multiple values into an array is not supported.

In addition to FireAphis's answer, if you are under C99 you can initialize a pointer to array of ints (not a 2D array) outside his definition using compound literals:
int (*grid_2d_array)[COLUMNS]; /* Pointer to array of n int's */
in main:
grid_2d_array = (int [ROWS][COLUMNS]){
{5, 7, 2, 8, 3, 6, 1, 4},
{1, 6, 3, 2, 4, 8, 9, 5}
};

Related

Is there a way to print an entire array and not just printing the contents of the array by iterating through them?

In C, is there a way to print an entire array. For example print (in full) each row from a 2D array, NOT iterating through its contents and printing them independently? Below is an example of how I imagined the code would be:
int main() {
int Numbers[4][4] = {{5, 1, 1, 6},
{2, 2},
{3, 3, 3},
{4, 4, 4}};
for (int i=0; i<=3; i++){
printf("%i \n", Numbers[i]);
}
return 0;
}
I expect the output to be along the lines of:
{5, 1, 1, 6}
{2, 2}
{3, 3, 3}
{4, 4, 4}
However, when run the code I get the following output
-414054224
-414054208
-414054192
-414054176
No, there is no way to do what you are asking. You cannot do operations on whole arrays in C; you must iterate through and print each element separately. Array expressions in C lose their "array-ness" under most circumstances.

How do I add different size of new array to an old array using a for loop?

How do I use a for loop to add array 1 to array 2 and become the expected array like the example below?
What I expect is adding the first element in array 1 to array 2's second 1234 and continue adding up:
Array 1 = [4, 8]
Array 2 = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Expected Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
I am thinking of something like this, but just feel very strange.
N=4
for (int i=N; i<array.size; i++)
{
array2[i] = array1[...];
}
I guess your question is when
int array1[] = {4, 8};
int array2[] = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
int arrayAnswer[] = {array2[0],array2[1],array2[2],array2[3],
array2[0]+array1[1],array2[1]+array1[1],array2[2]+array1[1],array2[3]+array1[1],
array2[0]+array1[2],array2[1]+array1[2],array2[2]+array1[2],array2[3]+array1[2]}
is it right? if so, please leave the comment and I will edit more
First of all, the question you made is not a good question. Please try to give more detailed information about your problem next time.
Answer for your question is, skip fist four member of array and add array1[1] on from array2[4] to array [7], and add array1[2] on from array2[8] to array [11]. You are going to need two loop. such as
for (i = 0; i < 2; i++){
for (j = 0; j < 4; j++){
//your function
}
}
I am not going to give you direct answer for the problem so that you can design your own. However, if your first array is {0, 4, 8}, your coding is going to be much easier. ty

Count Pairs from two arrays with even sum

Problem Statement
Given two arrays A[] and B[] of N and M integers respectively. The task is to count the number of unordered pairs formed by choosing an element from array A[] and other from array B[] in such a way that their sum is an even number.
Note that an element will only be a part of a single pair.
Input: A[] = {9, 14, 6, 2, 11}, B[] = {8, 4, 7, 20}
Output: 4
{9, 7}, {14, 8}, {6, 4} and {2, 20} are the valid pairs.
Source
https://www.geeksforgeeks.org/count-pairs-from-two-arrays-with-even-sum/
My Problem
I am wondering how output doesn't have so many other pairs whose sum will be even i.e. {11,7}, {2,3} etc. and many others.
As you mentioned in the question, Element will only be a part of single pair
{11,7} and {2,3} are already considered in {9, 7} and {2, 20}.
It's given in the problem statement,
Note that an element will only be a part of a single pair.
That means 7 has already been paired in {9, 7} so you can't pair {11, 7}

"too many braces around scalar initializer" error when using 2d array in C

I am still learning C but when I try and use a 2d array I get the errors:
/Users/harry/Dropbox/C/Practise/test_1/main.c:157:9: error: expected expression
int[,] numberGrid = {
^
/Users/harry/Dropbox/C/Practise/test_1/main.c:158:9: warning: too many braces
around scalar initializer [-Wmany-braces-around-scalar-init]
{1, 2, 3 },
My code is:
int main() {
int[,] numberGrid = {
{1, 2, 3 },
{4, 5, 6 },
{7, 8, 9 }
};
printf("%d\n", numberGrid[1, 1]);
}
As noted in the comments, the notation int[,] numberGrid = { … notation simply isn't C.
If you wrote either:
int numberGrid[3][3] = {
{1, 2, 3 },
{4, 5, 6 },
{7, 8, 9 }
};
or:
int numberGrid[][3] = {
{1, 2, 3 },
{4, 5, 6 },
{7, 8, 9 }
};
you would be able to get the array initializer to compile.
Then you need to fix the printf() to print an array element. The comma is no use in subscripts in C (for most practical purposes, it 'never' appears in a subscript — you can invent exceptions if you try hard enough, but you shouldn't write such code normally).
You could write:
printf("%d\n", numberGrid[1][1]);
to print 5. As written (printf("%d\n", numberGrid[1,1]);), you are printing an address as a signed decimal integer, which is not guaranteed to work well (data truncation on a 64-bit machine, etc).

How can I get Stream from 3 dimensional array in Java 8?

As I know to get Stream from 2 dimensional array but I want to know how I can get Stream from below 3 dimensional array?
int[][][] data = {
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
},
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
};
If you can do it with a two-dimensional array then doing it for N dimensional array is not that difficult.
The solution can be done as follows:
IntStream result = Arrays.stream(data)
.flatMap(Arrays::stream)
.flatMapToInt(Arrays::stream);
To better help understand what is going on above, you can split the method invocations as follows:
// the call to Arrays.stream yields a Stream<int[][]>
Stream<int[][]> result1 = Arrays.stream(data);
// the call to flatMap yields a Stream<int[]>
Stream<int[]> result2 = result1.flatMap(Arrays::stream);
// the call to flatMapToInt yields a IntStream
IntStream intStream = result2.flatMapToInt(Arrays::stream);
You just need to call flatMap another time to change the stream from int[][] to stream of int[].
IntStream stream = Arrays.stream(data)
.flatMap(twoDArray -> Arrays.stream(twoDArray))
.flatMapToInt(oneDArray -> Arrays.stream(oneDArray));

Resources