So I had to write a program that used the Pythagorean Threes concept where if you entered a number it would give you all the combinations less than that number that would produce a correct a^2 + b^2 = c^2 output.
Not sure if I explained the assignment well, but basically I understand the logic or at least I think I do I was wondering if you guys could help me find out why I am getting this error....
For the last line of my code it gives me, "warning: control reaches end of non-void function [-Wreturn-type]," As the error any idea what I am doing wrong?
#include <stdio.h>
int main(void){
int x = 0, y = 0, z = 0, n;
int count = 0;
printf("Please Enter A Positive Integer: \n");
scanf("%d", &n);
while(z <= n){
while(y < z){
while(x < y){
if(x * x + y * y == z * z)
printf("%d: \t%d %d %d\n", ++count, x, y, z);
x += 1; }
y += 1; }
z += 1;
}
}
int main(void){
Your function header indicates that you're looking to return an int. To fix this, return a number (0 usually indicates a normal termination) at the end of your function.
To break it down a little,
int indicates the return type,
main is the method name, and
void indicates that there are no parameters for this method.
Your main function is declared to return an int, but you don't return anything.
put return 0; before the closing brace of your main.
int main( void )
{
// ... code ...
return 0;
}
Related
I'm new to C and I'm having some problems in this code where I'm getting these errors.
sum2.c: In function 'main':
sum2.c:22:6: warning: 'z' is used uninitialized in this function [-Wuninitialized]
int z = twice(x, z);
On my code I needed to add a fuction twice, which, given a number, calculates its double, using only the elementary operations and the sum function. And I don't know if the way I put the function is correct.
//USER
//3532
#include <stdio.h>
int sum(int x, int y){
return y == 0 ? x : sum(x+1, y-1);
}
int twice(int x, int z){
z = x * x;
return 0;
}
int main(void){
int x;
int y;
scanf("%d%d", &x, &y);
int z = twice(x, z);
printf("%d\n", z);
return 0;
}
The instructions say that twice is "given a number", but you've defined it as taking two numbers. It only needs one parameter.
And you're supposed to use your sum() function. There's no need to multiply x*x (that's the square of the number, not twice the number), nor is there any point in assigning a variable in the return statement.
You only need to read one number as input to test this.
#include <stdio.h>
int sum(int x, int y)
{
return y == 0 ? x : sum(x+1, y-1);
}
int twice(int x)
{
return sum(x, x);
}
int main(void)
{
int x;
int w;
scanf("%d", &x);
int w = twice(x);
printf("%d\n", w);
return 0;
}
According to your description the function twice should accept only one argument.
So this function definition
int twice(int x, int z)
{
z = x * x;
return 0;
}
does not make sense. Moreover the function always returns 0.
Also take into account that the type int is a signed integer type. The user can input a negative number. In this case your function sum will yield a wrong result.
The sum of two integers of the type int can be too big to fit in an object of the type int. That is there can be for example an overflow.
The functions can be defined the following way as it is shown in a demonstrative program.
#include <stdio.h>
long long int sum( int x, int y )
{
return y == 0 ? ( long long int )x
: sum( y < 0 ? x - 1 : x + 1, y < 0 ? y + 1 : y - 1 );
}
long long int twice( int x)
{
return sum( x, x );
}
int main(void)
{
int x;
printf( "Enter a number: " );
scanf("%d", &x);
long long int result = twice( x );
printf("The sum is %lld\n", result );
return 0;
}
Its output might look like
Enter a number: -5
The sum is -10
Or
Enter a number: 5
The sum is 10
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <locale.h>
int main()
{
setlocale(LC_ALL, "Rus");
float arr[25];
int i;
double x;
int a, b, c, e, f;
double y = a * pow(x, 2) + b * x + c;
printf("a: ");
scanf_s("%d", &a);
printf("b: ");
scanf_s("%d", &b);
printf("c: ");
scanf_s("%d", &c);
printf("e: ");
scanf_s("%d", &e);
printf("f: ");
scanf_s("%d", &f);
double interval = (f - e) / 25.0 ;
for (int i = 0, double x = e; i < 25; i++, x += interval)
{
printf("%f", y);
x++;
}
system("pause");
}
I get [Error] expected identifier or '(' before 'double'. How can i fix it? It doesnt seem like i really need to change something in
for (int i = 0, double x = e; i < 25; i++, x += interval)
or maybe im wrong and dont know how to write multiple conditions.
Yeah, you can't do that.
By the way, those are declarations, not conditions. Only the middle part of a for loop is a condition.
You can declare multiple variables in the first part of a for loop, but only if they have the same base type (e.g. int):
for (int x = 1, y = 2, z = 3; ...; ...)
The workaround in your case is to declare at least one of the variables outside of the loop:
{ // this outer block limits the scope of x
double x = e;
for (int i = 0; i < 25; i++, x += interval)
{
printf("%f", y);
x++;
}
}
That said, your code doesn't really make sense. Your loop doesn't use x, so there's no point in setting it. On the other hand, the value you're printing 25 times (y) doesn't change in the loop. It's set at the top of your main function, computed from a different x variable that is uninitialized.
You should move the declaration and initialization of y into the loop and delete the outer x. See also https://stackoverflow.com/a/53238897/1848654.
You can't define variables with multiple types with the comma:
for(int i = 0, double x...
Instead:
x = e;
for (int i = 0; i<...
and the x is already defined above.
You could embedded them into a struct. I do not recommend it because IMO it is not a good coding practice as it is not easy to understand (at first sight)…
typedef struct {int i; double x;} S;
for (S yourStruct = {0,e}; yourStruct.i < 25 ; yourStruct.i++, yourStruct.x += interval)
{
printf("%f", y);
yourStruct.x++;
}
It is not really a matter of the for loop:
void f(void)
{
int x, y; // legal - and you can do the same in a for loop's initialization section
int z, double d; // can't do that either, even outside for loop...
};
All variables that you declare in a single expression need to have the same (base!) type, be it within for loop, function body or global. 'Base type': Well, because you legally can do stuff like int x, *p;, with x and p being of different type, but base/underlying type in both cases is int.
My issue is that I keep getting the same type of error and I can not understand why. I'm fairly sure I declared and defined it before the main function.
Here is my code.
#include <stdio.h>
void functn (int x);
int functn(int x, int result){
result = (x-1)+2;
if (x <= 0){
return 0;
}
else{
return result;
}
}
int main (){
int x, y;
printf ("Enter the value of x: ");
scanf ("%d", &x);
y = f(x);
printf ("%d", y);
return 0;
}
You have a number of problems in functn. Primarily passing result. When you pass result to functn, functn receives a copy of result and the only way to get the modified value is to return the modified value. You can also pass a pointer to result and update *result in functn (similar to what you do now), that would eliminate the need to return a value, as any changes to result would be visible back in the calling function (main() here). Further, there is no need for global variables. Simply declare the variables local to main and pass as parameters, as required.
The following example declares functn (simply f below) to do both, take a pointer to result (e.g. &result) as a parameter, while also returning result, which allows you to either assign the return or not, but always have the updated value for result back in main, e.g.
#include <stdio.h>
int f (int x, int *result);
int main (void){
int x, y, result = 0;
printf ("Enter the value of x: ");
if (scanf ("%d", &x) != 1) {
fprintf (stderr, "error scanf, invalid conversion.\n");
return 1;
}
y = f (x, &result);
printf ("y = f(%d) => %d\n", x, y);
return 0;
}
int f (int x, int *result)
{
*result = (x-1)+2;
if (x <= 0)
return 0;
return *result;
}
Example Use/Output
$ ./bin/yfx
Enter the value of x: 5
y = f(5) => 6
Look things over and let me know if you have questions.
Return type of functn() different in declaration and definition. So, use
int functn(int x);
instead of
int functn(int x, int result)
Remove int result from function argument and declare inside function. like,
int functn(int x)
{
int result = 0;
result = (x-1)+2;
if (x <= 0){
return 0;
}
else{
return result;
}
}
Also, correct function call, like
y = functn(x);
instead of
y = f(x);
When I run my code, for Y I am consistently getting the value -2147483648, regardless of what value y was fed into my equation.
Here is my code.
#define MAX 1000
#define EQ(y) ((2*(pow(y, 4)))+1)
int check(int value);
int main()
{
int i, y, x;
for(y = 1; y < MAX; y++)
{
i = EQ(y);
if(check(i))
printf("combination found: x = %d, y = %d", sqrt(i), y);
}
}
int check(int value)
{
int x = sqrt(value);
if ((x*x) == value)
return 1;
else
{
return 0;
}
}
After reviewing my code, I realized my problem was with my "int x = sqrt(value)". Aside from the problem with the "value" variable being an int, of course, a bogus value was still being returned due to the fact that the purpose of check is to evaluate whether or not (2*(pow(y, 4)))+1) returned a perfect whole square for any given value of y, and this was not possible due to variable x in check(double value) being datatype integer.
UPDATE: I rewrote my code as follows. I still don't get any correct returns
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*
* the solution I implemented basically involved dropping x from the equation, solving for y, checking to see if it has a
* perfect square root. if it does, then x = the squareroot of y in the function EQ.
* original problem: for equation x^2 - 2y^4 + 1 = 0, find all possible solutions up to arbitrary maximum
*/
#define MAX 100000
#define EQ(g) (((pow(g, 4.0)))+1)
int check(double value);
int main()
{
int y, x;
double i;
for(y = 1; y < MAX; y++)
{
i = EQ(y);
if(x = check(i) > 0)
printf("combination found: x = %d, y = %d\n", y, x);
}
}
int check(double value)
{
double x = sqrt(value);
int n = (int) x;
printf("%d\n%f\n%f\n", n*n, value, x);
if (n*n == value)
return n*n;
else
return 0;
}
Read the comments are the top of my code, and the purpose for this selection should be pretty obvious.
You don't have a prototype for double pow(double, double); so the compiler implicitly assumes its signature is int pow(int, int);. Not good!
The solution is to #include the appropriate header at the top of your .c file.
#include <math.h>
Make sure you enable warnings, and if they're already enabled, pay attention to them! Your compiler should warn you about the missing prototype. (It should also spit out a similar warning for printf.)
pow() returns double and you are using integer i to store the return value.
Due to type promotion during expression evaluation the expression:
((2*(pow(y, 4)))+1)
will give a double value and you are storing this in integer type which will give unexpected results.
In reference to your updated question, this line:
if(x = check(i) > 0)
needs to be parenthesized:
if((x = check(i)) > 0)
This is the declaration of pow:
double pow(double x, double y)
Which means it operates in double. By using int instead, variable y is overflowing.
so I have to write a recursive algorithm for exponentiation and I have to use this to make the algorithm faster: and then I'd have to figure out how many time multiplication is happening. I wrote it, but I am not sure if I am right - also I need some help with figuring out the multiplication part.
#include <stdio.h>
#include <math.h>
double intpower(double x, int n)
{
double result;
if(n>1&&n%2!=0) {result=x*intpower(x,(n-1)/2)*intpower(x,(n-1)/2);}
if(n>1&&n%2==0) {result=intpower(x,n/2)*intpower(x,n/2);}
if(n==1) return x;
else return result;
}
int main()
{
int n;
double x,result;
printf("x\n");
scanf("%lf", &x);
printf("n\n");
scanf("%d", &n);
printf("result = %.2f\n", intpower(x,n));
return 0;
}
The inductive definitions are saying
If k is even, then x^k = [ x^(k/2) ] ^ 2
If k is odd, then x^k = x * [ x^(floor(k)/2) ] ^ 2
With these it's a bit easier to see how to arrange the recursion:
#include <stdio.h>
double int_pwr(double x, unsigned k)
{
if (k == 0) return 1;
if (k == 1) return x; // This line can be omitted.
double y = int_pwr(x, k/2);
return (k & 1) ? x * y * y : y * y;
}
int main(void)
{
double x;
unsigned k;
scanf("%lf%u", &x, &k);
printf("x^k=%lg\n", int_pwr(x, k));
return 0;
}
I've changed types to be a bit more logical and saved an exponential (in k) amount of work that the OP's solution does by making two recursive calls at each level.
As to the number of multiplications, it's pretty easy to see that if k's highest order bit is 2^p (i.e. at position p), then you'll need p multiplications for the repeated squarings. Another way of saying this is p = floor(log_2(k)). For example if k=4=2^2, you'll square the square to get the answer: 2 multiplications. Additionally you'll need q-1 more, where q is the number of 1's in k's binary rep. This is the number of times the check for "odd" will be true. I.e. if k = 5 (which has 2 bits that are 1's), you'll square the square and then multiply the result by x one more time. To summarize, the number of multiplications is p + q - 1 with p and q as defined above.
To figure out how many times multiplication is happening, you could count them in intpower().
static int count = 0;
double intpower(double x, int n) {
double result;
if(n>1&&n%2!=0) {result=x*intpower(x,(n-1)/2)*intpower(x,(n-1)/2); count += 2;}
if(n>1&&n%2==0) {result=intpower(x,n/2)*intpower(x,n/2); count++;}
if(n==1) return x;
else return result;
}
int main() {
int n;
double x,result;
printf("x\n");
scanf("%lf", &x);
printf("n\n");
scanf("%d", &n);
mcount = 0;
printf("result = %.2f\n", intpower(x,n));
printf("multiplcations = %d\n", mcount);
return 0;
}
Try this
double intpower(double x, int n)
{
if(n == 0) return 1;
if(n == 1) return x;
if(n%2!=0)
{
return x*intpower(x,(n-1));
}
else
{
x = intpower(x,n/2);
return x*x;
}
}
or you can reduce your function to one line
double intpower(double x, int n)
{
return n == 0 ? 1 : n%2 != 0 ? x*intpower( x, (n-1) ) : (x = intpower(x, n/2), x*x);
}