Module 3
Arrays and strings
What is an Array?
An array is a collection of elements of the same datatype that are referred to
through a common name. (or)
An array is a container that holds a fixed number of elements, all of the same data
type. It allows you to store and access multiple values under a single variable name,
making it easy to work with sets of related data.
The lowest address corresponds to the first element and the highest address to
the last element.
The elements of an array are accessed with reference to its position in the array,
that is called Index or Subscript.
The memory locations in the array are known as elements of the array. The total
number of elements in the array is called Length of Array or Length.
Why an Array?
• For example, we have a list of 1000 students' marks of an integer type. If using the basic
data type (int), we will declare something like the following…
int studMark0, studMark1, ...studMark999
• But by, using an array, we can declare it like this,
int studMark[1000];
• This will reserve 1000 contiguous memory locations for storing the students’ marks.
Types of Arrays
Arrays can have from one to several dimensions, such as:
• One-Dimensional Array
• Two-Dimensional Array
• Multidimensional Array
The most common array type is the string, which is simply an array of
characters terminated by a null character(\0).
Indexing in Arrays:
• In most programming languages, array indices start at 0 rather than 1. This
means the first element is at index 0, the second at index 1, and so on.
• We can access specific elements in an array using their index, such as
array[0] or array[index].
Single-Dimension Arrays
• The general form for declaring a single-dimension array is
type var_name[size];
• Like other variables, arrays must be explicitly declared so that the compiler can allocate
space for them in memory, so for this, we give the length to be declared.
• Here, type declares the base type of the array, which is the type of each element in the
array, and size defines how many elements the array will hold. For example, to declare a
100-element array called balance of type double, use this statement: double balance[100];
• An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the array name.
• For example, balance[3] = 12.23; assigns element number 3 in balance the value 12.23.
• Above, we declared an array called balance[100] with data type double, so now we want to
store the 3rd value of an array as 12.23, and declare it as balance[3] = 12.23 thus we can
store the value at the 3rd position of 12.23
• In C, all arrays have 0 as the index of their first element. Therefore, when you write char
p[10];
• You are declaring a character array that has 10 elements, p[0] through p[9]. For example,
the following program loads an integer array with the numbers 0 through 99:
#include <stdio.h>
int main(void)
{
int x[100]; /* this declares a 100-integer array */
int t;
/* load x with values 0 through 99 */
for(t=0; t<100; ++t) {
x[t] = t;
}
/* display contents of x */
for(t=0; t<100; ++t) {
printf(''%d ", x[t]);
return 0;
}
}
• The amount of storage required to hold an array is directly related to its type
and size. For a single-dimension array, the total size in bytes is computed as
shown here:
total bytes = sizeof(base type) ×length of array
• Single-dimension arrays are essentially lists that are stored in contiguous
memory locations in index order.
• For example, figure below shows how array a appears in memory if it starts at
memory location 1000 and is declared as char a[7];
A seven-element character array beginning at location 1000
Sum of array elements
#include <stdio.h>
int main() {
int a[5];
int sum=0;
printf("enter the array elements:\n");
for(int i = 0; i < 5; i++) {
scanf("%d",&a[i]);
}
printf("sum of the the array elements:\n");
for(int i = 0; i < 5; i++) {
sum=sum+a[i];
}
printf("sum=%d",sum);
return 0;
}
Find the sum of all elements in an array of 5
integers.
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int sum = 0;
for(int i = 0; i < 5; i++)
{
sum = sum + arr[i];
}
printf("Sum of array elements = %d\n", sum);
return 0;
}
Write a c program to Count how many elements
are even and how many are odd in an array.
#include <stdio.h>
int main() {
int arr[6] = {10, 15, 20, 25, 30, 35};
int even = 0, odd = 0;
for(int i = 0; i < 6; i++) {
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Even numbers = %d\n", even);
printf("Odd numbers = %d\n", odd);
return 0;
}
Write a c program to Print array elements in
reverse order.
#include <stdio.h> output:
int main() { Array elements in reverse order:
int arr[5] = {10, 20, 30, 40, 50}; 50 40 30 20 10
printf("Array elements in reverse order:\n");
for(int i = 4; i >= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}