Guide for The C Programming Language Chapter 3

Guide for The C Programming Language Chapter 3 Q1. What is an array? Explain the declaration and initialization of single and double dimensional arr...
Author: Rafe Cobb
4 downloads 0 Views 307KB Size
Guide for The C Programming Language

Chapter 3

Q1. What is an array? Explain the declaration and initialization of single and double dimensional arrays with example. Answer: An array is a type of data structure that store fixed-size sequential collection of elements of the same type. If the variables num0,num1,…num9 are of same type(Integer), we can declare them with an array instead of declaring them individually as shown below: int num[10]; //size of the data structure is 10. Elements are num[0],num[1],…,num[9] num[0] num[1] num[2] num[3] num[4] num[5] num[6] num[7] num[8] num[9] Syntax of declaring single-dimensional array: type arrayname [size ]; where type is any valid C data type. arrayname is name of the array and size indicate the number of elements in the array. For example, 10 elements array of integer type with arrayname num can be declared as below: int num[10]; Initialization of single-dimensional array: int num[10]={42,54,1,96,8,35,17,23,,67,34}; The above initialization of array assign as below: num[0]=42 num[1]=54 num[2]=1 num[3]=98 num[4]=8 num[5]=35 num[6]=17 num[7]=23 num[8]=67 num[9]=34 Syntax of declaring double-dimensional array: type arrayname [arrayrow ][arraycol]; where type is any valid C data type. arrayname is name of the array. arrayrow indicates the number of row of elements in the array and arraycol indicates the number of column of elements in the array . For example, 10 elements array of integer type having arrayname num with 2 rows and 5 columns can be declared as below: int num[2][5]; Initialization of double-dimensional array: int num[2][5]={ {42, 54, 1, 96, 8}, {35, 17, 23, 67, 34} };

Page: 1

Guide for The C Programming Language

Chapter 3

The above initialization of array assign as below: num [0][0]=42 num [0][1]=54 num [0][2]=1 num [0][3]=98 num [0][4]=8 num [1][0]=35 num [1][1]=17 num [1][2]=23 num [1][3]=67 num [1][4]=34 Q2. Write a C program to search a name in a list of names using Binary Searching technique. Answer: #include #include void main() { Char name[20][20], key[20]; int n,i,low,high,mid,found=0; printf(“Enter the number of name:\n”); scanf(“%d”,&n); printf(“Enter the name in ascending order:\n”); for(i=0; i