Correct Alignment of Numbers in PASCAL TRIANGLE - c

I made a program for making a pascal triangle and for the input of numbers ( rows ) > 5 , there is an alignment problem i.e for ncr > 10. Help me out please.
I have included the images for output of the program.
Output Image
#include<stdio.h>
int factorial(int number)
{
int fact=1;
for(int i=1; i<=number; ++i )
{
fact*=i;
}
return fact;
}
int ncr(int n, int r)
{
int ncr;
int fact1=factorial(n);
int fact2=factorial(n-r);
int fact3=factorial(r);
ncr = fact1 /(fact2 * fact3);
return ncr;
}
int main()
{
int rows;
printf("enter the number of rows :\n");
scanf("%d",&rows);
for(int n=0; n<rows; n++)
{
for(int i=1; i<=rows-n; i++)
{
printf(" ");
}
for(int r=0; r<=n; r++)
{
printf("%d ",ncr(n,r));
}
printf("\n");
}
return 0;
}

You can change the inner loop like this
for(int i=1; i<=rows-n; i++)
{
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%3d ",ncr(n,r)); // Changed to %3d
}
This will work upto 9 rows. If you want it to work for more rows, you can add another space in the first printf and change the second printf to %5d

printf can take a precision before the formatter. Change printf("%d ",ncr(n,r)); to printf("%3d ",ncr(n,r)); to make the numbers 3 characters wide. Also change printf(" "); to printf(" ");.

If you use
printf ("Width trick: %*d \n", 5, 10);
this will add 5 more spaces before the digit value.

Related

how to print an array as a matrix in C

so I´m trying to print this 2d array as a matrix and it´s not working. any hints? no matter what i change i cant get to print an all 0 3x3 matrix
int main()
{
int i, j, m, n, primeira;
int matrix[10][20];
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
/* first input */
printf("1 ou 0");
scanf("%d", &primeira);
if (primeira = 0) {
matrix [0][0]=0;
matrix [0][1]=0;
matrix [1][0]=0;
matrix [1][1]=0;}
/* Display the matrix */
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
return 0;
}
You would need to create a nested loop to display the matrix. If you want to display a 3x3 matrix you can run something like this.
int matrix[3][3] = { 0 };
/* Display the matrix */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf ("%d\t", matrix[i][j]);
}
printf ("\n");
}
regarding:
printf("%d\t", matrix[i][j]);
The variables: i and j are not initialized!
Suggest:
for( int i =0; i<10; i++ )
{
for( int j=0; j<20; j++ )
{
printf( "%d ", matrix[i][j] );
}
puts( "" );
}
as that will print all the elements of the matrix and move to a new line after printing each row of the matrix.

Program for abundant numbers <=k

A natural number n > 0 is said to be an abundant number if the sum of its proper divisors (excluding itself) is greater than itself. For example, the number 12 is an abundant number because the sum of its divisors is 1+2+3+4+6=16, which is greater than 12 itself. in contrast, 16 is not an abundant number because the sum of its divisors is 1+2+4+8=15, which is not greater than 16. I have to write a program which will, for any entered natural number k, print all abundant numbers smaller or same as k. I have written a following program:
#include <stdio.h>
int main (void) {
int k, sum=0, i, n;
printf ("Enter k: ");
scanf ("%d", &k);
for (i=1; i<=k; i++) {
int n=1;
if (n%i==0);
sum+=i;
}
if (n<sum);
printf ("%d", &n);
return 0;
}
Output of this program is not as expected. For example, if I enter 16 (or any other natural number), output is 6356716, which is definitely not correct. I just started learning programming and we just started with functions, so I wrote this:
#include <stdio.h>
int abundant (int n)
{
int i, sum=0;
for (i=0; i<n; i++)
if (n%i==0) sum+=i;
if (n<sum) return 1;
else return 0;
}
int main (void)
{
int i, k;
printf ("Enter a number: ");
scanf ("%d", &k);
for (i=1; i<=k; i++) {
if (abundant(i))
printf ("%d",&i);
}
return 0;
}
but output is exactly the same. Can somebody please tell me what I have done wrong?
This code should work
#include <stdio.h>
int main(void)
{
int k, i, j;
printf("Enter k: ");
scanf("%d", &k);
for (i = 1; i <= k; i++)
{
int sum = 0;
for (j = 1; j < i; j++)
{
if (i % j == 0)
sum += j;
}
if (sum > i)
printf("%d is abundant\n", i);
}
return 0;
}

Why my program of Matrix multiplication using C doesn't work?

I wrote a program to calculate the matrix multiplication.I takes user input to define the size of the array and the elements contained.(My knowledge on programming should be considered as a beginner).When I execute the program it prints a null array. When I tested the code line by line. I found out that the program works correctly till the calculation of the matrix.(Taking user input and calling the function).
I couldn't find the source of the problem. I have included part of the code which contains the multiplication function.
#include <stdio.h>
#define MAX 100
void matrix_mul(int, int, int, int, int [MAX][MAX], int [MAX][MAX]);
int main()
{
int mat_1[MAX][MAX] ,mat_2[MAX][MAX];
int row1, row2, column1, column2;
printf("Input number of rows for the first matrix: ");
scanf("%d", &row1);
printf("Input number of columns for the first matrix: ");
scanf("%d", &column1);
printf("Input number of rows for the second matrix: ");
scanf("%d", &row2);
printf("Input number of columns for the second matrix: ");
scanf("%d", &column2);
if(column1 != row2 || column2 != row1)
{
printf("Incompatible matrices. Try Again! ");
return 0;
}
printf("Enter elements for matrix 1 of order %d x %d\n", row1, column1);
for(int i=0; i<row1; i++)
{
for(int j=0; j<column1; j++)
scanf("%d", &mat_1[i][j]);
}
printf("\n\nEnter elements for matrix 1 of order %d x %d\n", row2, column2);
for(int i=0; i<row2; i++)
{
for(int j=0; j<column2; j++)
scanf("%d", &mat_2[i][j]);
}
matrix_mul(row1, row2, column1, column2, mat_1, mat_2);
}
// for testing r1 = 3 c1 =2 r2 =2 c2 =3
void matrix_mul(int row1, int row2, int column1, int column2, int ar1[MAX][MAX], int ar2[MAX][MAX])
{
int arr [MAX][MAX];
for(int i=0 ; i<row1; i++)
{
for(int j=0; j<column2; j++)
{
int sum = 0;
for(int k=0; k<column1; k++)
sum += ar1[row1][k] * ar2[column1][row1];
printf("%d", sum);
arr[row1][column2] = sum;
}
}
for(int i=0; i<row1; i++)
{
for(int j=0; j<column2; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}
You're mixing up your loop values with your boundaries. Here's the corrected versions of the relevant lines:
sum += ar1[i][k] * ar2[k][j];
arr[i][j] = sum;
You're seeing 88888 instead of 8 because you left a debugging statement in:
printf("%d", sum);
Remove that and you'll only see the correct output.

EOF and free part of unused 2d array

My background is java so I'm not used to c syntax yet.
I need to do the following: let the user to input number k (number of rows), and after to insert values into 2d array with this form:
1 2
3 4
5 6
i.e two values with space between them and then new line for the new row.
If the user entered k=1000 but entered only 4 rows so the function call would be only with the array with 4 rows and not 100. the loop that reads the values should stop if: there are k rows or reaching to EOF
My questions:
I don't know how to implement the EOF part.
I don't know how to implement that for k=1000 and there only 4 rows so call the function with the array that contains only 4 rows
Here my code:
#include <stdio.h>
#define COLS 2
void foo(int** rows, int n);
int main()
{
int k;
printf("Please enter number of rows\n");
scanf_s("%d", &k);
int** matrix = (int**)malloc(k * sizeof(int*));
for (int i = 0; i < k; i++)
matrix[i] = (int*)malloc(COLS * sizeof(int));
int num1, num2;
for (int i = 0; i < k||num1!=EOF; i++)
{
printf("Enter two numbers separated by space \n");
scanf_s("%d %d", &num1, &num2);
matrix[i][0]=num1;
matrix[i][1] = num2;
}
printf("The array:: \n");
for (int i = 0; i < k; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("%d \t",matrix[i][j]);
}
printf("\n");
}
foo(matrix, k);
for (int i = 0; i < k; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
void foo(int** rows, int n)
{
//some stuff
}
Change the bellow portion of your code:
for (int i = 0; i < k||num1!=EOF; i++)
{
printf("Enter two numbers separated by space \n");
scanf_s("%d %d", &num1, &num2);
matrix[i][0]=num1;
matrix[i][1] = num2;
}
To:
int i;
for (i = 0; i < k; i++)
{
printf("Enter two numbers separated by space \n");
if(2 != scanf_s("%d %d", &num1, &num2)) break;
matrix[i][0]=num1;
matrix[i][1] = num2;
}
k = i;
Hope it will work as you want
check the return value of scanf
for (int i = 0; i < k; i++)
{
printf("Enter two numbers separated by space \n");
if(scanf_s("%d %d", &num1, &num2)==EOF)
break;
matrix[i][0]=num1;
matrix[i][1] = num2;
}

Making a table with generated array C- programming

My program should ask user how many numbers he wanna input in the array , and than input one by one. Any number that's higher than 99 should be replaced by '0'.
That part works fine. But in the end created array should be placed in the table which needs to be in format (5 columns / depending rows).
This is what I wrote:
#include <stdio.h>
#define MAX 80
int main()
{
int n = 0;
int i,j;
int field[MAX]={0};
printf("how many numbers do u want to input? ");
scanf("%d",&n);
for ( i = 0; i < n; i++)
{
printf("Input number %d: ",i+1);
scanf("%d",&field[i]);
if(field[i] / 100 >= 1 )
{
field[i] = 0;
}
}
for ( i = 0; i<n; i++)
{
printf("\nfield[%d] = %d\n", i, field[i]);
}
for(i=0; i <= 5;i+=5)
{
for(j =i; j <n;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
return 0;
}
This part of your code can not be output in five columns
for(i=0; i <= 5;i+=5)
{
for(j =i; j <n;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
So if you change this, it will be executed
for(i=0; i <n;i+=5)
{
for(j =i; j <i+5;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
I hope this code will be help.
if i am wrong, please tell me and give a chance.
I want to help you and be recognized.

Resources