long i, b = get();
equals to
long i;
long b = get();
or
long b = get();
long i = b;
?
I'm totally new to c
It is first option
long i;
long b = get();
You would find it out faster by trying then asking on SO.
It's called operator ,.
In this case both expressions are evaluated, but only second's value is returned.
int x = 5;
while (--x, x > 0)
{
printf("%d,", x);
}
has output
4,3,2,1,
This code is same as
--x;
while (x > 0)
{
printf("%d,", x);
--x;
}
Related
I working through a book on C on my own. This isn't homework to be turned in. I am writing a C program to determine the largest Fibonacci number my machine can produce. And instructed to use a nonrecursive method.
My Code:
#include<stdio.h>
double fibo(int n);
int main(void)
{
int n = 0; // The number input by the user
double value; // Value of the series for the number input
while (n >= 0)
{
// Call fibo function
value = fibo(n);
// Output the value
printf("For %d the value of the fibonacci series = %.0f\n", n,
value);
n++;
}
return 0;
}
double fibo(int n)
{
int i; // For loop control variable
double one = 0; // First term
double two = 1; // Second term
double sum = 0; // placeholder
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
{
for (i = 2; i <= n; i++)
{
sum = one + two;
one = two;
two = sum;
}
}
return sum;
Code works fine but I want to to break when the output gives me the fist instance of :
For 17127 the value of the fibonacci series = inf
Is there way to us an if statement like:
if (value == inf)
break;
The simplest is to use INFINITY or isinf().
Just did a little search and found this nice trick:
...
double value, temp; // Value of the series for the number input
while (n >= 0)
{
// Call fibo function
temp = fibo(n);
if (temp - temp != 0)
break;
else
value=temp;
...
well it turns out that whats happening is when temp hits Inf the if condition temp - temp produces Nan which equals nothing and the rest is just executing break; to exit the process.
I want to to break when the output gives me the first instance of : inf
Simply test against INFINITY from <math.h>. The output will not be an exact Fibonacci number.
#include <math.h>
#include <stdio.h>
int main(void) {
double a;
double b = 0;
double c = 1;
do {
a = b;
b = c;
c = a + b;
} while (c < INFINITY);
printf("%e\n", b);
return 0;
}
Output
1.306989e+308
long double
Use the widest floating point type and look for an inexact addition.
#include <fenv.h>
#include <stdio.h>
int main(void) {
long double a;
long double b = 0;
long double c = 1;
do {
a = b;
b = c;
c = a + b;
} while (fetestexcept(FE_INEXACT) == 0);
printf("%.0Lf\n", b);
return 0;
}
Output
12200160415121876738
Integers
Use the widest type available. This is akin to #Syed.Waris unsigned long long approach. Although common that unsigned long long and uintmax_t have the same range, using uintmax_t insures the widest.
uintmax_t: The following type designates an unsigned integer type capable of representing any value of any unsigned integer type:
#include <stdint.h>
#include <stdio.h>
uintmax_t a;
uintmax_t b = 0;
uintmax_t c = 1;
do {
a = b;
b = c;
c = a + b;
} while(c >= b);
printf("%ju\n", b);
Output
12200160415121876738
String
An alternative to double or some int type, is to create a simple string add function str_add(), then quite easy to form large Fibonacci numbers.
int main(void) {
char fib[3][4000];
strcpy(fib[0], "0");
strcpy(fib[1], "1");
int i;
for (i = 2; i <= 17127 && strlen(fib[1]) < sizeof fib[1] - 1; i++) {
printf("Fib(%3d) %s.\n", i, str_add(fib[2], fib[1], fib[0]));
strcpy(fib[0], fib[1]);
strcpy(fib[1], fib[2]);
}
printf("%zu\n", strlen(fib[2]));
return 0;
}
Output
Fib(1476) 13069...(299 digits)....71632. // Exact max `double`
Fib(17127) 95902...(3569 digits)...90818.
largest Fibonacci number my machine can produce
This question is not concerned with any data type but it is concerned with machine.
The basic rule of fibonacci is this:
n = (n-1) + (n-2)
You can take a big sized unsigned long long variable and you can keep on adding. But what if that datatype is overflowed? You are not concerned with data type. Your machine may produce a number even bigger than the long long. What would that number be ? Entire bits on RAM? Hard Disk ?
Since you are required to use an iterative method and not recursive method, your teacher/book/instructor might be testing you on loops (and not any standard API). Below is sample code using unsigned long long:
#include <stdio.h>
int main ()
{
unsigned long long a = 0;
unsigned long long b = 1;
unsigned long long c = a + b;
while(c >= b)
{
a = c;
c = b + c;
b = a;
}
printf("\n%llu\n", b);
return 0;
}
Output:
12200160415121876738
So, for my assignment, I'm supposed to code a function that takes 2 unsigned arguments and outputs their product.
unsigned multiply( unsigned a, unsigned b );
For instance,
multiply(3, 4)
should return 12
The thing is, I'm not allowed to use +, -, /, *, or % operators. I'm only allowed to call functions, and increment/decrement with ++ and --.
I have another function already made to add 2 arguments:
unsigned add(unsigned a, unsigned b)
{
if (a > 0)
add(--a, ++b);
else return(b);
}
and I'm allowed to call this, along with any helper functions I need.
I've spent the past 30 minutes trying out various permutations but I just cannot get the math right; the closest I've come is getting b to double itself a times, but that's not going to cut it. Any ideas?
Edit: Forgot to mention! For/while loops aren't allowed either
unsigned add(unsigned a, unsigned b){
if (a > 0)
return add(--a, ++b);
else
return b;
}
unsigned multiply( unsigned a, unsigned b ){
if( a > 0)
return add(b, multiply(--a, b));
else
return 0;
}
int multiply (int a, int b)
{
int result = 0;
for (int i = 0; i < a; i++) { //You repeat "a" times...
for (int j = 0; j < b; j++) { //...adding "b" to result.
result++;
}
}
return result;
}
Of course if recursivity is mandatory, this won't works.
i didn't compile it but i`m sure its working
mutiply(3, 4, 0)
int mutiply(int a, int b, int result) {
if(a > 0) {
for(int i=1;i<=b;i++)
result++
mutiply(--a, b, result);
} else return (result == 0 && a == 0) ? 0 : result;
}
Here is a tail-recursive solution that uses a helper function with an accumulator. This is essentially the same as the solution using nested for loops:
unsigned multiply(unsigned a, unsigned b)
{
return mult_helper(a, b, 0, a);
}
unsigned mult_helper(unsigned a, unsigned b, unsigned acc, unsigned reset)
{
if (b == 0)
return acc;
if (a > 0)
return mult_helper(--a, b, ++acc, reset);
return mult_helper(reset, --b, acc, reset);
}
I'm having a difficult time figuring out how'd I'd accomplish a task and am looking for help.
So, I need to use a function that'll find a pair of numbers (to be allocated dynamically) that meets a condition and returns a pointer to it.
Here's what I have:
int* f_cubes_sum(int n)
{
//Declaring array
int *numSet;
numSet = (int *)malloc(2); // Only two because I'm looking for a pair
//Declaring variables
int sum = 0;
int a = 0;
int b = 0;
bool results = false;
while (b < n && results == false)
{
while (a < n)
{
sum = a^3 + b^3;
if (sum == n)
{
results = true;
}
else
{
a = a + 1;
}
sum = 0;
}
if (results = false)
{
a = 0;
b = b + 1;
}
}
if (results = false)
{
a = NULL;
b = NULL;
}
numSet[0] = a;
numSet[1] = b;
free(numSet);
return numSet;
}
And in my main function, how would I access both numbers?
Thank you for your time
You are looking for a function that determines whether a number can be written a the sum of two cubes and if so, it should yield the two numbers whose cubes are summed. Basically you need:
an information on whether n can be written as a³ + b³ and
if so, the values of a and b.
You can use your approach of allocating an array of two integers and return that on success and NULL on failure. If you do so, you should allocate the array only if you have a result:
int *f_cubes_sum(int n)
{
int a, b;
// logic to work out whether a³ + b³ == n
if (a*a*a + b*b*b == sum) {
int *res = malloc(2 * sizeof(*res));
res[0] = a;
res[1] = b;
return res;
}
// no solution found
return NULL;
}
The drawback here is that the calling code has to free the returned pointer:
int *res = f_cubes_sum(2778);
if (res) {
printf("%d, %d\n", res[0], res[1]);
free(res);
}
Another approach is to pass in an array that your code has to fill and indicate success or failure with a boolean return value:
bool f_cubes_sum(int n, int res[2])
{
int a, b;
// logic to work out whether a³ + b³ == n
if (a*a*a + b*b*b == sum) {
res[0] = a;
res[1] = b;
return true;
}
// no solution found
return false;
}
Now the calling code has to provide the space for the result:
int res[2];
if (f_cubes_sum(2778, res)) {
printf("%d, %d\n", res[0], res[1]);
}
Of course, because you are always dealing with two possible result values, you could also pass pointers to these values instead of passing an array. Here's a variant of your function that does this:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
bool f_cubes_sum(int n, int *pa, int *pb)
{
int a = 0;
while (1)
{
int a3 = a*a*a;
if (a3 > n) break;
int b = cbrt(n - a3) + 0.5;
int b3 = b*b*b;
if (a3 + b3 == n) {
*pa = a;
*pb = b;
return true;
}
a++;
}
return false;
}
int main(void)
{
int a, b;
int n = 35001;
if (f_cubes_sum(n, &a, &b)) {
printf("%d^3 + %d^3 == %d\n", a, b, n);
} else {
printf("Nothing found for %d.\n", n);
}
return 0;
}
Also note what commenters have alreadey pointed out:
In C, = assigns, == compares. Unfortunately, an assignment inside an iF condition is valid C: It will assign the value and then test it, entering the clause if it isn't 0 or false. Therefore if (x = false) never enters the if clause. Switch on warnings to carch such mistakes.
The ^ operator isn't the power operator, it is the bitwise xor operator. Raising a number a to the power of b is done with the floating-point function pow(a, b). If the exponent is a known small integer, it is usually better to write the multiplication explicitly. Thus, pow(a, 3) is rendered better as a*a*a.
When allocating memory, be sure the make enough room for the desired type. Also don't use freed pointers; the memory they point to is invalid.
I try to figure out what I did wrong here, basically the function is for calculating the module inverse number to a-1 mod m. but program is getting errors.
can somebody point out what is causing the errors?
//calculating the inverse of the public key, for getting private key d. long
long long inverse(long long a, long long m)
{
long long p = a, q = m, t;
//Euclidean algorithm
long long x = 0, y = 1, z = (long long)q/p;
//start recursion
while(p != 1 && q != 1)
{
t = p;
p = q % p;
q = t;
t = y;
y = x - y * z;
x = t;
z = (long long)q/p;
}
y = (long long)y % m;
if(y < 0)
{
y += m;
}
//return inverse number;
return y;
}
The problem here is either of the below:
You have a mismatched forward declaration with the function definition, as mentioned rightly by Mr. #iharob in his answer.
You don't have a forward declataion at all. The function has been used (called) before it has been defined. So, the implicit declaraion (int return, any number of argument acceptance) is conflicting with the actual definition.
You declared a function prototype that doesn't match the function definition, example of how you can do that, put at the top of the file or in a header file the following
int inverse(int a, int n);
and then your definition is
long long inverse(long long a, long long n)
{
}
Suppose you input A = 15, B = 6, The answer is 18. What algorithm do I need?
This is what I try, but it doesn't work:
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
for ( ; a % b != 0; a++ ) {
if ( a % b == 0 ) {
printf("%d\n", a);
return a;
}
}
return 0;
}
I get infinite loop.
The question (now) asks for:
The next multiple of b that is not less than a?
Using your notation of a and b, you can write it directly like this:
int NextMultipleUp(int a, int b)
{
int r = a % b;
if (r == 0)
return a;
else
return a + b - r;
}
The question originally asked for
The next multiple of a that is not greater than b
And for that the answer is
int NextMultipleDown(int a, int b)
{
return b - b % a;
}
This was the answer for which the original comments applied to.
return (((a-1) / b )+1) * b;
Always returns a multiple of b. Increment the integer dividend to get a multiple that is larger than the original a - subtract one from the original, because we want 'not less than' rather than 'greater than' a
I think that you want a do - while iterative loop, so I will give an alternative answer.
How to find next multiple of B that is not less than A?
Obviously this code is slower than David's.
int main ( void ){
int a, b, c;
long int result;
scanf("%d %d", &a, &b);
c = 0;
do {
result = b * c;
c++;
} while ( result < a );
printf( " The number is: %d \n", result );
}
int nextMultiple(int a,int b)
{
if(a%b == 0) return a;
return a+(b-(a%b));
}
so if a=15 b=6
the ans is
=15+(6-(15%6))
=15+(6-(3))
=15+3
=18