Enter N (dimension of square matrix) such that N is odd and in interval [1,100]. For entered matrix, check if it is a target matrix: if yes, print YES; if no, print NO.
Target Matrix is a matrix that is organized in concentric circles starting from the centre. Each concentric circle has -1 the value of the previous one. Examples:
and
I've tried approaching this problem by using a while loop to increase the radius which starts at centre increases. inside, I've used two for loops to go through that part and check if the values are according the the rule given above.
I'm not really sure if this is a good approach. Do you have some suggestions?
#include <stdio.h>
#define DIM 100
int main() {
int matrix[DIM][DIM];
int N;
int targetMatrix = 1;
int matrixCenter;
int radius;
do{
printf("Enter N: ");
scanf("%d", &N);
if (N % 2 == 0 || N < 1 || N > 100){
printf("Invalid value of N.\n");
}
} while (N % 2 == 0 || N < 1 || N > 100);
// Matrix Entry
printf("Enter the matrix: ");
int i, j;
for (i = 0; i < N; i++){
for (j = 0; j < N; j++){
scanf("%d", &matrix[i][j]);
}
}
// Value at Center
matrixCenter = matrix[N/2][N/2];
radius = 1;
// (N - 1) / 2 is the distance from center of matrix to its side
while (radius <= (N - 1) / 2)
{
for(i = N/2 - radius; i <= N/2 + radius; i++){
for(j = N/2 - radius; j <= N/2 + radius; j++){
if (i == N/2 && j == N/2) // Center Value
continue;
if (matrix[i][j] != matrixCenter - radius)
targetMatrix = 0;
}
}
if (targetMatrix == 0){
printf("NO: This is not a target matrix"); // If not a target matrix
return 1;
}
radius++;
}
printf("YES: this is a target matrix"); // If it is a target matrix
return 0;
}
Testing shell of submatrix only :
int first = N/2 - radius;
int last = N/2 + radius;
for (int i = first; i <= last;i++) {
// first on last line: increment by one
// other : increment by radius * 2
for (int j = first; j <= last;j += ((i==first)||(i==last) ? 1 :radius*2)) {
// test i,j here
}
}
Related
so I've been struggling with this example for a good hour now and I can't even begin to process how should I do this.
Write a program that, for given n and m, forms a matrix as described.
The matrix should be m x m, and it's filled "spirally" with it's
beginning in the upper left corner. The first value in the matrix is
the number n. It's repeated until the "edge" of the matrix, at which
point the number increments. After the number 9 goes 0. 0 ≤ n ≤ 9, 0 ≤
m ≤ 9
Some time ago I had made a function to display the numbers 1 to n on an odd-sized grid.
The principle was to start from the center and to shift by ;
x = 1
x box on the right
x box on the bottom
x++
x box on the left
x box at the top
x++
With this simple algorithm, you can easily imagine to maybe start from the center of your problem and decrement your value, it seems easier to start from the center.
Here is the code that illustrates the above solution, to be adapted of course for your problem, it's only a lead.
#define WE 5
void clock(int grid[WE][WE])
{
int count;
int i;
int reach;
int flag;
int tab[2] = {WE / 2, WE / 2}; //x , y
count = 0;
flag = 0;
i = 0;
reach = 1;
grid[tab[1]][tab[0]] = count;
for (int j = 0; j < WE - 1 && grid[0][WE - 1] != pow(WE, 2) - 1; j++)
for (i = 0; i < reach && grid[0][WE - 1] != pow(WE, 2) - 1; i++, reach++)
{
if(flag % 2 == 0)
{
for(int right = 0 ; right < reach ; right++, tab[0]++, count++, flag = 1)
grid[tab[1]][tab[0]] = count;
if(reach < WE - 1)
for(int bottom = 0; bottom < reach; bottom++, count++, tab[1]++)
grid[tab[1]][tab[0]] = count;
}
else
{
for(int left = 0; left < reach; left++, count++, tab[0]--, flag = 0)
grid[tab[1]][tab[0]] = count;
for(int top = 0; top < reach; top++, tab[1]--, count++)
grid[tab[1]][tab[0]] = count;
}
}
}
I finally solved it. If anybody's interested, here's how I did it:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Fills the row number "row" with the number n
int fillRow(int m, int n, int arr[m][m], int row)
{
int j;
for(j=0;j<m;j++)
{
if(arr[row][j] == -1 || arr[row][j] == n-1) arr[row][j] = n;
}
}
//Fills the column number "col" with the number n
int fillCol(int m, int n, int arr[m][m], int col)
{
int i;
for(i=0;i<m;i++)
{
if(arr[i][col] == -1 || arr[i][col] == n-1) arr[i][col] = n;
}
}
int main()
{
int n, m, i, j, r=1, c=1, row=-1, col=-1;
scanf("%d %d",&n, &m);
int arr[m][m];
//Fill array with -1 everywhere
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
arr[i][j] = -1;
}
}
//Calculate which row/column to fill (variables row/col)
//Fill row then column then row than column...
for(i=0;i<2*m;i++)
{
if(i%2==0)
{
row = (r%2==0) ? m-r/2 : r/2;
fillRow(m, n, arr, row);
n++;
r++;
}
else if(i%2==1)
{
col = (c%2==0) ? c/2-1 : m-c/2-1;
fillCol(m, n, arr, col);
n++;
c++;
}
}
//If an element is larger than 9, decrease it by 10
//Prints the elements
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(arr[i][j]>9) arr[i][j] -=10;
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
So I have made this program where you can give in the parameters of a circle or a line and it will display said object by drawing an array on the display.
It works by "projecting" a coordinate-system onto an array. (The program also asks you to give the resolution of the array, the number of columns and rows are the same.) Then for every cell of the array it checks if the circle/line intersects the cell. If it does, or it is within a given range, the cell will get a value of 1. If it is out of range, it will be 0. When all the cells have been given a value, the program displays the array. So in the end you will see a circle or line made of ones, the rest of the arrays will show up in zeroes.
The problem is that it takes a relatively long time (7 to 10s) to print the array, while the actual calculations take like no time.
My question is as said in the title, can the process of displaying the array be sped up somehow? Or am I doing something wrong? I am using Code::Blocks as my compiler.
I know that my code is probably very poorly optimized, but I've only started programming like a week ago. So please forgive me if the code is hard to understand.
Thank you in advance!
#include <stdio.h>
#include <stdlib.h>
int main()
{
float x = 0, y = 0, ypos= 0 , xpos = 0, radius = 0, rsqrd = 0, rcheck = 0, thick = 0, grad = 0, offs = 0, lcheck = 0;
int matsize = 0, i, j, branch = 0;
char filled;
printf("\n0 - circle\n1 - line\nDo you want to draw a circle or a line? (0/1) ");
scanf("%d", &branch);
if(branch == 0)
{
printf("Value of radius: ");
scanf("%f", &radius);
printf("Position of circle on the x axis: ");
scanf("%f", &xpos);
printf("Position of circle on the y axis: ");
scanf("%f", &ypos);
printf("Is the circle filled? (y/n) ");
scanf(" %c", &filled);
if(filled == 'n')
{
printf("The thickness of circle: ");
scanf("%f", &thick);
}
if(filled == 'y' || filled == 'n')
{
printf("Resolution: ");
scanf("%d" , &matsize);
printf("\n");
}
rsqrd = radius*radius; //rsqrd is equal to radius squared.
x = -1*(matsize/2); //with this I make sure that the x and y values start from the top right corner of the matrix, so that each x, y value corresponds to the correct cell position (i, j)
y = matsize/2;
int mat[matsize][matsize];
if(filled == 'n')
{
for(i = 0; i < matsize; i++)
{
for(j = 0; j < matsize; j++)
{
rcheck = ((y - ypos)*(y - ypos)) + ((x - xpos)*(x - xpos)); // calculating the equation of the circle with the x and y values taking the offset into account
if(abs(rcheck-rsqrd) <= (thick*thick))
{
mat[i][j] = 1;
}
else
{
mat[i][j] = 0;
}
x = x+1; //stepping the values of x and y so they stay with the corresponding cell
}
x = -1*(matsize/2);
y = y-1;
}
}
if(filled =='y')
{
for(i = 0; i < matsize; i++)
{
for(j = 0; j < matsize; j++)
{
rcheck = ((y - ypos)*(y - ypos)) + ((x - xpos)*(x - xpos)); // calculating the equation of the circle with the x and y values taking the offset into account
if(rcheck <= rsqrd)
{
mat[i][j] = 1;
}
else
{
mat[i][j] = 0;
}
x = x+1; //stepping the values of x and y so they stay with the corresponding cell
}
x = -1*(matsize/2);
y = y-1;
}
}
if(filled == 'y' || filled == 'n')
{
for(i = 0; i < matsize; i++) // displaying the matrix
{ //
for(j = 0; j < matsize; j++) //
{ //
printf("%d ",mat[i][j]); //
} //
printf("\n"); //
} //
}
}
if(branch == 1)
{
printf("Value of gradient: ");
scanf("%f", &grad);
printf("Value of offset: ");
scanf("%f", &offs);
printf("Thickness of line: ");
scanf("%f", &thick);
printf("Resoultion: ");
scanf("%d", &matsize);
x = -1*(matsize/2); //with this I make sure that the x and y values start from the top right corner of the matrix, so that each x, y value corresponds to the correct cell position (i, j)
y = matsize/2;
int mat[matsize][matsize];
for(i = 0; i < matsize; i++)
{
for(j = 0; j < matsize; j++)
{
lcheck = y - (x * grad); // calculating the equation of the circle with the x and y values taking the offset into account
if(abs(lcheck-offs) <= thick)
{
mat[i][j] = 1;
}
else
{
mat[i][j] = 0;
}
x = x+1; //stepping the values of x and y so they stay with the corresponding cell
}
x = -1*(matsize/2);
y = y-1;
}
if(branch == 1)
{
for(i = 0; i < matsize; i++) // displaying the matrix
{ //
for(j = 0; j < matsize; j++)//
{ //
printf("%d ",mat[i][j]);//
} //
printf("\n"); //
} //
}
}
return 0;
}
As I stated in my comment maybe it has something to do with this stack overflow question and answer
After reading a bit, you could also try to buffer your stdout in order to make it faster.
I have a binary matrix (zeros and ones) D[][] of dimension nxn where n is large (approximately around 1500 - 2000). I want to find the inverse of this matrix in C.
Since I'm new to C, I started with a 3 x 3 matrix and working around to generalize it to N x N. This works for int values, however since I'm working with binary 1's and 0's. In this implementation, I need unsigned int values.
I could find many solutions for int values but I didn't come across any solution for unsigned int. I'd like to find the inverse of a N x N binary matrix without using any external libraries like blas/lapack. It'd be great if anyone could provide a lead on M x N matrix.
Please note that I need inverse of a matrix, not the pseudo-inverse.
/* To find the inverse of a matrix using LU decomposition */
/* standard Headers */
#include<math.h>
#include<stdio.h>
int main() {
/* Variable declarations */
int i,j;
unsigned int n,m;
unsigned int rows,cols;
unsigned int D[3][3], d[3], C[3][3];
unsigned int x, s[3][3];
unsigned int y[3];
void LU();
n = 2;
rows=3;cols=3;
/* the matrix to be inverted */
D[0][0] = 1;
D[0][1] = 1;
D[0][2] = 0;
D[1][0] = 0;
D[1][1] = 1;
D[1][2] = 0;
D[2][0] = 1;
D[2][1] = 1;
D[2][2] = 1;
/* Store the matrix value for camparison later.
this is just to check the results, we don't need this
array for the program to work */
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++) {
C[m][j] = D[m][j];
}
}
/* Call a sub-function to calculate the LU decomposed matrix. Note that
we pass the two dimensional array [D] to the function and get it back */
LU(D, n);
printf(" \n");
printf("The matrix LU decomposed \n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
printf(" %d \t", D[m][j]);
}
printf("\n");
}
/* TO FIND THE INVERSE */
/* to find the inverse we solve [D][y]=[d] with only one element in
the [d] array put equal to one at a time */
for (m = 0; m <= rows-1; m++) {
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[m] = 1;
for (i = 0; i <= n; i++) {
x = 0;
for (j = 0; j <= i - 1; j++){
x = x + D[i][j] * y[j];
}
y[i] = (d[i] - x);
}
for (i = n; i >= 0; i--) {
x = 0;
for (j = i + 1; j <= n; j++) {
x = x + D[i][j] * s[j][m];
}
s[i][m] = (y[i] - x) / D[i][i];
}
}
/* Print the inverse matrix */
printf("The Inverse Matrix\n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
printf(" %d \t", s[m][j]);
}
printf("\n");
}
/* check that the product of the matrix with its iverse results
is indeed a unit matrix */
printf("The product\n");
for (m = 0; m <= rows-1; m++) {
for (j = 0; j <= cols-1; j++){
x = 0;
for (i = 0; i <= 2; i++) {
x = x + C[m][i] * s[i][j];
}
//printf(" %d %d %f \n", m, j, x);
printf("%d \t",x);
}
printf("\n");
}
return 0;
}
/* The function that calcualtes the LU deomposed matrix.
Note that it receives the matrix as a two dimensional array
of pointers. Any change made to [D] here will also change its
value in the main function. So there is no need of an explicit
"return" statement and the function is of type "void". */
void LU(int (*D)[3][3], int n) {
int i, j, k;
int x;
printf("The matrix \n");
for (j = 0; j <= 2; j++) {
printf(" %d %d %d \n", (*D)[j][0], (*D)[j][1], (*D)[j][2]);
}
for (k = 0; k <= n - 1; k++) {
for (j = k + 1; j <= n; j++) {
x = (*D)[j][k] / (*D)[k][k];
for (i = k; i <= n; i++) {
(*D)[j][i] = (*D)[j][i] - x * (*D)[k][i];
}
(*D)[j][k] = x;
}
}
}
This is just a sample example that I tried and I have -1 values in the inverse matrix which is my main concern. I have 1000 x 1000 matrix of binary values and the inverse should also be in binary.
The matrix:
1 1 0
0 1 0
1 1 1
The matrix LU decomposed:
1 1 0
0 1 0
1 0 1
The Inverse Matrix:
1 -1 0
0 1 0
-1 0 1
The product:
1 0 0
0 1 0
0 0 1
I am simulating a random walk in 2d with periodic boundaries. My code is below:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/**
*creates the environment in the form of a matrix of ones and zeros, random walk
*/
#define ROWS 3
#define COLS 3
#define MOVES 1
#define DIRECTIONS 4
int main(void)
{
srand(time(NULL)); //seed for the random number generator
int array[ROWS][COLS]; //array for the environment
int x, y; //row and column for loop purposes
int min = 0; //min number in the array
int max = 1; // max number in the array
for(x = 0; x < ROWS; x++) //loop to fill array
{
for(y = 0; y < COLS; y++)
{
array[x][y] = min + rand() % (max - min + 1);
printf("%d", array[x][y]);
}
printf("\n");
}
int steps = 0; //step counter
int direction; //direction will be chosen at random
int i = rand() % ROWS; //Assign start position row
int j = rand() % COLS; //Assign start position column
printf("initial position: %d %d \n", i, j);
while (steps < MOVES)
{
direction = rand() % DIRECTIONS; // 0 = N, 1 = S, 2 = E, 3 = W
printf("direction: %d \n", direction);
switch (direction)
{
case 0: // north
i = ((i - 1) % ROWS);
break;
case 1: // south
i = ((i + 1) % ROWS);
break;
case 2: // east
j = ((j + 1) % COLS);
break;
case 3: // west
j = ((j - 1) % COLS);
break;
}
printf("new position: %d %d \n", i, j);
steps++; //update step counter
printf("number of steps: %d \n", steps);
}
return 0;
}
When I get directions S or E, the boundary condition works ok. The outcome for E with initial position 1 2 is new position 1 0. But if I get positions N or W the boundary condition does not work. The outcome for N with initial position 0 1 is new position -1 1 when it should be 2 1. How can I fix my code? Thank you.
Intro to problem
You are given a tree. If we select 2 distinct nodes uniformly at random, what's the probability that the distance between these 2 nodes is a prime number?
Input
The first line contains a number N: the number of nodes in this tree.
The following N-1 lines contain pairs a[i] and b[i], which means there is an edge with length 1 between a[i] and b[i].
Output
Output a real number denote the probability we want.
You'll get accept if the difference between your answer and standard answer is no more than 10^-6.
#include<stdio.h>
#include<math.h>
int checkprime(int d);
int fact(int m);
void main()
{
int N, i, j, f, p = 0, t, d, x, y, z;
float result;
int a[49999], b[49999];
printf("Enter the number of nodes\n");
scanf("%d", &N);
printf("\n");
for (i = 0; i < N - 1; i++)
{
scanf("%d\t%d", &a[i], &b[i]);//Inputting the nodes
}
for (i = 0; i < N - 1; i++)
{
for (j = i; j < N - 1; j++)
{
d = b[j] - a[i];//Taking distance between nodes
f = checkprime(d);//Checking if it is prime
if (f == 1)
{
p = p + 1;//If found prime,then increasing the number of
//possibilities
}
}
}
x = fact(N);
y = fact(2);
z = fact(N - 2);
y = y * z;
t = x / y;//finding C(N,2).Combination of number of nodes and pair of 2
result = p / t;//finding probability
printf("\n\n%f", result);
}
int checkprime(int d)//function to check prime
{
int k, flag = 1;
for (k = 2; k < d / 2; k++)
{
if (d % k == 0)
{
flag = 0;
}
}
return flag;
}
int fact(int m)//function to calculate factorial
{
int k, r = 1;
for (k = m; k > 1; k--)
{
r = r * k;
}
return r;
}