Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pointers and Functions This assignment focuses on pointers and its use in C. **C

ID: 3537049 • Letter: P

Question

Pointers and Functions

This assignment focuses on pointers and its use in C. **C only / NOT C++**

In this lab you will create a single source code file called PointerFunctions.c that uses the main routine shown below:

1. You will provide the functionality for the functions in blue

2. 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 and powerArgs. 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. Be careful to notice where pointers and variable addresses are used - the idea of this Lab is to practice writing code that uses pointers.


#include
#include

/* MAIN */

int main(int argc, char **argv) {
if (argc != 3) {
printf("?Invalid number of arguments ");
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);

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);

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 ",parmA,parmB);
printf("%.0lf ",
*powerArgs(&parmA, &parmB);

return 0;

}



OUTPUT:

> gcc -x c -ansi -o PointerFunctions.exe PointerFunctions.c
> PointerFunctions.exe 3 5
A=3, B=5. Swapped values A=5, B=3.
Quotient of 3 / 5 is 0, remainder is 3
3 raised to the 5 power is 243

Explanation / Answer

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

void swapArgs(int *p,int *q)

{

int *temp;

*temp=*p;

*p=*q;

*q=*temp;

}

int divideArgs(int *p,int *q)

{

return (*p)/(*q);

}

float *powerArgs(int *p,int *q)

{

float *temp;

*temp=(float)pow((float)(*p),(float)(*q));

return temp;

}

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

if (argc != 3) {

printf("?Invalid number of arguments ");

system("pause");

exit(1);

}

int parmA = atoi(argv[1]);

int parmB = atoi(argv[2]);

printf("A=%d, B=%d. ",parmA,parmB);

swapArgs(&parmA,&parmB);

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

parmA = atoi(argv[1]);

parmB =atoi(argv[2]);

printf("Quotient of %d / %d is %d , remainder is %d ",parmA,parmB,divideArgs(&parmA, &parmB),parmA%parmB);

parmA = atoi(argv[1]);

parmB = atoi(argv[2]);

printf("%d raised to the %d power is ",parmA,parmB);

printf("%.0lf ", *powerArgs(&parmA, &parmB));

return 0;

}