#include <stdio.h>
#include <math.h>
int Add_Array_Backwards(int * a , int f) /*array a, f is length and is used for exponent multiplication*/
{
for(int i=f-1; i>=0 ; i--) /*i ends at 0, first element of array should not be exponentiated*/
{
int backwards_sum = 0;
backwards_sum = backwards_sum a[i]*(pow(10,i)); //function to flip numbers
}
return backwards_sum; //final sum
}
int main() {
int a[]={1,2,3,4,5};
int f=5;
int x = Add_Array_Backwards(a,f);
printf("%d",x);
}
I Wrote a function that takes the elements within an array, and multiplies them exponentially from the back to the front(the numbers at the front or end of the array are the greatest and appear first in the new integer sum). Meaning that the array will be flipped and added to appear as one number. Ex: An array with {1,2,3,4,5} will return a sum of 54321. The equation "backwards_sum = backwards_sum a[i]*(pow(10,i));" is supposed to take a sum that starts at 0, and is added on to by the elements of the array to resemble the wanted outcome of reversing the array. The math is right and I checked myself, the problem is returning the sum. When I try to return int backwards_sum in place of int x in main, I get this error in output:
/*main.c: In function ‘Add_Array_Backwards’: main.c:11:16: error: ‘backwards_sum’ undeclared (first use in this function)
11 | return backwards_sum; | ^~~~~~~~~~~~~
main.c:11:16: note: each undeclared identifier is reported only once for each function it appears in */
I think there's something wrong with how I implemented the equation syntactically and the compiler is missing a piece of information so it can't return what I want it to. What am I doing wrong?
CodePudding user response:
In c, every variable you declare has a scope
, when you declare a variable in main
, you cannot access it in function f
unless you pass it by "reference", this also applies to loops
.
When you declare a variable inside a loop, it cannot be accessed outside the loop, as such, since you intend to return backwards_sum
, you should move it out of the loop. The "lifespan" of a variable is tied to it's scope, when it leaves the code block of that scope it is rendered unusable.
I'm going to leave a link to a good example of C's variable scope.
CodePudding user response:
Summarizing the feedback provided to the OP:
- the scope of
backwards_sum
is limited to thefor
loop. Trying to return its value triggers the compiler error. The definition ofbackwards_sum
should be moved outside thefor
loop. - using
pow
isn't actually necessary but, in case it is used and depending on the compiler/environment, it might require a-lm
flag added to the compiler's options, to make the linker happy. - The size of the array
a
passed to theAdd_Array_Backwards
function doesn't have to be hardcoded but can be computed usingsizeof
.
The following version of the program addresses item #1 and #3, and uses pow
(to be compiled with the -lm
flag to link against libm.a
):
#include <stdio.h>
#include <math.h>
int Add_Array_Backwards(int * a , int f) /*array a, f is length and is used for exponent multiplication*/
{
int backwards_sum = 0;
for(int i=f-1; i>=0 ; i--) /*i ends at 0, first element of array should not be exponentiated*/
backwards_sum = a[i]*(pow(10,i)); //function to flip numbers
return backwards_sum; //final sum
}
int main() {
int a[]={1,2,3,4,5};
size_t f = sizeof a / sizeof *a;
int x = Add_Array_Backwards(a,f);
printf("%d",x);
}
Alternatively, it is possible to do without pow
, as follows:
#include <stdio.h>
int Add_Array_Backwards(int * a , int f) /*array a, f is length and is used for exponent multiplication*/
{
int backwards_sum = 0;
for(int i=f-1; i>=0 ; i--) /*i ends at 0, first element of array should not be exponentiated*/
backwards_sum = backwards_sum*10 a[i]; //function to flip numbers
return backwards_sum; //final sum
}
int main() {
int a[]={1,2,3,4,5};
size_t f = sizeof a / sizeof *a;
int x = Add_Array_Backwards(a,f);
printf("%d",x);
}