Sei sulla pagina 1di 6

1)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{

char T[256];
char P[]={"aab"};
int i,n,m,result;
printf("input :");
scanf("%s",T);

n = strlen(T);
m = strlen(P);
if(n < m)
{
printf("input kurang\n");
exit(1);
}

//(1)

printf("\n");
return 0;
}

Complete the above code at (1) to be capable to identify “aab” pattern at given T string
and display which shift is the pattern occur.

Sample :

Input : aabaab, output : 0 3 (It should be read that pattern “aab” at given input string
appears at shift 0 and 3)

Input : baabbaab; output : 1 5

Case :

aabaabaab -> 0 3 6
acaabcaab -> 2 6
aabcaabdeaab -> 0 4 9
acaabcdaabeaab -> 2 7 11
aabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaa
baabaabaabaabaabaab -> 0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57
60 63 66 69 72 75 78
aabwaabfaavbaabaabaagbababaaaabaedfaabvabjabaabyabaabqabaababb
aabaadcaabaabaa -> 0 4 12 15 28 35 44 50 56 62 69 72
Answer :

A model

for(i = 0; i<=(n-m); i++)


if(!strncmp(T+i,P,3))
printf("%d ",i);

B model

for(i = 0; i<=(n-m); ++i)


{
int j, status = 0;
for(j = 0; j < 3; ++j)
if(*(T+i+j) == *(P+j))
status=1;
else
{
status=0;
break;
}
if(status)
printf("%d ",i);
}
2)

#include <stdio.h>
int main()
{
int a=0;
int b=1;
int result=0, num;
printf("Input: ");
scanf("%d",&num);
printf("%d %d ", a, b);

//(1)

printf("\n");
return 0;
}

Complete the above code at (1) to generate Fibonacci series at given input.

Sample:
Input : 10; output : 0 1 1 2 3 5 8
Input : 5; output : 0 1 1 2 3
Input : 7; output : 0 1 1 2 3 5

Case:

2 -> 0 1 1
3 -> 0 1 1 2
6 -> 0 1 1 2 3 5
100 -> 0 1 1 2 3 5 8 13 21 34 55 89
1000 -> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
1048576-> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
10946 17711 28657 46368 75025 121393 196418 317811 514229 832040

Answer:

result=a+b;
while(result < num)
{
printf("%d ",result);
a=b;
b=result;
result=a+b;
}
3)

#include <stdio.h>

int main()
{
int input, pnumber = 0, nnumber = 0;

//(1)

printf("%d %d\n",pnumber, nnumber);

return 0;
}

Complete the above code at (1) to determine how many positive and negative numbers
at given input have been read. The program ends with the input 0.

Sample:
Input : 1 2 -1 3 0; output : 3 1 (it should be read three positive numbers and one negative
number)

Input: 1 2 3 4 -5 0; output: 4 1
Input: -1 -2 -3 -4 5 0; output: 1 4

Case:
1 2 -2 -1 3 7 -10 -12 0 -> 4 4
0 -> 0 0
(any characters) -> 0 0
-3 -4 -2 -7 -1 1 3 5 7 9 120 34 2 0 -> 8 5
1 -2 3 -4 5 -6 7 -8 9 8 -7 6 -5 4 -3 2 -1 0 -> 9 8
1 -2 3 -4 5 -6 7 -8 9 8 -7 6 -5 4 -3 2 -1 -8 9 8 -5 4 -3 -7 6 -2 3 -4 3 -4 3 -4 5 -6 7 -5 4 -3 2 6
-5 4 0 -> 22 20

Answer :

while (scanf("%d",&input))
{
if(!input)
break;
if(input < 0)
++nnumber;
else
++pnumber;
}
4)

#include <stdio.h>
int main()
{
float d;
//(1)
return 0;
}

Complete the code above to convert from mile as an input to kilometer.

Sample:
Input: 1; output: 1.609000
Input: 10; output: 16.090000

Case:
123456789 -> 198641978.328000
238900 -> 384390.100000
0 -> 0
92960000 -> 149572640.000000
96000000 -> 154464000.000000

Answer:

scanf("%f",&d);
printf("%f\n",(d*1.609));
5)

What is an output of the following code?

#include <stdio.h>

int main()
{
int j,k=2,l,m;
j = k++;
m = j;
l = ++j;
printf("%d %d\n",m,l);
return 0;
}

Answer : 2 3

6)

What is an output of the following code?

#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
int *pa, *pb, *pc;
pa = &a;
pb = &b;
pc = &c;
printf("%d %d %d ", *pa, *pb, *pc);
pc = pa;
pa = pb;
pb = pc;
printf("%d %d %d", *pa, *pb, *pc);
printf("\n");
return 0;
}

Answer : 10 20 30 20 10 10

Potrebbero piacerti anche