Lab -5 More on Pointers- Learn to use pointers and operators at a more advanced level using C programming
60-141 – Introduction to Programming II
Objective: Learn to use pointers and operators at a more advanced level.
A. Pointer Operators
Type the following program and document every line by explaining its function and output. If any line produces an error, comment that specific line and explain the nature of the error. Write your code for this part in a file called: Lab5a.c
#include
int main()
{
int a = 7 ;
int *aPtr ;
aPtr = &a ;
printf( “%p”, &a );
printf( “%p”, aPtr );
printf( “%p”, &aPtr );
printf( “%d”, a );
printf( “%d”, *aPtr );
printf( “%p”, *&aPtr );
printf( “%p”, &*aPtr );
printf( “%d”, *&a );
printf( “%d”, &*a );
return 0;
}
Answer:
#include <stdio.h> /* Title: Lab #5a: More on Pointer-Pointer Operators Objective: Document every line of code and explaining its function and output. */ int main() { int a = 7 ; //value a is 7 int *aPtr ; //pointer variable aPtr = &a ; // points to address of a printf( "%p\n", &a ); //prints address of a printf( "%p\n", aPtr ); //prints address of a printf( "%p\n", &aPtr ); //prints address of pointer to a printf( "%d\n", a ); //prints value of 'a' printf( "%d\n", *aPtr ); //prints value 7 printf( "%p\n", *&aPtr ); //prints value of pointer (address of a) printf( "%p\n", &*aPtr ); //prints address of a printf( "%d\n", *&a ); //prints the value of 'a' //printf( "%d", &*a ); //error can't deallocate value return 0; } /* Output ------ 0060FF0C 0060FF0C 0060FF0B 7 7 0060FF0C 0060FF0C 7 */
B. Array Manipulation with Pointers
Write, document and test each of the following function specifications (write your code for this part in a file called: Lab5b.c):
[You should use only pointer arithmetic – DO NOT USE array indices (i.e. subscripts)].
1. A function called FillArray() that accepts a pointer to a integer as the array name, an integer for its size. This function pseudo-randomly fills the array with integers ranging from 0 to 100. Do not be concerned about possible duplicate values.
2. A function called PrintArray() that accepts a pointer to a integer as the array name, an integer for its size. This function only prints the array elements.
3. A function called BubbleSort() that accepts a pointer to a integer as the array name, an integer for its size. The function should sort the array passed, in descending order. Use the function Swap() that you designed in the last Lab #4, to swap any values in the function BubbleSort().
4. In the main() function:
a. Declare an integer array called NumList of size SIZE (define SIZE as 20, using #define).
b. Populate the array with pseudo-random numbers [0 -100] by calling the function FillArray().
c. Display the Array contents by calling the function PrintArray().
d. Sort the array NumList in descending order by calling the function BubbleSort().
e. Display the SORTED array by calling the function PrintArray().
For your convenience, the function prototypes are given bellow:
void FillArray ( int *array, int size );
void PrintArray ( int *array, int size );
void BubbleSort ( int *array, int size );
void Swap ( int *x, int *y );
Answer:
/* Title: Lab #5b: More on Pointer - Array Manipulation with Pointers Objective: Document every line of code and explaining its function and output. */ //Includes #include <stdio.h>//C-Preprocessor Directives #include <time.h>//C-Preprocessor Directives //Declare CONSTANTS #define SIZE 20 //Function Prototypes void FillArray ( int *array, int size ); void PrintArray ( int *array, int size ); void BubbleSort ( int *array, int size ); void Swap ( int *x, int *y ); int main(){ //Declare an integer array NumList int NumList[SIZE]; //call the function to populate the array FillArray( NumList, SIZE ); printf("\n Unsorted :\n"); //call the function to display the array PrintArray( NumList, SIZE ); //call the function to sort the array using bubbleSort BubbleSort( NumList, SIZE ); printf("\n Sorted :\n"); //call the function to display the array after sorting in descending order PrintArray( NumList, SIZE ); return 0; } /* Objective: Populate the array with pseudo-random numbers from 0-100 input: an array and its size output: array with 20 random numbers */ void FillArray (int *array, int size){ //seeding srand(time(NULL)); //Variable Declaration int random,i; //Choose 20 random number ranged from 1 to 100 for ( i = 0; i < size; i++){ random = rand() % 101; *(array + i) = random; } } /* Objective: Display the elements of the array on the screen input: an array and the size of the array output: Display elements of the array */ void PrintArray (int *array, int size){ int i; for ( i = 0; i < size; i++){ printf("%d ", *(array + i)); } //new line printf ("\n"); } /* Objective: sort the array in descending order using bubble sort input: an array and the size of the array output: sorted array in decending order */ void BubbleSort (int *array, int size){ int i,j; for ( i = 0; i < size ; i++){ for ( j = 0 ; j < size - i; j++){ if (*(array + j) < *(array + j + 1)){ Swap (array + j, array + j + 1); } } } } /* Objective: Swap two adjacent numbers in an array input: Two adjacent numbers in an array x.y output: two consecutive numbers sorted the higher first then the lowest */ void Swap (int *x, int *y){ int temp = *x; *x = *y; *y = temp; }
Leave a reply