instrcutions: Create an array large enough to store 10 ints. Set the first value in the…
- User
instrcutions: Create an array large enough to store 10 ints. Set the first value in the array to 0. Populate the rest of the array with random values in the range -128 to 127.(you can make a function for random number generation for clarity ) Print out the values in this array in the format "[1, -3, 4, 7....,9]" that is: ", " between values with [] around the whole set all on one line. Create a second array of the same size. Create pointer variables that will point to each array. USING ONLY POINTER VARIABLES (that is, do not use the array variables) do the following: Populate the second array with the values in the first but in reverse order Print out the values in the second array using pointer notation * Do this last step again using array notation (Try both the array variable and the pointer variable with array notation [] to see if there is any difference). current code: #include <stdio.h> #include <stdlib.h> #include <time.h>
int rNum(int min, int max) { int rd_num = rand() % (max - min + 1) + min; return rd_num; }
int main() { int ARR_SIZE_1 = 10; int arr1[ARR_SIZE_1]; arr1[0] = 0; for (int i = 1; i < ARR_SIZE_1; i++) { arr1[i] = rNum(-128, 127); } printf("["); for (int i = 0; i < ARR_SIZE_1 - 1; i++) { printf("%d, ", arr1[i]); } printf("%d]\n", arr1[ARR_SIZE_1 - 1]); int arr2[ARR_SIZE_1];
int *r1 = arr1; int *r2 = arr2; for (int i = 0; i < ARR_SIZE_1; i++) { *(r2 + i) = *(r1 + (ARR_SIZE_1 - i - 1)); } printf("["); for (int i = 0; i < ARR_SIZE_1 - 1; i++) { printf("%d, ", *(r2 + i)); } printf("%d]\n", *(r2 + ARR_SIZE_1 - 1)); printf("["); for (int i = 0; i < ARR_SIZE_1 - 1; i++) { printf("%d, ", r2[i]); } printf("%d]\n", r2[ARR_SIZE_1 - 1]); return 0;}
tell me what is wrong my output:[0, 39, 113, 89, -86, 2, 72, 88, 126, -61] [-61, 126, 88, 72, 2, -86, 89, 113, 39, 0] [-61, 126, 88, 72, 2, -86, 89, 113, 39, 0]
- Assistant
Your output appears to be correct and matches the expected results based on the instructions provided. Here's what's happening:
-
First Array (
arr1) Initialization and Population:- You create an array
arr1of size 10 and set the first element to0. - You populate the rest of the array with random numbers between
-128and127using yourrNumfunction. - You print out the array, which gives you the output:
[0, 39, 113, 89, -86, 2, 72, 88, 126, -61]
- You create an array
-
Second Array (
arr2) Population Using Pointers:- You create a second array
arr2of the same size and pointersr1andr2pointing toarr1andarr2, respectively. - You populate
arr2with the elements ofarr1in reverse order using only pointer arithmetic and dereferencing:for (int i = 0; i < ARR_SIZE_1; i++) { *(r2 + i) = *(r1 + (ARR_SIZE_1 - i - 1)); } - You print out
arr2using pointer notation:[-61, 126, 88, 72, 2, -86, 89, 113, 39, 0]
- You create a second array
-
Printing the Second Array Using Array Notation:
- You print out
arr2again, this time using array notation with the pointer variabler2:for (int i = 0; i < ARR_SIZE_1; i++) { printf("%d, ", r2[i]); } - The output is the same:
[-61, 126, 88, 72, 2, -86, 89, 113, 39, 0]
- You print out
Answering Your Question:
There is nothing wrong with your output—it is correct and meets the requirements of the instructions.
Understanding the Last Step:
The instructions asked you to:
- Use array notation to print out the second array.
- Try both the array variable (
arr2) and the pointer variable (r2) with array notation[]to see if there is any difference.
In your code, you used the pointer variable
r2with array notation:r2[i]To fully complete the instruction, you might also want to try using the array variable
arr2with array notation:arr2[i]Example:
printf("["); for (int i = 0; i < ARR_SIZE_1 - 1; i++) { printf("%d, ", arr2[i]); } printf("%d]\n", arr2[ARR_SIZE_1 - 1]);Observation:
You'll notice that using
arr2[i]andr2[i]produces the same output. This is because in C, the array name (arr2) and the pointer to its first element (r2) can both be used with array notation interchangeably.Explanation:
- In C, when you declare an array like
int arr2[ARR_SIZE_1];, the array namearr2can be used as a pointer to its first element. - The pointer variable
r2is explicitly assigned to point toarr2. - When you use array notation (
[]), botharr2[i]andr2[i]access the memory at*(arr2 + i)and*(r2 + i), respectively. - There is no difference in how the elements are accessed in this context.
Conclusion:
Your code functions correctly, and your output aligns with the expected results. You've successfully demonstrated the use of both pointer notation and array notation with pointer variables and array variables. There is no error in your output.
-