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!