Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 days ago.
Improve this question
Why when I run this code b in the end transforms to 1410065408?
But 1 step before b was 100000000000 and it should be the same
(It's calculator from dec form to binnar)
P.S. all program steps I watched in C visualize
#include<stdio.h>
int binnar(int a){
long long int b = 0, x = 1;
while(a != 0){
b += (a % 2) * x;
x *= 10;
a = a/2;
}
return b;
}
void main(){
int a = 1024;
printf("%lld", binnar(a));
}
I expect that b will be 100000000000, not 1410065408
% is the modulo operator (returns the remainder)
What happens here is
a=1024, a%2=0, x=1, b=0
a=512, a%2=0, x=10, b=0 (b is not incremented because a%2 is 0)
and it continues until
a=1, a%2=1, x=100000000000 BUT because the return type of binnar is an int, it does not work the way you want it to, and it overflows. Try changing the return type of binnar to a long long and see if it works as you intend.
edit: added code
#include<stdio.h>
long long int binnar(int a){
long long int b = 0, x = 1;
while(a != 0){
b += (a % 2) * x;
x *= 10;
a = a/2;
}
return b;
}
void main(){
int a = 1024;
printf("%lld", binnar(a));
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I found a new primality test below which determines if 1000000007 is prime.
How does its speed compare to other existing primality algorithms?
Does it win the award for most "computationally worthless" primality test?
Thanks.
EDIT
Was able to improve speed using this method described here:
https://math.stackexchange.com/questions/3979118/speedup-primality-test
"So it's enough to go from x=n/2 up to n/2+√n/2. With this improvement, your algorithm will still be somewhat slower than your isPrimeNumber routine--just because calculating a gcd is slower than calculating divisibility. This will be feasible for testing numbers with maybe 15-20 digits, but you would need completely different methods to test something much larger, like the 183-digit number you mention."
// Primality Test
// Every n is prime if all lattice points on x+y=n are visible from the origin.
#include <stdio.h>
#include <stdint.h>
#include <math.h>
uint64_t gcd(uint64_t a, uint64_t b)
{
return (b != 0) ? gcd(b, a % b) : a;
}
int isPrimeNumber(uint64_t n)
{
if (n == 1) return 0;
if (n == 2 || n == 3) return 1;
if (n % 2 == 0) return 0;
// Start near line x=y.
uint64_t x = (n / 2) + 2;
uint64_t y = n - x;
uint64_t count = sqrt(n) / 2;
for (uint64_t i = 0; i < count; ++i) {
// Check lattice point visibility...
if (gcd(x, y) != 1) return 0;
x++; y--;
}
return 1;
}
int main(int argc, char* argv)
{
uint64_t n = 1000000007;
if (isPrimeNumber(n) == 1)
{
printf("%llu prime.", n);
}
else
{
printf("%llu not prime.", n);
}
return 0;
}
When you write any code, you should do basic debugging to verify that your code does what you think it does. Run your code on a few small numbers; print values of x and y to verify that it does the correct checks.
In addition to this, if you mix integers and floating-point variables, you should be careful: implicit conversions e.g. from float to unsigned can lead to data loss and completely incorrect calculations. Compilers usually warn about this; you should compile with all warnings enabled -Wall and pay attention to what the compiler says.
It looks like you should always have x + y = n during your computations - this is your invariant. This can be more easily expressed like this:
// initialization
x = n / 2;
y = n - x;
// it should be evident that your invariant holds here
do {
...
x++; y--;
// your invariant holds here too, by induction
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm receiving Output: 1. I should count the number of times a digit appear in an integer, for example, for number 1222345 and n = 2 Should appear 3 times.
int countOccurrences(int n, int num)
{
int i,k;
i=0;
while(num!=0)
{
k=num%10;
num=num/10;
if(k==n)
{
i++;
}
}
}
// Main
void main()
{
int num= 1222345;
int n = 2;
printf("Occurance of a number: %d", countOccurrences(n,num));
}
You have undefined behavior in the code. The function is supposed to return an int and it didn't.
Solution is to add return i in the end of other function. This will give you correct result. In the countOccurrences() function
...
if(k==n)
{
i++;
}
}
return i;
}
I was skipping the discussion of error check and all that. As chux mentioned for n<=0 case you might want to add a different way of handling it but you didn't add it. Atleast consider those case and put an error message on whatever input you need.
Some corner cases are
n=0,m=0.
Negative value of n or m.
Put a return on your countOccurrences function please
int countOccurrences (int n, int num) {
int i, k;
i = 0;
while (num! = 0)
{
k = num% 10;
num = num / 10;
if (k == n)
{
i ++;
}
}
return i; }
As other have pointed out, there are important issues with your code.
Here is a recursive solution that you may find interesting:
int countOccurrences(int n, int num)
{
int count = ((num % 10) == n);
return (num < 10) ? count : count + countOccurrences(n, num / 10);
}
Few general remarks about your code:
When using printf(), you should #include <stdio.h>.
main() should return int.
Place spaces around operators and format your code consistently. This k = num % 10; is more readable than k=num%10;. (There's more to code formatting than a matter of taste; without spaces you create areas full of characters which are more difficult to parse for our visual system.)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Is this implementation of random number generation wrong?
int random_number(int l, int u)
{
int num;
num = rand()%u;
if (num<l && num>u)
{
while(num<l && num>u)
{
num = rand()%u;
}
}
return num;
}
This is not giving me the correct answer.
If I try random_number(4,8); it generates numbers like 0,1,2 etc.
Assuming u means upper and l means lower, then it is wrong. Try this:
int random_number(int l, int u) {
int num = rand() % (u - l);
return num + l;
}
Consider the lines.
int random_number(int l, int u)
num = rand()%u // result 0, 1, 3, .... 7
if (num<l && num>u)
random_number(4,8);
Code needs to follow the if() when num <4 and num > 8. An int cannot both be less than 4 and greater than 8 at the same time.
The usual idiom is
int random_number(int lower, int upper) {
int num;
num = rand()%(upper - lower + 1) + lower;
return num;
}
Extra code is needed to cope/detect upper < lower, upper - lower >= RAND_MAX
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am rather new in C and i was trying to solve some exercises in my textbook and encountered a wierd Problem. although my Task is undeniably easy the program just wont work right. after some Trials it seems that the fault is on the Compiler but as much as this sounds unreasonable its the only justification i can bring up
without any further delay here is the code
#include <stdio.h>
double power(double n, int p);
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf("%lf%d", &x, &exp) == 2)
{
xpow = power(x,exp);
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double pow = 1;
int i ;
if ( n == 0 && p == 0)
{
printf("0 to zeroth power is undefined\nwe will use therefor 1 instead\n");
p = 1 ;
}
if (p >= 0)
{
for (i = 1 ; i <= p ; i++);
pow *= n ;
}
else
{
for (i = -1 ; i >= p ; i--);
pow *= 1/n ;
}
return pow;
}
the programs intent is clear the problem is when I input some test cases the output is wrong
like
(5 2)
the output should be 25 but I get 5
(5 6)
the output should be 15625 but i get 5
after examining this problem with gdb I found that instead of initializing i to 1 it is initialized to 3 for no obvious reason and with the second input i is initialized to 7
i want to know why
I'm using gcc.
Why do you blame the compiler, I would blame my eyes first you have an extra semicolon in both your for loops
for (i = 1 ; i <= p ; i++);
change it to
for (i = 1 ; i <= p ; i++)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example:
1: 123456 should be converted to 214365
2: 12345 should be converted to 103254
int32_t conv(int32_t n){
char buff[16], *p = &buff[1];
int i, v = n < 0 ? -n : n;
sprintf(p, "%010d", v);
for(i=0;i<5;++i,++p){
char c;
c = *p;
*p = p[1];
*(++p) = c;
}
buff[0] = (n < 0) ? '-' : ' ';
return atoi(buff);
}
size_t conv(size_t n){
size_t q, r, wk, mul=1, ret = 0;
for(;n;n/=100, mul*=100){
wk = n % 100;
r = wk % 10;
q = wk / 10;
ret += (r * 10 + q)*mul;
}
return ret;
}
,
First to clarify: there's a difference between an integer and the decimal representation of that integer in a string. You want the second.
As H2CO3 points out you can do this on your own using modulo's: decompose your number into the decimal base, do the swaps then recompose the number (only mul/add).
Here's another way:
Create a string from your integer using malloc and sprintf.
Loop through the string and do the swapping of characters
print that string or if you want convert it back to an integer with atoi