0% found this document useful (0 votes)
179 views9 pages

Wipro

The document contains three programming problems: 1. A cell programming problem where a line of 8 cells compete each day to be active or inactive based on their neighbors' states. The function returns the new cell states after a given number of days. 2. An LRU cache algorithm problem where a function returns the number of cache misses for a given page request array and maximum cache size. 3. A maze problem where a function returns 1 if a mouse named Mooshak can reach cheese in a maze represented as a 2D array, or 0 otherwise. It describes test cases and input/output.

Uploaded by

Deepthi Atluru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views9 pages

Wipro

The document contains three programming problems: 1. A cell programming problem where a line of 8 cells compete each day to be active or inactive based on their neighbors' states. The function returns the new cell states after a given number of days. 2. An LRU cache algorithm problem where a function returns the number of cache misses for a given page request array and maximum cache size. 3. A maze problem where a function returns 1 if a mouse named Mooshak can reach cheese in a maze represented as a 2D array, or 0 otherwise. It describes test cases and input/output.

Uploaded by

Deepthi Atluru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

cell pgm
1) Problem: There is a colony of 8 cells arranged in a straight line where each day
every cell competes with its adjacent cells(neighbour). Each day, for each cell, if
its neighbours are both active or both inactive, the cell becomes inactive the next
day, otherwise it becomes active the next day. Assumptions: The two cells on the
ends have single adjacent cell, so the other adjacent cell can be assumed to be
always inactive. Even after updating the cell state. consider its previous state
for updating the state of other cells. Update the cell information of all cells
simultaneously. Write a function cellCompete which takes takes one 8 element array
of integers cells representing the current state of 8 cells and one integer days
representing te number of days to simulate. An integer value of 1 represents an
active cell and value of 0 represents an inactive cell. program:
int* cellCompete(int* cells,int days)
{
/ /write your code here
} //function signature ends
TESTCASES 1: INPUT: [1,0,0,0,0,1,0,0],1 EXPECTED RETURN VALUE: [0,1,0,0,1,0,1,0]
TESTCASE 2: INPUT: [1,1,1,0,1,1,1,1,],2 EXPECTED RETURN VALUE: [0,0,0,0,0,1,1,0]

#include<stdio.h>
int* cellCompete( int* , int , int);
void display( int* , int);
int main()
{
int arr[]={1,1,1,0,1,1,1,1} , size , days;
size = sizeof arr / sizeof arr[0];
days = 2;
cellCompete( arr , size , days);
display( arr , size);
return 0;
}
void display( int* arr , int size)
{
int ctr;
for( ctr = 0 ; ctr < size ; ctr++)
printf("%d " , arr[ctr]);
}
int* cellCompete( int* arr , int size , int days)
{
int ctr ,prev , nextprev ;
while( days)
{
prev = 0;
for( ctr = 0 ; ctr < size-1 ; ctr++)
{
nextprev = arr[ctr];
arr[ctr] = prev ^ arr[ctr+1];
prev = nextprev;
}
arr[ctr] = prev ^ 0;
days--;
}

return arr;
}

2.LRU
The LeastRecentlyUsed(LRU) cache algorithm exists the element from the
cache(when it's full) that was leastrecentlyused. After an element is requested
from the cache, it should be added to the cache(if not already there) and
considered the most recently used element in the cache. Initially, the cache is
empty. The input to the function LruCountMiss shall consist of an integer
max_cache_size, an array pages and its length len. The function should return an
integer for the number of cache misses using the LRU cache algorithm. Assume that
the array pages always has pages numbered from 1 to 50.
TEST CASES:
TEST CASE1:
INPUT: 3,[7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0],16 EXPECTED RETURN VALUE: 11
TESTCASE 2:
INPUT: 2,[2,3,1,3,2,1,4,3,2],9
EXPECTED RETURN VALUE: 8 EXPLANATION: The following page numbers are missed one
after the other 2,3,1,2,1,4,3,2.This results in 8 page misses. CODE:
int lruCountMiss(int max_cache_size, int *pages,int len)
{
/ /write tour code
}

#include<stdio.h>
#include<limits.h>
int isHit(int **arr , int r , int c , int page);
int findMin( int**arr , int r , int c);
void init( int** arr , int r , int c);
int main()
{
int input[]={2,3,1,3,2,1,4,3,2} , noe = 9 ;
int ctr , cache_size = 2 ,miss = 0 , find ,min_ind;
int **arr;
// allocate memory
arr = (int**) malloc( cache_size * sizeof(int*));
for(ctr = 0 ; ctr < cache_size; ctr++)
arr[ctr] = (int*)malloc( sizeof(int) * 2);
// initialize an array with -1
init(arr , cache_size , 2);

for( ctr = 0 ; ctr < noe ; ctr++)


{
find = isHit( arr , cache_size,2,input[ctr]);
if( find == 0)
{
min_ind = findMin(arr , cache_size , 2);
arr[min_ind][0]=ctr;
arr[min_ind][1]=input[ctr];
miss++;
}
else
{
arr[find][0] = ctr;
}
}
printf("%d" , miss);
return 0;
}
int isHit(int **arr , int r , int c , int page)
{
int row;
for( row = 0 ; row < r ; row++)
{
if( arr[row][1] == page)
return row;
}
return 0;
}
int findMin( int**arr , int r , int c)
{
int row , min =INT_MAX , minpos;
for( row = 0 ; row < r ; row++)
{
if( arr[row][0] < min)
{
min = arr[row][0];
minpos = row;
}

}
return minpos;
}
void init( int** arr , int r , int c)
{
int row , col;
for(row = 0 ; row < r ; row++)
{
for( col = 0 ; col < c; col++)
arr[row][col] = -1;
}
}

LRU(using struct)

#include<stdio.h>
#include<malloc.h>

typedef struct cache


{
int page_number;
int serial_number;
} CACHE;

typedef CACHE * CACHEPTR;


#define HIT 0
#define MISS 1
int search(CACHEPTR c, int size, int pn)
{
int ctr, minval, minpos;
static int sn=0;
sn++;
for(ctr = 0; (c+ctr)->serial_number!=-1 && ctr < size; ctr++)
if((c+ctr)->page_number == pn)
{
(c+ctr)->serial_number = sn;
return HIT;
}
if(ctr < size && (c+ctr)->serial_number == -1)
{
(c+ctr)->page_number = pn;
(c+ctr)->serial_number = sn;
return MISS;
}
minval = (c+0)->serial_number;
minpos=0;
for(ctr=1; (c+ctr)->serial_number!=-1 && ctr < size; ctr++)
if( (c+ctr)->serial_number < minval)
{
minval = (c+ctr)->serial_number;
minpos = ctr;
}

(c+minpos)->page_number = pn;
(c+minpos)->serial_number = sn;
return MISS;

int LRUCountMiss(int cache_size, int pages[], int pages_size)


{
CACHEPTR c;
int ctr, misscount=0;
c = (CACHEPTR)calloc(sizeof(CACHE), cache_size);
for(ctr=0; ctr < cache_size; ctr++)
(c+ctr)->page_number = (c+ctr)->serial_number = -1 ;
for(ctr = 0; ctr < pages_size; ctr++)
if (search(c, cache_size, pages[ctr])==MISS)

misscount++;

return misscount;
}

int main()
{
int pagereq[] = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0};
//int pagereq[] = {2,3,1,3,2,1,4,3,2};
int misscount;
printf("%d pages %d hits and %d misses\n",sizeof(pagereq) / sizeof(pagereq[0]),
sizeof(pagereq) / sizeof(pagereq[0]) - misscount, misscount = LRUCountMiss(5,
pagereq, sizeof(pagereq) / sizeof(pagereq[0])) );
return 0;

3.MOOZAK
Mooshak the mouse has been placed in a maze.There is a huge chunk of
cheese somewhere in the maze. The maze is represented as a two
dimensional array of integers, where 0 represents walls.1 repersents paths
where mooshak can move and 9 represents the huge chunk of cheese. Mooshak
starts in the top left corner at 0. Write a method is Path of class
Maze Path to determine if Mooshak can reach the huge chunk of cheese.
The input to is Path consists of a two dimensional array gnd for the
maze matrix. the method should return 1 if there is a path from Mooshak
to the cheese.and 0 if not Mooshak is not allowed to leave the maze
or climb on walls.
EX: 8 by 8(8*8) matrix maze where Mooshak can get the cheese.
1 0 1 1 1 0 0 1
1 0 0 0 1 1 1 1
1 0 0 0 0 0 0 0
1 0 1 0 9 0 1 1
1 1 1 0 1 0 0 1
1 0 1 0 1 1 0 1
1 0 0 0 0 1 0 1
1 1 1 1 1 1 1 1
Test Cases:
Case 1: Input:[[1,1,1,][9,1,1],[0,1,0]]
Expected return value :1
Explanation: The piece of cheese is placed at(1,0) on the grid Mooshak can move
from (0,0) to (1,0) to reach it or can move from (0,0) to (0,1) to (1,1) to (1,0)
Test case 2: Input: [[0,0,0],[9,1,1],[0,1,1]] Expected return value: 0
Explanation: Mooshak cannot move anywhere as there exists a wall right on (0,0) 6)
Test whether an input string of opening and closing parentheses was balanced or
not. If yes , return the no. of parentheses. If no, return -1.

#include<stdio.h>
#include<conio.h>
#define NO 0
#define YES 1
#define VALID(x) ((x) >=0 && (x)<8)
int findCheese(int maze[8][8] , int sr , int sc,int prevDir)
{
int dir,tempDir;
static int found = NO;
if(maze[sr][sc] == 0) return found;
if(maze[sr][sc] == 9)
{
found = YES;
printf("\nCheese found AT ROW : %d COL : %d",sr,sc);
return found;
}
tempDir = prevDir;
for( dir = 1; dir<=4 && found == NO ;dir++)
{
switch(dir)
{
case 1: if(VALID(sc-1) && prevDir != dir) //left
{
prevDir = 3;
//printf("\ncall to :%d %d",sr,sc);
findCheese(maze,sr,sc-1,prevDir);
prevDir = tempDir;
}break;
case 2: if(VALID(sr-1) && prevDir != dir) //up
{
prevDir = 4;
//printf("\ncall to :%d %d",sr,sc);
findCheese(maze,sr-1,sc,prevDir);
prevDir = tempDir;
}
break;
case 3: if(VALID(sc+1) && prevDir != dir) //right
{
prevDir=1;
//printf("\ncall to :%d %d",sr,sc);
findCheese(maze,sr,sc+1,prevDir);
prevDir = tempDir;
}
break;
case 4: if(VALID(sr+1)&& prevDir != dir) //down
{
prevDir = 2;
// printf("\ncall to :%d %d",sr,sc);
findCheese(maze,sr+1,sc,prevDir);
prevDir = tempDir;
}
break;
}

}
return found;

}
int main()
{
int maze[8][8]={{1,1,1,1,1,0,0,1},
{1,0,0,0,1,1,1,1},
{1,0,0,0,0,0,0,9},
{1,0,1,0,0,0,1,0},
{1,1,1,0,1,0,0,1},
{1,0,1,0,1,1,0,1},
{1,0,0,0,0,1,0,1},
{1,1,1,1,1,1,1,1}};

clrscr();
if( findCheese(maze , 0 , 0 , -1) == NO)
printf("\nNo Path Found");
getch();
return 0;
}

1.
#include<stdio.h>
void main()
{
int inp = 6,col,row;
for(row = 1; row <= inp ; row++,printf("\n"))
{
for(col = 1; col<=inp ; col++)
printf("*");
}
}

2.#include<stdio.h>
void main()
{
int inp = 6,col,row;
for(row = 1; row <= inp ; row++,printf("\n"))
{
for(col = 1; col<=row ; col++)
printf("*");
}
}

3.
#include<stdio.h>
void main()
{
int inp = 4,row,no_of_spaces;
int no_of_stars,sp,stars;
for(row =1 ; row <= inp; row++,printf("\n"))
{
no_of_spaces = inp -row;
no_of_stars = (row * 2 ) -1;
for(sp = 1 ; sp <= no_of_spaces ; sp++)
printf(" ");
for(stars =1 ;stars<=no_of_stars;stars++)
printf("*");
}
}

4.#include<stdio.h>
void main()
{
int inp = 5,row,col,toPrint;
toPrint=1;
for(row = 1 ; row <= inp ; printf("\n"),row++ )
{
for(col = 1 ; col <= inp ; col++)
printf("%02d ",toPrint++);
}
}
5.#include<stdio.h>
void main()
{
int inp = 4,col,row,toPrint=1;
for(row = 1; row <= inp ; row++,printf("\n"))
{
for(col = 1; col<=row ; col++)
printf("%02d ",toPrint++);
}
}
6.#include<stdio.h>
void main()
{
int inp = 4,row,no_of_spaces;
int no_of_stars,sp,stars;
for(row =1 ; row <= inp; row++,printf("\n"))
{
no_of_spaces = inp -row;
no_of_stars = (row * 2 ) -1;
for(sp = 1 ; sp <= no_of_spaces ; sp++)
printf(" ");
for(stars =1 ;stars<=no_of_stars;stars++)
printf("%d",row);
}
}

7.#include<stdio.h>
void main()
{
int inp = 5,row,col,odd,even,no_of_times;
odd=1;
even = 2;
for(no_of_times =1 ;no_of_times<= inp/2 ; no_of_times++,printf("\n"))
{

for( col = 1 ; col <= inp ; col++ , odd+=2)


printf("%02d ",odd);
for( col = 1,printf("\n") ; col <= inp ; col++ , even+=2)
printf("%02d ",even);
}
if( inp % 2 ==1)
{
for( col = 1 ; col <= inp ; col++ , odd+=2)
printf("%02d ",odd);

8.#include<stdio.h>
void main()
{
int inp = 6,col,row;
for(row = 1; row <= inp ; row++,printf("\n"))
{
for(col = 1; col<=row ; col++)
printf("%d ",row);
}
for(row = inp; row >= 1 ; row--,printf("\n"))
{
for(col = 1; col<=row ; col++)
printf("%d ",row);
}
}
9.#include<stdio.h>
void main()
{
int inp = 4,col,row,toPrint=1;
for(row = 1; row <= inp ; row++,printf("\n"))
{
for(col = 1; col<=row ; col++)
printf("%02d ",toPrint++);
}
for( row = inp ; row>=1; row--,printf("\n"))
{
toPrint = ((row * (row +1)) /2 ) - row + 1;
for(col = 1; col<=row ; col++)
printf("%02d ",toPrint++);
}
}

10.#include<stdio.h>
void main()
{
int inp = 6,col,row,toPrint=1;
for(row = 1; row <= inp ; row++,printf("\n"))
{
if(row % 2 == 1)
{
toPrint = ((row * (row +1)) /2 ) - row + 1;;
for( col =1 ; col <= row ; col++ )
printf("%02d ",toPrint++);
}
else
{
toPrint = ((row * (row +1)) /2 );
for( col = 1 ; col<= row ; col++)
printf("%02d ",toPrint--);
}
}

}
11.#include<stdio.h>
void main()
{
int inp = 4,col,row;

for(row =1 ; row <= inp ; row++,printf("\n"))


{
if(row % 2 == 0)
printf("%d ",row+1);
for( col = 1 ; col <= inp ; col++)
printf("%d ",row);
if(row % 2 ==1)
printf("%d ",row+1);
}
}

12.
#include<stdio.h>
void main()
{
int inp = 6,col,sp,row,firstHalf,secondHalf;
firstHalf = 1;
for(row = inp ; row >=1 ; row--,printf("\n"))
{
for(sp =1 ; sp <=inp-row; sp++)
printf(" ");
for( col = 1 ; col<= row; col++)
printf("%02d ",firstHalf++);
secondHalf= (row * (row + 1) / 2) + ((inp*(inp+1))/2) - row + 1;
for( col = 1; col <= row ;col++)
printf("%02d ",secondHalf++);
}
}

You might also like