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

CM304

The document summarizes key points about arrays from a computer engineering lecture, including: 1) Arrays can be accessed by specifying a subscript or index number with the array name. Values can be assigned to array elements using the assignment operator. 2) Array elements can be initialized within the declaration statement by enclosing initialized values in curly braces. If fewer values are provided than the array size, remaining elements are assigned zero. 3) A quiz is provided to test understanding of array initialization, accessing elements, and what values are assigned if initialization is incomplete.

Uploaded by

api-3849444
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

CM304

The document summarizes key points about arrays from a computer engineering lecture, including: 1) Arrays can be accessed by specifying a subscript or index number with the array name. Values can be assigned to array elements using the assignment operator. 2) Array elements can be initialized within the declaration statement by enclosing initialized values in curly braces. If fewer values are provided than the array size, remaining elements are assigned zero. 3) A quiz is provided to test understanding of array initialization, accessing elements, and what values are assigned if initialization is incomplete.

Uploaded by

api-3849444
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name of the faculty : P. Alimullah Khan.
Designation : Lecturer
Branch : Computer Engineering
Institute : V.K.R & V.N.B Polytechnic, Gudivada
Year/Semester : III Semester
Subject : UNIX & C.
Subject Code : CM-304.
Topic : Understand basics of Arrays.
Duration : 50 Min
Sub Topic : Access and initialization of array .
Teaching Aids : Diagram and animations,ppt.
CM304.58TO59 1
Objective

On completion of this period, you would be able


to know
 How to the access an array elements.
 Initialize an array.

CM304.58TO59 2
Recap

Arrays: definition & declaration.


 One dimensional arrays.
 Two-dimensional arrays.
 Three-dimensional arrays.

CM304.58TO59 3
Accessing array elements
 A particular array can be accessed by
specifying appropriate subscript or index
number with the array name.
 To assign a value from out side the array by
using assignment operator.

CM304.58TO59 4
Example of accessing array elements

Example1
matrix [0][3]=1;
the value ‘1’ is assigned to the element
at first row and fourth column.
0 1 2
0 3

CM304.58TO59 5
Array initialization

 Array elements can be initialized within their

declaration statements in the same manner


as variables.
 The initialization elements must be included
within braces { }.

CM304.58TO59 6
General form of initialization of array
Datatype variable name [size]={list of values};

 The values in the list are separated by commas.

Example:
int a[5]={2,3,5,6,7};

CM304.58TO59 7
General form of initialization of array

 Suppose you enter three values only out of five


values, then the array takes the three values
and the remaining two positions are zero.
For example,
int a[5]={2,3,5};
The array takes
int a[5]={2,3,5,0,0};

CM304.58TO59 8
Example of array initialization

char name[5]={‘a’, ’r’, ’r’, ’a’, ’y’};


Here name is array name.
array size is 5.
name[0] is initialized ‘a’,
name[1] is initialized ‘r’,
name[2] is initialized ‘r’,
name[3] is initialized ‘a’,
name[4] is initialized ‘y’.

CM304.58TO59 9
Simple program using initialization of arrays
#include<stdio.h> List of
initialized
main( ) values
Output:
{ The initialized
int marks[5]={45,55,50,75,60}; marks are
45
int i; 55
50
for(i =0;i<5;i++) 75
{ 60

printf (“The initialized marks are %d \n”, marks[ i ] );


}

CM304.58TO59 10
Summary

In this class, we have learnt about…

How to initialize the array elements.

How to access array elements.

CM304.58TO59 11
Quiz

1.Which one is the general form of array


initialization?
a) data type array_name [size]={list of values}.
b) data type array_name [size]=[list of values].
c) data type variable name= {list of values

CM304.58TO59 12
Quiz

1.Which one is the general form of array


initialization?
a) data type array_name [size]={list of values}.
b) data type array_name [size]=[list of values].
c) data type variable name= {list of values

CM304.58TO59 13
Quiz

2. Choose correct statement for initializing an


array?
a) int a[5]= {1,2,3,4,5};
b) int a[5]= 1,2,3,4,5;
c) int a[5]= (1 2 3 4 5);

CM304.58TO59 14
Quiz

2. Choose correct statement for initializing an


array?
a) int a[5]= {1,2,3,4,5};
b) int a[5]= 1,2,3,4,5;
c) int a[5]= (1 2 3 4 5);

CM304.58TO59 15
Quiz

3. What is the output for the following problem?


char a[8]={‘k’, ’u’, ’m’, ’a’, ’r’};
printf(“%c”,a[6]);
a) r.
b) ‘\0’
c) garbage value.

CM304.58TO59 16
Quiz

3. What is the output for the following problem?


char a[8]={‘k’, ’u’, ’m’, ’a’, ’r’};
printf(“%c”,a[6]);
a) r.
b) ‘\0’
c) garbage value.

CM304.58TO59 17
Quiz

4. If the number of initialization is less than the


number of elements in array, what are the
remaining elements are?
a) zero.
b) one.
c) empty.

CM304.58TO59 18
Quiz

4. If the number of initialization is less than the


number of elements in array, what are the
remaining elements are?
a) zero.
b) one.
c) empty.

CM304.58TO59 19
Quiz

5.To assign a value from outside the array by


using which operator?
a) logical operator &.
b) assignment operator.
c) comma operator.
.

CM304.58TO59 20
Quiz

5.To assign a values from outside the array by


using which operator?
a) logical operator &.
b) assignment operator.
c) comma operator.
.

CM304.58TO59 21
Quiz

6. The assigning values mentioned within or


without ________?
a) parenthesis.
b) square brackets.
c) single quotes.

CM304.58TO59 22
Quiz

6. The assigning values mentioned within or


without ________?
a) parenthesis.
b) square brackets.
c) single quotes.

CM304.58TO59 23
Quiz

7. - - matrix[0] [5]; here where the decrement is


located?
a) 0th row 5th column.
b) 1st row 6th column.
c) 1st column 6th row.

CM304.58TO59 24
Quiz

7. - - matrix[0] [5]; here where the decrement is


located?
a) 0th row 5th column.
b) 1st row 6th column.
c) 1st column 6th row.

CM304.58TO59 25
Frequently Asked Questions

1. Explain how array elements can be accessed


with suitable example.
2. Explain how array initialization can be done,
its general form of initialization with suitable
example.

CM304.58TO59 26

You might also like