Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Review the following information about C printf and memory address of variables and array elements. a. printf: http://www.cplusplus.com/reference/cstdio/printf/ b. Pointers, array, address of array

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
1. Review the following information about C printf and memory address of variables and array elements. a. printf: http://www.cplusplus.com/reference/cstdio/printf/ b. Pointers, array, address of array elements: lecture slides for C Basics (https://passlab.github.io/ITSC3181otes/lectre02 CBasics.pdf) 2. Use https://replit/languages/c website to develop, compile and run your program. First, open the file from the web browser https://passlab. github.io/ITSC3181otes/Lab 02/lab02 address 1Dstencil.c, go to https://repl.it/languages/c site, and cut-paste the content of the lab02_address_1Dstencil.c f to the editor. It is named as main.c file in website editor. Please ignore the file name. Then compile and run the program first (gcc main.c; ./a.out) to make sure you have the correct to start with. See the following screenshot: 3. Edit the file and make changes for those TODOs. Each TODO are given with detailed comments for the task. The first two TODOs are for showing variable memory addresses, variable sizes and array element addresses. For TODO \#1 (10 points), you will need add printf statements to print out the memory address (obtained using \& operator) and the size (using sizeof operator) of each variable. For TODO \#2 ( 20 points), you will need to add printf to print the base memory address of the array A. After that, you will need to add a double-nested loop. In each inner loop iteration, add printf to print the memory address of each array element obtained using \&, the offset in bytes calculated using the formula we discussed in the class, and the sum of base (cast to char* type first) and offset. For each iteration, base+offset should be same as the address of the element you obtain using \&. After this exercise, you should know that array references, e.g. A[3][2], A[i][], B[i] are all address calculation operations, i.e. to calculate the address of a specific array element based on the formula of base + offset. 4. The last three TODOs (TODO \#3, \#4 and \#5) are for implementing a simple 1-D stencil operation (a simplified version of the algorithm that is used for lots of image processing algorithms such as image blurring, enhancing, etc). The 1-D stencil operation is to update each array element of B[M] and store the results in another array B2[M] using the operation B2[i]=(B[i1]+B[i]+B[i+1])/3 (i=1,2, .., M-2). For the first and last element of B2 (boundary condition), we do B2[0]=B[0], and B2[M1]=B[M1]. You need to implement this operation using only pointers, the "int *iterator" variable, and array element access should use the address calculated based on the pointer arithmetic and array element address calculation (base+offset). TODO \#3 (10 points) is for calculating the address of array element B[i] so value can be assigned to it. TODO \#4 ( 35 points) is for the operation B2[i]=(B[i1]+B[i]+B[i+1])/3. in this TODO, your code should reference each needed array elements of B and B2 using pointers. Using \& and [] operators to do it are not counted as your implementation. TODO \#5 ( 5 points) is for calculating the address of array element B2[i] so values can be read for printing. It is important to note that when calculating address of an element of an array, you calculate the byte address (casting to char "), but when you assign that byte address to an "int * ptr" variable, or use that pointer as address to read or write a valwe from/to that address (by using *operator), you will need to specify the type of the pointer (e.g. int * by casting). E.g. int * ptr; ptr = (int*) ((char *)A + sizeof(int) * i); By doing that, the program knows both the address and the number of bytes to access for each pointer variable. 5. For all the TODOs, I recommend you do one by one. When you complete one, compile and run to see whether you have the expected output. If the output is what is expected, move on to the next TODO. 6. Using either of the approaches for editing and develop the program, if you want to save the file for future use, you need to find a way to copy the file to a location that you can access later on. Ainclude estatio.hs Int main(int argc, char angv[]) [ Int at =10; int a2 = 10; double b2=3.145 double b2=9283 short c1=1ee; short e2=20 : char d1=a3 char d2=cz Int M=48 int N=6; int A[M][M] : int 1 , j5 INou can fand the nemory address of a function by using a operator prntf (mm); printf("*=" Menory addresses of array elenents using 8 operator and basetoffset calcualtion =aw-(n")) pre T0oo 42 (20 points): add your code for printing addresses of array elenents using a operator and basetoffset calcualtion *f f 1-D stenel1 operationt for an array 0[M], update each elenent by cot[1]=(D[11]+0[1]+0[1+1])/3=/ arand(1cc12); /1 Initialize randem nueber generator seed, should only be colled once. M=1ee; Int B[M]s int eiterater a D1 I/generate rand nuaber for array B and print array 8 printff("Elenent values of array erxd] Nn,M) ) for (ieds icth it+) i //r000 63 (10 points): update the iterator to store the address of elenent 1 of 8 . *iterator = rand() 20;f assign the array elchent a randon value betuean and 20% printf(=Mdt", "itarater); 13 Iterator = B y int az[M] for (imis ick-1; fet) f fo vooo 44(35 points ) : performe operation 82[1]=(8[11]+8[1]+8[1+1])/3. You are only allowed to use - the Iterator and i variable to calcusite the menory addresses of needed elenents of B and B2. - You should wor use [] ar a operator for any purpose hare "/ 1 fo boundary copy printr("InElenent values of array B2[kd] after 1-D stencil operation on array 0", M)t - the iterator and 1 variable to calcualte the menory addresses of needed elenents of B and B2. - You should NOT use [] or \& operator for any purpose here */ The screenshot below shows the correct output of the program if it is completed: yanyh@vm: . /a.out The address of main function: 04066a6 Variable memory addresses using \& operator and variable sizes using sizeof operator Menory address of variable a1: 07ff ce61c718c, size: 4 bytes Memory address of variable a2: 07ff ce61c7190, size: 4 bytes Memory address of variable b1: 07ff ce61c71a8, size: 8 bytes Memory address of variable b2: 07ff ce61c71be, size: 8 bytes Memory address of variable c1: 07ff ce61c7188, size: 2 bytes Memory address of variable c2: 07ff ce61c718a, size: 2 bytes Memory address of variable d1: 07ff ce61c7186, size: 1 bytes Memory address of variable d2: 07ff cev1c7187, size: 1 bytes -.- Memory addresses of array elements using \& operator and base+offset calcualtion Base memory address of array A[4][6]:07ffce61c7b Memory address (A[][1]):07ffce61c7b4, offset: 0004 , base+offset: 07ff ce61c70b4 Memory address (A[][2]):0x7ffce61c70b8, offset: 0008 , base+offset: 07ff fce61c7eb8 Memory address \( (\varangle A[\theta][3]): 0 \times 7 f f c e 61 c 70 b c \), offset: 000c, base+offset: 07ffce61c70bc Memory address \( (\varangle A[\theta][4]): 0 \times 7 f \mathrm{fce} 61 \mathrm{c} \theta \mathrm{c} \theta \), offset: 0010 , base+offset: 07ffce61c7e Memory address \( (\varangle A[\theta][5]): 0 \times 7 \mathrm{ffce} 61 \mathrm{c} \theta \mathrm{c} 4 \), offset: 6014 , base+offset: 07ffce61c7c4 Memory address (&A[1][0]):07ffce61cc8, offset: 0018 , base+offset: 07ffce61c70c8 Memory address (&A[1][1]):07ffce61c6cc, offset: 01c, base+offset: 07ffce61c7cc Memory address ( A[1][3]):0x7ff ce61c70d4, offset: 0024 , base+offset: 07ffce61c70d4 Memory address ( A A [1] [4]): Ox7ffce61c76d8, offset: 8028 , base+offset: 07f ce 61c70d8 Memory address (\&A[1] [5]): 67ffce61c7dc, offset: 002c, base+offset: 07ffce61c7dc Memory address ( [1][3]):07ff ce61c7ed4, offset: 0024 , base+offset: 07ff ce61c7ed4 Memory address ( \&A[1] [4]): Ox7ffce61c70d8, offset: 0028 , base+offset: 07ffce61c70d8 Memory address ( EA[1][5]) : 07ffce61c7dc, offset: 002c, base+offset: 07ffce61c7dc Memory address ( \( \varangle \mathrm{A}[2][0]): ~ 0 \times 7 f \mathrm{fce} 61 \mathrm{c} \theta \mathrm{e} \theta \), offset: 003, base+offset: 07ffce61c7ee Memory address ( &[2][1]):0x7ff ce61c70e4, offset: 0034 , base+offset: 07f ce 61c70e4 Memory address ( &[2][2]):07ffce61c70e8, offset: 0038 , base+offset: 67ffce61c70e8 Memory address ( A[2][3]):07ffce61c70ec, offset: 003c, base+offset: 67ffce61c70ec Menory address ( A[2][4]) : 07ffce61c70f0, offset; 0040 , base+offset: 7ffce61cef Memory address ( \( \varangle A[2] \) [5]): 07ffce61c70f4, offset: 0044, base+offset: 07ffce61c7f4 Memory address ( 8A[3][0]):07ff ce61c70f8, offset: 0048, base+offset: 0x7ffce61c7ef8 Memory address ( A A [3] [1]): 07ff ce61c7ifc, offset: 004c, base+offset: Ox7ffce61c70fc Memory address ( A[3][2]):07ffce61c7100, offset: 0e50, base+offset: 07ff ce61c7100 Memory address (\&A[3] [3]): 07ffce61c7104, offset: 0054 , base+offset: 07ffce61c7104 Memory address (\&A[3] [4]): 07ff ce61c7108, offset: 0058 , base+offset: 07ffce61c7108 Memory address ( \( \varangle A[3][5] \) ): 07ff ce61c710c, offset: 005c, base+offset: 07ff ce61c710c 1-D stencil operation Element values of array B [100] 1449182961416111616910161153185120511551986650461317131697716111734201818141851122321641641918710969510101813191612503120211141281219811615271312 Elementvaluesofarray14812126877814B2[100]11861195412911after99361123138131068286281388stenciloperationonarray5108118271067B31391578127542149131014101298484124131413129795781181313107995879121212

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

Discuss the significance of evolutionary theory to psychology.

Answered: 1 week ago