Printing a pattern in C using loops - c

The pattern to be printed(for user input n) is:
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
My attempt:
#include <stdio.h>
int main()
{
int i,j,k,l,p,n,tc,i1,j1,k1;
n=4;
for(i=1;i<=n*2-1;i++)
{
p=n;
if(i<=n)
{
for(j=1;j<=i-1;j++)
{
printf("%d ",p);
p--;
}
for(k=j;k<=n*2-j;k++)
printf("%d ",p);
for(l=k;l<=n*2-1;l++)
{
p++;
printf("%d ",p);
}
}
else
{
p=n;
for(i1=1;i1<=n*2-i-1;i1++)
{
printf("%d ",p);
p-=1;
}
for(j1=i1;j1<=n*2-i1;j1++)
printf("%d ",p);
for(k1=j1;k1<=n*2-1;k++)
{
p+=1;
printf("%d ",p);
}
}
printf("\n");
}
return 0;
} //main
I am using a gcc 6.3 compiler and it is giving
Output File Size Exceeded
Where am I going wrong?I can't figure it out. I have tried this code in gcc 4.x and I was getting the correct output.

In your last loop, you increment the wrong variable.
for(k1=j1;k1<=n*2-1;k++){
p+=1;
printf("%d ",p);
}
change k to k1.

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

Alternative way to Pascal's triangle using user defined functions

#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

function to fill the top part of a table

I have a function that allows me to fill the top part of a table by a number like in this example with 1:
2 3 3 4
2 3 3 4
2 3 4 5
3 4 2 4
becomes after applying the function :
2 1 1 1
2 3 1 1
2 3 4 1
3 4 2 4
This is my code (using an if test):
/* function */
void remplitPartieSup(int tab[N][N])
{
int i,j;
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
if (i<j)
tab[i][j]=1;
}
}
}
Question
How can I achieve the same result without using an if test?
Try this.
void remplitPartieSup(int tab[N][N])
{
int i,j;
for (i=0;i<N;i++)
{
for (j=i+1;j<N;j++)
{
tab[i][j]=1;
}
}
}

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

Resources