Starting from:

$30

COMP1410-Lab 5 Pointer Operator Solved

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
 

#include <stdio.h

 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;

 

 }

 

 

 

 

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

More products