How to delete a row from a 2D array in C? - c

How do I remove a specific row from a matrix, keeping the same order?
Example:
1 1 1
2 2 2
3 3 3
Let's say I need to remove the row with all even elements, so after deleting it should look like:
1 1 1
3 3 3
I tried writing code myself, (condition not the same as i mentioned above!) but it doesn't actually work properly:
for (i = 0 ; i < no_of_rows ; i++) {
if (abs(prosjeci[i] - prosjek) < 0.1) { /* condition */
for (k = i ; k < no_of_rows - 1 ; k++) {
for (j = 0 ; j < no_of_columns ; j++) {
matrica[k][j] = matrica[k+1][j];
}
}
i--;
no_of_rows--;
}
}

I don't see anything wrong with your code.
In the comments, someone asked you to post a "Minimal, Complete, and Verifiable example". Here's what that means. I fleshed out your program, adding a declaration and initialization of your matrica array and other variables, changing the condition to match your example, and printing out the array at the end. I ended up with this:
#include <stdio.h>
int matrica[][3] = {
{1, 1, 1},
{2, 2, 2},
{3, 3, 3}
};
int no_of_columns = 3;
int no_of_rows = 3;
int main()
{
int i, j, k;
for (i = 0 ; i < no_of_rows ; i++) {
if (matrica[i][0] % 2 == 0) { /* even row */
for (k = i ; k < no_of_rows - 1 ; k++) {
for (j = 0 ; j < no_of_columns ; j++) {
matrica[k][j] = matrica[k+1][j];
}
}
i--;
no_of_rows--;
}
}
for (i = 0 ; i < no_of_rows ; i++) {
for (j = 0 ; j < no_of_columns ; j++) {
printf("%d ", matrica[i][j]);
}
printf("\n");
}
}
So it would have been better if you had posted something like that in the first place.
But when I compile and run this program, it works perfectly. (I'm not surprised -- as I said, I don't see anything wrong with it.)
So whatever your problem is, it's in something you haven't showed us. What did you mean when you said "it doesn't actually work properly"? What did you expect to see, and what did you see instead?
[P.S. There's one more problem with your question. Until you ask it better, I am not even supposed to answer it. This answer of mine was actually downvoted to remind me of this fact. I'm not complaining; I expected it. But please, ask a more complete and answerable question next time.]

Your method does not work because you modify the matrix in place, update the i index and the number of rows no_of_rows accordingly, but fail to update the separate array prosjeci. Whenever a row matches the filter, all subsequent rows in the matrix are removed.
You can fix this problem by using a separate index for the matrix and the filter array:
int ii; // index into the prosjeci array.
for (i = ii = 0; i < no_of_rows ; i++, ii++) {
if (abs(prosjeci[ii] - prosjek) < 0.1) { /* condition */
for (k = i; k < no_of_rows - 1; k++) {
for (j = 0; j < no_of_columns; j++) {
matrica[k][j] = matrica[k+1][j];
}
}
i--;
no_of_rows--;
}
}
Alternately, if you can update the filtering array, you can do this:
for (i = 0; i < no_of_rows ; i++) {
if (abs(prosjeci[i] - prosjek) < 0.1) { /* condition */
for (k = i; k < no_of_rows - 1; k++) {
for (j = 0; j < no_of_columns; j++) {
matrica[k][j] = matrica[k+1][j];
}
prosjeci[k] = prosjeci[k+1];
}
i--;
no_of_rows--;
}
}

First of all in fact you can not remove elements from an array without creating a new copy of the array. You can only overwrite them with some other values and keep the number of actually used elements in the array.
Here is a demonstrative program that shows how it can be done. The variable with the name n is used to keep the number of actually used rows of elements in the array.
#include <stdio.h>
#define N 10
int all_of_even( const int *row, size_t n )
{
size_t i = 0;
while ( i < n && row[i] % 2 == 0 ) i++;
return i == n;
}
int main(void)
{
int a[][N] =
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
{ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 },
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
{ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 },
{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
{ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 },
{ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 },
{ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }
};
const size_t M = sizeof( a ) / sizeof( * a );
size_t n = M;
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < N; j++ ) printf( "%d ", a[i][j] );
printf( "\n" );
}
printf( "\n" );
n = 0;
for ( size_t i = 0; i < M; i++ )
{
if ( !all_of_even( a[i], N ) )
{
if ( n != i )
{
for ( size_t j = 0; j < N; j++ ) a[n][j] = a[i][j];
}
++n;
}
}
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < N; j++ ) printf( "%d ", a[i][j] );
printf( "\n" );
}
printf( "\n" );
return 0;
}
The program output is
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9
1 1 1 1 1 1 1 1 1 1
3 3 3 3 3 3 3 3 3 3
5 5 5 5 5 5 5 5 5 5
7 7 7 7 7 7 7 7 7 7
9 9 9 9 9 9 9 9 9 9
As for your approach then it is inefficient because for each iteration with checking the condition you copy all rows of the array after the given row instead of copying only one row.
Also it is a bad practice to change control variables of a for loop in its body and in the for statement itself simultaneously. This makes difficult to read the code.

i tried to do what you meant..
main(){
int matrica[3][3] = { { 1,2,3 },
{ 4,4,4 },
{ 7,8,9 } };
double no_of_rows = 3;
int line_removed = 0;
for (int i = 0; i < no_of_rows; i++) {
double sum = 0;
for (int j = 0; j < no_of_rows; j++)
{
sum = sum + matrica[i][j];
}
for (int j = 0; j < no_of_rows; j++)
{
int checker = 0.1 + (sum / no_of_rows);
if ( checker > matrica[i][j] || checker < matrica[i][j])
{
break;
}
if (j = (no_of_rows-1))
{
for ( int k = i ; k < no_of_rows; k++)
{
for ( j = 0; j < no_of_rows; j++)
{
matrica[k][j] = matrica[k + 1][j];
}
}
line_removed++;
}
}
}
for (int i = 0; i < (no_of_rows-line_removed); i++)
{
for (int j = 0; j < no_of_rows; j++)
{
printf("%d ", matrica[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}

Related

How to format a matrix in c with specific additional symbols?

I want to make a function that formats a matrix that's passed like this:
1 2 3 6
4 5 6 15
7 8 9 24
12 15 18 45
Into a matrix like this:
1 2 3 | 6
4 5 6 | 15
7 8 9 | 24
=============
12 15 18 | 45
I somewhat got the part with vertical bars, but I have no idea how to do the equals part, here's my take on vertical bar:
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (j == n - 2)
printf("%d | ", mat[i][j]);
else if (j == n - 1)
printf("%d\n", mat[i][j]);
else if (i == n - 1)
printf("%d ", mat[i][j]);
else printf("%d ", mat[i][j]);
Printing === before the last row just ends up like this:
1 2 3 | 6
4 5 6 | 15
7 8 9 | 24
===
12 ===
15 18 | 45
I tried everything, but it failed miserably, any suggestions?
Here is demonstrated a straightforward approach to output your array.
#include <stdio.h>
int main( void )
{
enum { N = 4 };
int a[N][N];
for ( int i = 0; i < N; i++ )
{
a[i][N-1] = 0;
for ( int j = 0; j < N - 1; j++ )
{
a[i][N-1] += a[i][j] = i * N + j + 1;
}
}
for ( int i = 0; i < N; i++ )
{
if ( i == N - 1 )
{
for ( int j = 0; j < 3 * N + 1; j++ )
{
putchar( '=' );
}
putchar( '\n' );
}
for ( int j = 0; j < N; j++ )
{
if ( j != N - 1 )
{
printf( "%-3d", a[i][j] );
}
else
{
printf( "| %-2d", a[i][j] );
}
}
putchar( '\n' );
}
}
The program output is
1 2 3 | 6
5 6 7 | 18
9 10 11 | 30
=============
13 14 15 | 42
You can write a more general function that outputs such an array by using the approach. What you need to do so is to calculate the length of the last element of the array (in the array above the length of the last element 45 is equal to 2) and instead of such a call of printf
printf( "%-3d", a[i][j] );
to use
printf( "%-*d", len + 1, a[i][j] );
so I did a little of modifications on your code like , you outer for loop must loop extra one time to print that = symbol , that's why I used that flag called printedHorizontalLineFlag so that not to go out of array boundaries.
and here is the full edited code :
#include <stdio.h>
void func(int column, int rows, int mat[rows][column])
{
int printedHorizontalLineFlag = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < column; j++) {
if (j == column - 2) {
printf("%d\t| ", mat[i][j]);
}
else if (j == column - 1) {
printf("%d\n", mat[i][j]);
}
else if (printedHorizontalLineFlag == 1 && i == rows - 1) {
printf("%d\t", mat[i][j]);
}
else if (printedHorizontalLineFlag == 0 && i == rows - 1) {
printf("============================\n");
i--;
printedHorizontalLineFlag = 1;
break;
}
else {
printf("%d\t", mat[i][j]);
}
}
}
}
int main()
{
int mat[][4] = {{1, 2, 3, 6},
{4, 5, 6, 15},
{7, 8, 9, 24},
{12, 15, 18, 45}};
func(4, 4, mat);
return 0;
}
and here is image of the output :

Checking for duplicate numbers in a 2D matrix and containing the data into two variables

I am trying to check 2D matrix array for the correct numbers and extras on each row and then printing them out only once per correct number/extra.
I.e. if 34 is twice in jackpot and once in the matrix row it should only record it once.
Correct numbers are first 7 numbers of jackpot array and the extras are the last 3 numbers.
Can anyone explain why the Row 2's 1 extra is being eliminated from my current output?
I am guessing it is because the correct number 34 is already recorded in previous[] and this is then compared to the extra 34 and thus extra is substracted by 1.
How would one go about fixing this, should I make another array that stores the previous extras or something?
The correct output:
Row 1 has 0 correct and 0 extras!
Row 2. has 1 correct and 1 extras!
Row 3. has 3 correct and 0 extras!
Row 4. had 1 correct and 1 extras!
Row 5. has 1 correct and 0 extras!
My current output:
Row 1. has 0 correct and 0 extras!
Row 2. has 1 correct and 0 extras!
Row 3. has 3 correct and 0 extras!
Row 4. has 1 correct and 1 extras!
Row 5. has 1 correct and 0 extras!
for (int z = 0; z < NUM_PER_ROW; z++)
{
if (previous[z] == jackpot[k] && k <= 6)
{
correct -=1;
break;
}
else if (previous[z] == jackpot[k] && k > 6)
{
extra -=1;
break;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_ROWS 5
#define NUM_PER_ROW 7
#define MAX_NUM 40
int doStuff();
int doStuff()
{
int matrix[MAX_ROWS][NUM_PER_ROW] = {
{ 1, 2, 3, 4, 5, 6, 7},
{30, 38, 6, 34, 4, 39, 30},
{ 9, 37, 32, 27, 9, 39, 17},
{ 8, 26, 6, 31, 28, 29, 16},
{33, 21, 19, 7, 9, 19, 30} };
int jackpot[NUM_PER_ROW + 3] =
{20, 37, 22, 9, 34, 28, 34, 24, 26, 29};
int num = 0, i, j, cnt = 0, temp = 0;
for(i = 0; i < MAX_ROWS; i++)
{
int correct = 0;
int extra = 0;
for(j = 0; j < NUM_PER_ROW; j++)
{
int previous[7] = {0, 0, 0, 0, 0, 0, 0};
for (int k = 0; k < NUM_PER_ROW + 3; k++)
{
if (matrix[i][j] == jackpot[k])
{
for (int z = 0; z < NUM_PER_ROW; z++)
{
if (previous[z] == jackpot[k] && k <= 6)
{
correct -= 1;
break;
}
else if (previous[z] == jackpot[k] && k > 6)
{
extra -=1;
break;
}
}
if (k <= 6)
{
correct += 1;
}
else
{
extra += 1;
}
previous[j] = jackpot[k];
}
}
}
printf("Row %d. has %d correct and %d extras! \n", i + 1, correct, extra);
}
return 0;
}
int main()
{
int num;
srand(time(NULL));
doStuff(num);
return 0;
}
Thank you for your time!
I suggest using an array to mark the duplicates in jackpot[]:
char jp_dupe[NUM_PER_ROW + 3];
for (i = 0; i < NUM_PER_ROW + 3; i++)
{
for (j = 0; j < i && jackpot[i] != jackpot[j]; j++)
;
jp_dupe[i] = (i != j); /* 0 if first occurrence, 1 if a duplicate */
}
Then there is no need for the previous[] array or the z loop, and the k loop can be simplified:
for (int k = 0; k < NUM_PER_ROW + 3; k++)
{
if (!jp_dupe[k] && matrix[i][j] == jackpot[k])
{
if (k < NUM_PER_ROW)
{
correct += 1;
}
else
{
extra += 1;
}
}
}
I took the liberty of replacing the <= 6 test to avoid the magic number. The magic number 3 could also be replaced with a macro.

Why it is this program not finding pairs in an array that sum to a given value?

My code so far:
#include<stdio.h>
int main() {
int a[5][5] = {
{-1, 2, 3, 4 },
{ 5, 3, -2, 1 },
{ 6, 7, 2, -3 },
{ 2, 9, 1, 4 },
{ 2, 1, -2, 0 }
};
int sum = 11;
int i, j;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (a[i][j] + a[j][i] == sum && i != j) {
printf("%d %d\n", i, j);
}
}
}
}
I want to find pairs with given sum such that elements of pair are in different rows.
Note that a is a 5x5 array with an extra column of zeros. That has a benign effect.
Your algorithm is faulty! If you adjust your conditional to
if (a[i][j] + a[j][i] == sum && i != j) {
printf("Yes %d %d\n", i, j);
} else {
printf("No %d %d\n", i, j);
}
it becomes obvious that you are not considering the sum of every possible pair. The ones that get missed out are, by coincidence, the pairs that sum to 11.
Essentially you need 4 nested loops, and you need to consider every number with every other number in the array.
First, you should notice that you defined a 5X5 int array but only filled in 4 ints in each sub-array. If you would print out your array, you'll see the values are:
-1 2 3 4 0
5 3 -2 1 0
6 7 2 -3 0
2 9 1 4 0
2 1 -2 0 0
I guess that wasn't your intention...
Now, for your question - you want to find a[i][j] + a[j][i] == 11 for i!=j. The reason you get no output is because there aren't such elements that satisfy your conditions in the array you defined.
Below program will solve your problem,
#include<stdio.h>
int main()
{
int a[5][5] = {
{-1, 2, 3, 4 },
{ 5, 3, -2, 1 },
{ 6, 7, 2, -3 },
{ 2, 9, 1, 4 },
{ 2, 1, -2, 0 }
};
int sum=11,i,j,k,l;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
for(k=0;k<5;k++)
{
for(l=0;l<5;l++)
{
if(a[i][k]+a[j][l]==sum)
printf("a[%d][%d] = %d\ta[%d][%d] = %d\n",i,k,a[i][k],j,l,a[j][l]);
}
}
}
}
}
if you want "pairs with given sum such that elements of pair are in different rows." then you should try code like this:
#include<stdio.h>
int main()
{
const int rows = 5;
const int cols = 4;
int sum = 11;
int i, j, i1, j1;
int a[rows][cols] =
{
{-1, 2, 3, 4 },
{ 5, 3, -2, 1 },
{ 6, 7, 2, -3 },
{ 2, 9, 1, 4 },
{ 2, 1, -2, 0 }
};
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
for(i1 = i + 1; i1 < rows; i1++)
for(j1 = 0; j1 < cols; j1++)
if(a[i][j] + a[i1][j1] == sum)
printf("[%d %d](%d); [%d %d](%d)\n", i, j, a[i][j], i1, j1, a[i1][j1]);
}

Why does this sorting doesn't work ? (C)

I am trying to create a sorting method based on selection sort algorithm
With this current code, the array [10, 9, 8 .. 1] is "sorted" to
[9, 8 .. 2, 10, 1]
I mean like, it doesn't even put 10 in the right place
10 9 8 7 6 5 4 3 2 1
"sorted" to
9 8 7 6 5 4 3 2 10 1
What's the problem ?
void selectionSort(int array[], int length)
{
int i = 0, j = 0, temp = 0, swap = 0;
for(i = 0; i < length; i++)
{
temp = i;
for(j = 0; j < length; j++)
{
if(array[temp] > array[j])
{
temp = j;
}
}
swap = array[temp];
array[temp] = array[i];
array[i] = swap;
}
}
The inner loop should be written like
for(j = i + 1; j < length; j++)
^^^^^^^^^
After each iteration of i, the array upto i , should be sorted. You can print the array after each iteration of i and can see the logical error.

Sudoku code checker in c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm writing a Sudoku solution checker for a class and I've hit a wall.
I'm at the point where I'm checking if I can see whether or individual columns and rows are unique. For some reason the code works on 4x4 grids but as soon as I get up to a 5x5 grid or higher (goal is to get to a 9x9 grid) the program starts to print out that it had failed even when it should succeed.
Any help would be much needed, I want need a point in the right direction or where I should look into
Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i, j, n, k, p, q;
int fail;
int array[5][5];
int check[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int a = 0;
char *output = NULL;
scanf("%d", &n);
// memory allocated for yes or no at end
output = malloc(sizeof(int) * (n));
while (a < n)
{
fail = 0;
// create this 2D array
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
scanf("%d", &(array[i][j]));
}
}
// seeing if row is unique
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
{
if (array[i][k] == array[i][k+1])
fail += 1;
}
}
}
// seeing if column is unique
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
{
if (array[k][j] == array[k+1][j])
fail += 1;
}
}
}
/* for (WHAT DO I DO FOR ROWS)
{
for (WHAT DO I DO FOR ROWS AGAIN BUT REPLACE ROWS WITH COLUMNS)
{
for (NOW IM LOST)
}
}
*/
// success or failure? 0 success, 1 failure
if (fail >= 1)
output[a] = 1;
else
output[a] = 0;
a++;
}
// print out yah or nah
for (i = 0; i < n; i++)
{
if (output[i] == 0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Forget my for loop for the grids, I'll work on that once I figure out how to get the columns and rows working correctly.
Thanks for the help!
Here is an input that would cause the program to fail when it should succeed
1
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
output would be
NO
EDIT: It is now working with a 9x9 grid! Thanks for the help!
#include <stdio.h>
#include <stdlib.h>
#define SIDE_LENGTH 9
int main ()
{
int i, j, n, k, p, q;
int fail;
int array[SIDE_LENGTH][SIDE_LENGTH];
int check[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int a = 0;
char *output = NULL;
scanf("%d", &n);
// memory allocated for yes or no at end
output = malloc(sizeof(int) * (n));
while (a < n)
{
fail = 0;
// create this 2D array
for (i = 0; i < SIDE_LENGTH; i++)
{
for (j = 0; j < SIDE_LENGTH; j++)
{
scanf("%d", &(array[i][j]));
}
}
// seeing if row is unique
for (i = 0; i < SIDE_LENGTH; i++)
{
for (j = 0; j < SIDE_LENGTH; j++)
{
for (k = 0; k < SIDE_LENGTH - 1; k++)
{
if (array[i][k] == array[i][k+1])
fail += 1;
}
}
}
// seeing if column is unique
for (i = 0; i < SIDE_LENGTH; i++)
{
for (j = 0; j < SIDE_LENGTH; j++)
{
for (k = 0; k < SIDE_LENGTH - 1; k++)
{
if (array[k][j] == array[k+1][j])
fail += 1;
}
}
}
/* for (WHAT DO I DO FOR ROWS)
{
for (WHAT DO I DO FOR ROWS AGAIN BUT REPLACE ROWS WITH COLUMNS)
{
for (NOW IM LOST)
}
}
*/
// success or failure? 0 success, 1 failure
if (fail >= 1)
output[a] = 1;
else
output[a] = 0;
a++;
}
// print out yah or nah
for (i = 0; i < n; i++)
{
if (output[i] == 0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
input:
1
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
#ameyCU helped find the error in my code
Setting k to one less than what i and j were set to allowed the code to successfully run on any X*X sized grid. Because k is one less than i and j, it won't try to access a part of the array that hasn't been allocated yet which is where my problem lied.
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
{
if (array[i][k] == array[i][k+1])
fail += 1;
}
}
}
Despite the overwriting of the array as already pointed out, your logic is flawed. You don't use j at all. You are just comparing the same values five times.
The problem is the comparison.
if (array[i][k] == array[i][k+1])
I think you are using i as row and column index, then using j to iterate for duplicates. k will be what you compare against so ...
/* compare if j'th value is same as k'th value */
if (j != k && array[i][j] == array[i][k]) /* Don't check same against same */
the second comparison should be
/* compare if j'th value is same as k'th value */
if (j != k && array[j][i] == array[k][i]) /* Don't check same against same */
That would fix your overflow (k+1) bug, and get you going.
The squares could be fixed with
struct co_ords {
int x;
int y;
};
struct co_ords boxes[][9] = {{ {0,0}, {0,1}, {0,2},
{1,0}, {1,1}, {1,2},
{2,0}, {2,1}, {2,2}
},
{ {3,0}, {3,1}, {3,2},
{4,0}, {4,1}, {4,2},
{5,0}, {5,1}, {5,2} },
... /* more boxes go here */
{ {6,6}, {6,7}, {6,8},
{7,6}, {7,7}, {7,8},
{8,6}, {8,7}, {8,8} }};
for( i = 0; i < 9; i++ ){
struct co_ords current_box * = boxes[i];
for( j = 0; j < 9; j++ ) {
for( k = 0; k < 9; k++ ){
if( j != k && array[ current_box[j].x ][ current_box[j].y] == array[ current_box[k].x ][ current_box[k].y] )
fail += 1;
}
}
}
int array[5][5];
so the array is allocated as 5x5
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
scanf("%d", &(array[i][j]));
}
}
and you are indexing from 0 to 5 ..
to use larger, please do replace all those "5"s with a precompiler definition.
#define SUDOKU_SIDE_LENGTH 5
...
int array[SUDOKU_SIDE_LENGTH ][SUDOKU_SIDE_LENGTH ];
...
for (i = 0; i < SUDOKU_SIDE_LENGTH ; i++)
{
for (j = 0; j < SUDOKU_SIDE_LENGTH ; j++)
{
scanf("%d", &(array[i][j]));
}
}
etc.. that will ensure that you always allocate enough space for the array.
adjust size on the definition, not in the code..

Resources