Hi everyone,

I could really use some help on modifying this pointer program by
Here are the directions I am given:

Your task is to create the special functions used by this routine, which are shown in blue in the above program. The special functions are swapArgs, divideArgs, powerArgs, and fibArgs. You need to write the functions so that they can be used by the main routine as shown. In other words, do not change the way the functions are used, rather write your functions to accept the parameters and return the values needed to make the program work properly.

Here is the original code:

#include <stdio.h>
#include <stdlib.h>

/* MAIN */
int main(int argc, char **argv) {
if (argc != 3) {
printf("?Invalid number of arguments\n");
system("pause");
exit(1);
}
int parmA = atoi(argv[1]);
int parmB = atoi(argv[2]);

/* Part A: Swap the values. Value in parmA is moved to parmB and vice versa */
/* Reset the original values after we print the result */
printf("A=%d, B=%d. ",parmA,parmB);
swapArgs(&parmA,&parmB);
printf("Swapped values A=%d, B=%d.\n",parmA,parmB);
parmA = atoi(argv[1]);
parmB = atoi(argv[2]);

/* Part B: Divide parmA by parmB. Put the quotient in parmA, remainder in parmB */
/* Reset the original values after we print the result */
printf("Quotient of %d / %d is ",parmA,parmB);
divideArgs(&parmA, &parmB);
printf("%d, remainder is %d\n",parmA,parmB);
parmA = atoi(argv[1]);
parmB = atoi(argv[2]);

/* Part C: Raise parmA to the power of parmB. Return pointer to the result */
/* Reset the original values after we print the result */
printf("%d raised to the %d power is %.0lf\n",parmA,parmB,*powerArgs(&parmA, &parmB));
parmA = atoi(argv[1]);
parmB = atoi(argv[2]);

/* Extra credit: Fibonacci generator function. The first call to fibArgs returns the
first Fibonacci number (0) and each subsequent call to fibArgs returns the
next Fibonacci number. In this case parmA will specify the number of
Fibonacci numbers to display. Fibonacci numbers:
F(0) = 0
F(1) = 1
F(n) = F(n-1)+F(n-2)
The first few numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
*/
int idx;
printf("The first %d Fibonacci Numbers are: ",parmA);
for(idx=0; idx < parmA; idx++) {
printf(" %d",*fibArgs());
}
printf("\n");

system("pause");
exit(0);
}

Here is what I have done so far:

#include <stdio.h>
#include <stdlib.h>
#define TERMINAL -9


/*Not sure if these are right*/
void swapArgs(int *a, int *b);
void divideArgs(int *a, int *b);
int *powerArgs(int *a, int *b);



/* MAIN */
int main(int argc, char **argv)
{


if (argc != 3)
{
printf("?Invalid number of arguments\n");
system("pause");
exit(1);
}


int parmA = atoi(argv[1]);
int parmB = atoi(argv[2]);



/* Part A: Swap the values. Value in parmA is moved to parmB and vice versa */
/* Reset the original values after we print the result */
printf("A=%d, B=%d. ",parmA,parmB);


swapArgs(&parmA,&parmB);

printf("Swapped values A=%d, B=%d.\n",parmA,parmB);

parmA = atoi(argv[1]);
parmB = atoi(argv[2]);


/* Part B: Divide parmA by parmB. Put the quotient in parmA, remainder in parmB */
/* Reset the original values after we print the result */
printf("Quotient of %d / %d is ",parmA,parmB);


divideArgs(&parmA, &parmB);


printf("%d, remainder is %d\n",parmA,parmB);

parmA = atoi(argv[1]);
parmB = atoi(argv[2]);

/* Part C: Raise parmA to the power of parmB. Return pointer to the result */
/* Reset the original values after we print the result */
printf("%d raised to the %d power is %.0lf\n",parmA,parmB,*powerArgs(&parmA, &parmB));

parmA = atoi(argv[1]);
parmB = atoi(argv[2]);


system("pause");
exit(0);
}

void swapArgs(int *a, int *b)
{
int temp;

temp = *a;
*a = *b;
*b = temp;

int swapArgs(int *a, int *b)
{
return temp;
}



}

/*I am not sure how to calculate the remainder*/
void divideArgs(int *a, int *b)
{


*a = *a / *b;



}



int *powerArgs(int *a, int *b)
{


*a = *a ^ *b;

return a;




}

I am not sure if I am doing this right. Right now when I compile my code it gives me the ?Invalid number of arguments so that means its returning a value of 1 instead of 0, but I don't know why. And I know I have to do something with *powerArgs(&parmA, &parmB) but I have no idea how to fix it.

Basically any tips and hints would really be appreciated. Thank you!

Recommended Answers

All 16 Replies

lets look at the program you have modified and get rid of some obvious errors.

(1) you dont have a "compile error". the program you posted (the second one) compiles without any error even though it may have other flaws. what you have is a runtime condition ... if (argc != 3) ... that determined you didnt pass the required arguments, so it exited with an explanation why.

If i compile the program like so:
gcc -o taiko taiko.c
then call it like so
./taiko

it will correctly exit with the warning that i did not provide the correct number of arguments.

so you must call it, for instance, like:
./takio <arg1> <arg2>
where <arg1> and <arg2> are values you (the user) provides that will be swapped, divded, etc.

(2) you have an "int swapargs( )" function embedded within the "void swapargs( )" function. i do not imagine this is correct. i assume you should only have a single function, "void swapargs( )"

part of the problem here is your complete lack of indentation, for blocks of code within the curly braces. maybe you had it at one point and lost it when you posted here. without indentation of code blocks, its very easy to make typos.

here is repost your code with the "void swaparg ( )" function fixed, and indentations and syntax coloring so we can at least see it better. you can use the code tags to frame your code, and indent your code with tabs or spaces (preferrably)

recompile the code and run it with the correct number of arguments in teh command line, and see if it works correctly. if it doesn't, at least you'll be able to track down why, in a more coherent manner.

#include <stdio.h>
#include <stdlib.h>
#define TERMINAL -9

/*Not sure if these are right*/
void swapArgs(int *a, int *b);
void divideArgs(int *a, int *b);
int *powerArgs(int *a, int *b);

/* MAIN */
int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("?Invalid number of arguments\n");
        system("pause");
        exit(1);
    }

    int parmA = atoi(argv[1]);
    int parmB = atoi(argv[2]);

    /* Part A: Swap the values. Value in parmA is moved to parmB and vice versa */
    /* Reset the original values after we print the result */
    printf("A=%d, B=%d. ",parmA,parmB);

    swapArgs(&parmA,&parmB);

    printf("Swapped values A=%d, B=%d.\n",parmA,parmB);

    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    /* Part B: Divide parmA by parmB. Put the quotient in parmA, remainder in parmB */
    /* Reset the original values after we print the result */
    printf("Quotient of %d / %d is ",parmA,parmB);

    divideArgs(&parmA, &parmB);

    printf("%d, remainder is %d\n",parmA,parmB);

    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    /* Part C: Raise parmA to the power of parmB. Return pointer to the result */
    /* Reset the original values after we print the result */
    printf("%d raised to the %d power is %.0lf\n",parmA,parmB,*powerArgs(&parmA, &parmB));

    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    system("pause");
    exit(0);
}

void swapArgs(int *a, int *b)
{
    int temp;

    temp = *a;
    *a = *b;
    *b = temp;
}

/*I am not sure how to calculate the remainder*/
void divideArgs(int *a, int *b)
{
    *a = *a / *b;
}

int *powerArgs(int *a, int *b)
{
    *a = *a ^ *b;
    return a;
}

thank you! I think I figured most of it out. The problem I'm having now is calculating parmA raised to parmB power. How do you calculate powers in C? What return value should it have in line 74?

taikoprogrammer> How do you calculate powers in C
If you are referring to especial operator, C doesn't have an operator for power. If you include the math.h file you may make use of the function pow()
Otherwise you define a function that calculates the power the same way you would without calculator, multipling a base number n times.

How do you calculate remainder?
There is an operation called modulo that will do this. Its used like: 5 % 6 = remainder of 5/6.

BTW, the program you were given is not vary good programming practice. It uses the exit() every single time it needs to exit! Generally, exit() is used in sub-functions that may fail, but have no way to tell the main program to quit. system("pause") is not portible, meaning that it will only work on certian operating systems.

Also, in you function:

int *powerArgs(int *a, int *b)
{
  *a = *a ^ *b;
   return a;
}

Remember you are editing *a before returning the result.

BTW, the program you were given is not vary good programming practice. It uses the exit() every single time it needs to exit! Generally, exit() is used in sub-functions that may fail, but have no way to tell the main program to quit. system("pause") is not portible, meaning that it will only work on certian operating systems.

If it were only that.

Thank you for all the help! This is what I have now. Everything works, except calculating parmA raised to parmB.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TERMINAL -9

/*Not sure if these are right*/
void swapArgs(int *a, int *b);
void divideArgs(int *a, int *b);
float *powerArgs(int *a, int *b);

/* MAIN */
int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("?Invalid number of arguments\n");
        system("pause");
        exit(1);
    }

    int parmA = atoi(argv[1]);
    int parmB = atoi(argv[2]);

    /* Part A: Swap the values. Value in parmA is moved to parmB and vice versa */
    /* Reset the original values after we print the result */
    printf("A=%d, B=%d. ",parmA,parmB);

    swapArgs(&parmA,&parmB);

    printf("Swapped values A=%d, B=%d.\n",parmA,parmB);

    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    /* Part B: Divide parmA by parmB. Put the quotient in parmA, remainder in parmB */
    /* Reset the original values after we print the result */
    printf("Quotient of %d / %d is ",parmA,parmB);

    divideArgs(&parmA, &parmB);

    printf("%d, remainder is %d\n",parmA,parmB);

    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    /* Part C: Raise parmA to the power of parmB. Return pointer to the result */
    /* Reset the original values after we print the result */
    printf("%d raised to the %d power is %.0lf\n",parmA,parmB,*powerArgs(&parmA, &parmB));

    parmA = atoi(argv[1]);
    parmB = atoi(argv[2]);

    system("pause");
    exit(0);
}

void swapArgs(int *a, int *b)
{
    int temp;

    temp = *a;
    *a = *b;
    *b = temp;
    

    
}


void divideArgs(int *a, int *b)
{
     
      int temp;
      temp = *b;
      *b = *a;
      *a = temp;
      
    int remainder;
    
    *a = *b / *a;
    
    remainder = *a;
    
      

}

float *powerArgs(int *a, int *b)
{
      pow(*a, *b);
      
      return pow;

      }

This is what my output is:

A=12, B=28. Swapped values A=28, B=12.
Quotient of 12 / 28 is 0, remainder is 12
12 raised to the 28 power is 38694547456
Press any key to continue . . .

I don't know if I initialized *parmArgs right. And I'm getting this random huge number for 12^28.

float *powerArgs(int *a, int *b)
{
pow(*a, *b);
return pow;
}

Uhh... what are you doing? pow() returns an integer. So you need to switch float with int. Second off, pow is not a function that magicly turns into a variable. :P Try:

return pow(*a, *b);

//Or

int result;
result = pow(*a, *b);
return result;

Uhh... what are you doing? pow() returns an integer.

pow

commented: Prisoner Of War ? +17

I already tried this code:

int result;
result = pow(*a, *b);
return result;

It makes bloodshed stops working when I try and run it.

Wow.. Guess I never took a closer look at the math library :$
Anyways, since your function already returns float *, try:

float result;
result = (float)pow(*a, *b);
return &result;  //return the address of result

Returning the address of a variable local to a function is undefined behavior. This particular part of this assignment to me is rather ugly. Assuming main is supposed to remain as-is, result should be static to make that work.

But given this line of code:

printf("%d raised to the %d power is %.0lf\n",parmA,parmB,*powerArgs(&parmA, &parmB));

it really makes me question the person who wrote the assignment.

I guess I am supposed to use loops to make the power function. This is what I have so far. I'm not sure where to go from here.

float *powerArgs(int *a, int *b)     
{
int i,
    result=0;

for( i = 1; i <= *b; i++)

    result = result * *a;
    
    return &result;

}

I guess I am supposed to use loops to make the power function.

You dont have to write your own. The pow() function in math.h should work fine. Dave Sinkula's solution look's like it's worth a try.
If the dosen't work, try allocating the variable and return the pointer. You may even be able to free the memory without editing main() by using atexit() in stdlib.h.
Good luck.

I am doing this for my class, and my teacher told me I can't use the pow function. I have to write my own. So I'm trying to do it with loops.

So loop.

double *powerArgs(int *a, int *b)
{
   static double result;
   int i;
   for ( i = 0, result = 1; i < *b; ++i )
   {
      result *= *a;
   }
   return &result;
}

But when this assignment is over, I'd love to pick over the instructor's solution -- please share it if possible.

commented: That would be amusing! +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.