What's faster? (a+a vs 2*a and more) [closed] - c++

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
In C/C++, I was wondering which is faster?
int a;
int b = a + a; // this
int b = 2 * a; // or this?
Also, is important the datatype? What about long? what about the number of times we add up?
(what about...)
long a;
long b = a + a + a +a;
long b = 4 *a;

Trust your optimizing compiler. It knows how to optimize for a specific CPU/architecture in ways that you will only be able to guess. Without reference to a specific architecture, there is no meaning to statements like "is x faster than y?", because it all depends on a huge number of factors.
And as always with performance questions, measuring is going to answer the question more completely than us offering semi-informed opinions and guesses.

Related

What is the best implementation of min? [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
Is there a difference between
int min(int a, int b) {
return (a < b) ? a : b;
}
and
int min(int a, int b) {
return (b < a) ? b : a;
}
Is there any specific reason to prefer one over the other?
This question is specifically intended for both the C and the C++ languages. I understand these are different languages and a similar question was asked for C++ here: Correct implementation of min .
I am interested in reasons that may pertain to one language and not the other.
No, there isn't. The two implementations are equivalent under all defined circumstances. (An individual compiler might exhibit performance differences, but not functional differences.)
It would be different if the function weren't concerned solely with ints. Floating point numbers, as chux mentioned in a comment, can have both a<b and b<a false even when their bit patterns differ, as with negative/positive zero, or when at least one is a NaN. Technically this could also occur with an exotic (but standards-compliant) integer representation (through -0 or padding bits), but AFAIK no otherwise standards-compliant compiler does that.

time complexity of loops for programming contests problems [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Improve this question
Which has greater time complexity, a for loop or a while loop?
Can anyone give me a chart to compare the time complexity of different loops?
If possible suggest good references to learn about time complexity.
Barring a few technicalities, a while loop and a for loop are isomorphic:
for (a; b; c) {
body;
}
can be turned into:
a;
while (b) {
body;
c;
}
As such, there's no difference in computational complexity between the two.
As to choosing between the two: the usual rule of thumb is to use a for loop if at least two of the three clauses are meaningful for that loop.
If you are doing the exact same thing with them, they should be pretty much exactly the same. For instance:
int i = 0;
while (i < 10)
i++;
and
for (int i = 0; i < 10; i++);
should have pretty much exactly the same time complexity, since they are doing the exact same thing.
The only thing that changes the time complexity would be what is inside of the loop. But there's really no reason to favor for over while other than it adds a convenient way to declare and iterate a variable.

Obtaining an algorithm [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
Let's say I have a variable a and b. Now I want to find a value for c such that the fraction a / c is a positive integer, and where c is as close to b as possible.
For example: a = 100 and b = 30.
In this case I want c to be 25; because a / c is an integer, and c is as close as b for which this holds.
Any ideas how I can program a function in C++ which does exactly this?
Find the factors of a. (search web for methods)
Scan resulting list for minimum difference vs b.
Is this a homework assignment? Either way, think about how you would solve this problem without writing any code. A good algorithm comes from a good design. Break the problem down into pieces and walk through some more examples. For example, how would you solve the problem of determining whether the division results in an integer value? Hint: There is a different operator you could use as opposed to division to achieve this easily. Now, how would you solve the problem of determining what number to start at for c in the algorithm? Do not write any code until you have the pseudocode figured out.

C++ sort an array in descending order using a separate function [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
Hi there need help with an assignment in C++
I must randomly generate 100 numbers under 250 in an array and then sort them in descending order using a separate function.
#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
double sample[100];
int t;
for (t=0; t<250; ++t) {
sample[t] = (double) (rand()%100);
}
for (t=0; t<100; ++t) {
cout<< sample[t]<< "\n";
}
return 0;
}
I have managed to successfully complete part 1, would appreciate some help in the sorting of the random numbers.
http://www.cplusplus.com/reference/algorithm/sort/
It even has one of its examples directly relevant to your problem.
Sorting algorithms are one of the cornerstones of most beginner computer science courses and are a rather important study in the science itself. They are also very well documented on the internet.
You can ask for help with specific errors but asking for us to write it for you is not going to teach you anything.

Finding value of int x [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
int x;
x=x+30;
cout << x;
the output will be 33, why is it so?
I didn't even declare x as 3.
Can someone guides me? Thanks!
Using an uninitialized variable is undefined behavior. You got 33 due to an unreliable sequence of implementation quirks. The program is free to produce any value at all, fail to compile, or hire an assassin to stab you.
In C++, variables are given space (memory allocation) by default equal to the size of the variable, but they are not given values by default.