Related
I need to program a number pattern pyramid like that:
Here is my code:
#include<stdio.h>
int main()
{
int i, j, rows = 5;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
}
Where am I doing wrong? I need to flip somehow the first triangle pattern and mix it with the second. Please help.
Your pyramid is 6 levels high. The max number is 5. This is not a coincidence. There is a direct relationship.
If n is 5, you need to do something 6 times, from 0 to n. There's a loop.
for (int i = 0; i <= n; i++) {
...
}
On each row you need to do print from i to 0, and then from 1 to i. That's two loops.
for (int j = i; j > 0; j--) {
...
}
for (int j = 1; j <= i; j++) {
...
}
Of course, you also need to indent a number of spaces inversely related to i, which is another loop.
for (int j = 0; j <= /* Fill in here for inverse relationship */; j++) {
...
}
I need to flip somehow the first triangle pattern and mix it with the second.
TL;DR The trick is very simple: your row index i should start from |n - j| (or abs(n - j)) where j is the column index and n is the number.
Here is how you can do it:
for (int j = 0; j < ncols; ++j) {
for (int i = abs(n - j); i < nrows; ++i) {
matrix[i][j] = '0' + abs(n - j);
}
}
Explanation:
Imagine you have a matrix. In order to build a pyramid with a number n, you will need:
n + 1 rows (since you will have 0 included in the top). Let's call it nrows.
2n + 1 columns (2n because the numbers will appear twice in a row, 1 because you will have one 0 per row). Let's call it ncols.
0 1 2 3 4 5 6 7 8 9 10
- - - - - - - - - - -
0 | 0
1 | 1 0 1
2 | 2 1 0 1 2
3 | 3 2 1 0 1 2 3
4 | 4 3 2 1 0 1 2 3 4
5 | 5 4 3 2 1 0 1 2 3 4 5
First, you want to fill the first half, i.e. from column 0 to n: for (int j = 0; j <= n; ++j).
J | End | Start | Pattern of Start
---------------------------------------
0 | N | N | N - 0
---------------------------------------
1 | N | N - 1 | N - 1
---------------------------------------
2 | N | N - 2 | N - 2
---------------------------------------
... | ... | ..... | N - J
---------------------------------------
N | N | 0 | N - N
The pattern is: for (int i = n - j; i < nrows; ++i). You would fill that first half with n - j.
Second, you want to fill the second half, i.e. from column n + 1 to 2n: for (int j = n + 1; j < ncols; ++j).
J | End | Start | Pattern of Start
------------------------------------------
N + 1 | N | 1 | N + 1 - N = J - N
------------------------------------------
N + 2 | N | 2 | N + 2 - N = J - N
------------------------------------------
N + 3 | N | 3 | N + 3 - N = J - N
------------------------------------------
..... | ... | ..... | N + X - N = J - N
------------------------------------------
2N | N | N | 2N - N = J - N
The pattern is: for (int i = j - n; i < nrows; ++i). You would fill that second half with j - n.
Now, you can do two separate for loops to fill the first and second halves.
// Fill first half
for (int j = 0; j <= n; ++j) {
for (int i = n - j; i < nrows; ++i) {
matrix[i][j] = '0' + n - j;
}
}
// Fill second half
for (int j = n + 1; j < ncols; ++j) {
for (int i = j - n; i < nrows; ++i) {
matrix[i][j] = '0' + j - n;
}
}
You can write one for loop by replacing n - j and j - n with abs(n - j), since both of them are positive values (see TL;DR above, or the code below).
Full functionning code:
#include <stdio.h>
#include <stdlib.h>
void print_matrix(const int nrows, const int ncols, const char matrix[nrows][ncols])
{
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j)
printf("%c ", matrix[i][j]);
printf("\n");
}
}
void init_matrix(const int nrows, const int ncols, char matrix[nrows][ncols], const char value)
{
for (int i = 0; i < nrows; ++i)
for (int j = 0; j < ncols; ++j)
matrix[i][j] = value;
}
void print_pyramid(const int n)
{
// Calculate the number of rows and columns
const int nrows = n + 1;
const int ncols = n * 2 + 1;
char matrix[nrows][ncols];
// Initialization
init_matrix(nrows, ncols, matrix, '\0');
// Fill the matrix
for (int j = 0; j < ncols; ++j) {
for (int i = abs(n - j); i < nrows; ++i) {
matrix[i][j] = '0' + abs(n - j);
}
}
// Printing
print_matrix(nrows, ncols, matrix);
}
int main(void)
{
print_pyramid(5);
}
You have to include all 3 for loops in one for loop which runs 6 times. First you have to print spaces. Then first half of the triangle and then the other half. After that you have to print \n .
#include<stdio.h>
int main()
{
int i, j,k,l;
for (i = 0; i <=5; i++)
{
for (j = 5; j > i; j--)
{
printf(" ");
}
for (k = i; k >=0; k--)
{
printf("%d ", k);
}
for (l = 1; l <=i; l++)
{
printf("%d ", l);
}
printf("\n");
}
}
I tried to code a right rotation for a 2D array.
Here is the main code:
for (int k = 0; k < rotate; k++) { //rotate is the no of times to shift right
for (int i = 0; i < n1; i++) { //n1 is no: of rows
for (int j = 0; j <n2; j++) { //n2 is no: of columns
//Shifting right
int temp = A[i][n2 - 1];
A[i][n2 - 1] = A[i][j + 1];
A[i][j + 1] = A[i][j];
A[i][j] = temp;
}
}
}
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
printf("%d", A[i][j]);
}
printf("\n");
}
It is working for size 2x2 where:
Input:
1 2
3 4
Output:
2 1
4 3
But it's not working for size 3x3 and above.
Input:
1 2 3
4 5 6
7 8 9
Output:
3 2 1
6 5 4
9 8 7
Where expected output is:
3 1 2
6 4 5
9 7 8
Please guide me about where I'm wrong and I apologize for any mistakes in my question.
See:
https://www.programiz.com/c-programming/examples/matrix-transpose ;)
You can change that solution to use one array.
In your code you are referring to both left and right neighbours (though left one is wrongly referred because it should be last cell only for first interation) and don't keep value for next iteration.
It should be implemented as follows:
For each row left is initalized with the value of very last item in the row, because it is on the left of 0th item. Then while iterating over row items we first save current value to temp to use it later, then save left to current item, and then use previosly saved temp as new left for next iteration.
for (int k = 0; k < rotate; k++) { //rotate is the no of times to shift right
for (int i = 0; i < n1; i++) { //n1 is no: of rows
int left = A[i][n2 - 1];
for (int j = 0; j < n2; j++) { //n2 is no: of columns
//Shifting right
int temp = A[i][j];
A[i][j] = left;
left = temp;
}
}
}
I have a homework problem. It requires us to make a matrix based on user's input. For example : if user input 4 so the matrix will be 4 X 4. After that, the program will check if the matrix has the same value in a row or column. and it will give yes or no output.
For example :
input :
2
1 2
2 1
Output :
Yes
(because that matrix doesnt has a same value in a row or a column.)
Input 2 :
3
4 5 6
7 8 9
7 3 3
Output :
No
(Because that matrix have same values in a row or column (3 & 3 and 7 & 7)
Input 3:
2
1 2
3 2
Output :
No
(because that matrix have same value on column 1.)
Input 4
2
1 1
3 4
Output :
No
(because that matrix has same value in first row(1 1)
I have tried to do that, but some 'cases' still doesnt work. For example, i tried to include a count in my code but some of the count is not true.
example :
input :
4
3 4 5 6
2 3 4 5
6 5 6 3
5 4 6 3
OUTPUT :
No
count : 2
(it supposed to be 3 because it has the same value which are 6 (on row 3), 6 on column 3, and 3 on column 4.)
#include "stdio.h"
int main()
{
int matrix[500][500];
int testcase;
int count = 0;
scanf("%d",&testcase); getchar();
for(unsigned i = 0; i < testcase; i++) {
for(unsigned j = 0; j < testcase; j++) {
scanf("%d",&matrix[i][j]); getchar();
}
}
// printf("[0,0] = %c",matrix[0][0]);
// printf("\n[0,1] = %c",matrix[0][1]);
// printf("\n[1,0] = %c",matrix[1][0]);
// printf("\n[1,1] = %c",matrix[1][1]);
for(unsigned i = 0; i < testcase; i++) {
for(unsigned j = 0; j < testcase; j++) {
if(matrix[i][j] == matrix[i][j+1]) {
count = count + 1;
}
else if(matrix[i][j] == matrix[i+1][j]) {
count = count + 1;
}
}
}
if(count > 0) {
printf("No\n");
} else{
printf("Yes\n");
}
printf("Count : %d\n",count );
getchar();
return 0;
}
As I see you check if 2 numbers of the same value differ only one column or one row here:
if(matrix[i][j] == matrix[i][j+1]) {
count = count + 1;
}
else if(matrix[i][j] == matrix[i+1][j]) {
count = count + 1;
}
I think that you might need a temp variable so that you can scan each line and then each column separately , for example:
temp = matrix[i][j];
if(checkRow(temp, i, j, matrix, testcase) == true) count++;
if(checkColumn(temp, i, j, matrix, testcase) == true) count++;
and the checkRow would be something like this:
bool checkRow(int temp, int row, int col, int matrix[][500], int size)
{
for(int i=col; i < size;)
{
if(temp == matrix[row][i]) return true;
}
return false;
}
and respectively you will build the checkColumn function.
EDIT:
Since you told me you haven't learned how to use functions yet, this would be your final program. It works and I might suggest that the final test case should output "count = 4" since there is a case that you might missed.
Here is the code:
#include "stdio.h"
int main()
{
int matrix[500][500];
int testcase;
int count = 0;
scanf("%d",&testcase); getchar();
int temp;
for(unsigned i = 0; i < testcase; i++) {
for(unsigned j = 0; j < testcase; j++) {
scanf("%d",&matrix[i][j]); getchar();
}
}
// printf("[0,0] = %c",matrix[0][0]);
// printf("\n[0,1] = %c",matrix[0][1]);
// printf("\n[1,0] = %c",matrix[1][0]);
// printf("\n[1,1] = %c",matrix[1][1]);
for(unsigned i = 0; i < testcase; i++) {
for(unsigned j = 0; j < testcase; j++) {
temp = matrix[i][j];
//Scan current row
for(unsigned k = j+1; k < testcase; k++)
{
if(temp == matrix[i][k])
{
count++;
break;
}
}
//Scan current column
for(unsigned k = i+1; k < testcase; k++)
{
if(temp == matrix[k][j])
{
count ++;
break;
}
}
}
}
if(count > 0) {
printf("No\n");
} else{
printf("Yes\n");
}
printf("Count : %d\n",count );
getchar();
return 0;
}
May I suggest that before you copy the code you must understand the algorithm that lies behind it. It's simple and brute force thinking.
Problem Statement:
Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum.
Sample Inputs,
[1,6,5,11] => 1. The 2 subsets are {1,5,6} and {11} with sums being 12 and 11. Hence answer is 1.
[36,7,46,40] => 23. The 2 subsets are {7,46} and {36,40} with sums being 53 and 76. Hence answer is 23.
Constraints
1 <= size of array <= 50
1 <= a[i] <= 50
My Effort:
int someFunction(int n, int *arr) {
qsort(arr, n, sizeof(int), compare);// sorted it for simplicity
int i, j;
int dp[55][3000]; // sum of the array won't go beyond 3000 and size of array is less than or equal to 50(for the rows)
// initialize
for (i = 0; i < 55; ++i) {
for (j = 0; j < 3000; ++j)
dp[i][j] = 0;
}
int sum = 0;
for (i = 0; i < n; ++i)
sum += arr[i];
for (i = 0; i < n; ++i) {
for (j = 0; j <= sum; ++j) {
dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
if (j >= arr[i])
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], arr[i] + dp[i][j + 1 - arr[i]]);
}
}
for (i = 0; i < n; ++i) {
for (j = 0; j <= sum; ++j)
printf("%d ", dp[i + 1][j + 1]);
printf("\n");
}
return 0;// irrelevant for now as I am yet to understand what to do next to get the minimum.
}
OUTPUT
Let's say for input [1,5,6,11], I am getting the dp array output as below.
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
0 1 1 1 1 5 6 7 7 7 7 11 12 12 12 12 12 12 12 12 12 12 12 12
0 1 1 1 1 5 6 7 7 7 7 11 12 12 12 12 16 17 18 18 18 18 22 23
Now, how to decide the 2 subsets to get the minimum?
P.S - I have already seen this link but explanation is not good enough for a DP beginner like me.
You have to solve subset sum problem for SumValue = OverallSum / 2
Note that you don't need to solve any optimization problem (as using max operation in your code reveals).
Just fill linear table (1D array A) of size (SumValue + 1) with possible sums, get the closest to the last cell non-zero result (scan A backward) wint index M and calculate final result as abs(OverallSum - M - M).
To start, set 0-th entry to 1.
Then for every source array item D[i] scan array A from the end to beginning:
A[0] = 1;
for (i = 0; i < D.Length(); i++)
{
for (j = SumValue; j >= D[i]; j--)
{
if (A[j - D[i]] == 1)
// we can compose sum j from D[i] and previously made sum
A[j] = 1;
}
}
For example D = [1,6,5,11] you have SumValue = 12, make array A[13], and calculate possible sums
A array after filling: [0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1]
working Python code:
def besthalf(d):
s = sum(d)
half = s // 2
a = [1] + [0] * half
for v in d:
for j in range(half, v - 1, -1):
if (a[j -v] == 1):
a[j] = 1
for j in range(half, 0, -1):
if (a[j] == 1):
m = j
break
return(s - 2 * m)
print(besthalf([1,5,6,11]))
print(besthalf([1,1,1,50]))
>>1
>>47
I'll convert this problem to subset sum problem
let's take array int[] A = { 10,20,15,5,25,33 };
it should be divided into {25 20 10} and { 33 20 } and answer is 55-53=2
Notations : SUM == sum of whole array
sum1 == sum of subset1
sum2 == sum of subset1
step 1: get sum of whole array SUM=108
step 2: whichever way we divide our array into two part one thing will remain true
sum1+ sum2= SUM
step 3: if our intention is to get minimum sum difference then
sum1 and sum2 should be near SUM/2 (example sum1=54 and sum2=54 then diff=0 )
steon 4: let's try combinations
sum1 = 54 AND sum2 = 54 (not possible to divide like this)
sum1 = 55 AND sum2 = 53 (possible and our solution, should break here)
sum1 = 56 AND sum2 = 52
sum1 = 57 AND sum2 = 51 .......so on
pseudo code
SUM=Array.sum();
sum1 = SUM/2;
sum2 = SUM-sum1;
while(true){
if(subSetSuMProblem(A,sum1) && subSetSuMProblem(A,sum2){
print "possible"
break;
}
else{
sum1++;
sum2--;
}
}
Java code for the same
import java.util.ArrayList;
import java.util.List;
public class MinimumSumSubsetPrint {
public static void main(String[] args) {
int[] A = {10, 20, 15, 5, 25, 32};
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
subsetSumDynamic(A, sum);
}
private static boolean subsetSumDynamic(int[] A, int sum) {
int n = A.length;
boolean[][] T = new boolean[n + 1][sum + 1];
// sum2[0][0]=true;
for (int i = 0; i <= n; i++) {
T[i][0] = true;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (A[i - 1] > j) {
T[i][j] = T[i - 1][j];
} else {
T[i][j] = T[i - 1][j] || T[i - 1][j - A[i - 1]];
}
}
}
int sum1 = sum / 2;
int sum2 = sum - sum1;
while (true) {
if (T[n][sum1] && T[n][sum2]) {
printSubsets(T, sum1, n, A);
printSubsets(T, sum2, n, A);
break;
} else {
sum1 = sum1 - 1;
sum2 = sum - sum1;
System.out.println(sum1 + ":" + sum2);
}
}
return T[n][sum];
}
private static void printSubsets(boolean[][] T, int sum, int n, int[] A) {
List<Integer> sumvals = new ArrayList<Integer>();
int i = n;
int j = sum;
while (i > 0 && j > 0) {
if (T[i][j] == T[i - 1][j]) {
i--;
} else {
sumvals.add(A[i - 1]);
j = j - A[i - 1];
i--;
}
}
System.out.println();
for (int p : sumvals) {
System.out.print(p + " ");
}
System.out.println();
}
}
Working Java code if anyone is interested but the idea remains the same as what #MBo has answered
class Solution{
public int minDifference(int arr[]) {
int sum = 0;
for(int x : arr) sum += x;
int half = (sum >> 1) + (sum & 1);
boolean[] sums = new boolean[half + 1];
sums[0] = true;
for(int i = 0; i < arr.length; ++i){
if(arr[i] > half) continue;
for(int j = half; j >= arr[i]; --j){
if(sums[j - arr[i]]){
sums[j] = true;
}
}
}
for(int i = sums.length - 1; i >= 1; --i){
if(sums[i]) return Math.abs((sum - i) - i);
}
return sum; // for arrays like [2] or [100] etc
}
}
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..