Coding
Question 1
Question 1
Q1. WASHING MACHINE
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 –
<Integer>
Output must have the following format –
TimeEstimated: <Integer> Minutes
Example 1
Input Value
2000
Output Value
Time Estimated: 25 Minutes
//Solution in C++
#include <iostream>
using namespace std;
int main() {
int weight;
cin >> weight;
if(weight < 0)
cout << "INVALID INPUT";
else if(weight == 0)
cout<<"Time Estimated : 0 Minutes";
else if(weight > 0 && weight <= 2000)
cout << "Time Estimated : 25 Minutes";
else if(weight > 2001 && weight <= 4000)
cout << "Time Estimated : 35 Minutes";
else if(weight > 4001 && weight <= 7000)
cout << "Time Estimated : 45 Minutes";
else
cout << "OVERLOADED!";
return 0;
}
TCS NQT MOCK TEST / PRACTICE SET
Click Here!
Question 2
Question 2
Q2. 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.
//Solution in C++
#include <iostream>
using namespace std;
int main() {
int key;
char str[100];
scanf("%[^\n]s",str);
scanf("%d",&key);
customCaesarCipher(key,str);
return 0;
}
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);
}
}