0% found this document useful (0 votes)
79 views

Arrays and Matrices

An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Arrays and Matrices

An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Programming in C and Data Structures

MODULE III
C HAPTER-4: ARRAYS

In this chapter we are going to learn the following topics:

• Basic C oncepts of Arrays


• Why we need an Array
• Definition of Array
• Properties of an Array
• One-Dimensional Arrays
• Two-Dimensional Arrays
• Arrays and Functions
• Sorting and searching techniques
• Declaration and initialization of arrays
• Assigning values to an array
• Using #define for Array Sizes
• Programming Examples
• Exercise
• Quiz

4.1 To Understand Basic C oncepts of Arrays

Arrays 1
Programming in C and Data Structures

Array is a data structure which collects related data items that share a common array name and
data type is called an array.
4.4 Properties of an Array

Arrays 2
Programming in C and Data Structures

Num[3]

Arrays 3
Programming in C and Data Structures

Num[4]

The values can be assigned as follows:


Num[0]=57;
Num[1]=20;
Num[2]=56;
Num[3]=17;
Num[4]=23;

The table below shows the values that are stored in the particular numbers.
Num[0]
Num[1]
Num[2]
Num[3]
Num[4]
57
20
56
17
23

4.5.1 Different applications of One dimensional Arrays

Arrays 4
Programming in C and Data Structures

/* Determine sum of /* values in x must be sorted ! */


values in the array. */ double median(double x[], int n)
sum = x[0]; {
for (k=1; k<=n-1; k++) /* Declare variables. */
sum = sum + x[k]; int k;
/* Return avg value. */
return sum/n;
}

Arrays 5
Programming in C and Data Structures

double median_x;
/* Determine median of
values in the array. */
k = floor(n/2);
if( n % 2 != 0)
median_x = x[k];
else
median_x = (x[k-1]+x[k])/2;
/* Return median value. */
return median_x;
}

Arrays 6
Programming in C and Data Structures

Function variance() Function Standard Deviation

double variance(double x[], int n)


{
/* Declare variables. */ double std_dev(double x[], int n)
int k; {
double mu, sum=0; /* Return standard deviation */
mu = mean(x, n); return sqrt(variance(x, n));
}
for (k=0; k<=n-1; k++)
sum = sum + pow(x[k]-mu, 2);
/* Return variance value. */
return sum/(n-1);
}

4.5.2 What is a multidimensional array?

programming language allows programmer to create arrays of arrays known as multidimensional


arrays.

For example: float a[2][6];

Here, a is an array of two dimension, which is an example of multidimensional array. This array
has 2 rows and 6 columns.

Now let us discuss the “advantages and disadvantages of arrays?”

Advantages:

Arrays 7
Programming in C and Data Structures

Arrays 8
Programming in C and Data Structures

For example: Consider the unsorted elements


10, 50, 25, 15

Arrays 9
Programming in C and Data Structures

After sorting in ascending order, we get the following


10,15, 25, 50

1. Write a program to sort the arrays in ascending order using bubble sort:

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

void bubble_sort(int a[], int n)


{
int i, j, temp;
for(j=1; j<n; j++)
{
for(i=0; i<n-j; i++)
{
if(a[i]>=a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
}
void main()
{
int a[20],b[20],n, i;
printf(“Enter the number of elements”);
scanf(“%d”, &n);
printf(“Enter the unsorted elements”);
for(i=0; i<n; i++)
{
scanf(“%d”, a[i]);
b[i]=a[i];
}
bubble_sort(a, n);
printf(“Given Array is:”);
for(i=0;i<n;i++)
{
printf(“%d, %d”,b[i], a[i]);

Arrays 10
Programming in C and Data Structures

}
}

Advantages of Bubble sort:

Arrays 11
Programming in C and Data Structures

return -1;
}
void main()
{
int n, key, a[20], i, pos;
printf(“enter the value of n”);
scanf(“%d”, &n);
prinf(“enter the elements of array”);
for(i=0; i<n;i++)
scanf(“%d”, &a[i]);
printf(“enter the item to be searched”);
scanf(“%d”, &key);
pos=linear(key, a, n);

if(pos==-1)
printf(“Item not found”);
else
printf(“Item found\n”);
}

Advantages of Linear search

Arrays 12
Programming in C and Data Structures

{
int low, high, mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;

if(key==a[mid])
return mid;
if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
return -1;
}

void main()
{
int n, a[10], key, pos;
printf(“enter the no. of elements”);
scanf(“%d”, &n);
printf(“enter the elements”);
for(i=0;i<n;i++)
{
Scanf(“%d”,&a[i]);
}
prinf(“enter the elements to be searched”);
scanf(“%d”, &key);
pos=binary_search(key, a, n);
if(pos==-1)
printf(“Item not found”);
else
printf(“Item found\n”);
}

Advantages of binary search:

Arrays 13
Programming in C and Data Structures

Disadvantages of binary search:

Arrays 14
Programming in C and Data Structures

The type specifies the type of element that will be contained in the array, such as int, float,or char
and the size indicates the maximum number of elements that can be stored inside the array.

Example: 1. float weight[40];


Declares the weight to be an array containing 40 real elements. Any subscripts 0 to 39 are valid.

2. int group1[11];
Declares the group1 as an array to contain a maximum of 10 integer constants.

The C language treats character strings simply as arrays of characters. The size in a character
string represents the maximum number of characters that the string can hold.

For example:
char text[10];
Suppose we read the following string constant into the string variable text.
“HOW ARE Y OU”

Each character of the string is treated as an element of the array text and is stored in the
memory as follows.
‘H’
‘O’
‘W’
‘A’
‘R’
‘E’
‘Y ’
‘O’
‘U’
‘\o’
When the compiler sees a character string, it terminates it with an additional null character. Thus,
the element text[11] holds the null character ‘\o’ at the end. When declaring character arrays, we
must always allow one extra element space for the null terminator.

4.7.2 Initialization of arrays


The general form of initialization of arrays is:

static type array-name[size]={ list of values};

Arrays 15
Programming in C and Data Structures

The values in the list are separated by commas.


For example, the statement below shows
static int num[3]={2,2,2};
Will declare the variable num as an array of size 3 and will assign two to each element. If the
number of values is less than the number of elements, then only that many elements will be
initialized. The remaining elements will be set to zero automatically.

For example:
static float num1[5]={0.1,2.3,4.5};
Will initialize the first three elements to 0.1,2.3 and 4.5 and the remaining two elements to zero.
The word static used before type declaration declares the variable as a static variable. In some
cases the size may be omitted. In such cases, the compiler allocates enough space for all
initialized elements.
For example, the statement
static int count[ ]= {2,2,2,2};
Will declare the counter array to contain four elements with initial values 2.
Character arrays may be initialized in a similar manner. Thus, the statement

static char name[ ]={ ‘S ‘W,’A,’N}


Declares the name to be an array of four characters, initialized with the string “SWAN”
There certain draw backs in initialization of arrays.
There is no convenient way to initialize only selected elements.
There is no shortcut method for initializing a large number of array elements.

4.9 Assigning values to an array

for loops are often used to assign values to an array:

int list[5], i;
for(i=0; i<5; i++){
list[i] = i;
}
OR
for(i=0; i<=4; i++){
list[i] = i;
}

Arrays 16
Programming in C and Data Structures

4.8 Using #define for Array Sizes

#define SIZE 39
#define GRADES 5
int main ( void )
{
int score [ SIZE ] ;
int gradeCount [ GRADES ] ;



}

4.9 Arrays and Functions


Arrays can be passed to functions using two methods:
1. Passing individual element of the array.
2. Passing the whole array.

1. Individual elements of an array can be passed as regular arguments.

While passing array elements some rules to be followed:

Arrays 17
Programming in C and Data Structures

int array[5] = {1,2,3,4,5};


int res;
res = sum(array[2], array[4]);

Arrays 18
Programming in C and Data Structures

printf(“Result = %d”,res);
}

2. Passing the whole array.


Suppose, we want to pass whole array to a function. In such situation, we must follow
the two rules:

Arrays 19
Programming in C and Data Structures

printf(“%5d”, a[i][j]);
printf(“\n”);
}

Arrays 20
Programming in C and Data Structures

2. Program showing one-dimensional array

main()
{
int i;
float a[10],value1,total;
printf(“Enter 10 Real numbers\n”);
for(i=0;i<10;i++)
{
scanf(“%f”, &value);
x[i]=value1;
}
total=0.0;
for(i=0;i<10;i++)
total=total+a[i]*a[i];
printf(“\n”);
for(i=0;i<10;i++)
printf(“x[%2d]= %5.2f\n”, i+1, x[i]);
printf(“\ntotal=%.2f\n”, total);
}

3. Program to print multiplication tables


#define R1 4
#define C1 4
main()
{
int row,col,prod[R1][C1];
int i,j;
printf(“ MULTIPLICATION TABLE \n\n”);
printf(“ “);
for(j=1;j<=C1;j++)
printf(“%4d”,j);
printf(“\n”);
printf(“-------------------------------------------\n”);
for(i=0;i<R1;i++)

Arrays 21
Programming in C and Data Structures

{
row=i+1;
printf(“%2d|”, R1);
for(j=1;j<=C1;j++)
{
col=j;
prod[i][j]=row*col;
printf(“%4d”, prod[i][j]);
}
printf(“\n”); } }

Output
MULTIPLICATION TABLE
1234
---------------------------------------
1 |1 2 3 4
2 |2 4 6 8
3 |3 6 9 12
4 |4 8 12 16

4. Program to show swapping of numbers.


#include<stdio.h>
main()
{
int x[10], i,j,temp;
for(i=0;i<=9;++i)
scanf(“%d”, &x[i]);
for(j=0;j<=8;j+=2)
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
for(i=0;i<=9;++i)
printf(“%d”, x[i]);
printf(“\n”);
}

Arrays 22
Programming in C and Data Structures

5. Program to sort a list of numbers using bubble sort:

#define N 10
main()
{
int i,j,k;
float a[N],t;
printf(“Enter the number of items\n”);
scanf(“%d”, &n);
printf(“Input %d values \n”, n);
for(i=1; i<=n ;;i++)
scanf(“%f”, &a[i]);
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i; j++)
{
if)a[j]<=a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
else
continue;
}
}
for(i=1;i<=n;i++)
printf(“%f”, a[i]);
}

6. Program to calculate standard deviation


#include<math.h>
#define MAX 100
main()
{

Arrays 23
Programming in C and Data Structures

int i,n;

Arrays 24
Programming in C and Data Structures

float val[MAX ],deviation,sum,ssqr,mean,var,stddev;


sum=ssqr=n=0;
printf(“Input values: input-1 to end\n”);
for(i=1;i<MAX ;i++)
{
scanf(“%f”, &val[i]);
if(val[i]==-1)
break;
sum+=val[i];
n+=1;
}
mean=sum/(float)n;
for(i=1;i<=n;i++)
{
deviation=val[i]-mean;
ssqr+=deviation*deviation;
}
var=ssqr/(float)n;
stddev=sqrt(var);
printf(“\n Number of items:%d\n”,n);
printf(“Mean: %f \n”, mean);
printf(“Standard deviation: %f\n”, stddev);
}

7. Program to find the largest and smallest of numbers

#include<stdio.h>
main()
{
int i,s, largcount,smcount;
float num[30],lar,small;
printf(“\n size of array (MAX 30): \t”);
scanf(“%d”, &size);
printf(“\n Array elements:\t”);
for(i=0;i<size;i++)
scanf(“%f”, &num[i]);
for(i=0;i<size;i++)
printf(“%f”, &num[i]);
lar=small=num[0];
larcount=smcount=1;
for(i=1;i<size;i++)

Arrays 25
Programming in C and Data Structures

{
if(num[i]>lar)
{
lar=num[i];
larcount=i+1;
}
elseif(num[i]<small)
{
small=num[i];
smcount=i+1;
}
}
printf(“\n Largest value is % f found at %d”, lar,larcount);
printf(“\n Smallest value is %f found at %d “, small, smcount);
}

8. Design a program to find maximum value in data array where the elements in the
array are random number between 0 - 99

#include <stdio.h>
{
int data[10], max, i;
for (i=0; i<5; i++)
data[i] = rand()%100;
max = data[0];
for (i=1; i<5; i++)
{
if (data[i] > max)
max = data[i];
}
printf("Max = %d\n",max);
return 0 ;
}

Output:
Consider 5 random numbers: 35, 4,78,12,45
Max = 78

Arrays 26
Programming in C and Data Structures

9. Write a program to generate Fibonacci numbers using Arrays


void Fibonacci (int a[], int n)
{
int i;
a[0]=0;
a[1]=1;
/* generate other Fibonacci numbers*/
for(i=2;i<n;i++)
{
a[i]=a[i+1] +a[i+2];
}
}
void main()
{
int n, i, a[100];
printf(“Enter the value for n \n “);
scanf(“%d”, &n);
Fibonacci(a, n);
Printf(“ The Fibonacci numbers are:”);
for(i=0;i<n;i++)
{
printf(“%d\n,a[i]”);
}
}

10. Write a program to add all the element of single dimensional array.

#include<iostream.h>
#include<conio.h>

int main()
{
int Arr[100],n,sum=0;
cout<<"Enter number of elements you want to insert ";
cin>>n;

Arrays 27
Programming in C and Data Structures

for(int i=0;i<n;i++)

Arrays 28
Programming in C and Data Structures

{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

for(i=0;i<n;i++)
sum+=Arr[i];

cout<<"\nThe sum of Array is :"<<sum;


cout<<"\nThe average of Array is :"<<sum/i;

getch();
return 0;
}

11. Write a program to find largest and smallest array in an array.

#include <stdio.h> //include header file

int main () //start of main fcn


{

int values[ 20 ]; //delcares array and how many elements


int small,big; //declares integer
big=small=values[0]; //assigns element to be highest or lowestvalue

for ( int i = 0; i < 20; i++ )


{
printf("Enter value of i”);
scanf(“%d”, &values[i]);
}

for (int i = 0; i < 20; i++) //works out bigggest number


{
if(values[i]>big) //compare biggest value with current element
{
big=values[i];
}
}

Arrays 29
Programming in C and Data Structures

for (int i = 0; i < 20; i++) //works out smallest number


{
if(values[i]<small) //compares smallest value with current element
{
small=values[i];

Arrays 30
Programming in C and Data Structures

}
}

printf("The biggest number is %d",big ); //prints outs biggest no


printf("The smallest number is %d ",small); //prints out smalles no
}

12. Write a c program to find out second largest element of an unsorted array.

#include<stdlib.h>
main()
{
int a[100],i,num,n,max;
printf("enter number of elements to be entered");
scanf("%d",&n);
printf("enter numbers");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
num=a[0];
for(i=0;i<n;i++)
{
if(a[i]>num)
{
if(a[i]>max)
{num=max;
max=a[i];}
if(a[i]<max)
num=a[i];
}
}
printf("second largest no. is %d",num);
system("pause");
}

13. Write a c program for delete an element at desired position in an array.

#include<stdio.h>
#include<stdlib.h>
main()
{

Arrays 31
Programming in C and Data Structures

int a[100],i,n,loc;
printf("enter number of elements to be entered");
scanf("%d",&n);
printf("enter numbers");
for(i=0;i<n;i++)

Arrays 32
Programming in C and Data Structures

scanf("%d",&a[i]);
printf("enter the position at which element has to be deleted");

scanf("%d",&loc);
for(i=loc-1;i<n-1;i++)
a[i]=a[i+1];
for(i=0;i<n-1;i++)
printf("%d\n",a[i]);
system("pause");
}

14. Write a c program for insert an element at desired position in an array.

#include <stdio.h>

void main()
{
int array[10];
int i, j, n, m, temp, key, pos;

printf("Enter how many elements \n");


scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;

Arrays 33
Programming in C and Data Structures

}
}
}
printf("Sorted list is \n");
for (i = 0; i < n; i++)

Arrays 34
Programming in C and Data Structures

{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted \n");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
array[pos] = key;
printf("Final list is \n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
}
15. Write a program to swap two integers with and without using temporary
variables.
#include<stdio.h>
#include<conio.h>
int main()
{
int first, second; // declaring two variables
printf(“Read two values ”);
scanf(“%d%d”, &first, &second);
first=first+second;
second=first-second;
first=first-second;
printf(“After swapping the values are: %d, %d ”, first, second);
getch();

Arrays 35
Programming in C and Data Structures

return 0;
}

Arrays 36
Programming in C and Data Structures

16. Write the program to add and subtract two matrices

# i nc l ud e < s t d i o . h>

v o i d ma i n( )
{
i nt a r r a y 1 [ 1 0] [ 1 0] , a r r a y 2[ 1 0] [ 1 0] , a r r a y s um[ 1 0] [ 1 0] ,
a r r a y d i f f [ 1 0] [ 1 0] ;
i nt i , j , m, n, o p t i o n;

p r i nt f ( " Ent e r t he o r d e r of t he ma t r i x a r r a y 1 a nd a r r a y 2 \ n" ) ;


s c a nf ( " %d %d " , &m, &n) ;
p r i nt f ( " Ent e r t he e l e me nt s o f ma t r i x a r r a y 1 \ n" ) ;
f or (i = 0; i < m; i ++)
{
f or (j = 0; j < n; j ++)
{
s c a nf ( " %d " , &a r r a y 1 [ i ] [ j ] ) ;
}
}

p r i nt f ( " Ent e r t he e l e me nt s o f ma t r i x a r r a y 2 \ n" ) ;


f or (i = 0; i < m; i ++)
{
f or (j = 0; j < n; j ++)
{
s c a nf ( " %d " , &a r r a y 2[ i ] [ j ] ) ;
}
}
p r i nt f ( " Ent e r y o ur o p t i o n: 1 f or Ad d i t i o n a nd 2 f o r Sub t r a c t i o n \ n" ) ;
s c a nf ( " %d " , &o p t i o n) ;
s wi t c h ( o p t i o n)
{
c as e 1 :
f or (i = 0; i < m; i ++)
{
f or (j = 0; j < n; j ++)
{
a r r a y s um[ i ] [ j ] = ar r ay1 [ i ] [ j ] + a r r a y 2[ i ] [ j ] ;
}
}
p r i nt f ( " Sum ma t r i x i s \ n" ) ;
f or (i = 0; i < m; i ++)
{
f or (j = 0; j < n; j ++)
{
p r i nt f ( " %3d " , a r r a y s um[ i ] [ j ] ) ;
}
p r i nt f ( " \ n" ) ;

Arrays 37
Programming in C and Data Structures

}
br e ak;
c a s e 2:
f or (i = 0; i < m; i ++)
{
f or (j = 0; j < n; j ++)
{

Arrays 38
Programming in C and Data Structures

ar r aydi f f [ i ] [ j ] = ar r ay1 [ i ] [ j ] - a r r a y 2[ i ] [ j ] ;
}
}
p r i nt f ( " Di f f e r e nc e ma t r i x i s \ n" ) ;
f or (i = 0; i < m; i ++)
{
f or (j = 0; j < n; j ++)
{
p r i nt f ( " %3d " , ar r aydi f f [ i ] [ j ] ) ;
}
p r i nt f ( " \ n" ) ;
}

br e ak;
}

17. Program to find the transpose of matrices.

# i nc l ud e < s t d i o . h>

v o i d ma i n( )
{
s t at i c i nt a r r a y [ 1 0] [ 1 0] ;
i nt i , j , m, n;

p r i nt f ( " Ent e r t he o r d e r of t he ma t r i x \ n" ) ;


s c a nf ( " %d %d " , &m, &n) ;
p r i nt f ( " Ent e r t he c o e f i i c i e nt s o f t he ma t r i x \ n" ) ;
f or (i = 0; i < m; ++i )
{
f or (j = 0; j < n; ++j )
{
s c a nf ( " %d " , &a r r a y [ i ] [ j ] ) ;
}
}
p r i nt f ( " T he g i v e n ma t r i x i s \ n" ) ;
f or (i = 0; i < m; ++i )
{
f or (j = 0; j < n; ++j )
{
p r i nt f ( " %d " , ar r ay[ i ] [ j ] ) ;
}
p r i nt f ( " \ n" ) ;
}
p r i nt f ( " T r a ns p o s e o f ma t r i x i s \ n" ) ;
f or (j = 0; j < n; ++j )
{

Arrays 39
Programming in C and Data Structures

f or (i = 0; i < m; ++i )
{
p r i nt f ( " %d " , ar r ay[ i ] [ j ] ) ;
}
p r i nt f ( " \ n" ) ;
}

4.11 Exercise
1. What is an array? What is the use of using array? How are they declared in C?
what are the rule to be followed while using arrays? (JULY /AUG 2005).
2. Write a C program to multiply two matrices A, B and store the result in C.
(JAN/FEB 2003)
3. Write a note on one dimensional and two dimensional arrays (JULY /AUG 2003)
4. Write a C program to read ‘n’ integer numbers interactively and to print the
biggest and smallest of ‘n’ numbers (JULY /AUG 2004)
5. Given two sets A and B of integers, write a program to read them, determine its
UNION and INTERSECTION and print the resultant sets. (FEB/MAR 2005)
6. Write a program to find the intersection of 2 arrays A and B with size m and n
respectively (JAN2006)
7. Explain initialization and declaration of 2D array (JUNE/JULY 2014)
8. Write a c program for insert an element at desired position in an array.
9. Write a c program for delete an element at desired position in an array.

10. Write a c program to find out second largest element of an unsorted array.

11. Write a program to find largest and smallest array in an array

12. Write a program to add all the element of single dimensional array.

13. What is multi dimensional array? How they are declared?

14. Differentiate between i++ and i--?

15. What are the advantages and disadvantages of arrays?

16. Write a program to swap two integers with and without using temporary variables

17. Write the program to add and subtract two matrices

18. Program to find the transpose of matrices.

19. Program to find determinants of matrices.

Arrays 40
Programming in C and Data Structures

4.12 Quiz:

1. What will happen if in a C program you assign a value to an array element


whose subscript exceeds the size of array?
a) The element will be set to 0;
b) The complier may report an error
c) The program may crash if some important data gets overwritten.

Arrays 41
Programming in C and Data Structures

d) The array size would appropriately grow.

2. Are the expressions arr and &arr same for an array of 10 integers
a) Y ES b) NO

3. What does the following declaration mean?


int (*ptr) [10];
a) ptr is array of pointers to 10 integers
b) ptr is pointer to an array of 10 integers
c) ptr is an array of 10 integers
d) ptr is an pointers to an array

4. In C, if you pass an array as an argument to a function, what actually gets passed?


a) Value of elements in array b)First elements of the array
c) Base address of the array d) Address of the last elements of the array

5. A pointer to a block of memory is effectively same as an array


a) TRUE b) FALSE

6. Does this mentioning array name gives the base address in all the contexts?
a) Y ES b) NO

7. Is there any difference int the following declaration?


int fun(int arr[2]);
int fun(int arr[]);
a)Y ES b)NO

8. What will be the output of the program ?


#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}

Arrays 42
Programming in C and Data Structures

a) 2, 1, 15 b) 1, 2, 5 c) 3, 2, 15 d)2, 3, 20

9. The index or subscript value for an array of size n ranges from


a) 1 to n-1 b) 0 to n-1 c)1 to n d) 0 to n

10. What will be output if you will execute following c code?


#include<stdio.h>
void main()
{
char arr[11]="The African Queen";
printf("%s",arr);

}
a) The African Queen b) Queen c)Queen d) Null

11. What will be output if you will execute following c code?


#include<stdio.h>
void main()
{
char arr[7]=”Network”
printf("%s”, arr);
}

12. The number of elements in array A[3][4] is (JUNE/JULY 2014)


a) 8 b)12 c)16 d) none of these

13. If A[4] is declaration, then the first and last array index will be
(JUNE/JULY 2014)
a) 1,4 b) 0, 3 c) 3, 0 d) none of these

14. Given A[3][2] = { 1, 2, 3, 4, 5, 6}; The element in 3rd row 2nd col is
a) 3 b) 4 c) 6 d) 2

Arrays 43

You might also like