print a series of numbers optimization part 2 [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 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);
}

Related

how a program maps onto a computer's memory and operations? [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.
What writes compiler for functions and does it allocate memory for it ?and how?
Generally it is easy to visualize that data structures are stored in memory. But how OPERATIONS? like statements in a function body? what compiler does for them?
Operations are the steps you need to make something work. Say you have the following code fragment:
int a = 1;
int b = 2;
int c = a + b;
This will involve some storage for a, b and c, and some sort of operations to store the values 1 into a, the value 2 into b, and then to calculate a+b and store that into c.
Understanding how this works is key to understanding how computers work in general, and it's quite a complex subject to cover completely, and I doubt it's suitable as a simple question here.

Need Multiple Sudoku Solutions [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 output multiple sudoku solutions in my program. For example, when You enter this as input:
8..6..9.5.............2.31...7318.6.24.....73...........279.1..5...8..36..3......
.'s denote blank spaces. Numbers represent already-filled spaces. The output should be a sudoku solution like so:
814637925325149687796825314957318462241956873638274591462793158579481236183562749
However, I want to output multiple solutions. This would be all the solutions that should be printed:
814637925325149687796825314957318462241956873638274591462793158579481236183562749
814637925325941687796825314957318462241569873638472591462793158579184236183256749
834671925125839647796425318957318462241956873368247591682793154579184236413562789
834671925125839647796524318957318462241956873368247591682793154519482736473165289
834671925125839647796524318957318462241965873368247591682793154519482736473156289
But my program only prints out one solution.
Could anyone help me come up with a way to print out multiple solutions? Thanks.
You shouldn't return here:
if(testTheNumber(arr, row, column+1)==true)
{
return true;
}
You should instead let the algorithm try all the possible k values.
And you should only print the solution at the end (when you've found all the numbers).
You can do this by simply not stopping your recursion when you find a solution. For example, something like:
if (row == 9) {
// print solution here
return true;
}
and remove the other return true; and just recurse:
testTheNumber(arr, row, column+1);
The above will stop the recursion when you find a solution (by reaching the end row), and will also continue trying more numbers after that point.
Also, you may have a bug in the if(k == 10) part, because k should never be 10 at that point. You will want to set the cell to 0 after existing from the k loop.

c++ array min max range fraction [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 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.

Fastest way to input integers [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.
What is the fastest way(code) to take integer input from user via terminal (not file...:P).
P.S 1: Integers are of small size( within size of int) but the total number of intergers is very large.
P.S 2: Scanf toooo... slow
P.S 3: Forget the human limits ,talk technical...plz
I think an approach based on scanf will be hard to beat. In any case, it will be easy to implement. So I'd start with that, if it's not sufficient, benchmark before trying anything else.
If the input consists of whitespace-separated integers:
scanf("%d ", &input)
for continuous input processing you can try this
while( scanf("%d ", &val) == 1)
{
// processing : do what you want
}
also you can use this for file inputs reading (fscanf)

How do I make a smaller range with two given integers? [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.
For example,
I have a range 14269-14274.
To conserve space on the screen my users want to have it display in the format 14269-74.
Another example would be a range of 14269-14529 which should output as 14269-529.
How would I achieve this?
Something like this should do the trick:
int a = 14269;
int b = 14529;
int endrange = b % pow(10, floor(log10(b - a) + 1));
You need to make sure that a < b though.
You can check the first digit that differs, output the first number and then the second one, starting at the first different digit.
This of course only makes sense if the two numbers have the same length.
Were you expecting the implementation?