c++ array min max range fraction [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array which fluctuates about between 0.1429 and 0.1428 it doesn't seem to have a real top or bottom though so those numbers could vary.
if(myarray[N-1]<myarry[N]){/*always happens*/}
if(myarray[N-1]>myarry[N]){/*never happens*/}
the numbers are fractional so there must be smaller fraction in the numbers to show curves on my chart eg: 0.14285216
I am having real trouble with 'greater than' 'smaller than' < > I think it's because i've not got numbers bigger than 1 (myarray[N-1] shows 0 always)
can I do something to my data like increase the range or use another method to '<>'?
really stuck

I'm guessing that what you want to do is display the numbers in an array so as to see the differences between them? The reason for your always/never situation is that the array is sorted, which is probably a good thing. Anyway, to display a greater number of digits, you can use format specifiers, such as
printf ("my ith number: %.10f", myarray[i]);
This will give you myarray[i] with 10 decimal places.

what is:
myarray versus myarry (missing the a between the second r and the y)
Direct from your code given above:
if(myarray[N-1]<myarry[N]){/*always happens*/}
if(myarray[N-1]>myarry[N]){/*never happens*/}
Shouldnt it be:
if(myarray[N-1]<myarray[N]){/*always happens*/}
if(myarray[N-1]>myarray[N]){/*never happens*/}
Also I hope you arent ever using N = 0 as an input to this set of if statements.

You might want to multiply all numbers by 7 and subtract 1 - that will make the relative differences larger.

Related

calculate decimal fraction and show digits after decimal in a precise manner [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Given a fraction (103993/33102), I need to find 50000 digits after the decimal point for this fraction.
Initially I used setprecision(k) in C++, but it gives only 17 digits after decimal point. I also tried
sprintf (str, "%.500000f", num)
but the result is the same.
I need an algorithm that can solve this and which does not round off the digits after the decimal point i.e., it should be precise.
The best way to work out how to solve a problem like this is to take a ridiculously easy problem and work out the algorithm on that. That way, you won't get confused or lose your place, and the algorithm is the same regardless of the problem. So let's take 4/3.
4 goes into 3 once. We have 1 left over. We output the 1. for the one time it went in. We keep the 1 left over.
We multiply the 1 left over by 10 to get 10. 3 (our denominator) goes into 10 three times. With 1 left over.
We output the 3 because it went in three times. We still have 1 left over.
We go to step 2 and repeat as many times as we need to.
This same algorithm works just this simply regardless of the numerator and denominator, so long as they are positive integers.
Float or double will not do it because they are not precise enough. Others have suggested to use a bignum library. This can be done, but there is another way that works directly with integers.
A technique called modular exponentiation can be used to solve this problem. This lets you calculate all digits without ever running into precision problems.
Fortunately an answer how to do this has already been written:
Getting a specific digit from a ratio expansion in any base (nth digit of x/y)

How to determine what bin a float should be in? C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of floats Float_t xbins[41] that defines 40 bins i.e. ranges of floats.
E.g. y is in bin 7 if y > xbins[7] && !(y > xbins[8]).
How do I determine what bin a given float should belong to without having 40 if statements?
Please answer in C++ as I don't speak other languages.
If the array is sorted, then do a binary search to locate the correct bin. You'll need a combination of std::sort (if not sorted), then something like std::lower_bound, to locate. You'll need to ensure that operator< is implemented correctly for Float_t.
As it turned out that the bins are not uniformly spaced but have integer bounds, the probably fastest method is to have a (inverse) look up table that apparently has about 100 entries. One needs to make basically two comparisons for the lower & higher bounds.
If the array bounds are derived with a formula, it could be possible to write an inverse formula that outperforms the LUT method.
For a generic case binary search is the way -- and even that can be improved a bit by doing linear interpolation instead of exactly subdividing the range to half. The speed (if the data is not pathological) would be O(loglogn) compared to O(logn) for binary search.

Base 10 to base n conversions [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to write a C++ program that does base-conversions.
I want to convert a decimal number to all the other integer bases from 2 to 20.
Is there an efficient and easy-to-implement algorithm for base conversions?
I don't understand where exactly is the problem? It's very easy and straigtforward to do base conversion: you do it as you would by hand.
divide the number by base
write down the remainder
repeat the process with the integer part of the division
stop when you reach zero
the remainders in reverse order give you the digits in base
Example:
1025 (decimal) to base 15:
1025 / 15 = 68 , remainder 5
68 / 15 = 4 , remainder 8
4 / 15 = 0 , remainder 4
The number in base 15 is 485
You may have two problems:
Parsing from the original base to the computer's native integer representation (strtol is quite good at this).
Formatting into the new base. (itoa is quite good at this).
If you want to write it yourself, you might like the div function. You feed in the number and the base, and it splits off the rightmost digit. Repeat to get all digits.
If you want to be more efficient, you can divide by the base squared, and get two digits at a time (use a lookup table to get the ASCII characters for both digits). Here's an example of some very efficient implementations. Changing it to use a different base would not be difficult.

Generate all Binary Combinations C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to write a program that you give an input (number of digits n) and you get as an output a vector of all possible binary grey code generated.
For example,
if n=2, results should be:
V= {00, 01, 10,11}
Actually I am interested in having them bit by bit and not whole integer. Meaning I want to have a 2 D array of integers where I have each word in rows and bits (as int) in cols
Hint: Gray code of num is (num>>1) ^ num. Go through all numbers of the 0..2^N-1 range, and compute their Gray code representation using this simple formula.
EDIT Another hint: the simplest way to convert an integer to binary is using bitset:
bitset<N>((num>>1) ^ num).to_string()
The set V equals { 0, ..., 2^n-1 }. Just compute this set.

print a series of numbers optimization part 2 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
earlyer i posted part 1 and got some interesting responces
print a series of numbers optimization part 1
here is another way you could have the program print a repeating series of numbers to the screen, the goal here is to make the most efficiant/fastest algorithm
int series[] = [2,3,4,5,6,7,8,9,1]
int i = 9;
while(true)
{
print(series[i])
i = series[i] - 1;
}
of course ignore any extra overhead created by actually printing the number because that is not the purpose of the problem
the one boolean conditional statement (while true) is required for the infinite loop is required no matter what solution you do, so you can ignore that too
this solution uses memory for 11 int variables, but otherwise it only does one simple computation and one variable assignment per iteration.
so would this be the most time efficiant way to solve the infiniate number series problem?
I would say it's not the most efficient way.
There's a multiplication involved in addressing the array. It's essentially
destinationAddress = baseAddressOfArray + indexRequested * sizeof(elementOfArray)
I think the most efficient way would be to cache the string of one iteration and simply spit out that string over and over again. I'm not up on my exact C++ syntax, it'd be something like
string s = "123456789";
while(true) {
print(s);
}