Alternative way to Pascal's triangle using user defined functions - c

#include <stdio.h>
#include <conio.h>
int fact(int x);
int coeff(int y);
int main(){
int n,i,j,k;
printf("\nENTER THE POWER:");
scanf("%d",&n);
for (i=0;i<=n;i++){
for(k=n;k>i;k--){
printf(" ");
}
printf("%4d",coeff(i));
printf("\n");
}
getch();
}
int fact(int x){
int a , f=1 ;
for (a=x;a>0;a--){
if(x==0){
return 1;
}
return f*=a;
}
}
int coeff(int y){
int m ;
for(m=0;m<=y;m++) {
return (fact(y))/(fact(m)*fact(y-m));
}
}
I am a newbee in C programming . I have recently started printing patterns using C . I did the above coding of Pascal's Triangle myself . But my code doesn't give the correct answer in the CODE BLOCKS editor . I know that there are solutions to this in websites . But I want to develop my own reasonings(though it is oddly long ) . But I'm unable to find the fault here . Would someone kindly help me ?
THANKS ...

The solution below consists of correcting the errors in code along with some improvements:
1) Use getchar from stdio.h instead of getch from the non-standardconio.h.
2) The coeff function does not need a for loop.
3) The fact function calculates the factorial completely and only then returns the value.
4) Another for loop included in the main to print all the coefficients of a power in one line.
#include <stdio.h>
int fact(int x);
int coeff(int n, int y);
int main(){
int n,i,j,k;
n = i = j = k = 0;
printf("\nENTER THE POWER:");
scanf("%d",&n);
for (i=0;i<=n;i++){
for(k=n;k>i;k--){
printf(" ");
}
for(j=0;j<=i;j++){
printf("%4d",coeff(i, j));
}
printf("\n");
}
getchar();
}
int fact(int x){
int a , f=1 ;
if(x==0){
return 1;
}
for (a=x;a>0;a--){
f*=a;
}
return f;
}
int coeff(int n, int y){
return (fact(n))/(fact(y)*fact(n-y));
}
Output:
a.exe
ENTER THE POWER:5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
a.exe
ENTER THE POWER:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
a.exe
ENTER THE POWER:7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1

Related

Printing all permutations of n numbers

Print all n! permutations of the number 1,2,3,...,n.
Example: Input: 3
Output: 1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Following is my approach. My program is not working for inputs greater than 3. I understand the logic why it is not working , but I am unable to translate that logic into a code block to overcome that issue.
#include <stdio.h>
int permute(int n)
{
int a[n];
int i,j,k,store;
for(i=0;i<n;i++)
a[i]=i+1;
for(i=1;i<=n;i++)
{
for(j=0;j<n-1;j++)
{
store=a[j+1];
a[j+1]=a[j];
a[j]=store;
for(k=0;k<n;k++)
printf("%d ",a[k]);
printf("\n");
}
}
}
int main()
{
int n;
scanf("%d",&n);
permute(n);
return 0;
}
Following is the output for n as 4:
We can clearly see that some permutation are missing, and I know exactly the fault in my code. But I am unable to fix it.( I am a beginner , hence I don't know much advanced C libraries or functions)
One solution consists in calling the function recursively: you set the first number (n possible choices), then call the function for a size n-1.
Output, for n=4
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
1 4 2 3
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
2 4 1 3
3 2 1 4
3 2 4 1
3 1 2 4
3 1 4 2
3 4 1 2
3 4 2 1
4 2 3 1
4 2 1 3
4 3 2 1
4 3 1 2
4 1 3 2
4 1 2 3
#include <stdio.h>
#include <stdlib.h>
void swap (int *i, int *j) {
int temp = *i;
*i = *j;
*j = temp;
}
void permute(int index, int* arr, int n) {
if (index == n-1) {
for (int k = 0; k < n; ++k) {
printf ("%d ", arr[k]);
}
printf ("\n");
return;
}
for (int i = index; i < n; i++) {
swap (arr + index, arr + i);
permute (index+1, arr, n);
swap (arr + i, arr + index);
}
return;
}
int main()
{
int n;
if (scanf("%d",&n) != 1) exit (1);
int arr[n];
for (int i = 0; i < n; ++i) arr[i] = i+1;
permute(0, arr, n);
return 0;
}

Pattern print from a given single integer

Given a number — 4 — I have to output
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
The main code I wrote has an error for direction. According to me, I wrote the correct code, but it only works for 1 and 3 and loops between it — 1 3 1 3 1 3.... it is supposed to work 0 1 2 3 0 1 2 3 0 1.... accordingly in a spiral manner. I would really appreciate if you could help me!
The code I wrote is as follows:
int main()
{
int n;
scanf("%d", &n);
int x=n+(n-1);
int size=x*x;
int t,b,l,r;
t=0;l=0;
b=x-1;
r=x-1;
int i,j;
int res[size];
int index=0;
int dir=0;
while(t<=b && l<=r){
if(dir==0){
for(i=l;i<r;i++){
res[index]=n;
index++;
}
t++;
dir=1;
}
else if (dir==1) {
for(i=t;i<b;i++){
res[index]=n;
index++;
}
r--;
dir=2;
}
else if (dir==2) {
for(i=r;i>l;i--){
res[index]=n;
index++;
}
b--;
dir=3;
}
else if(dir==3){
for(i=b;i>l;i++){
res[index]=n;
index++;
}
l++;
dir=0;
}
n--;
dir=(dir+1)%4;
// printf("%d",dir); if u want to check
}
/* yet to proceed
for(i=0;i<x;i++){
for(j=i;j<x;j=j+x){
printf("%d ",res[j]);
}
printf("\n");
}
*/
return 0;
}

(C Language) prime factorization. How to make output 2 2 2 3 7 7 become 2^3 * 3^1 * 7^2?

#include <stdio.h>
int main() {
int n,i;
scanf("%d",&n);
for(i=2;i>=n/2;i++)
{
while(n%i==0)
{
printf("%d ",i);
n=n/i;
}
}
return 0;
}
(C Language) prime factorization. How to make output 2 2 2 3 7 7 become 2^3 * 3^1 * 7^2 ?

how to use recursion to count down after counting up

I am trying to get my program to count down after counting up to ten. I have tried to alter the code from counting up to make it count down to no avail.
#include <stdio.h>
void count(int k)
{
if (k > 0) {
count(-k + 1);
printf("%d", k);
}
else {
if (k == 0)
{
printf("%d,", k);
}
else {
count(k + 1);
printf("%d,", -k);
}
}
}
int main(int argc, char ** argv)
{
count(10);
getchar();
return 0;
}
Here is a simple example of the recursion which does this, illustrating Eugene's comment:
#include <stdio.h>
void count(int n) {
if (n > 10) {
printf("\n");
return;
}
printf("%d ", n);
count(n+1);
printf("%d ", n);
}
int main() {
count(0);
printf("\n");
return 0;
}
it counts up on the way into recursion and counts down while it exits it. Actually on the way down it only re-prints the state which it was before diving into the next level:
0 1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1 0
The function can be easy implemented if to use a static local variable inside it. For example.
#include <stdio.h>
void count(unsigned int n)
{
static unsigned int m;
printf("%u ", m);
if (n != m)
{
++m;
count(n);
--m;
printf("%u ", m);
}
}
int main( void )
{
const unsigned int N = 10;
unsigned int i = 0;
do
{
count(i);
putchar('\n');
} while (i++ != N);
return 0;
}
The program output is
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 5 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0
Within the function the static variable m behaves as an index in a for loop (or there will be more suitable a do-while loop).
At first it is initialized implicitly by zero (as any static variable)
static unsigned int m;
You can use the initializer explicitly if you want
static unsigned int m = 0;
then it is changed from 0 to n and afterward backward from n again to 0.
++m; // changing from 0 to n
count(n);
--m; // changing from n to 0

C pattern printing vertical numbers using for loop

I am working on a pattern that prints the following code using for loop
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
code is as follows, which results in slightly wrong output.
main(){
int i,j,k,n,num;
printf("\n Enter no of rows: ");
scanf("%d",&num);
for(i=1;i<=num;i++,k=num){
for(j=1,n=i;j<=i;j++,n+=k){
printf("%d ",n);
}
printf("\n");
}
}
And the code gives me this .
which is wrong from the output i wanted
1
2 6
3 7 11
4 8 12 16
5 9 13 17 21
The key to this is how you update n in the inner loop. You need it to take into account not just num but also i as you descend through each iteration. I've fixed num at 5 here and placed the assignment to k at the start of the outer loop as this will always be constant:
#include <stdio.h>
int main(void) {
int i, j, k, n, num;
num = 5;
for(i = 1, k = num; i <= num; i++){
for(j = 1, n = i; j <= i; n += k - j, j++){
printf("%d ", n);
}
printf("\n");
}
}
Gives:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

Resources