0% found this document useful (0 votes)
3 views11 pages

Understanding Arrays in C Programming

This lecture covers the concept of arrays in C programming, explaining their declaration, access, and initialization. It details how to declare arrays, access their elements using indices, and initialize them either during compilation or execution. Additionally, it presents problems that involve user input to demonstrate array usage, such as finding the minimum value, the second smallest number, and counting prime numbers in an array.

Uploaded by

sun29021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Understanding Arrays in C Programming

This lecture covers the concept of arrays in C programming, explaining their declaration, access, and initialization. It details how to declare arrays, access their elements using indices, and initialize them either during compilation or execution. Additionally, it presents problems that involve user input to demonstrate array usage, such as finding the minimum value, the second smallest number, and counting prime numbers in an array.

Uploaded by

sun29021
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Lecture 12

Arrays
CSE115: Computing Concepts
Introduction to Array

• In C, a group of items of the same type can be set up


using Array
• An array is a group of consecutive memory locations
related by the fact that they all have the same name
and the same type.
• The compiler must reserve storage (space) for each
element/item of a declared array.
• The size of an array is static (fixed) throughout
program execution.
• To refer to a particular location or element in the
array, we specify the name of the array (index or
subscript) and the position number of the particular
element in the array.
Array Declaration

• Array declaration is made by specifying the data type, it’s


name and the number of space (size) so that the computer
may reserve the appropriate amount of memory.
• General syntax:
data_type array_name[size];
• Examples:
• int my_array[100];
• char name[20];
• double bigval[5*200];
• int a[27], b[10], c[76];
Accessing Array Elements
Name of array
(Note that all
elements of this
• Declare array have the
• int c[12]; same name, c)

• To refer to an element, specify c[0] ?

• Array name c[1] ?


c[2] ?
• Position number c[3] ?

• Format: c[4]
c[5]
?
?
arrayname[ position number ] c[6] ?
• c[ 0 ], c[ 1 ]...c[ 11 ] c[7] ?
c[8] ?
c[9] ?
c[10] ?
c[11] ?

Position number
of the element
within array c 4
Accessing Array Elements
Name of array
(Note that all
elements of this
• Declare array have the
• int c[12]; same name, c)

• To refer to an element, specify c[0] 5

• Array name c[1] ?


c[2] ?
• Position number c[3] ?

• Format: c[4]
c[5]
?
?
arrayname[ position number ] c[6] ?
• c[ 0 ], c[ 1 ]...c[ 11 ] c[7] ?

• Example
c[8] ?
c[9] ?
• c[0] = 5; c[10] ?
c[11] ?

Position number
of the element
within array c 5
Accessing Array Elements
Name of array
(Note that all
elements of this
• Declare array have the
• int c[12]; same name, c)

• To refer to an element, specify c[0] 5

• Array name c[1] ?


c[2] -12
• Position number c[3] ?

• Format: c[4]
c[5] ?
?

arrayname[ position number ] c[6] ?


• c[ 0 ], c[ 1 ]...c[ 11 ] c[7] ?

• Example
c[8] ?
c[9] ?
• c[2] = -12; c[10] ?
c[11] ?

Position number
of the element
within array c 6
Array Initialization
• During compilation
• int n[ 5 ] = { 1, 2, 3 };
• If not enough initializers, rightmost elements become 0
• int n[ 5 ] = { 0 };
• All elements 0
• int n[] = { 1, 2, 3, 4, 5 };
• If size omitted, initializers determine it
• 5 initializers, therefore 5 element array

7
Array Initialization
• During execution:
• Using loop to initialize all elements to zero
int arr[3], index;
for (index = 0; index < 3; index++)
arr[index] = 0;
• Using loop and asking the user to specify the value for each
element.
int arr[3], index;
for (index = 0; index < 3; index++)
{
printf (“arr[%d]:”,index);
scanf(“%d”,&arr[index]);
} 8
Problem
• Take N numbers from the user as input, put them in an
array and output the minimum of them.
For example, if user input is
25 14 78 5 90 67 32 85
then your program’s output is
5
Problem
• Take N numbers from the user as input, put them in an
array and output the 2nd smallest of them.
For example, if user input is
25 14 78 5 90 67 32 85
then your program’s output is
14
Problem
• Take N numbers from the user as input, put them in an
array and print the number of primes present in them.
For example, if user input is
25 14 78 5 90 31 32 85
then your program’s output is
2

You might also like