C++ iostream recruitcoders [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have found 4 tasks on recruitcoders.com and
I have completed all of them, but in the first one i have scored only 1/10:
Write a program that works as a simple calculator that supports five operations: addition, subtraction, multiplication, division and modulo.
Input:
There is an unknown number of tests. Each test consists of one-character symbol which corresponds to specific operation (+ addition, - subtraction, * multiplication, / division and % modulo) and two following integers. Each test will be separated by spaces and followed by a newline. Number of tests doesn't exceed 100 and the result is less than 2^31. You can assume that there is no situation in which you would have to divide by 0.
Output:
For each test you should print a single number being the result of each operation.
Example:
Input:
+ 7 9
- 0 4
* 5 6
/ 8 3
% 5 2
Output:
16
-4
30
2
1
MyCode:
#include <iostream>
using namespace std;
int fcount(char, int, int);
int main() {
char znak;
long a, b;
long* wynik=new long[100];
for(char i=0;i<100;i++){
cin>>znak>>a>>b;
wynik[i]=fcount(znak,a,b);
}
for(char i=0;i<100;i++)
cout<<wynik[i]<<endl;
return 0;
}
int fcount(char znak, int a, int b){
switch(znak){
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
case '%':
return a%b;
}
}
THIS CODE IS WORKING FINE, IT IS JUST UNDERRATED BY RECRUITCODERS (1/10)
I am not asking you for better code, I just wonder where am I loosing so many points in such an easy task? Any suggestions? I have completed all 4 tasks scoring 28/40 total (1/10, 10/10, 10/10, 7/10), so the task with score 1/10 is a pain in a** for me :/

The requirement says that there is an unknown number of tests, but you are assuming exactly 100 tests. Change it to:
while (cin >> znak >> a >> b)
cout << fcount(znak, a, b) << endl;

Related

Recursive function, geometric progression [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I've just started learning C++ and I came across a simple problem but couldn't solve. Help me please,
In geometric progression, b1=2, r=3.
Create recursive function that outputs initial n sequences of progression. n should be input.
Assuming b1 is the initial number and r is the multiplier (ratio?), that would give 2, 6, 18, 54, ....
The pseudo-code for such a beast would be:
def geometric (limit, current, multiplier):
if limit <= 0: return
print(current)
geometric(limit - 1, current * multiplier, multiplier)
def main():
b1 = 2
r = 3
n = input("How many terms? ")
geometric(n, b1, r)
The reason it's pseudo-code is that I tend to prefer for educational questions since people learn a lot more by converting it rather than just being given an answer.
So, in terms of how the recursive function works, you pass it the number of terms remaining along with the current term and the multiplier.
Provided it hasn't reached its base case (number of terms remaining is zero), it prints the current term then recursively calls itself, adjusting the arguments so as to approach the base case.
Once it has reached the base case, it will return (from all recursive levels).
Turning that into C++ is an exercise I'll leave for the reader, but I can provide something similar so you will see the approach taken.
Let's say you wanted to sum all the numbers between 1 and a given number. It has the same features as any recursive solution, including the one posed in your question:
an operation that can be defined in terms of a simpler operation; and
the base case.
In C++, a complete program to do that would be something like:
#include <iostream>
void recurse(int number, int sum) {
// Base case for printing and returning.
if (number <= 0) {
std::cout << "Sum is " << sum << '\n';
return;
}
// Recurse to next level.
recurse(number - 1, sum + number);
}
int main() {
int myNum;
std::cout << "Enter your number: ";
std::cin >> myNum;
recurse(myNum, 0);
}
Some sample runs:
Enter your number: 3
Sum is 6
Enter your number: 4
Sum is 10
Enter your number: 5
Sum is 15
Enter your number: 6
Sum is 21
Enter your number: 100
Sum is 5050
The explanatory text on your specific question, along with the C++ code showing how to do it for a different question, should hopefully be enough for you to work it out.

Reducing Run Time C or C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.

Using C++, get f(n+1)=3/4*f(n)+4 result [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm really fresh to C++, and I faced difficulties with the next question :
Write a program that asks the user to enter a number : n
THEN calculate f(n).
Notice that :
f(0)=3 and f(n+1) = 3/4 * f(n) + 4 ?
For example :
f(1)= 6.25
f(2)= 8.69
f(3)= 10.52
f(4)= 11.89
f(5)= 12.92
====================================================
So, how can I solve this?
thanks for all..
I try this code depending on Mr. paxdiablo answer :
#include<iostream>
using namespace std;
int main()
{
double x=0.0;
cout<<" enter an integer N:";
cin>> x;
double f1(double x)
{
if x==0.0
return 3;
return 3 / 4 * f1 (x-1) + 4;
}
return 0;
}
But the program never runs!
================================================
The Correct Solution is :
#include<iostream>
#include<iomanip> //To enable "setprecision" tool
using namespace std;
double f(int x){
if (x==0)
{return 3;}
return (3.0/4.0) * f(x-1) + 4.0; //we add zeros to get "double" results
}
int main()
{
int n=0;
cout<<" Please, enter an integer :";
cin>> n;
cout<<fixed<<setprecision(2)<<f(n); //"setprecision" used to get only two digits after the point
return 0;
}
BIG thanks to everyone gave me a hand and special thank to Mr. paxdiablo.
This sounds like a job for ... Recursion Man!
Simply define a recursive function that returns 3 when n is zero or calls itself with a reduced problem.
Pseudo-code would be something like
def f(x):
if x == 0:
return 3
return 3 / 4 * f(n-1) + 4
You can use std::cin >> dv to get a value from the user and that function (recoded in C++) to do the grunt work. Then std::cout << result to output the result. Just remember to use doubles rather than ints, including constants like 3.0.
You have to define the recursive function f, then read the input from the user, then apply the function to the input you've read (probably as an integer?), and print the output.

Fast input method of number in c/c++? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
inline int input()
{
int c;
int n = 0;
while ((c = getchar_unlocked()) >= '0' && c <= '9')
{
// n = 10 * n + (c - '0');
n = (n << 3) + ( n << 1 ) + c - '0';
}
return n;
}
Can someone explain how this way of inputting the number is working and how it is the fast way to input a number?
Compilers are generally very stupid, and have no understanding of the logic you're trying to implement. Moreover, they're often written by less-than-competent people who don't understand much of modern hardware.
The author of the code has realized this, and cleverly analyzed that 10 is the same as 8 + 2, and that 8 and 2 are both powers of two. For the flourish, he proceeded to turn the mathematics of exponentials into native, bitwise hardware instructions. This combination of mathematics and deep understanding of the hardware leads him to factor 10 * x as 8 * x + 2 * x and express the result in terms of instructions that are far more optimal than the naive "stupid multiplication" that would otherwise have taken place. Naturally, such optimizations are far beyond the reach of any kind of technology and cannot possibly be performed automatically.
The result is a vastly improved method of multiplying a number by ten.
Patent pending.
n << 3 equals n * 8
n << 1 equals n * 2
i.e. (n << 3) + ( n << 1 ) equals 10 * n
bitwise shift is faster than multiplication, though I'm not sure the whole thing should be faster.

Translation of number into strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to know whether there is a way in which we can print the number alphabetically i.e
123 should be printed as one two three.
The only condition is that we should not reverse the number and we should not use array.
I only know these two ways:
"Reverse the number", that is, taking the last digit and cutting it off. For each cut-off digit, one can use an array to look up the correct string.
using switch and a lot of cases
Any ideas?
for hundreds place:
int hundreds = my_num / 100 //Needs "/", NOT "%"
if(hundreds == 0)
cout << "zero";
else if(hundreds == 1)
cout << "one";
//repeat for 2-9
This process could be tweaked to do the other digits as well. It is also worth mentioning that the if/else block a) could be done with a switch/case if preferred, and b) could pretty easily be made into a separate function to avoid having to repeat the block of code over and over, I just wrote out as much as I did for clarity's sake. Note that this assumes the number you're "translating" is an integer. With integers the "/" operator will return the full quotient WITHOUT the remainder, e.g. 123 / 100 = 1, not 1.23
Not necessarily the easiest route, but you can make a function, say DigitToWord which will take a digit 0, 1, 2, ...etc to its word with a switch statement. Then I recommend using a for loop over the number, continuously dividing by 10 and taking the mod for the loop:
int num; //my number i want to print
int div = pow(10, (int)log10(num)); //find the largest power of 10 smaller than num
while(num > 0) {
int remainder = num%div;
int digit = num/div;
DigitToWord();
num = remainder;
}