pp2, practice problems, skillrack solutions, vit skillrack solutions, csc 1002 solutions, csc vit solution,
Question 1 (Row Maximum of a Matrix)
Given an nXn matrix with entries as numbers, print the maximum value in each row of the matrix.
Input Format
Value of ‘n’
Element in first row first column
Element in first row second column
..
Element in the first row nth column
Element in second row first column
Element in second row second column
..
Element in the second row nth column
…
Element in nth row first column
Element in nth row second column
..
Element in nth row nth column
Output Format
Maximum value in the first row
Maximum value in the second row
…
Maimum value in the nth row
Solution
#include< stdio.h >
void main()
{
int i,j,n,max,num;
scanf("%d",&n);
for(i=0;i < n;i++)
{
max=-1;
for(j=0;j < n;j++)
{
scanf("%d",&num);
if(num > max)
max=num;
}
printf("%d\n",max);
}
}
Input
INPUT:
n - number of rows and cloumns
Output
OUTPUT:
printf("%d\n",max);
Processing
for(i=0;i < n;i++)
{
max=-1;
for(j=0;j < n;j++)
{
scanf("%d",&num);
if(num > max)
max=num;
}
}
Pseudocode
BEGIN
Read n
for(i=0;i < n;i++)
{
max=-1;
for(j=0;j < n;j++)
{
scanf("%d",&num);
if(num > max)
max=num;
}
printf("%d\n",max);
}
END
Question 2 (Salary of Employees)
A company stores the following details of employees such as name, employee id, basic pay, % of DA and HRA. Given details of ‘n’ employees of an organization. Write a C code to
i. get the details of each employee.
ii. print their employee id and
iii. Total salary.
Total salary = Basic Pay + % of DA * basic pay + HRA.
Input Format
value of ‘n’
Employee name of employee1
Employee id of employee1
Basic pay of employee1
Percentage of DA of employee1
HRA of employee1
…
Employee name of employee – n
Employee id of employee – n
Basic pay of employee – n
Percentage of DA of employee – n
HRA of employee – n
Output Format
Employee id of employee1
Total salary of employee1
Employee id of employee2
Total salary of employee2
…
Employee id of employee – n
Total salary of employee – n
Solution
#include< stdio.h >
void main()
{
char name[20];
int i,n,id,bp,da,hra;
scanf("%d",&n);
for(i=0;i < n;i++)
{
scanf("%s",name);
scanf("%d%d%d%d",&id,&bp,&da,&hra);
printf("%d\n%d\n",id,(bp+((bp*da)/100)+hra));
}
}
Input
INPUT:
n - number of emloyees
Output
OUTPUT:
printf("%d\n%d\n",id,(bp+((bp*da)/100)+hra));
Processing
(bp+((bp*da)/100)+hra)
Pseudocode
BEGIN
Read n
for(i=0;i < n;i++)
{
scanf("%s",name);
scanf("%d%d%d%d",&id,&bp,&da,&hra);
printf("%d\n%d\n",id,(bp+((bp*da)/100)+hra));
}
END
Question 3 (Verification of Circular Prime Number)
A circular prime number is a prime number ‘p’ with a property that all the numbers got by cyclically permuting the digits of ‘p’, are also a prime number.
A number is said to be a prime if it has no factors other than the number 1 and itself. 19937 is a circular prime number, as all the numbers obtained by cyclically permuting the number 19937 : 99371, 93719,37199,71993,19937 are all prime.
Develop an algorithm and write a C program to check if the given number is circular prime or not.
Input Format
A number
Output Format
Print Circular prime or Not circular prime
Solution
#include< stdio.h >
#include< string.h >
int prime(int n)
{
int i,flag=1;
for(i=2;i < =n/2;i++)
if(n%i==0)
{
flag=0;
break;
}
return(flag);
}
int nnum(char s[])
{
int n=0,i;
char temp=s[0];
for(i=0;i < strlen(s);i++)
{
n*=10;
n+=s[i]-'0';
s[i]=s[i+1];
}
s[strlen(s)]=temp;
return(n);
}
void main()
{
int i,j,n,num,flag=1;
char s[20];
scanf("%s",s);
n=nnum(s);
while(num!=n)
{
num=nnum(s);
if(prime(num)==0)
{
flag=0;
break;
}
}
if(flag==0)
printf("Not circular prime");
else
printf("Circular prime");
}
Input
INPUT:
s - string containing number
Output
OUTPUT:
if(flag==0)
printf("Not circular prime");
else
printf("Circular prime");
Processing
int n=0,i;
char temp=s[0];
for(i=0;i < strlen(s);i++)
{
n*=10;
n+=s[i]-'0';
s[i]=s[i+1];
}
s[strlen(s)]=temp;
return(n);
Pseudocode
BEGIN
Read s
n=nnum(s);
while(num!=n)
{
num=nnum(s);
if(prime(num)==0)
{
flag=0;
break;
}
}
if(flag==0)
printf("Not circular prime");
else
printf("Circular prime");
END
Question 4 (Identify machines in same local network)
Numeric addresses for computers on the international network, ‘Internet’ has four parts, separated by periods, of the form xxx.yyy.zzz.mmm where xxx , yyy , zzz , and mmm are positive integers. Locally, computers are usually known by a nickname as well.
Sample Data
IP address Name
111.22.3.44 platte
555.66.7.88 wabash
111.22.5.66 green
0.0.0.0 none
A pair of computers are said to be in same locality when the first two components of the addresses are same. Given the details of some computers, design an algorithm and write a C program to display a list of messages identifying each pair of computers from the same locality. In the messages, the computers should be identified by their nicknames. In this example, the message to be displayed will be Machines platte and green are on the same local network. For example, given IP address and nick name of machines as follows:
101.33.2.1 Atlas
101.33.56.80 Horizon
101.43.45.74 Pluto
101.33.56.80 Horizon
101.43.45.74 Pluto
Print ‘Machines Atlas and Horizon are on the same local network’.
Input Format
Number of computers ‘n’
IP address of the computer1 as a String
Nick names of the computer1 as a String
IP address of the computer2 as a String
Nick names of the computer2 as a String
….
IP address of the computer n as a String
Nick names of the computer n as a String
Output Format
For each pair of machines in the same local network, print:
Machines A and B are on the same local network (A and B are names of the respective machines)
Solution
#include< stdio.h >
#include< string.h >
void main()
{
int n,p,i,j;
char s[20][20],na[10][10];
scanf("%d",&n);
for(i=0;i < n;i++)
{
scanf("%s%s",s[i],na[i]);
p=0;
j=0;
while(p!=2)
{
j++;
if(s[i][j]=='.')
p++;
}
s[i][j]='\0';
}
for(i=0;i < n-1;i++)
for(j=i+1;j < n;j++)
if(strcmp(s[i],s[j])==0)
printf("Machines %s and %s are on the same local network",na[i],na[j]);
}
Input
INPUT:
n - number of computers
for(i=0;i < n;i++)
scanf("%s%s",s[i],na[i]);
Output
OUTPUT:
if(strcmp(s[i],s[j])==0)
printf("Machines %s and %s are on the same local network",na[i],na[j]);
Processing
for(i=0;i < n;i++)
{
scanf("%s%s",s[i],na[i]);
p=0;
j=0;
while(p!=2)
{
j++;
if(s[i][j]=='.')
p++;
}
s[i][j]='\0';
}
Pseudocode
BEGIN
Read n
for(i=0;i < n;i++)
{
scanf("%s%s",s[i],na[i]);
p=0;
j=0;
while(p!=2)
{
j++;
if(s[i][j]=='.')
p++;
}
s[i][j]='\0';
}
for(i=0;i < n-1;i++)
for(j=i+1;j < n;j++)
if(strcmp(s[i],s[j])==0)
printf("Machines %s and %s are on the same local network",na[i],na[j]);
END
Question 5 (Verification of ‘L’ shaped arrangement of coins on game board)
Consider an nxn board game with four types of coins red, green, blue and yellow. Given the state of the board with coins in all cells, develop an algorithm and write a C program to check if the same coins are placed in the shape of ‘L’ on the board. The number of cells in the vertical and horizontal line of ‘L’ shape, is same. Red coins are represented by ‘r’, blue coins are represented by ‘b’, green coins are represented by ‘g’ and yellow coins are represented by ‘y’.
For example, given the configuration of a 4 X 4 board with coins as shown below, the program must print ‘Yes’ since the coin ‘r’ is placed in the positions (3,2), (4,2),(4,3),(4,4) form an ‘L’ shape.
b r y r
r r y b
y r b b
b r r r
Input Format
Number of rows
Number of columns
Elements in the matrix(board), given row by row
Output Format
Print Yes or No
Solution
#include< stdio.h >
void main()
{
char m[10][10];
int r,c,i,j,f=0;
scanf("%d%d",&r,&c);
for(i=0;i < r;i++)
for(j=0;j < c;j++)
{
m[i][j]=getchar();
while(!((m[i][j] > ='a')&&(m[i][j] < ='z')))
m[i][j]=getchar();
}
if(r > 2 && c > 2)
{
for(i=0;i < r-1;i++)
for(j=0;j < c-2;j++)
{
if((m[i][j]==m[i+1][j])&&(m[i][j]==m[i+1][j+1])&&(m[i][j]==m[i+1][j+2]))
{
f=1;
printf("Yes");
break;
}
}
if(f==0)
printf("No");
}
else
printf("No");
}
Input
INPUT:
for(i=0;i < r;i++)
for(j=0;j < c;j++)
{
m[i][j]=getchar();
while(!((m[i][j] > ='a')&&(m[i][j] < ='z')))
m[i][j]=getchar();
}
Output
OUTPUT:
if(f==1)
printf("Yes");
else
printf("No");
Processing
for(i=0;i < r-1;i++)
for(j=0;j < c-2;j++)
if((m[i][j]==m[i+1][j])&&(m[i][j]==m[i+1][j+1])&&(m[i][j]==m[i+1][j+2]))
Pseudocode
BEGIN
Read matrix
for(i=0;i < r-1;i++)
for(j=0;j < c-2;j++)
if((m[i][j]==m[i+1][j])&&(m[i][j]==m[i+1][j+1])&&(m[i][j]==m[i+1][j+2]))
flag=1;
if(flag==1)
printf("Yes");
else
printf("No");
END
No comments:
Post a Comment