Lab -4-Pointers- Learn how to use pointers using C programming
60-141 – Introduction to Programming II
Objective: Learn to use pointers.
1. Consider the following C program. Assume that memory addresses are expressed in decimal numbers and an integer takes 4 bytes. Also, assume ids = &ids[0] = 2000. What would be printed by the following program? Do NOT compile and execute this program as it will not provide the correct results – do the tracing of the program by hand and using only the assumptions stated.
#include
int main(){
int ids[3] = {100,200,300};
int *salary, salary1, salary2, *salary3;
salary1 = ids[0] * ids[1];
salary = &ids[1] ;
salary2 = *(ids+1)* *(ids+2);
salary3 = ids+2;
printf(“*salary = %d\nsalary1 = %d\n”, *salary, salary1);
printf(“salary2 = %d\nsalary3 = %p\n”, salary2, salary3);
}
2. Write a documented function called Largest that finds and returns the address of the largest element in the array passed to it. (Assume an integer array of size 10). You must use pointer arithmetic on the array name instead of the array subscript notation to implement the function. (NOTE: This function will be included as part of step 4 below.)
int *Largest( int *array, int size );
3. Write a documented function called Swap that takes two integer pointers and exchanges the values of each. It returns void. Example: given two integers ‘a = 2’ and ‘b = 4’, after Swap (&a, &b) is called, ‘a’ will be 4 and ‘b’ will be 2. (NOTE: This function will be included as part of step 4 below.)
void Swap( int *x, int *y );
4. Write a complete, well documented C language program (called Lab4.c) to test both the functions Largest and Swap. In order to test your functions you will need to define and populate an array, within main, that you can pass to Largest, and also
two initialized int variables to pass into Swap; do not forget to write printf() statements to show Before/After views of any data that will undergo changes. Ensure that your testing clearly demonstrate each function and its required result. There is no need for a menu, nor for the program logic to repeat itself (unless you choose to implement such logic).
Answer:
/* Title: Lab #4: Pointer Objective: To test both functions, Largest and Swap using pointer. */ //Includes #include<stdio.h> //Definitions //Maximum number in the Array #define MAX 10 //Function prototypes int *Largest(int *A, int size); //Function to find the largest element in the Array void Swap(int *a, int *b); //Function to swap to element in the Array //The main function int main() { //Declarations int A[] = {5,3,6,7,11,6,34,41,23,10}; int *address = NULL, *ptrIndex = NULL,i; ptrIndex = A; printf("\nThe Array is : [ "); //Display the elements of the array for( i = 0; i < MAX; i++) { printf("%d ", *ptrIndex); ptrIndex++; } printf("]\n"); ptrIndex = NULL; address = Largest( A, MAX ); printf("\nThe largest number in the Array is %d ", *address); //Swap the first and the second element in the array Swap(A, A+1); //The pointer is pointing to the first element in the array ptrIndex = A; printf("\nThe Array after the modification is : [ "); //Display the elements of the array after the modification for( i = 0; i < MAX; i++) { printf("%d ", *ptrIndex); ptrIndex++; } printf("]\n"); ptrIndex = NULL; return 0; } /* Objective: Function to find the largest element in an Array //Input: pointer Array, size of Array //Output: Largest element in the Array */ int *Largest(int *A, int size) { //Declarations int k, *address; address = A; //Loop through the Array to find the largest element for( k =0; k < MAX; k++) { if(*address < *(A+k)) address = A+k; } return address; } /* Objective: Function swaps two numbers Input: two pointers Output: values at two pointers swapped */ void Swap(int *a, int *b) { int storePtr; storePtr = *a; *a = *b; *b = storePtr; }
Leave a reply