ARRAY INITIALIZATION

Array can be initialized at the time of declaration.The initial values must appear in the order in which they will be assigned to the individual array elements,enclosed within the braces and separated by commas.

Syntax of array initialization :

data type array-name[size]={value 1,value 2......value n};

Value 1 is the  value for the  first array element,value 2 is the value for second element,value n is the value for n array element.When you are initializing the values at the time of declaration,then there is no need to specify size.

int[10]={1,2,3,4,5,6,7,8,910};

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


Character Array Initialization

The array of characters is implemented as in C.Strings are handled differently as far as initialization is concerned.A special character called null character '\0', implicitly suffixes every string.When the external or static string character array is assigned a string constant,the size specification is usually omitted and is automatically assigned;it will include the '\0' character ,added at the end.For example,consider the following two assignment statements:

char thing[3]="TIN";
char thing[]="TIN";

In the above statements the assignments are done differently .The first statement is not a string but simply an array storing three characters 'T','I','N' and is same as writing:

char thing[4]={'T','I','N'};
whereas ,the second one is four character string TIN\0.The change in the first assignment,as given below,can make it a string.

Example

Write a program to illustrate how the marks of 10 students are read in an array and then used to find the maximum marks obtained by a student in the class.

#include<stdio.h>
#define SIZE 10 

main()
{
int i=0;
int max=0;
int stud_marks[SIZE];
for(i=0;i<SIZE;i++)
 {
printf("Student no.=%d",i++);
printf("Enter the marks out of 50: ");
scanf("%d",&stud_marks[i]);
}
for(i=0;i<SIZE;i++)
{
 if(stud_marks[i]>max)
max=stud_marks[i];
}

printf("\n\n The maximum of the marks obtained among all the 10 students: %d",max);
} 
OUTPUT
Student no.=1 Enter the marks out of 50:10
Student no.=2 Enter the marks out of 50:17
Student no.=3 Enter the marks out of 50:23
Student no.=4 Enter the marks out of 50:40
Student no.=5 Enter the marks out of 50:49
Student no.=6 Enter the marks out of 50:34
Student no.=7 Enter the marks out of 50:37
Student no.=8 Enter the marks out of 50:16
Student no.=9 Enter the marks out of 50:08
Student no.=10 Enter the marks out of 50:37

0 comments:

Post a Comment