How to change the position of sectors in the matrix? - c

A task:
Fill in the sectors of the matrix that lie to the left and right of the main and secondary diagonals, from the upper left corner to the right - down. Fill the rest of the matrix with zeros.
What happened to me:
#include <stdio.h>
int main()
{
int a[9][9], n=9, t=1, i, j;
for(j=0; j<n; j++)
for(i=0; i<n; i++)
if((j<i && i<n/2) || (j<n-i-1 && i>=n/2) || (j>n-i-1 && i<n/2) || (j>i && i>=n/2))
a[i][j]=t++;
else
a[i][j]=0;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%4d", a[i][j]);
printf("\n");
}
return 0;
}

Your first pair of nested nested loops are inverted. The following
for(j=0; j<n; j++)
for(i=0; i<n; i++)
should be
for(i=0; i<n; i++)
for(j=0; j<n; j++)
Better code formatting and variable names help to spot errors like these.
Note that introducing a third control variable for the edge distance in each line helps to remove a lot of complexity from the conditional statement.
For example:
#include <stdio.h>
#define SIZE 9
#define GROWTH 1
int main(void)
{
for (int row = 0, edge = 0, number = 1; row < SIZE; row++) {
for (int column = 0; column < SIZE; column++) {
printf("%4d", (column < edge || SIZE - column <= edge) ? number++ : 0);
}
printf("\n");
edge += row < SIZE / 2 ? GROWTH : -GROWTH;
}
}
Fixing the for loop order in your program, or using the example above, both produce this output:
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 2
3 4 0 0 0 0 0 5 6
7 8 9 0 0 0 10 11 12
13 14 15 16 0 17 18 19 20
21 22 23 0 0 0 24 25 26
27 28 0 0 0 0 0 29 30
31 0 0 0 0 0 0 0 32
0 0 0 0 0 0 0 0 0

Related

Function. 2 2D arrays, the second is full of 0's, after applying condition,copy the whole row from first 2D array to the second 2D array

So, for example I have 2 Arrays, one is
0 -1 1 0 1
0 -1 0 0 0
0 1 1 -1 0
0 0 0 -1 0
0 -1 0 1 0
the second is full of 0's.
After I meet the condition a[i][j] == -1 && a[i][k] == 1, where k=j+1, I need to copy the whole row and put it into the second array, so the result should be:
0 -1 1 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 -1 0 1 0
I've already alloced the memory for both arrays, and here's the part of the code that I've tried but it doesnt work, it copies the whole array
void copy_row(int **a, int **b, int m){
for(int i=0; i<m; i++){
b[i]=a[i];
}
}
int main(){
...
for(int i=0 ; i<n ; i++){
for(int j=0 ; j<m ; j++){
for(int k=j+1; k<m ; k++){
if (a[i][j]==-1 && a[i][k]==1){
copy_row(a,b,n);
}
}
}
}
If you want to copy a row, you would need to pass the index of the current row to copy_row, plus the number of columns.
The call from main could be
// Notice that we pass index i and not n
copy_row(m, a, b, i);
The copy_row could be:
void copy_row(int numOfColumns, int a[][numOfColumns], int b[][numOfColumns], int rowIndex) {
for (int ii = 0, ii < numOfColumns, ++ii) {
b[rowIndex][ii] = a[rowIndex][ii];
}
}
You could add a break after the call to copy_row in the if statement. Once a row is copied, there is no need to keep checking the rest of the columns for -1.
E.g., you might reduce the checks made by using
for(int j = 0 ; j < m ; ++j) {
if (a[i][j] == -1) {
for(int k = j + 1; k < m ; ++k) {
if (a[i][k] == 1) {
copy_row(m, a, b, i);
break;
}
}
break;
}
}

check column and row in sodoku- C programming

I'm trying to check if number (from 1 to n)
appears more than once in
row or column in sodoku board
I wrote a code that goes over each row and column and checks the numbers between 1 to n (every time checking number at a time after finishing with row 0 (for example) goes to the next one (if it wasn't true in the row before)
my code is working just if there is a number
appearing more than once
in row 0 (if there is in other rows it returns nothing!)
and for the column it doesn't return any thing
this is a matrix that I use for the test (matrix with solution and I change numbers in rows or columns):
7 1 0 0 0 0 6 0 9
2 0 0 0 0 3 0 0 0
0 0 0 1 5 0 0 0 8
0 0 7 0 0 0 0 9 0
0 0 6 0 0 0 7 0 0
0 2 0 0 0 0 4 0 0
1 0 0 0 2 9 0 0 0
0 0 0 3 0 0 0 0 4
9 0 5 0 0 0 0 8 6
and this is my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX4USED 10
int n; //size of the sodoku board
bool CheckAppearingInRow(int matrix [n][n]) {
int used[MAX4USED] = {0};
int index = 1;
int row = 0;
while (index <= n) {
for(int j = 0; j < n; j++) {
if(matrix[row][j] == index ) {
used[index] +=1;
}
}
for(int i = 0; i <= n ; i++) {
if (used[i] > 1) {
return true;
}
used[i] = 0;
}
index += 1;
}
row += 1;
while (row < n) {
CheckAppearingInRow(matrix);
}
return false;
}
bool CheckAppearingInColumn(int matrix [n][n]) {
int used[MAX4USED] = {0};
int index = 1;
int col = 0;
while (index <= n) {
for(int i = 0; i < n; i++) {
if(matrix[i][col] == index ) {
used[index] +=1;
}
}
for(int i = 0; i <= n ; i++) {
if (used[i] > 1) {
return true;
}
used[i] = 0;
}
index += 1;
}
col += 1;
while(col < n){
CheckAppearingInColumn(matrix);
}
return false;
}
int main() {
printf("Please enter your sodoku dimension:");
scanf("%d", &n);
int a[n][n];
printf("\nInsert your sodoku board\n");
printf("Instruction: Enter 0 for blank\n");
for(int i=0; i<n; i++){
for(int j=0; j<n; j++) {
scanf("%d", &a[i][j]);
}
}
if (CheckAppearingInRow(a)== true || CheckAppearingInColumn(a) == true) {
printf("\nNo solution!\n");
}
return 0;
}

How to find neighbours with the same value in 2-D array in C?

I have a matrix with dimensions NxN, E.g. in this case 5x5:
1 0 1 0 1
0 1 0 1 0
0 0 1 0 1
0 0 1 0 0
1 1 0 1 0
and I want to get all the neighbours with the same value like the element at position (3,3), and also to get neighbours of the neighbours with same value, etc. The constraint is that I can not use recursion, does anyone know how to solve this problem?
This might be the simplest solution.
Assuming that diagonals aren't considered as neighbours.
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (i > 0 && tab[i][j] == tab[i-1][j]) {
// same value
}
if (i < N-1 && tab[i][j] == tab[i+1][j]) {
// same value
}
if (j > 0 && tab[i][j] == tab[i][j-1]) {
// same value
}
if (j < N-1 && tab[i][j] == tab[i][j+1]) {
// same value
}
}
}

Moving elements in 2d array

I have 2d array filled with random numbers.
For example:
#define d 4
int main(void)
{
int a[d][d];
int primary[d], secondary[d];
size_t i, j;
srand(time(NULL)); /* fill array with random numbers */
for (i = 0; i < d; i++)
{for (j = 0; j < d; j++)
a[i][j] = rand() % 100;
}
How to change diagonals . For example :
1 0 0 0 2 2 0 0 0 1
0 3 0 4 0 0 4 0 3 0
0 0 5 0 0 to 0 0 5 0 0
0 6 0 7 0 0 7 0 6 0
8 0 0 0 9 9 0 0 0 8
Task is to print random matrix of d size then change diagonals placement using cycle and print it again.However i`m not getting how cycle should look like.
Appreciate any hints or examples.
Loop while j < d / 2 and then swap the values:
for (i = 0; i < d; i++) {
for (j = 0; j < d / 2; j++) {
int temp = a[i][j];
a[i][j] = a[i][d - j -1];
a[i][d - j -1] = temp;
}
}

Convert decimal values in 1D array to 2D array with bit values, transpose the 2D bit array and convert to 1D decimal array again

My problem is part of a larger communication algorithm I'm trying to implement. The point is to generate packets from messages, to send over the network. You fetch a batch of messages (decimal values), and form the packets from the bits from each message that are in the same column. The following figure illustrates this.
Packet formation from messages
My problem is the 'transpose' operation. How I'm trying to approach this is by transposing the bits of this 1D decimal value array of messages. Maximum decimal value of each message is 255, so 8 bits in length each.
I want to convert all decimal values to bits in a 2D array, where each column is a bit from the decimal value in that row. Finally I want to convert this 2D bit array to a 1D array with decimal values again.
Example:
Input is a decimal 1D array
decimal[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
Convert this 1D array to a 2D array representing the bits
bits[16][8] = { 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1
....
0 0 0 0 1 1 1 1 };
Transpose this bit array
bits2[8][16] = {
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 };
Convert it back to a decimal value 1D array
result[8] = { 0, 0, 0, 0, 255, 3855, 13107, 21845}
The code I have so far:
#define n 8 // COLUMNS
#define m 16 // ROWS
int data[m];
int result[n];
int i,j;
int counter = 0;
memset(data, 0, sizeof(data));
memset(result, 0, sizeof(result));
for ( i = 0; i < m; ++i) {
data[i] = counter;
++counter;
}
int a[m][n], b[n][m], x;
// Convert decimal array to 2D bit array
for(i=0; i<m; i++)
{
x = data[i];
for(j=0; j<n; j++)
{
a[i][j] = (x & 0x8000) >> 8;
x <<= 1;
}
}
// Transpose bit array
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
b[j][i] = a[i][j];
}
}
// Convert back to decimal
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if (b[i][j] == 1) result[i] = result[i] * 2 + 1;
else if (b[i][j] == 0) result[i] *= 2;
}
}
I hope my explanation is clear! If not, I'll gladly explain some more. I've searched endlessly for ways to do this but I'm still not getting up with a solid solution.
PS: Apologies for the bad code formatting of the arrays, didn't find a proper way to visualize it without linking an image.
This should provide the desired output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define n 8 // COLUMNS
#define m 16 // ROWS
int main(void)
{
int data[m];
int result[n];
int i, j;
int counter = 0;
memset(data, 0, sizeof(data));
memset(result, 0, sizeof(result));
for (i = 0; i < m; ++i) // print initial data
{
data[i] = counter;
printf("%d ", data[i]);
++counter;
}
putchar('\n');
char a[m][n], b[n][m];
int x;
// Convert decimal array to 2D bit array
for (i = 0; i < m; i++)
{
x = data[i];
for (j = n - 1; j >= 0; j--)
{
a[i][j] = x & 1;
x >>= 1;
}
}
// Transpose bit array
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
b[j][i] = a[i][j];
}
}
// Convert back to decimal
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (b[i][j] == 1)
result[i] = result[i] * 2 + 1;
else if (b[i][j] == 0)
result[i] *= 2;
}
}
for (i = 0; i < n; ++i) // print result
{
printf("%d ", result[i]);
}
putchar('\n');
return 0;
}
What you were doing wrong was the conversion to the 2d bit array , it was all filled with 0's.
You were doing (x&0x8000) >> 8;
0x8000 = 1000 0000 0000 0000 (grouped in nibbles to see clearly)
so (x&0x8000) will always be 0 considering that x will in your case take values <=255 .
I also changed the int arrays which were using way too much space than needed to char arrays.

Resources