get 2D array from another 2D array using if statement in c - arrays

I have a 2D array (big_array) and there are four values inside of it = 1,
I want to get the indexes of these four values and put them in the first index of each array in another 2D array (small_array) one by one.
I tried this:
for (int x = 0; x < 3; x++) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; x++) {
if (big_array[i][j] == 1) {
small_array[x][0] = j;
break;
}
}
}
}
And I get only the index of the last value=1 in all of array of 'small_array'.

I don't get what you are trying to achieve, but If you're trying to store the second index of each element that equals 1 of the big_array in the first column of the small_array, then here is how :
int x = 0;
while (x < 3)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (big_array[i][j] == 1)
{
small_array[x][0] = j;
x++;
}
}
}
}

Related

How to add values to the first row and the first column of two dimensional array in C?

I am trying to add numbers from 0 to 9 in the row and 0 to 11 in column of a two dimensional array in C. And as for the rest of the empty spaces, I would like to add 0.
The matrix size is 9 x 11. And this is what the output looks like with the empty blocks filled with 0:
And this is the code I have so far but it does not work:
int i;
int j;
int arr[i][j];
int value = 0;
for (i = 0; i < 9; i++){
for (j = 0; j < 11; j++){
arr[i][j] = value;
printf("%d\n", arr[i][j]);
value++;
}
printf("\n");
}
The screenshot that you've posted has 10 rows and 12 columns, so assuming that, here is the code:
int i;
int j;
int arr[10][12];
for (i = 0; i < 10; i++) {
for (j = 0; j < 12; j++) {
if (i == 0) {
arr[0][j] = j;
} else if (j == 0) {
arr[i][0] = i;
} else {
arr[i][j] = 0;
}
printf("%d", arr[i][j]);
}
printf("\n");
}

access violation writing to 2d array in c

I'm trying to initialise a 2d array, and for some reason I'm getting an exception whenever I try to enter a value into the array.
I'm passing it to the function in a struct, if that makes a difference?
Here's the code:
#define board_size 10
typedef struct {
int b[board_size][board_size];
} board;
board dead_state(); // initialise a dead board state
void main() {
board test_board = dead_state();
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
test_board.b[i][j] = 0;
}
}
}
board dead_state() {
// generate the matrix, set all values to 0, return
board deadboard;
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
deadboard.b[i][j] = 0;
}
}
return deadboard;
}
edit: problem solved, thanks to hyde's comment: i am blind and cannot see, apparently.
You have what looks like a typo.
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
test_board.b[i][j] = 0;
}
}
Both loops increment i, which means i goes out of bounds. You likely meant for the inner loop to increment j, but I don't know why you're only incrementing i and j up to 8, instead of 9.

Removing duplicates in a C array

I am writing a program which determines the intersection of 2 integer arrays (size of 10 elements). I think I got every other parts covered except for sorting out duplicates. Does anyone know a way of checking duplicates without making a function or using an external C library?
#include <stdio.h>
#define SIZE 10
int main(void){
//Initialization
int array1[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set A: ", i + 1);
scanf("%d", &array1[i]);
}
int array2[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set B: ", i + 1);
scanf("%d", &array2[i]);
}
int intersection[SIZE];
for (int i = 0; i < SIZE; i++)
{
intersection[i] = '\0';
}
//Intersection check
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (array1[i] == array2[j])
{
intersection[i] = array1[i];
break;
}
}
}
//duplicate check
int count = SIZE;
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (intersection[i] == intersection[j])
{
for (int k = j; j < count; i++)
{
intersection[k] = intersection[k + 1];
}
count--;
}
}
}
//printing set
for (int i = 0; i < SIZE ; i++)
{
//printf("%d\n", intersection[i]);
if (intersection[i] != '\0')
{
printf("%d\n", intersection[i]);
}
}
return 0;
}
In the code above i was trying one method although it didn't work and instead made the program stuck after inputting all the elements. I am open to other methods as long it doesn't require an external library to run. Thanks
As i see it now , in the third loop where you checking your duplicates i thing that you have to increese k not i :
for (int k = j; j < count; k++), also you must decrise the size of j in your code under the count--;.So your code for checking duplicates seems right but , you want the intersection of this two arrays you made , so you dont have to check for duplicates because in the array intersection[SIZE] you will put only one number from the two arrays, so you will not have duplicates .You should check for duplicates if you wanted to make the union of this two arrays .I make some changings to your code acording what you want to create and this code here find the intersection from two arrays.Try this and delete the duplicate check because that makes your code to run to infinity . One last thing your intersection check must be replace whith this :
//Intersection check
int i = 0, j = 0,k=0; // k is for the intersection array !
while (i < SIZE && j < SIZE) {
if (array1[i] < array2[j])
i++;
else if (array2[j] < array1[i])
j++;
else if(array1[i]==array2[j]) // if array1[i] == array2[j]
{
intersection[k]=array2[j];
//printf("intersection[%d]=%d\n",i,intersection[i]);
intersectCount++;
k++;
i++;
j++;
}
}
printf("intersectCount=%d\n",intersectCount);

How to build a table (2D array) with variable column length in C?

Is it possible to build a table (2D array) with variable column length ?? and how can I make this if it is possible?
Here I have the code but the column length is fixed.
#include <stdio.h>
const int CITY = 2; // column number
const int WEEK = 2; // row number
int i,j;
int z=0;
int c[4]={1,2,3,4};
int main()
{
int temperature[CITY][WEEK]; // create temp 2d array of city and weak
for ( i = 0; i < CITY; ++i)
{
for( j = 0; j < WEEK; ++j) {
temperature[i][j]= c[z];
z++;
}
}
printf("\nDisplaying values: \n\n");
for ( i = 0; i < CITY; ++i) {
for(j = 0; j < WEEK; ++j)
{
printf("City %d, Day %d = %d\n", i, j, temperature[i][j]);
}
}
return 0;
}
You can create an array of pointers which will point to arrays of different lengths.
int *p[row], i;
for(i = 0; i < row; i++) {
p[i] = malloc(len * sizeof(int)); // len is length for row i
}
Then you can access the jth element of ith row by p[i][j].
// displaying ith row of length = len
for ( j = 0; j < len; ++j) {
printf("%d\n" p[i][j]);
}

Is it possible to copy from int** to another int** but not element by element?

I have a n x n matrix stored in int** matrix1.
I have another int** matrix2 with m x m dimensions with m > n.
I'd like to copy the first matrix in the top left corner of the second one and fill in the missing locations from n+1 to m.
Is there any other way than copying element by element?
you can use
for (int i = 0; i < n; ++i)
{
memcpy ( matrix2[i], matrix1[i], n*sizeof(int));
}
and then fill in the missing locations
matrix1 = (int**)malloc(sizeof(int*)*n);
matrix2 = (int**)malloc(sizeof(int*)*m);
for (int i = 0; i < n; ++i)
{
matrix1[i] = (int*)malloc(sizeof(int*)*n);
}
for (int i = 0; i < m; ++i)
{
matrix2[i] = (int*)malloc(sizeof(int*)*m);
}
// assigning some random values
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
matrix1[i][j] = j;
}
}
// copying elements using memcpy
for (int i = 0; i < n; ++i)
{
memcpy ( matrix2[i], matrix1[i], n*sizeof(int));
}

Resources