Pattern print from a given single integer - arrays

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

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

I am stuck with C programming question based on arrays

The question is:
Given a 2-D array A of size M x N . Write a program in C to print A in spiral form starting from bottom right corner in anti-clockwise direction. There must be space after each element. Note the following
1<=M
N<=20
0<=Aij<=1000
Example:
Input
1 8
1 2 3 4 5 6 7 8
Output
8 7 6 5 4 3 2 1
Input
3 3
1 2 3
4 5 6
7 8 9
Output
9 6 3 2 1 4 7 8 5
I have tried it....can anyone help me with this.
#include<stdio.h>
int main()
{
int m,n;
scanf("%d%d",&m,&n) ;
int matrix[m][n] ;
int last_row = (m-1) ;
int last_column = (n-1) ;
int first_row = 0;
int first_column = 0;
int i=0,j=0,r=0,c=0;
while(i<m)
{
while(j<n)
{
scanf("%d",&matrix[i][j]) ;
++j;
} j=0;
++i;
}
while(first_column <=last_column && first_row <= last_row)
{ for( r=last_row ; r>=first_row ; --r)
{ c=last_column;
printf("%d ",matrix[r][c]) ;
} --last_column;
for (c=last_column; c>=first_column ; --c)
{
r=first_row;
printf("%d ",matrix[r][c]) ;
} ++first_row ;
for(r=first_row; r<=last_row; r++)
{
c=first_column;
printf("%d ",matrix[r][c]) ;
} ++first_column;
for(c=first_column ; c<=last_column ; c++)
{
r=last_row;
printf("%d ",matrix[r][c]) ;
} --last_row;
}
return 0;
}
The current output which I am getting is :
Input
8 1
1
2
3
4
5
6
7
8
Output
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
Input
1 8
1 2 3 4 5 6 7 8
Output
8 7 6 5 4 3 2 1 2 3 4 5 6 7

Printing a pattern in C using loops

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.

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