Home
Locate
CS 101
CS 496
Login
Tutors
Marks
About
Send
Close
Add comments:
(status displays here)
Got it!
This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.
nbsp; Note: This appears on each machine/browser from which this site is accessed.
2D array processing using functions
by RS
admin@ycp.powersoftwo.org
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. 2D array processing using functions
2. Array grid
The example below shows how to create a 2D array, pass it to get initialized, and then pass it to be displayed.
Remember that arrays in C are always passed by reference and not by value.
Record structures in C, on the other hand, can be passed by value or by reference (address/pointer). Here is the C code.
#include <stdio.h> #define RMAX 3 #define CMAX 4 void gridInit(int grid1[RMAX][CMAX]) { int rpos1 = 0; while (rpos1 != RMAX) { int cpos1 = 0; while (cpos1 != CMAX) { grid1[rpos1][cpos1] = rpos1+cpos1; cpos1 += 1; } rpos1 += 1; } } void gridShow(int grid1[RMAX][CMAX]) { printf("(begin grid)\n"); int rpos1 = 0; while (rpos1 != RMAX) { int cpos1 = 0; while (cpos1 != CMAX) { int value1 = grid1[rpos1][cpos1]; printf("\tr=%d c=%d v=%d\n",rpos1,cpos1,value1); cpos1 += 1; } rpos1 += 1; } printf("(end grid)\n"); } int main() { printf("(begin main)\n"); int grid1 [RMAX][CMAX]; gridInit(grid1); gridShow(grid1); printf("(end main)\n"); return 0; }
Here is the output of the C code.
(begin main) (begin grid) r=0 c=0 v=0 r=0 c=1 v=1 r=0 c=2 v=2 r=0 c=3 v=3 r=1 c=0 v=1 r=1 c=1 v=2 r=1 c=2 v=3 r=1 c=3 v=4 r=2 c=0 v=2 r=2 c=1 v=3 r=2 c=2 v=4 r=2 c=3 v=5 (end grid) (end main)
3. End of page
by RS
admin@ycp.powersoftwo.org