Increased Pascal Triangle printed with wrong values - C - c

I want to print a triangle that is similar to Pascal Triangle but the sides are increased instead of containing the value 1.
Regular Pascal Triangle:
Wanted Triangle:
Regular Pascal method:
void PascalTriangle(int rows) {
int i =0,j = 0,space,coef = 0;
for (i = 0 ; i<rows ; i++){
for (space = 1 ; space <= rows - i ; space++)
printf(" ");
for (j = 0 ; j <= i ; j++) {
if (i == 0 || j == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d",coef);
}
printf("\n");
}
}
what I was trying to do:
void PascalTriangle(int rows) {
int i =0,j = 0,space,coef = 0;
for (i = 0 ; i<rows ; i++){
for (space = 1 ; space <= rows - i ; space++)
printf(" ");
for (j = 0 ; j <= i ; j++) {
if (i == 0 || j == 0)
coef ++;
else
coef = coef * (i - j + 1) / j;
printf("%4d",coef);
}
printf("\n");
}
}
When increasing coef my output looks good only on the sides of the triangle:
I would like to clarify - I am not looking for a solution but to learn where I went wrong, I will appreciate any help.

As you ask for directions and not for a solution, I'll say that you cannot use the same index algebra as in the standard Pascal's triangle formula when the outer coefficients aren't equal to one.

Related

Pascal's triangle with modification

I wrote a function to print Pascal's triangle with modifications.
The sides of the triangle will appear in ascending order instead of the value 1.
Each values at the base of the triangle will continue to contain the sum of the 2 members above it.
Also need to print the max value of a given level.
Example :
Pascal Triangle With Modification Example
void printTriangle2(int rows,int level) {
int i =0,j = 0,space,res = 0, isLevel = 0,max = 0;
for (i = 0 ; i<rows ; i++){
if (i == level)
isLevel = 1;
else isLevel = 0;
for (space = 1 ; space <= rows - i ; space++) {
printf(" ");
}
for (j = 0 ; j <= i ; j++) {
if (i == 0 || j == 0)
res++;
else {
res = res * (i - j + 1) / j;
}
if (isLevel == 1) {
if (res > max)
max = res;
}
printf("%4d",res);
}
printf("\n");
}
printf(" the max value is : %d",max);
}
my output for the input rows = 5 , level = 3:
Output Example
I have an issue with the calculation of res if the value is not in the side of the triangle
res = res * (i - j + 1) / j;
What am I doing wrong?

How to Check if Matrix is Circular / Target Matrix?

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
}
}

Inverse of a binary matrix in C

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

Pascal's triangle in C weird output

I'm trying to write a program to display Pascal's triangle up to a user-inputted number of levels. We aren't allowed to use the factorial method. My current code is this:
#include <stdio.h>
void trianglePrint(const int numLevels);
int main() {
int numLevels;
printf("Please enter how many levels of Pascal's Triangle you would
like to see: ");
scanf("%d", &numLevels);
trianglePrint(numLevels);
return 0;
}
void trianglePrint(const int numLevels) {
int pascalTriangle[28][28];
int i, j;
for (i = 0; i < numLevels; ++i) {
for (j = 0; j <= i; ++j) {
if (i == 0 || i == 1 || j == 0 || j == numLevels) {
pascalTriangle[i][j] = 1;
printf("%d ", pascalTriangle[i][j]);
}
else {
pascalTriangle[i][j] = pascalTriangle[i - 1][j - 1] +
pascalTriangle[i - 1][j];
printf("%d ", pascalTriangle[i][j]);
}
}
printf("\n");
}
}
We're only supposed to be able to go up to 28 levels, which is why I am using an array of size 28 in both dimensions.
This works fine for about 6 levels of the triangle, but for larger levels it gives really large integers. I assumed it was due to uninitialized arrays, but I'm not sure. Does anyone know where the error is?
If you change
if (i == 0 || i == 1 || j == 0 || j == numLevels)
to
if (i == 0 || i == 1 || j == 0 || j == i)
(thanks to Melpomene), then all accesses to your array end up on already intiailised members.
That solves the strange numbers.
Output:
Please enter how many levels of Pascal's Triangle you would like to see: 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Process returned 0 (0x0) execution time : 2.264 s
Press any key to continue.
Note:
Also initialising an array is a wise precaution. You could initialise with values which help finding an error, instead of hiding it, e.g. 42.
The problem is that you haven't set pascalTriangle[i - 1][j] before you use it to compute pascalTriangle[i][j] in the else clause.
Try the code
void Pascal(int n)
{
int arr[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
// First and last values in every row are 1
if (i == j || j == 0)
arr[i][j] = 1;
else // Other values are sum of values just above and left of above
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

C - Populate matrix with some density

I've got this code to populate matrix with 0/1 values and RHO density. I need the same for values from 0 to 2. I mean, the percentage of zeros should be the same, but other values in range 1-2.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
grid[cur][i][j] = (((float)rand())/RAND_MAX) < rho;
}
}
The only thing I've been able to do is something inelegant like this. This leaves zero/non zero percentage inalterate and random modifies the 1 cells:
...
if(grid[cur][i][j] > 0) {
grid[cur][i][j] += rand()%2;
}
I think this code will create 0 with RHO density and other values in range 1-2.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
grid[cur][i][j] = (((float)rand())/RAND_MAX) < rho ? 0 : rand() % 2 + 1;
}
}

Resources