Array as a Function’s Parameter




In C program we can pass array as like as variable of a function’s parameter. But in here the syntax of formal parameter and actual parameter are difference. Like-

Formal parameter syntax:

return_data_type function_name(data_type array_name[])
{
……………………….
}

Actual parameter syntax:

function_name(array_name);


Hence in formal parameter we need to write array name include bracket with it’s data type and don’t need to declare array size. where in actual parameter should write only array name in parenthesis.

Example:
#include<stdio.h>
#include<conio.h>
void funcx(int get_array[]) /*function definition*/ /* formal parameter*/
{
int n;
for(n=0;n<5;n++)
{
printf("\n %d",get_array[n]);
}
}
int main()
{
int hsn[5]={10,20,30,40,50};
clrscr();
funcx(hsn); /*function calling*//* actual parameter*/
getch();
return 0;
}

Fibonacci Series Using an Array




Fibonacci Number:
It is an interesting sequence in which each number is eqal to the sum of previous two numbers. As follows:
0 1 1 2 3 5 8 13 21 34 55 89 ……………………………………
This is a program which prints out a table of Fibonacci numbers. This program stores the series in an array, and after calculating it, prints the numbers out as a table.


#include<stdio.h>
#include<conio.h>
void main()
{


int fib[100];
int i,n;
clrscr();
printf("How many Fibonacci numbers you want:");
scanf("%d",&n);
fib[0] = 0;
fib[1] = 1;
printf("\n\nSerial Fibonacci number\n\n");
for(i = 2; i < n; i++)
fib[i] = fib[i-1] + fib[i-2];

for (i = 0; i < n; i++)
printf("%3d %6d\n", i, fib[i]);
getch();
}



int fib[100];
This defines an array called fib of 100 integers. We in C, the array index is enclosed by square brackets (it avoids misunderstanding with a function call).One can choose how many Fibonacci number he want less then 100.for this I use int n.Also, the first element of the array(fib[0]) has index zero, and for this n element array, the last element is index n-1.

Multiplication table using two-dimensional array:



#include<stdio.h>
#include<conio.h>
#define ROW 5
#define COLOUMN 5
void main()
{
clrscr();
int row,coloumn,product[ROW][COLOUMN],i,j;
printf("Multiplication table:\n\n");
printf(" ");
for(j=1;j<=COLOUMN;j++)
{
printf("%4d",j);
}
printf("\n");
printf("-----------------------\n");
for(i=0;i
{
row=i+1;
printf("%2d|",row);
for(j=1;j<=COLOUMN;j++){
coloumn=j;
product[i][j]=row*coloumn;
printf("%4d",product[i][j]);
}
printf("\n");
}

getch();
}

Two-dimensional array




We have already studied about n-element one-dimensional array, which is same as a list of element. Similarly an m*n, two- dimensional array is same as a table of values which have m rows and n columns.

(For more visual clear display please click on the above format)
Syntax: data_type array_name[row_size] [column-size];

Example: int x[5] [5];
This example define a table as integer array having 5 rows and 5 columns.

I explain a two dimensional array in below:

int x[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};

Hence x can be thought as a table contains 3 rows and 4 columns.

x[0] [0]=1 x[0][1]=2 x[0][2]=3 x[0][3]=4
x[1] [0]=5 x[1][1]=6 x[1][2]=7 x[1][3]=8
x[2] [0]=9 x[2][1]=10 x[2][2]=11 x[2][3]=12

Program using array




 Finding avarage using array:

#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int n,i;
    float avg,sum=0;
    float arr[50];
    printf("How many number you will take for avarage calculation:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("arr[%d]=",i+1);
        scanf("%f",&arr[i]);
        sum=sum+arr[i];
    }
    avg=sum/n;
    printf("Avarage is %f",avg);
    getch();
}