Unit-2
Unit-2
SOLVING AND C
PROGRAMMING
UNIT-2
ARRAYS AND STRINGS
Array
• Array is a collection of same type elements under the same
variable identifier referenced by index number
• Arrays – Are a sequence of memory locations referred to
by a common name – Can store multiple values of the
same data type
• The individual data items are called elements of the array.
• Array data structure, an arrangement of items at equally
spaced addresses in computer memory
• Array data type, used in a programming language to
specify a variable that can be indexed
CHARACTERISTICS OF
ARRAYS IN C
• Array is a subscripted variable
• Random access via indexing operator []
• Indexing – starts at 0 –Ends at N-1 (where
N is maximum the array size)
• The name of the array refers to the address
of the first element in the array
TYPES OF ARRAYS
STATIC vs DYNAMIC
• Static arrays have their sizes declared from
the start and the size cannot be changed
after declaration
• Dynamic arrays that allow you to
dynamically change their size at runtime,
but they require more advanced techniques
such as pointers and memory allocation.
DECLARING SINGLE
DIMENSION ARRAYS
• SYNTAX: []
– data type specifies the data type of the values that will be stored in the
array
– dimension size indicates the maximum number of elements that can be
stored in the array
The name of the array refers to the address of the first element in the array
Example: int num[5];
<data type> <variable name>[<dimension size>
• This declares an array num of size 5
• Elements of the array are num[0], num[1], num[2], num[3] and num[4]
• 0, 1, 2, 3 and 4 are called subscripts or indexes
• The array name num refers to the address of num[0] i.e. first element of
the array
INITIALISING ARRAYS
• Initialisation of an array – Refers to
assigning values to the elements of the array
#include
void main()
{
int marks[5]={34,21,33,12,15};
}
STRING
• String is an array, the declaration of a string is the same as
declaring a char array.
– char string_var[30];
– char string_var[20] = “Initial value”;
Memory Storage for a String
• The string is always ended with a null character ‘\0’.
• The characters after the null character are ignored.
• e.g., char str[20] = “Initial value”; I n i t i a l v a l u e \0 ? ?..
ARRAYS OF STRINGS
• An array of strings is a two-dimensional array of characters in which each
row is one string.
– char names[People][Length];
– char month[5][10] = {“January”, “February”, “March”, “April”, “May”};
Input/output of a String
• The placeholder %s is used to represent string arguments in printf and scanf.
– printf(“Topic: %s\n”, string_var);
• The string can be right-justified by placing a positive number in the
placeholder. – printf(“%8s”, str);
• The string can be left-justified by placing a negative number in the
placeholder. – Printf(“%-8s”, str);
STRING FUNCTIONS (string.h)
DISTINCTION BETWEEN
CHARACTERS AND STRINGS
• The representation of a char (e.g., ‘Q’) and
a string (e.g., “Q”) is essentially different.
– A string is an array of characters ended with
the null character.
INPUT/OUTPUT OF
CHARACTERS AND STRINGS
• The stdio library provides getchar function which
gets the next character from the standard input.
– “ch = getchar();” is the same as “scanf(“%c”,
&ch);”
– Similar functions are putchar, gets, puts.
• For IO from/to the file, the stdio library also
provides corresponding functions.
– getc: reads a character from a file.
– Similar functions are putc, fgets, fputs.
SORTING
• Data are arranged according to their values.
Sorting algorithms:
1.Selection Sort
2.Bubble Sort
3. Insertion Sort
SEARCH
• Searching is the process of determining
whether or not a given value exists in a data
structure or a storage media.
• Two searching methods on one-dimensional
arrays: linear search and binary search.
LINEAR SEARCH
• The linear (or sequential) search algorithm on an
array is:
– Sequentially scan the array, comparing each
array item with the searched value.
– If a match is found; return the index of the
matched element; otherwise return –1.
• Note: linear search can be applied to both sorted
and unsorted arrays.
BINARY SEARCH
• Binary search uses a recursive method to search an array to find a
specified value
• The array must be a sorted array.
• If the value is found, its index is returned
• If the value is not found, -1 is returned
• Note: Each execution of the recursive method reduces the search
space by about a half.
• An algorithm to solve this task looks at the middle of the array or
array segment first.
• If the entire array (or array segment) has been searched in this way
without finding the value, then it is not in the array
• The binary search algorithm is extremely fast compared to an
algorithm that tries all array elements in order
• If desired, the recursive version of the method search can be
converted to an iterative version that will run more efficiently