1. ## functions using arrays

I too am making a calculator and I looked the other threads about calculators and mine is not that complicated because we haven't learned some of those things but anyway. we have to do ours in functions. i've wriiten these functions using arrays because the input is coming at a max of 20 for all functions accept the power function. I think my power function is wrong because it's not doing the right thing also i think my other functions are missing something because it seems to easy. any ideas?thank you.
Code:
```float power_func(float firsval, int seconval){
int count;
float powerval;
if (firsval < 0){
firsval = -1;
seconval= 1/seconval;
}
for(count = 0; count < firsval; count++){
powerval *= seconval;
}
return powerval;
}

int count;
for(count = 1; count < MAX; count++){
}
return sum;
}

float subtract_arr(float subarry[]){
int count;
float difference = subarry[0];
for(count = 1; count < MAX; count++){
difference -= subarry[count];
}
return difference;
}

float multiply_arr(float multarry[]){
int count;
float product = multarry[0];
for(count = 1; count < MAX; count++){
product *= multarry[count];
}
return product;
}```

2. I have a large widescreen monitor set the maximum resolution and I still can't read your post without scrolling. Fix it please!
Hilight the text and you'll see the problem. Why the hell are there so many spaces after the text on every line?

Turn on your compiler warnings and pay attention to them. They should get you most of the way to fixing this.

3. ## retry

Sorry...i have all of the errors and warnings on that my
teacher told us to use (-ansi, -pedantic,-errors,-Werror,-Wall)
I too am making a calculator and I looked the other threads
about calculators and mine is not that complicated because
we haven't learned some of those things but anyway.
we have to do ours in functions. i've wriiten these functions
using arrays because the input is coming at a max of 20 for
all functions accept the power function. I think my power
function is wrong because it's not doing the right thing
also i think my other functions are missing something. any ideas?
thank you.

Code:
```float power_func(float firsval, int seconval){
int count;
float powerval;
if (firsval < 0){
firsval = -1;
seconval= 1/seconval;
}
for(count = 0; count < firsval; count++){
powerval *= seconval;
}
return powerval;
}

int count;
for(count = 1; count < MAX; count++){
}
return sum;
}

float subtract_arr(float subarry[]){
int count;
float difference = subarry[0];
for(count = 1; count < MAX; count++){
difference -= subarry[count];
}
return difference;
}

float multiply_arr(float multarry[]){
int count;
float product = multarry[0];
for(count = 1; count < MAX; count++){
product *= multarry[count];
}
return product;
}```

It's pointlessly padding every single line with trailing spaces out to 144 characters.
That's why your code triggers horizontal scroll bars.

5. (1) In power_func(), it appears you have firsval and seconval's roles reversed. seconval would need to be the exponent, since it's declared to be an int.

(2) powerval is never set to an initial value, so when you multiply by seconval, powerval becomes some unknown value.

(3) It would be better if the array functions were passed a count of the number of items in the array, instead of using a global like MAX. That way it could operate on any size array, and also maybe the whole array isn't filled, but only part of the array.

(4) Unless you were instructed to use floats, I would use doubles instead, as this is the type used by most functions in the C math library.

6. sorry once again but i don't know how to fix my editor. this doesn't happen at my computer at home. if you tell me how then i will

7. Originally Posted by trprince
sorry once again but i don't know how to fix my editor. this doesn't happen at my computer at home. if you tell me how then i will
What exactly do you do to copy'n'paste the text into the page here. Are you by any chance copying and pasting out of a command prompt window? In that case, perhaps you could copy'n'paste through for example notepad instead - copying from command prompt will make the entire line of spaces that are "after the end of your text" be spaces - which makes the text REALLY WIDE.

--
Mats

8. does this way work to do the power_func?
and for the others is sizearr better to use then the global MAX?
Do i have that correct?
THanks

Code:
```float power_func(float firsval, int seconval){
int count;
float powerval =0.0;
if (seconval < 0){
firsval = -1;
seconval= 1/seconval;
}
for(count = 0; count < seconval; count++){
powerval *= firsval;
}
return powerval;
}

int count, sizearr;
for(count = 1; count < sizearr; count++){
}
return sum;
}```

9. oh ok..i see..i will fix it

10. > this doesn't happen at my computer at home.
So you're not at home now, but you're copy/pasting from some ssh terminal window perhaps?

11. > firsval = -1;
I don't think you want to make firsval equal to -1. If you do that, you've lost the value you want raised to the power.

>and for the others is sizearr better to use then the global MAX?
Don't you think sizearr should be something passed to the function? So the caller can do:
Code:
`sum = add_arr(array, num_elements);`

12. Originally Posted by trprince
does this way work to do the power_func?
and for the others is sizearr better to use then the global MAX?
Do i have that correct?
THanks

Code:
```float power_func(float firsval, int seconval){
int count;
float powerval =0.0;
if (seconval < 0){
firsval = -1;
seconval= 1/seconval;
}
for(count = 0; count < seconval; count++){
powerval *= firsval;
}
return powerval;
}

int count, sizearr;
for(count = 1; count < sizearr; count++){
}
return sum;
}```
Ehm, the sizearr should be an argument to the function. As it is now, it's a "random" number based on what happens to be in that location of memory - which is most likely not the size of your array.

You set powerval to zero, then multiply it. What's zero multiplied by something?

Seconval is an integer. "Inverting" that by 1/x is leading to zero if abs(x) != 1 - since it's an integer divide. I agree with the other poster that said "you've got your values swapped"

--
Mats

13. ## fixed editor

ok so i think i may have fixed it. here is the functions that I am working on

Code:
```void fill_arr(float farry[], int *size){
int fills = 0;
while (fills < *size && scanf(“%f”, &farry) !=EOF)
fills ++;
*size = fills;
}
}

float ascend_arr(float aarry[]){
int ascend, sizearr;
float order;
int next = aarry[0];
for(ascend = 1; ascend < sizearr; ascend ++){
if (next < aary[ascend])
order = 1;
else
order = -1;
}
return (order);
}

float descend_arr(float darry[]){
int descend, sizearr;
float order;
int next = darry[0];
for(descend = 1; descend < sizearr; descend ++){
if (next > darry[descend])
order = 1;
else
order = -1;
}
return (order);
}

float power_func(float firsval, int seconval){
int count;
float powerval =0.0;
if (seconval < 0){
firsval = -1;
seconval= 1/seconval;
}
for(count = 0; count < seconval; count++){
powerval *= firsval;
}
return powerval;
}

int count, sizearr;
for(count = 1; count < sizearr; count++){
}
return sum;
}

float subtract_arr(float subarry[]){
int count, sizearr;
float difference = subarry[0];
for(count = 1; count < sizearr; count++){
difference -= subarry[count];
}
return difference;
}

float multiply_arr(float multarry[]){
int count, sizearr;
float product = multarry[0];
for(count = 1; count < sizearr; count++){
product *= multarry[count];
}
return product;
}

float division_arr(float divarry[]){
int count, size;
float quotient = divarry[0];
for(count = 1; count < size; count++){
quotient /= divarry[count];
}
return quotient;
}```

14. That's much better - no spaces at end of line.

Is this a "find five faults competition" ? [That is a joke, I know it's definitely not easy as a beginner - which is why I'm posting some comments to help you fix it up]

Code:
```void fill_arr(float farry[], int *size){
int fills = 0;
while (fills < *size && scanf(“%f”, &farry) !=EOF)
fills ++;
*size = fills;
}
}```
You don't really want to update *size inside the loop and use it as a loop limit at the same time. I'd update *size when the loop is finished.

Code:
```float ascend_arr(float aarry[]){
int ascend, sizearr;
float order;
int next = aarry[0];
for(ascend = 1; ascend < sizearr; ascend ++){
if (next < aary[ascend])
order = 1;
else
order = -1;
}
return (order);
}```
As per my previous post: sizearr should be passed in, not a local variable - as it stands, it's going to crash your program, most likely - sooner or later at least.
Different name to the parameter into your function

I'm not entirely sure what the purpose of this function is - it returns 1 or -1 depending on the first and last entry.

Code:
```float descend_arr(float darry[]){
int descend, sizearr;
float order;
int next = darry[0];
for(descend = 1; descend < sizearr; descend ++){
if (next > darry[descend])
order = 1;
else
order = -1;
}
return (order);
}```
Same functionality as above - just different sign - so perhaps return "-ascend()"??

Code:
```float power_func(float firsval, int seconval){
int count;
float powerval =0.0;
if (seconval < 0){
firsval = -1;
seconval= 1/seconval;
}
for(count = 0; count < seconval; count++){
powerval *= firsval;
}
return powerval;
}```
I've already done this one in a previous post - please review that part.

Code:
```float add_arr(float adarry[]){
int count, sizearr;
for(count = 1; count < sizearr; count++){
}
return sum;
}

float subtract_arr(float subarry[]){
int count, sizearr;
float difference = subarry[0];
for(count = 1; count < sizearr; count++){
difference -= subarry[count];
}
return difference;
}

float multiply_arr(float multarry[]){
int count, sizearr;
float product = multarry[0];
for(count = 1; count < sizearr; count++){
product *= multarry[count];
}
return product;
}

float division_arr(float divarry[]){
int count, size;
float quotient = divarry[0];
for(count = 1; count < size; count++){
quotient /= divarry[count];
}
return quotient;
}```
These all have the same problem with a "uninitialized local sizearr" - and you should really use the same name for same use variables, don't call it size in one place and sizearr in all the others - consistency is a VERY good thing in programming - it just makes everyones life much easier [and no, I don't always achieve it either].

--
Mats

15. ## i'm trying

ok...thankeveryone again for helping me..I'm so slow at this and trying to undersand what yall are saying. ok so here is the edited code. The ascend function returns 1 if it does ascend and -1 if it does not. the same is for the descend.
Code:
```void fill_arr(float farry[], int *size){
int fills = 0;
while (fills < *size && scanf(“%f”, &farry) !=EOF)
fills ++;
}
*size = fills;
}

float ascend_arr(float aarry[], int sizearr){
int ascend;
float order;
int next = aarry[0];
for(ascend = 1; ascend < sizearr; ascend ++){
if (next < aarry[ascend])
order = 1;
else
order = -1;
}
return (order);
}

float descend_arr(float darry[], int sizearr){
int descend;
float order;
int next = darry[0];
for(descend = 1; descend < sizearr; descend ++){
if (next > darry[descend])
order = 1;
else
order = -1;
}
return (order);
}

float power_func(float firsval, int seconval){
int count;
float powerval =1.0;
if (seconval < 0){
firsval = 1.0;
seconval= 1/firsval;
}
for(count = 0; count < seconval; count++){
powerval *= firsval;
}
return powerval;
}

int count;
for(count = 1; count < sizearr; count++){
}
return sum;
}

float subtract_arr(float subarry[], int sizearr){
int count;
float difference = subarry[0];
for(count = 1; count < sizearr; count++){
difference -= subarry[count];
}
return difference;
}

float multiply_arr(float multarry[], int sizearr){
int count;
float product = multarry[0];
for(count = 1; count < sizearr; count++){
product *= multarry[count];
}
return product;
}

float division_arr(float divarry[], int sizearr){
int count;
float quotient = divarry[0];
for(count = 1; count < sizearr; count++){
quotient /= divarry[count];
}
return quotient;
}```