Automata fix.pdf

sudarshanhacker 78 views 47 slides Aug 12, 2022
Slide 1
Slide 1 of 47
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47

About This Presentation

Automata Problems which is based on the basic problem solving methods.


Slide Content

Sample Input: Sample Output:
1111112
3222222
3333334
5444444
5555556
Question 1
Write a C program to print the following pattern.
5

#include<stdio.h>
intmain()
{
inti,j,k,N;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
k=i%2;
for(j=1;j<8;j++)
{
if((j==7) && (k==1))
{
printf("%d",i+1);
break;
}
if((j==1) && (k==0))
{
printf("%d",i+1);
continue;;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
printf("%d",i);
}
printf("\n");
}
}
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

5
1111112
3222222
3333334
5444444
5555556
OUTPUT

Sample Input: Sample Output:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Question 2
Write a C program to print the following pattern.
5

#include<stdio.h>
#include<math.h>
intmain()
{
inti,j,n,size,left;
inttop,box[10][10],s;
scanf("%d",&size);
left=0,n=1,top=size-1;
s=ceil((double)size/2);
for(i=1;i<=s;left++,i++,top--)
{
for(j=left;j<=top;j++,n++)
{
box[left][j]=n;
}
for(j=left+1;j<=top;j++,n++)
{
box[j][top]=n;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for(j=top-1;j>=left;j--,n++)
{
box[top][j]=n;
}
for(j=top-1;j>=left+1;j--,n++)
{
box[j][left]=n;
}
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
printf("%4d",box[i][j]);
}
printf("\n");
}
return 0;
}
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
OUTPUT

Sample Input: Sample Output:
H o
e l
l
e l
H o
Question 3
Write a C program to print the following pattern.
Hello

#include<stdio.h>
#include<string.h>
int main()
{
int i,j,k=1;
char s[10];
scanf("%s",s);
k=strlen(s);
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
if(i==j||k-j-1==i)
printf("%c",s[j]);
else
printf(" ");
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Hello
H o
e l
l
e l
H o
OUTPUT

Sample Input: Sample Output:
*
*
*****
*
*
Question 4
Write a C program to print the following pattern.
5

#include<stdio.h>
intmain()
{
inti,j,k;
scanf("%d",&k);
for(i=1;i<=k;i++)
{
for(j=1;j<=k;j++)
{
if( i==(k/2)+1 || j==(k/2 )+1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

5
*
*
*****
*
*
OUTPUT

Sample Output:
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
Question 5
Write a C program to print the following pattern.

#include<stdio.h>
int main()
{
int i, j, k;
int blank = 0;
int lines = 6;
char symbol = 'A';
int temp;
int diff[7]={0,1,3,5,7,9,11};
k = 0;
for (i = lines; i >= 0; i --)
{
printf("\n");
symbol = 'A';
for (j = i; j >= 0; j --)
{
printf("%c ", symbol++);
}
blank = diff[k++];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

for (j = 0; j < blank; j++)
{
printf(" ");
}
symbol = 'F' -(blank / 2);
if (blank == 0)
{
temp = i -1;
}
else
{
temp = i;
}
for (j = 0; j <= temp; j++)
{
printf("%c ", symbol--);
}
}
return (0);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

A BC DE FG FE DC BA
A BC DE F FE DC BA
A BC DE E DC BA
A BC D DC BA
A BC C BA
A B BA
A A
OUTPUT

Sample Input: Sample Output:
1 2 3 6 9 8 7 4 5
Question 6
Write a C program to print the following pattern.
3
1 2 3
4 5 6
7 8 9

#include<stdio.h>
#define S 10
void print_spirally(inta[S][S], intn);
intmain()
{
intn;
scanf("%d ", &n);
inti, j, a[S][S];
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
}
print_spirally(a, n);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

void print_spirally(int a[S][S], int n)
{
int r_min= 0, c_min= 0;
int r_max= n-1, c_max= n-1;
int i;
while ((r_min<= r_max) && (c_min<= c_max))
{
for(i = c_min; (i <= c_max); i++)
printf("%d ", a[r_min][i]);
for(i = r_min+ 1; (i <= r_max); i++)
printf("%d ", a[i][c_max]);
for(i = c_max-1; (i >= c_min); i--)
printf("%d ", a[r_max][i]);
for(i = r_max-1; (i >= r_min+1); i--)
printf("%d ", a[i][c_min]);
r_min++; c_min++; r_max--; c_max--;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

3
1 2 3
4 5 6
7 8 9
1 2 3 6 9 8 7 4 5
OUTPUT

Sample Input: Sample Output:
*****
***
*
***
*****
Question 8
Write a C program to print the following pattern.
3

#include<stdio.h>
intmain()
{
intr,star,sp,n;
scanf("%d",&n);
for(r=n;r>=2;r--)
{
for(sp=1;sp<=n-r;sp++)
printf(" ");
for(star=1;star<=2*r-1;star++)
printf("*");
printf("\n");
}
for(r=1;r<=n;r++)
{
for(sp=1;sp<=n-r;sp++)
printf(" ");
for(star=1;star<=2*r-1;star++)
printf("*");
printf("\n");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

3
*****
***
*
***
*****
OUTPUT

Sample Input: Sample Output:
xxxx
x xx x
xx xx
xx xx
x xx x
xxxx
Question 9
Write a C program to print the following pattern.
xxxxxx

#include<stdio.h>
#include<string.h>
intmain()
{
inti,j,k=1;
char s[10];
scanf("%s",s);
k=strlen(s);
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
if(i==j||k-j-1==i)
printf(" ");
else
printf("%c",s[j]);
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

xxxxxx
xxxx
x xx x
xx xx
xx xx
x xx x
xxxx
OUTPUT

Sample Input: Sample Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Question 10
Write a C program to print the following pattern.
5

#include <stdio.h>
int main()
{
int i,j,l=1,N;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
for(j=0;j<i;j++,l++)
{
printf("%3d",l);
}
printf("\n");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

l=l-N;;
for(i=N-1;i>=0;i--)
{
for(j=0;j<=i;j++)
{
printf("%3d",(l+j));
}
l=l-i;
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Input: Sample Output:
*****
****
***
**
*
Question 11
Write a C program to print the following pattern.
5

#include<stdio.h>
int main()
{
int n,r,c;
scanf("%d",&n);
for(r=n;r>=1;r--)
{
for(c=1;c<=r;c++)
{
printf("*");
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Input: Sample Output:
*
**
***
****
*****
Question 12
Write a C program to print the following pattern.
5

#include<stdio.h>
int main()
{
int n,r,c;
scanf("%d",&n);
for(r=n;r>=1;r--)
{
for(c=1;c<=n;c++)
{
if(r<=c)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Input: Sample Output:
*****
****
***
**
*
Question 13
Write a C program to print the following pattern.
5

#include<stdio.h>
int main()
{
int n,r,c;
scanf("%d",&n);
for(r=1;r<=n;r++)
{
for(c=1;c<=n;c++)
{
if(r<=c)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Input: Sample Output:
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
Question 14
Write a C program to print the following pattern.
5

#include<stdio.h>
int main()
{
int n,r,c;
scanf("%d",&n);
for(r=1;r<=n-1;r++)
{
for(c=1;c<=r;c++)
{
if(r==c)
printf("%d",r);
else
printf("%d*",r);
}
printf("\n");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

for(r=n;r>=1;r--)
{
for(c=1;c<=r;c++)
{
if(r==c)
printf("%d",r);
else
printf("%d*",r);
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Input: Sample Output:
1
21
321
4321
54321
Question 15
Write a C program to print the following pattern.
5

#include<stdio.h>
int main()
{
int n,r,c;
scanf("%d",&n);
for(r=n;r>=1;r--)
{
for(c=1;c<=n;c++)
{
if(r<=c)
printf("%d",n-c+1);
else
printf(" ");
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Input: Sample Output:
**
**
****
****
******
******
Question 16
Write a C program to print the following pattern.
6

#include<stdio.h>
int main()
{
int n,r,c;
scanf("%d",&n);
for(r=1;r<=n;r++)
{
for(c=1;c<=r;c++)
{
if(r%2==1 && r==c)
printf("**");
else
printf("*");
}
printf("\n");
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Input: Sample Output:
*
***
*****
*******
*********
*******
*****
***
*
Question 17
Write a C program to print the following pattern.
5

#include<stdio.h>
int main()
{
int r,star,sp,n;
scanf("%d",&n);
for(r=1;r<=n-1;r++)
{
for(sp=1;sp<=n-r;sp++)
printf(" ");
for(star=1;star<=2*r-1;star++)
printf("*");
printf("\n");
}
for(r=n;r>=1;r--)
{
for(sp=1;sp<=n-r;sp++)
printf(" ");
for(star=1;star<=2*r-1;star++)
printf("*");
printf("\n");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Sample Input: Sample Output:
*********
*******
*****
***
*
***
*****
*******
*********
Question 18
Write a C program to print the following pattern.
5

#include<stdio.h>
int main()
{
int r,star,sp,n;
scanf("%d",&n);
for(r=n;r>=2;r--)
{
for(sp=1;sp<=n-r;sp++)
printf(" ");
for(star=1;star<=2*r-1;star++)
printf("*");
printf("\n");
}
for(r=1;r<=n;r++)
{
for(sp=1;sp<=n-r;sp++)
printf(" ");
for(star=1;star<=2*r-1;star++)
printf("*");
printf("\n");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Tags