0% found this document useful (0 votes)
17 views2 pages

C2 LESSON 2 Passing An Array To A Function

The document contains C code demonstrating how to pass one-dimensional and two-dimensional arrays to functions. In the first part, a one-dimensional array is modified by a function that sets its values to -9, while the second part shows a two-dimensional array where odd values are decremented. The code includes functions for displaying the original and modified arrays.

Uploaded by

ivex.stvn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

C2 LESSON 2 Passing An Array To A Function

The document contains C code demonstrating how to pass one-dimensional and two-dimensional arrays to functions. In the first part, a one-dimensional array is modified by a function that sets its values to -9, while the second part shows a two-dimensional array where odd values are decremented. The code includes functions for displaying the original and modified arrays.

Uploaded by

ivex.stvn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Passing an array to a function - One dimension

#include<stdio.h>
#include<conio.h>

int count;
int a[3];

void modify(int a[])


{ int count;
printf("\nFrom the function after modifying the values\n");
for (count =0; count <=2; count++)
{ a[count] = -9;
printf("a[%d] = %d\n", count, a[count]);
}
}
void main()
{
clrscr();
printf("\n From the main, before the function call: \n");
for (count =0; count <=2; count++)
{ a[count] = count +1;
printf("a[%d] = %d\n", count, a[count]);
}
modify(a);
printf("\n From the main, after the function call: \n");
for (count =0; count <=2; count++)
printf("a[%d] = %d\n", count, a[count]);
getch();
}
#include<stdio.h>
#include<conio.h>
#define rows 3
#define columns 4

// Passing an array to a function - 2 dimension

void sub1(int z[][columns])


{ int a,b;
for (a=0;a<rows;++a)
for(b=0;b<columns;++b)
if (z[a][b] %2 !=0)
z[a][b]--;
}
main()
{ int z[rows][columns] = {1,3,5,7,9,2,4,6,8,10,11,13};
int a,b;
clrscr();
printf("Original array contains:\n\n");

for (a=0;a<rows;++a)
{ for(b=0;b<columns;++b)
printf("%4d",z[a][b]);
printf("\n");
}

printf("\n\n");

sub1(z);
printf("Changed array:\n\n");

for (a=0;a<rows;++a)
{ for(b=0;b<columns;++b)
printf("%4d",z[a][b]);
printf("\n");
}
getch(); return 0;
}

You might also like