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();
}

Array Representation




Array Representation:

Shown below some example of array defination for assignment of initial values:
int digit[10] ={1,2,3,4,5,6,7,8,9,10};
float f[10]={0.3,0,0.25}
For example, i want to represent five integer number in an array,say(10,20,30,40,50) by an array variable name number,then declaration of array variable number as followes:
int number[5] ;
The values to the array elements can be assigned as follows:

number[0]=10;
number[1]=20;
number[2]=30;
number[3]=40;
number[4]=50;
For this case the array number stored valued as shown below:
number[0]

  10
number [1]

  20
number [2]

  30
number [3]

  40
number [4]

  50
  
  Also consider the following array definition:
   int number[5]={1,5,8};
 
The values to the array elements can be assigned as follows:

number[0]=1;
number[1]=5;
number[2]=8;
number[3]=0;
number[4]=0;

Note:character  type array use last index for null('\0') .
char name[6]={"hasan"};
the array elements are stored as follows:

name[0]='h';
name[1]='a';
name[2]='s';
name[3]='a';
name[4]=n'';
name[5]='\0';                                                                                                           
                                                    

C Array




Array: Array is one kind of variable declaration of same types of data with a name and identify them one from another followed by subscripts or index.
Example: int arr[10];
Hence int is data type.arr is array name.arr[10] means we can keep ten integer type value in arr[10];
        
The declaration int arr[10]; would reserve enough space for an array called arr that could hold up to 10   integers. The
picture given below  clear to concept about the Array reserved storage space. 


arr[0]


arr[1]


arr[2]


arr[3]


arr[4]


arr[5]


arr[6]


arr[7]


arr[8]


arr[9]





Note: Array index range from 0 to n-1.Here index of arr[10] is start from arr[0] and end at arr[9]

Array Declaration:

data_type variable_name[size];
Example:int month[12];
                float number[10];
                char name[10];


Array Representation:

  Shown below some example of array defination for assignment of initial values:
  int digit[10] ={1,2,3,4,5,6,7,8,9,10};
  float f[10]={0.3,0,0.25}
 For example, i want to represent five integer number in an array,say(10,20,30,40,50) by an array variable name number,then declaration of array variable number as followes:
int number[5] ;
The values to the array elements can be assigned as follows:

number[0]=10;
number[1]=20;
number[2]=30;
number[3]=40;
number[4]=50;
For this case the array number stored valued as shown below:
number[0]

  10
number [1]

  20
number [2]

  30
number [3]

  40
number [4]

  50
  
  Also consider the following array definition:
   int number[5]={1,5,8};
 
The values to the array elements can be assigned as follows:

number[0]=1;
number[1]=5;
number[2]=8;
number[3]=0;
number[4]=0;

Note:character  type array use last index for null('\0') .
char name[6]={"hasan"};
the array elements are stored as follows:

name[0]='h';
name[1]='a';
name[2]='s';
name[3]='a';
name[4]=n'';
name[5]='\0';                                                                                                           
                                                                                                                                                        

















Difference between array variable and ordinary variable: In an ordinary variable we can assign same types of only one value, but in an array variable we can assign same types of more then one values.

For example:
int number;
                        number=5;
In this number variable we can take only one integer value.
For  an array variable we can assign more then one same types of values.Shown in below:
                        int number[5];
                        number[0]=10;
                        number[1]=20;
                        number[2]=30;
                        number[3]=40;
                        number[4]=50;

Taking input throw keyboard and output with a C program:

#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int number[5],i;
    printf("Take five integer value throuh keyboard:\n\n");
    for(i=0;i<5;i++)
    {
        printf("number[%d]=",i+1);
        scanf("%d",&number[i]);
    }
    printf("\n\nDisplay the array\n\n");
        for(i=0;i<5;i++)
    {
        printf("number[%d]=%d\n",i+1,number[i]);

    }
    getch();
}