Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose The purpose of this lab is to help you to practice working with pointers and basic operations using pointers. Learning Outcomes Understand what is

Purpose The purpose of this lab is to help you to practice working with pointers and basic operations using pointers. Learning Outcomes Understand what is a variable pointer and what do we mean by pointers only store address Understand how call by value and call by reference (pointer) works? Why pointers need to have an explicit data type when we declare them. Part 1.1: Test and Observe #include int main(){ int a = 7; int *aPtr; aPtr = &a; // This statement will print the value of a

printf("stat 01: %d ", a); // This statement will print ............

printf("stat 02: %p ", &a); // This statement will print ............

printf("stat 03: %p ", aPtr); // This statement will print ............

printf("stat 04: %p ", &aPtr); // This statement will print ............

printf("stat 05: %d ", a); // This statement will print ............

printf("stat 06: %d ", *aPtr); // This statement will print ............

printf("stat 07: %p ", *&aPtr); // This statement will print ............

printf("stat 08: %p ", &*aPtr); // This statement will print ............

printf("stat 09: %d ", *&a);

return 0;

}

Q1) - Read the above code carefully and try to guess what each print statement is going to print when you execute the code. Write down your guessing by completing the code comments. Q2)- Run the code and compare your guessing with the actual output and change your code comments if needed.

Part 1.2: Test and Observe

int main() { float b = 3.7f; float * bPtr;

// This statement will print ............

printf("stat 01: value of b = %f ", b); // This statement will print the unsigned int presentation of the address

// that bPtr pointing at in memory

printf("stat 02: %u ", bPtr); // let bPtr point to the address of variable b in memory

bPtr = &b; // Write down the value you will get from this print statement, call it var1

printf("stat 03: %u ", bPtr); // this statement will print ..........

printf("stat 04: %u ", &b); printf("stat 05: currently bPtr store the address of variable b which is %u in unsigned int format or %p in hexadecimal format ", bPtr, bPtr); // this statement will print .........

printf("stat 06: the size of b in memory is: %d ", sizeof(b)); // this statement is going to .........

bPtr++; // Write down the value you will get from this print statement, call it var2

// Write the value

printf("stat 07: %u ", bPtr); return 0;

}

Q1) - Read the above code carefully and try to guess what each print statement is going to print when you execute the code. Write down your guessing.

Q2) - Run the code and compare your guessing with the actual output

Q3) - What is the difference between var2 and var1, in other words, va2 - var1 = ??. Could you explain it?

Part 2: Programming Problems Q2.1) The following code should implement a sort algorithm to sort an array of integer numbers of size n in ascending order. Complete the implementation of the Sort and Swap functions void PrintArray(int size, int array []){ for(int i=0; i

printf("array[%d] = %d ", i, array[i] );

}

}

void swap(int * var1, int * var2){

// provide the implementation

}

void Sort(int size, int array[]){

// provide the implementation

}

int main() { int data [] = {2, 7, 8, -8, 1, 9, 3, 5}; SelectionSort(sizeof(data)/sizeof(int), data);

PrintArray(sizeof(data)/sizeof(int), data); return 0;

}

Q2.2) Change the PrintArray Function to print the memory address of each element in the array in addition to the element index within the array and the element value. What do you notice about the memory addresses of the array elements?

Q2.3) Write a C function named ArrayInfo( ) of type void that takes an array of n integers and calculate the sum of the elements in the array, the average, find the minimum value, the maximum value and made this information available to the main function. Hint use pointers and call by references.

Q2.4) Implement a function ReverseArray to reverse the elements of the array to sort it in descending order. The function ReverseArray has the following prototype:

void ReverseArray( int size, int *ptr2Array):

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Define Administration?

Answered: 1 week ago