difference between two ways of division in c++ [closed] - c++

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 5 years ago.
Improve this question
what is the difference between
int n,s;
cin>>n;
s=n*1/10;
cout<<s;
and
int n,s;
cin>>n;
s=n*10/100;
cout<<s;
?
and also 100/1000 and 1000/10000 and ...
when I enter big numbers for n(such as 1000000000),it has different outputs.
I got my answer!!
the problem is because of order of evaluation and also overflowing in integer

If you have a large number and multiply by 10, the number may overflow. This means that the number that you're trying to store is too big to fit into the memory allocated for that number. The exact consequence for a signed integer overflowing is called "undefined behaviour" meaning that it's up to the compiler to decide how to deal with this.
When you divide by 100, the number you're dividing is now different than the number than you're expecting - thus giving you the wrong result.
This is likely only a problem on debug builds; as the compiler will probably replace the *10/100 to be /10 as part of the optimisation that it will do for a release build.
The other item where you multiply by 1 and then divide by 10 will always be defined however, since the multiplication will leave the number unchanged.

Related

Suppose you have an array of N elements. You need to find for how many i, Ai + A(i+1) is a square number. Is this question trivial? If so how? [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 2 years ago.
Improve this question
I just wanted to know if the above question is trivial or not. More importantly, how can you recognize if an algorithm is trivial?
It depends what do you mean by trivial. If you talk about complexity, it is O(n*M(N)) where M(N) is the complexity of the underlying multiplication algorithm with N maximum of the array's values and n is the length of the array.
If you talk about implementation, it is one loop with one check that the sum of the neighbors is a perfect square. If the elements fit into int, double etc. you have sqrt function in the standard library. If your elements are arbitrary length integers or float point numbers, you either need to use an appropriate library or implement the handling of these numbers on your own, which might be not trivial.
This understanding should help you to answer your last question

C++ full number of degree [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 4 years ago.
Improve this question
This is a simple code that counts two to the right degree. Starting from somewhere around 60 degrees the answer is incorrect.
I need to count 2^200. The answer shouldn't be in the form like "1.606938e+60", but by numbers. How to do this in C++?
#include <iostream>
using namespace std;
int main()
{
unsigned long long int n,z;
cin>>n;
z=pow(2,n);
cout<<z<<endl;
return 0;
}
You need to use std::set_precision(n) to get it to print in the format that you're expecting it, but if your numbers get high enough, you'll run into a second issue. pow returns a double, which loses precision in a big way with huge numbers. For more information on how to solve that, refer to this Stack Overflow answer.
Anyway you cant print such big number as a single integer or double value in c++ (not yet). Maybe there exist some 256 or 512 machine architectures and implementations which build in types are large enough, but its not possible in common. You probably need to use some data structure to store you number and operate on that.
This, this and this examples may be helpful.

Why C++ not have in starting double main() or like that and only int/void main()? [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
Also how would I write a program to work on a very large number. I want to find the largest prime factor of a number in the range of 600000000000
Because that int main () returns an exit code of the program. If returned 0 - everything is fine. You need to perform calculations in this function because it is the program entry point. If the number is too large advise to use the example of long arithmetic.
int main => return 0; which indicates if the program finished it's running as the programmer expected. Return 2^31 gives one enough numbers for indicating error codes.
For large numbers linked lists can be used.

A computing trick to calculate for e.g number of boxes required to place N objects given each box can hold M objects? [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 9 years ago.
Improve this question
As a part of a recent topcoder SRM problem we had to compute number of buses "B" required to carry "N" people given that each bus has "S" seats.
What is the smartest way to compute this in C++?
The obvious way is to do:
if(N%S==0){B=N/S;}
else{ B=N/S + 1;}
^ ALL VARIABLES ARE INTEGERS, N AND S ASSIGNED APPROPRIATE VALUES
However I cant understand the logic behind the following code which is one particular topcoder user's solution which I was checking out;
B = (N + (S-1))/S;
How does this work?
The code
B = (N + (S-1))/S;
is a common rounding trick. We know that in integer division, the remainder is cut-off, essentially what floor does. In this case, we enforce a ceil operation by adding S-1 first.
This is similiar to the common way of rounding floating point numbers:
n = floor(n + 0.5);

This works for any numbers smaller than 521153. This way it does not work and returns a very large negative number [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 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{int primes[521153];
return 0;
}
This works for any numbers smaller than 521153. This way it does not work and returns a very large negative number.
You have exceeded you stack size. The C languages work like "portable assembler", where all language operations map directly onto machine operations, even if those operations cause "undefined behavior". In this case your stack collides with the heap, and your program overwrites its own brains and dies.
Use int *primes = new int[BIG_NUMBER]; IIRC, that's well-defined to raise an exception if it cannot allocate that much memory. And your heap (where new gets its storage) can grow arbitrarily (on modern architectures), where your stack cannot. You C++ tutorial will define all these terms for you.