Problem 1:Multipurpose
Time limit: 2 secs
Perform the following operations:
1. Find the square of a number
2. Find the product of two numbers
3. Find the summation of three numbers
4. Concatenate two strings
Input :
Each line contains a input test-case. First integer, m, in every line denotes the operation to be performed, followed by the integers or strings needed for the operation:
m=1 for finding the square of the followed integer.
m=2 for finding the product of followed two integers.
m=3 for finding the summation of followed three integers.
m=4 for concatenating two strings
Output :
For each line in the input, output the required result.
Constraints :
Number of test cases are less than 30
1<=m<=4
Remaining integers will be from 1 to 1000, inclusive.
Strings will contain only upper and lower case alphabets and will be of length less than 30.
Sample Input :
1 2
2 2 3
3 3 4 5
4 Code Fest
Sample Output :
4
6
12
CodeFest
Score :
USE OF FUNCTIONS IN "math.h" IS NOT ALLOWED
I = Number of if, else, switch, ternary
B = Number of bitwise operators ( ~, |, &, ^, >>, << )
K = Number of keywords
C = Number of parentheses '(' ')' and number of semicolons ';'
A = Number of logical operators ( &&, ||)
L = Number of loops ( for, while, do), goto
T = 20000/ ( 100 + C + I*2000 + A^10 + L*500 + B*200 + K*20 )
Solution:
Function Pointers are pointers, i.e. variables, which point to the address of a function. This is useful because functions encapsulate behavior.
For instance, every time you need a particular behavior such as drawing a line, instead of writing out a bunch of code, all you need to do is call the function.
In the given problem there are four different operations to be performed. First of all we make 4 functions having same prototype, so that we can make
an array of function pointers. Now we call the required function in accordance to the given input. This makes the proper use of function pointer, and
reduces the use of conditional statements.
Reference:5.11 Pointers to Functions, Dennis Ritchie
Code:
u,x,y,z;
G()
{}
H()
{
scanf("%d",&x);
printf("%d\n",x*x);
}
I()
{
scanf("%d%d",&x,&y);
printf("%d\n",x*y);
}
J()
{
scanf("%d%d%d",&x, &y, &z);
printf("%d\n",x+y+z);
}
K()
{
char a[30],b[30];
scanf("%s%s",a,b);
printf("%s%s\n",a,b);
}
(*p[5])() = {G,H,I,J,K};
main(o)
{
(scanf("%d",&o)+1)&&( p[o](), main());
}
Check the Best Solutions
discussions
| ayushdash | how to take input? |
| Q: | which program we have to paste which we have to upload? |
| A: | You can use either. Both are modules used to submit your solution. |
| m3m | q1!!!!!!! |
| Q: | why ican\\\'t submit with java??? |
| A: | This is an event based on C. If you are interested in Java participate in MANTHAN this sunday. |
| titasde08 | problem 1 |
| Q: | When I am checking my program in Turbo C, it is alright, but when I am submitting my problem, it is returning wrong answer...why is it so ? |
| A: | Turbo C is not a standard compilor. Use gcc. |
| abuddy2 | ashish |
| Q: | sir/mam my program is working fine on my pc but while uploading it is showing compilation error wat shd i do?? |
| A: | compile using \"gcc -O2 -Wall -x c file.c\" |
| maddikolkata | Input Format |
| Q: | Is the Input set continuous for a single run? According to above input set, will the program be run for four times? |
| A: | Yes, the input set is continuous. Program for given sample input will be run only once. |
| chhabraamit | Unexpected server error |
| Q: | Hello Sir/mam,\\ni\\\'ve been getting unexpected server error for past half an hour..(my code works fine on my machine.)\\n\\nplease do some thing about it..\\n |
| A: | no such error should be occuring now, refresh your page |
| saurabhkr_1989 | queued for a long time |
| Q: | my ans are showing status QUEUED from long time?? |
| A: | check out now, all queue has been cleared now |
| rakshas | address of operator |
| Q: | will addressof operator(&) be count in bitwise operators??? |
| A: | for scanf it wont be counted in bitwise |
| rahmitter | END OF INPUT |
| Q: | How do I mark the end of input??\\n |
| A: | The input will end by end of file |
| greed | Bitwise & |
| Q: | Will \\\'&\\\' be counted in printf(\\\"%d\\\",&a) ? |
| A: | No |
| manoharsingh | Q1 |
| Q: | Without header file how to take input?? |
| A: | You don\'t need to worry about this. gcc implicitly includes any standard header file producing warnings. |
| AshimaBhatia | problem1 |
| Q: | how will we come to know the number of times user wants to give the input? |
| A: | till you reach end of file. |
post a question
Loading...
If you have a doubt about this problem, and don't find it listed in the discussion above, post it here.
If we think your question is relevant to everybody, we'll publish it to the discussion. Otherwise, we'll send you a private reply via email.
To post question you need to login.