0% found this document useful (0 votes)
43 views17 pages

ISTS Pseudo Codes

The document contains a series of C programming exercises and their answers, focusing on various programming concepts such as ASCII values, functions, loops, bitwise operations, and sorting algorithms. It also includes questions on time and space complexity, pseudo-code, and control structures. Additionally, it features multiple-choice questions related to programming keywords and concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views17 pages

ISTS Pseudo Codes

The document contains a series of C programming exercises and their answers, focusing on various programming concepts such as ASCII values, functions, loops, bitwise operations, and sorting algorithms. It also includes questions on time and space complexity, pseudo-code, and control structures. Additionally, it features multiple-choice questions related to programming keywords and concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Day - 1

1.
#include<stdio.h>
int main ()
{
char c,a,b;
c='f';
a='s';
b='x';
int sum= c+a+b;
printf ("%d", sum);
}

Ans - Sum of ASCII values of ‘f’, ‘s’, ‘x’

2.
#include<stdio.h>
int func(int a)
{
return a--;
}
int main()
{
int a= func(5);
printf("%d",a++);
return 0;
}

3.
#include<stdio.h>
int main()
{
int val=5;
do{
Val++; #6
++val; #7
}
while(val++>7); #8
printf("%d",val);
return 0;
}

4.
#include<stdio.h>
int main()
{
int m=0;
if(m==0)
{
m=((5,(m=3)),m=1);
printf("%d",m);
}
else
printf("Test");
return 0;
}

5.
#include<stdio.h>
void main()
{
int i=0;
while(*(/—i)!=0)
i=i+4;
printf("%d",i);
}

6.

#include<stdio.h>

void main()
{
int a=8,b=6,c=5;
if(b>a || a>c || c>b)
b++;
else
a=b-–;
printf("%d",a+b+c);
}

7.

#include<stdio.h>
int main( )
{
int n=5, k, f1, f2, f;
if ( n < 2 )
return n;
else
{
f1 = f2 = 1;
for(k=2;k<n;k++)
{
f = f1 + f2;
f2 = f1;
f1 = f;
}
printf("%d",f) ;
}
}
Ans - 5

8.

initialize i,n
initialize an array of size n
accept the values for the array
for i= 0 to n
arr[i] = arr[i]+arr[i+1]
end for
print the array elements

9.

Integer x, y, Z, a
Set x=2,y=1,z=5
a = (x AND y) OR (z + 1)
Print a

Options :
A. 5
B. 3
C. 2
D. 1

10.

A. Code executes successfully and the value of salary is displayed once.

B. Code executes successfully and nothing is displayed.

C. Code executes successfully and the value of salary is displayed an infinite number of times.

D. Compile time error.


Day - 2

Logical AND - and (python - largest) && (C, C++ - 1 or 0))

Bitwise AND - & (binary number bitwise operator)

&, |, ^

<< - left shift


>> - right shift
1.

#include<stdio.h>
int fun(int x, int y);
int main()
{
int i,n;
i=5;
n=7;
int f = fun(5,7);
printf("%d", f);
}

int fun(int x, int y)


{
if(x<=0)
return y;
else
return(fun(x-1,y-1));
}

Ans : 2

2.

int main()
{
int array[] = {5, 3, 1, 9, 8, 2, 4, 7};
int size = sizeof(array)/sizeof(array[0]);
int i, j, min_idx,temp;
for (i = 0; i < size-1; i++)
{
min_idx = i;
for (j = i+1; j < size; j++)
{
if (array[j] < array[min_idx])
min_idx = j;
}
temp = array[min_idx];
array[min_idx] = array[i];
array[i] = temp; }
}

Ans - snippet is the code of selection sort


3.

#include<stdio.h>

int main()
{
float m=3.0;
switch((int)m)
{
case 1:
printf("ninja");
break;
case 2:
printf("digital");
break;
case 3:
printf("prime");
break;
}
return 0;
}

Ans - prime, included to recollect the switch case.

4.

#include<stdio.h>
int func(int n)
{
int i=0;
while(n%10!=0)
{
n=n+3;
i++;
}
n=n-i;
return n;
}
void main()
{
printf("%d",func(35));
}

a. 50
b. 55
c. 53
d. 45

Ans : 50-5=45
5.

// C Program to demonstrate use of bitwise operators

#include <stdio.h>
int main()
{
// a = 5 (00000101), b = 9 (00001001)

unsigned int a = 5, b = 9;

printf("a = %u, b = %u\n", a, b);


printf("a&b = %u\n", a & b);

// The result is 00001101


printf("a|b = %u\n", a | b);

// The result is 00001100


printf("a^b = %u\n", a ^ b);

// The result is 11111111111111111111111111111010 (4294967290 in decimal)


// (assuming 32-bit unsigned int)
printf("~a = %u\n", a = ~a);

// The result is 00010010


printf("b<<1 = %u\n", b << 1);

// The result is 00000100


printf("b>>1 = %u\n", b >> 1);

return 0;
}
1.

Find space and time complexity :


a=0
b=0

for i in range(N):
A = a + random()

for i in range(M):
b = b+ random()

Ans
Time - O(N+M)
Space - O(1)

2.

Find time complexity :


a = 0;
for i=0 to i=N:
for j=i to j=N:
a = a + i +j;

Ans
O(N^2)
End

3.

int i, j, k=0;
for (i=n/2; i<=n; i++)
for (j=2; j<=n; j=j*2)
k = k+n/2;

Ans :
O((n/2)logn) = O(n*logn)

4.

Int a = 0, i = N;
while(i>0) {
a += i;
i /= 2;
}

Ans - O(log(n))
Day - 3
1.

#include <stdio.h>
#include <stdlib.h>
#define LIMIT 10 /*size of integers array*/
int main(){
unsigned long long int i,j;
int *primes;
int z = 1;
primes = malloc(sizeof(int)*LIMIT);
for (i=2;i<LIMIT;i++)
primes[i]=1;
for (i=2;i<LIMIT;i++)
if (primes[i])
for (j=i;i*j<LIMIT;j++)
primes[i*j]=0;
for (i=2;i<LIMIT;i++)
if (primes[i])
// printf("%dth prime = %dn\n",z++,i);
printf("%dth prime = %llun\n",z++,i);
return 0;

Ans
Prime numbers filtering question
1th prime = 2n
2th prime = 3n
3th prime = 5n
4th prime = 7n
End

2.

count = 0
for (int i = N; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count++;

Ans :
O(N)
End
3.

Declare an array of string type variable called word


Declare a loopcounter
Store a string in the array word
for loopcounter = (length of the word) – 1 to 0
loopcounter = loopcounter – 1
print arrayword[loopcounter]
endfor
Algorithm end

Ans :
Reversing a string
End
Which of the following is not a keyword?
a) Read
b) Write
c) start
d) endif

The statement that tells the computer to get a value from an input device and store it in a memory location.
a) read
b) write
c) READ
d) WRITE

_____________ are identified by their addresses, we give them names (field names / variable names) using
words.
a) Memory variables
b) Memory Locations
c) Memory Addresses
d) Data variables

____________ begins with lower case letters.


a) Keywords
b) Variables
c) Tokens
d) Functions

Another notation for exponentiation.


a) *
b) **
c) ***
d) *^

A symbol used for grouping.


a) ()
b) {}
c) [].
d) ” ”

What is the primary purpose of using control structures in pseudo-code?

a) Adding complexity

b) Enhancing readability

c) Creating graphics

d) Formatting output
How does pseudo-code contribute to the debugging process?

a) It automatically fixes errors

b) It identifies errors early in development

c) It generates error reports

d) It compiles the code

Can pseudo-code be used as documentation for a program?

a) No, it’s not detailed enough

b) Yes, it provides a high-level overview

c) Only for certain programming languages

d) It depends on the compiler


Space complexity:

1.
{
int a = x + y + z;
return (a);
}

2.
//Sum Of N Natural Number
int sum(int n)
{
int i,sum=0;
for(i=n;i>=1;i--)
sum=sum+i
return sum;
}

3.
factorial(N){
if(N<=1)
{
return 1;
}
else
{
return (N*factorial(N-1));
}
}

4.
Initialize i, n
Set arr[]= 1, 2, 3, 4, 5
for i = 0 to n - 2
arr[i] = arr[i] + arr[i+1]
End for

Ans
3, 5, 7, 9, 5
End

5.
Integer x, y, z
Set x = 0, y = 3, z = 11
if(x + (1 | 2) && y + (2 | 3))
x=x-2
y=x
Else
x=z
y=y^2
End if
Print x + y + z

Ans
7
End

2, 4
Integer solve(Integer x, Integer y)
if(y > 0)
if(x > 0)
return x + y + solve(2, y - 5) + solve(x - 10, 1)
End if
End if
return x + y
End function solve()

Ans
18
End

12345
Initialize i, n
Initialize an array of size n
Take the values for the array
for i = 0 to n - 2
arr[i] = arr[i] - arr[i+1]
End for

Ans
11111
End

N = -5 , m = -2
Integer solve(Integer n, Integer m )
if(n > 4 && m < 7)
return solve(n +1, m + 1)
Else
Return n + m
End if
End function solve()

Ans
13
End

6.
Integer x, y, z
Set x = 10, y = 16,z = 3
if(x > y)
x=2*y
Else
y=x/2
End if
if(z > y)
z=2*y
Else
y=z/2
End if
Print x + y + z

Ans
14
End

7.
Integer w, x, y, z
Set w = 21, x = 11
for (each y from 21 to 30 )
for (each z from -1 to 0 )
w=w-1
if(w > y)
Continue
End if
w=1
if(w > z)
Jump out of the loop
End if
End for
End for
Print w + x

Ans
12
End

8.
Integer x, y, z
Set x = -2, y = 3, z = 1
if(x + (2 & 2) && y + (3 & 3) && z + (2 ^ 2))
x=x-2
y=x
Else
x=z
y=y^2
End if
Print x+ y+ z

Ans
3
End

9.
X = 9 y= 7
Integer funn(Integer x, Integer y)
Integer z
Set z = 2
y = y mod z
x = x mod z
return x + y
End function funn()
Time complexity of fibonacci program :

def f(n):
if n == 0 or n == 1:
return 1
return f(n - 1) + f(n - 2)

def fibonacci(n, memo = {}):


if n <= 1:
return n
elif n not in memo:
memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo)
return memo[n]

Time and space complexity :

def fun(n,m):
arr=[[0]*m for i in range(n)]
for i in range(n):
for j in range(m):
k=1
while k<n*m:
k*=2

Ans
Time complexity : O(n*m* log (n*m))
Space complexity: O(n*m)
End

You might also like