While loop increments in c [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 9 years ago.
I am trying to make my "Steps" or increments in a while loop such that my values of are like N=2, 4, 8, 16... basically powers of 2 till 2^20. I have tried to do
for(j=1;j<=20;j++){
m=pow(2,2*i);
MAX=pow(2,20);
INC=pow(2,i);
while(m<=MAX){
then have my code running inside this. But in the output it gives me 2,4,6,8,10,12.. does anyone know what the right way is to do this?
Thanks!

You can start with a value of 2 for m and multiply m by two every iteration. As an optimization you can replace the multiplication by two with an left shift:
int MAX = pow(2, 20);
int m = 2;
while(m <= MAX) {
//do your work
m <<= 1;
}

In each iteration of the while loop; multiply your variable by two.
int m = 2;
while(m < maxpow)
{
m = m * 2;
printf("%d\n", m);
}

Related

a complicate example using loop invariant [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.
can anyone supply a complicate example using loop invariant example such as sum(int n) is so trivial that it can not show the power of loop invariant. I want a example that is not that obivious, and we can use method like loop invariant to solve it.
The Wikipedia example is quite good:
for (int i = 0; i < n; i++) {
x = y + z;
a[i] = 6 * i + x * x;
}
Two invariant can be moved (y + z and x * x). The advantage of this example is that after LICM has been applied, you can apply other optimizations on the code to have something very easy.
There are plenty on papers/slides/courses about that, you sure can find a satisfying example.

Function to return a specific value in a given input range [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.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...

Why for loop does not run by other than int variables? [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.
Why for loop does not run by other than int variables? I tried to run for loop by taking float variable but it does not run by any other variables than int type?
You can use a for loop with integer variables, floating-point variables, even no variables at all.
int i;
for(i = 0; i < 10; i++) continue;
float f;
for(f = 0.0; f < 5; f += 0.5) continue;
for(;;) break;
But see What Every Computer Scientist Should Know About Float-Point Arithmetic for why you should think twice before using example 2.
You should be able to do it by using the STEP command
float X = 0;
//
//increase in steps of 1 x 1 thousandth
for (X = 1; X <= 100; X += 0.001) {
// DISPLAY YOUR RESULT maybe using: Math.Round(X, 3)
}

Online Judgement System - Why am I getting Wrong Answer for this thread? [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.
Original page: http://acm.whu.edu.cn/learn/problem/detail?problem_id=1036
This should be a simple Dynamic Programming problem. I figured out the solution to be the following:
int main(void)
{
double d[501];
int i;
d[0] = d[1] = 1.;
d[2] = 2.;
for(i = 3; i<=500; i++)
d[i] = d[i-1] + d[i-2] + d[i-3];
int n;
while(scanf("%d", &n) == 1) {
if(n == 0) return 0;
printf("%.0lf\n", d[n]);
}
return 0;
}
But Wrong Answer reported after submission. I really don't know why.
double is not enough for the precision
you should use high-precision to solve it
Decimal point maybe. printf("%.0lf\n", 1.0); will print 1.0 but system may wait for 1.

What is the best answer for finding the maximum sum possible in an array [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.
The question is :
Find the maximum sum possible in an array of positive integers by selecting the elements in such a way that no two elements are next to each other.
there is an answer like this :
but what is the best answer for this question
Let's denote the array by "t" and index it from 0. Let f be a
function so that
f(k)=the maximal sum in the [0..k] subarray with the conditions of the problem.
Now use dynamic programming:
f(0) = t[0]
f(1) = max{ t[0], t[1] }
f(k) = max{ f(k-2) + t[k], f(k-1) } if k >= 2
If the array has n elements we need f(n-1).
Thanks in advance.
Solution you proposed is good one.
Similar approach (page 7 here):
Let m[i] be the maximum sum of any subarray that ends at the element a[i].Then
m[i] is simply max(a[i], m[i-1]+a[i]).
This is O(n).
and you cant get anything below O(n) as you have to visit every item of the array atleast once to compute the result.
Well, I think this is already the best answer.
Since you need O(n) to read in the data.
an O(n) algorithm is the fastest in the big-O notation.
public static int maxSum(int[] A){
return maxSum(A,0,1);
}
private static int maxSum(int[] A, int x, int y){
int c =0, d=0;
if(x<A.length){
c = A[x]+maxSum(A,x+2,x+3);
}
if(y<A.length){
d = A[y]+maxSum(A,y+2,y+3);
}
return c>d?c:d;
}

Resources