I am desperately looking for a way to easily calculate the number of all possible execution paths in a C function.
For example, for the following function I would expect to get a result of 3 (if there is a chance based on the values that 'i' gets to enter any of the 'if' statements)
void test(void)
{
if (i>0)
x = x + 1;
else if (i>10)
x = x + 2;
else
x = x + 3;
}
Use comma operator as
int test(void)
{
int ways = 0;
if (++ways, i>0)
x = x + 1;
else if (++ways, i>10)
x = x + 2;
else
{
x = x + 3;
++ways;
}
return ways;
}
Related
int x , sum = 0;
for (x = 10; x > 0; x++) {
sum += x;
}
I am expecting the loop never end because x will never hit 0.
System.out.println(sum);
It surprises me when it outputs this value -1073741869 , where my x is incrementing instead of decrementing.
I try two function for modular exponentiation for big base return wrong results,
One of the function is:
uint64_t modular_exponentiation(uint64_t x, uint64_t y, uint64_t p)
{
uint64_t res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
For input x = 1103362698 ,y = 137911680 , p=1217409241131113809;
It return the value (x^y mod p):749298230523009574(Incorrect).
The correct value is:152166603192600961
The other function i try, gave same result, What is wrong with these functions?
The other one is :
long int exponentMod(long int A, long int B, long int C)
{
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
long int y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (long int)((y + C) % C);
}
With p = 1217409241131113809, this value as well as any intermediate values for res and x will be larger than 32 bits. This means that multiplying two of these numbers could result in a value larger than 64 bits which overflows the datatype you're using.
If you restrict the parameters to 32 bit datatypes and use 64 bit datatypes for intermediate values then the function will work. Otherwise you'll need to use a big number library to get correct output.
The numeric root of x is computed as follows:
a) Compute the sum, y, of all of x’s (decimal) digits;
b) If y is greater than 10, then set x to y and go to step a). Otherwise, y is x’s numerical root.
Thus, the numeric root of 10, 202 and 875 are 1, 4 and 2, respectively.
Here is my code:
#include <stdio.h>
int main(void)
{
int x, y, index, found;
found = 0;
scanf("%d", &x);
if (x < 0)
{
printf("The input number must be nonnegative.\n");
}
else
{
y = 0;
index = x % 10;
while (found != 1)
{
while (x > 10)
{
y = y + index;
x = (x - index) / 10;
index = x % 10;
}
y = y + index;
if (y < 10)
{
printf("%d\n", y);
found = 1;
}
else
{
x = y;
}
}
}
return 0;
}
My output are always numbers like "-2147483623" etc.
Any help will be appreciated.
Your solution can be greatly simplified with the use of a simple function.
See the following solution using the Ada language:
with Ada.Integer_Text_IO; use Ada.Integer_Text_Io;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
Inpt_Value : Natural;
Root : Natural := 0;
function Find_Root(X : Natural) return Natural is
Value : Natural := X;
Root : Natural := 0;
begin
while Value > 0 loop
Root := Root + (Value mod 10);
Value := Value / 10;
end loop;
return Root;
end Find_Root;
begin
Put("Enter a non-negative integer: ");
Get(Item => Inpt_Value);
Root := Find_Root(Inpt_Value);
while Root > 10 loop
Root := Find_Root(Root);
end loop;
Put_Line(Root'Image);
end Main;
I think that your problem is here :
if(y < 10){
printf("%d\n",y);
found = 1;
}
else{
x = y;
}
in your else statement you assign the value of x to y, but you don't change the value of y.
Maybe you should do something like this :
if(y < 10){
printf("%d\n",y);
found = 1;
}
else{
x = y;
y = 0;
index = x % 10;
}
In your entire program you don't seem to decrease or reset the value of y, that's why once the else block entered, there is no way to enter the if block, because y will increase and always be greater than 10 until an overflow happens, that's why y is negative at the end.
PS: Always use a debugger or printf statements it will help you detect such problems.
My task is
Show on the screen n-element of the progression {xi}.
Xi = Xi-1 - 3Xi-2
X0 = 0
X1 = 2
i = [2,n]
Here is done, but I didn't understand this theme very well, so i need some help with it.
My code(doesn't work):
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int n = Edit1->Text.ToInt();
int i, x;
if(n==0){
i=0;
Label1->Caption = IntToStr(i);
}
if(n==1){
i=2;
Label1->Caption = IntToStr(i);
}
else {
for(i=2;i<=n;i++){
x=(i-1)-3*(i-2);
Label1->Caption = IntToStr(x);
}
}
}
It's not very nessesary to write code in C++ Builder
You misunderstood the progression formula. Xi-1 and Xi-2 refer to previous elements calculated in your progression.
So you need two variables, which will be carrying previous values that you have just calculated. At any given loop, you calculate the current Xi value using the general progression formula, then copy the value of Xi-1 into Xi-2, throwing the previous value of Xi-2. Then you copy the value of Xi (the up to now current value) into Xi-1.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int n = Edit1->Text.ToInt();
int i, x;
int xim1, xim2
if(n==0){
i=0;
Label1->Caption = IntToStr(i);
}
if(n==1){
i=2;
Label1->Caption = IntToStr(i);
}
else {
xim1 = 2;
xim2 = 0;
for(i=2;i<=n;i++){
x = xim1-3*xim2;
xim2 = xim1;
xim1 = x;
}
Label1->Caption = IntToStr(x);
}
}
Given this generating function:
X_0 = 0
X_1 = 2
X_i = X_{i-1} + 3*X_{i-2} i = [2,n]
How would you calculate x_4? We know that X_4 = X_3 + 3*X_2; which means that we need to be able to calculate X_3 and X_2. We can write these as:
X_2 = X_1 + 3*X_0 = 2 + 3*0 = 2
X_3 = X_2 + 3*X_1 = 2 + 3*2 = 8
X_4 = X_3 + 3*X_2 = 8 + 3*2 = 14
This can normally be written as a recursive function:
int calcSeries(int n)
{
if(0 == n)
return 0;
if(1 == n)
return 2;
return calcSeries(n-1) + 3*calcSeries(n-2)
}
BTW, this is a very naive implementation for this series, the main problem is that we have two recursive trees; if you look at the hand expansion of X_4 above notice that X_2 appears twice (in the calculation of X_3 and X_4), but we don't store this value so we need to calculate it twice.
I'm trying to compute ln(x) by Taylor series. Here is my code:
#define N 10
float ln(float x){
int i;
float result;
float xt;
float xtpow;
int sign;
if(x > 0 && x <= 1){
xt = x - 1.0;
sign = -1;
xtpow = 1.0;
result = 0;
for(i = 1 ; i < N + 1; i++ );
{
// Problem here
printf("%d\n", i);
sign = sign * (-1);
xtpow *= xt;
result += xtpow * sign / i;
}
}else if(x >= 1)
{
return -1 * ln(1.0 / x);
}
return result;
}
The problem is with my series cycle(see above). It seems like after 1 cycle variable i becomes equal N + 1, and nothing going on after it. Have you any ideas why it is so?
It seems like after 1 cycle variable i becomes equal N + 1
remove ; after for loop:
for(i = 1 ; i < N + 1; i++ );
^
Your loop continue execute without executing code in block you putted in braces { } after for loop and for loop just increments i till for loop breaks. After the loop code block (where you commented "problem is here") get executes with i = N + 1 value.
I am not sure but I have an additional doubt about conditional expressions in if(). You code pattern is something like:
if(x > 0 && x <= 1){ <-- "True for x == 1"
// loop code
}
else if(x >= 1){ <-- "True for x == 1"
// expression code here
}
So for x == 1 else code never execute. Check this code too.