SLOT 1
Hands on Exercise 1
Candies Problem
There is a JAR full of candies for sale at a mall counter. JAR has the capacity N, that is JAR can contain maximum N candies when JAR is full. At any point of time. JAR can have M number of Candies where M<=N. Candies are served to the customers. JAR is never remain empty as when last k candies are left. JAR if refilled with new candies in such a way that JAR get full.
Write a code to implement above scenario. Display JAR at counter with available number of candies. Input should be the number of candies one customer can order at point of time. Update the JAR after each purchase and display JAR at Counter.
Output should give number of Candies sold and updated number of Candies in JAR.
If Input is more than candies in JAR, return: “INVALID INPUT”
Given,
N=10, where N is NUMBER OF CANDIES AVAILABLE
K =< 5, where k is number of minimum candies that must be inside JAR ever.
Example 1:(N = 10, k =< 5)
Input Value
3
Output Value
NUMBER OF CANDIES SOLD : 3
NUMBER OF CANDIES AVAILABLE : 7
Example : (N=10, k<=5)
Input Value
0
Output Value
INVALID INPUT
NUMBER OF CANDIES LEFT : 10
- #include <iostream>
- using namespace std;
- int main()
- {
- int n=10, k=5;
- int num;
- cin>>num;
- if(num>=1 && num<=5)
- {
- cout<<“NUMBER OF CANDIES SOLD : “<<num<<“\n”;
- cout<<“NUMBER OF CANDIES LEFT : “<<n-num;
- }
- else
- {
- cout<<“INVALID INPUT\n”;
- cout<<“NUMBER OF CANDIES LEFT : “<<n;
- }
- return 0;
- }
i = int(input())
n = 10
k = 5
if(i >n):
print(“INVALID INPUT”)
print(“NUMBER OF CANDIES AVAILABLE:”, end=””)
print(n)
elif(i <= 0):
print(“INVALID INPUT”)
print(“NUMBER OF CANDIES AVAILABLE:”, end=””)
print(n)
else:
print(“NUMBER OF CANDIES SOLD:”, end=””)
print(i)
print(“NUMBER OF CANDIES AVAILABLE:”, end=””)
print(n-i)
Selection of MPCS Problem
Selection of MPCS exams include a fitness test which is conducted on ground. There will be a batch of 3 trainees, appearing for running test in track for 3 rounds.
You need to record their oxygen level after every round. After trainee are finished with all rounds, calculate for each trainee his average oxygen level over the 3 rounds and select one with highest oxygen level as the most fit trainee. If more than one trainee attains the same highest average level, they all need to be selected.
Display the most fit trainee (or trainees) and the highest average oxygen level.
Note:
The oxygen value entered should not be accepted if it is not in the range between 1 and 100.
If the calculated maximum average oxygen value of trainees is below 70 then declare the trainees as unfit with meaningful message as “All trainees are unfit”
Average Oxygen Values should be rounded.
Example 1:
INPUT VALUES
95
92
95
92
90
92
90
92
90
OUTPUT VALUES
Trainee Number : 1
Trainee Number : 3
Note:
Input should be 9 integer values representing oxygen levels entered in order as
Round 1
Oxygen value of trainee 1
Oxygen value of trainee 2
Oxygen value of trainee 3
Round 2
Oxygen value of trainee 1
Oxygen value of trainee 2
Oxygen value of trainee 3
Round 3
Oxygen value of trainee 1
Oxygen value of trainee 2
Oxygen value of trainee 3
Output must be in given format as in above example. For any wrong input final output should display “INVALID INPUT”
#include <stdio.h>
int main() {
float a1,a2,a3,b1,b2,b3,c1,c2,c3,res1,res2,res3,max;
scanf(“%f”,&a1);
scanf(“%f”,&b1);
scanf(“%f”,&c1);
scanf(“%f”,&a2);
scanf(“%f”,&b2);
scanf(“%f”,&c2);
scanf(“%f”,&a3);
scanf(“%f”,&b3);
scanf(“%f”,&c3);
if(a1>=1 && a1<=100 && a2>=1 && a2<=100 && a3>=1 and a3<=100)
{
if(b1>=1 && b1<=100 && b2>=1 && b2<=100 && b3>=1 and b3<=100)
{
if(c1>=1 && c1<=100 && c2>=1 && c2<=100 && c3>=1 and c3<=100)
{
res1=(a1+a2+a3)/3;
res2=(b1+b2+b3)/3;
res3=(c1+c2+c3)/3;
if(res1<70)
{
printf(“Unifit\n”);
}
if(res2<70)
{
printf(“Unifit\n”);
}
if(res3<70)
{
printf(“Unifit\n”);
}
}
max=res1 > res2 ? ( res1 > res3 ? res1 : res3) : (res2 > res3 ? res2 : res3);
if(res1==max)
printf(“Trainee Number: 1\n”);
if(res2==max)
printf(“Trainee Number: 2\n”);
if(res3==max)
printf(“Trainee Number: 3\n”);
}
}
return 0;
}
SLOT 2
Washing Machine Problem
A Washing Machine works on the principle of a Fuzzy system, the weight of clothes put inside it for wash is uncertain. But based on weight measured by sensors, it decides time and water levels which can be changed by menus given on the machine control area. For low Water level, time estimate is 25 minutes, where approximate weight is 2000 grams or any non-zero positive number below that. For Medium Water level, time estimated is 35minutes, where approximate weight is between 2001 grams and 4000 grams. For High Water level, time estimated is 45 Minutes, where approximate weight is above 4000 grams. Assume the Capacity of the Machine is maximum 7000 grams. Where the approximate weight is zero, the time estimate is 0 minutes. Write a function which takes numeric weight in the range [0,7000] as input and produces estimated time as output; if input is more than 7000, then output is: “OVERLOADED!”, and for all other inputs, the output statement is “INVALID INPUT”. Input should be in the form of integer value – Output must have the following format – Time Estimated: Minutes
Example 1
Input Value
2000
Output Value
Time Estimated: 25 Minutes
#include<stdio.h>
int main() {
int n,t;
scanf(“%d”,&n);
if(n<=2000 &&n>0)
{
t=25;
printf(“Time Estimated : %d Minutes”,t);
}
else if(n>2000 && n<=4000)
{
t=35;
printf(“Time Estimated : %d Minutes”,t);
}
else if(n>4000 && n<=7000)
{
t=45;
printf(“Time Estimated : %d Minutes”,t);
}
else if(n>7000)
{
printf(“OVERLOADED”);
}
else
{
printf(“INVALID INPUT”);
}
}
SLOT 2
Caesar Cipher
The Caesar cipher is a type of substitution cipher in which each alphabet in the plaintext or message is shifted by a number of places down the alphabet.
For example, with a shift of 1, P would be replaced by Q, Q would become R, and so on.To pass an encrypted message from one person to another, it is first necessary that both parties have the ‘key’ for the cipher, so that the sender may encrypt it and the receiver may decrypt it.
the key is the number of OFFSETs to shift the cipher alphabet. Key can have basic shifts from 1 to 25 positions as there are 26 total alphabets. As we are designing custom Caesar Cipher, in addition to alphabets, we are considering
numeric digits from 0 to 9. Digits can also be shifted by key places.
For example, if given plain text contains any digit with value 5 and key = 2, then 5 will be
replaced by 7. “-” (Minus sign) will remain as it is. Key value less than 0 should result into
“INVALID INPUT”
Example 1:
Input:
Enter your PlainText: All the Best
Enter the Key: 1
Output:
The encrypted Text is: Bmm uif Cftu
Write a function customCaesarCipher(int key, String message) which will accept plaintext and
key as input parameters and returns its cipher text as output.
#include<stdio.h>
#include<string.h>
void customCaesarCipher(int key,char str[])
{
int n=0,i=0;
for(n=0;str[n]!=’\0′;n++);
if(key<0)
{
printf(“INVALID INPUT”);
return;
}
else
{
for(i=0;i<n;i++)
{
if(str[i]!=’ ‘)
{
if(str[i]>=65 && str[i]<=90)
{
if((int)(str[i]+key)<=90)
str[i] = (int) (str[i]+key);
else
str[i] = (int)(str[i]+key-90+65-1);
}
else if(str[i]>=97 && str[i]<=122)
{
if((int)(str[i]+key)<=122)
str[i] = (int) (str[i]+key);
else
str[i] = (int)(str[i]+key-122+97-1);
}
else if((str[i]>=48 && str[i]<=57))
{
if((int)(str[i]+key)<=57)
str[i] = (int)(str[i]+key);
else
str[i] = (int)(str[i]+key-57+48-1);
}
}
}
printf(“%s”,str);
}
}
int main()
{
int key;
char str[100];
scanf(“%[^\n]s”,str);
scanf(“%d”,&key);
customCaesarCipher(key,str);
return 0;
}