0% found this document useful (0 votes)
9 views4 pages

M06 Single Dimenstional Array

This document provides an introduction to single-dimensional arrays in C programming, detailing their structure, declaration, initialization, and access methods. It emphasizes the importance of contiguous memory allocation and includes examples of how to declare, initialize, and manipulate arrays. Additionally, it presents a problem set for practicing array-related programming tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

M06 Single Dimenstional Array

This document provides an introduction to single-dimensional arrays in C programming, detailing their structure, declaration, initialization, and access methods. It emphasizes the importance of contiguous memory allocation and includes examples of how to declare, initialize, and manipulate arrays. Additionally, it presents a problem set for practicing array-related programming tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Leaning Objective:

In this Module you will be learning the following:

 Single dimension array

Introduction:

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same
type. A Single -dimensional array is like a list.

Material:

C Array is a collection of variables belongings to the same data type. You can store group of data of
same data type in an array.

 Array might be belonging to any of the data types


 Array size must be a constant value.
 Always, Contiguous (adjacent) memory locations are used to store array elements in
memory.
 It is a best practice to initialize an array to zero or null while declaring, if we don’t assign
any values to array.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare
one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.

Declaring Arrays

To declare an array in C, a programmer specifies the type of the elements and the number of elements
required by an array as follows –

data_type array_name[array_size];
For Example: float marks[5];

Here, we declared an array, mark, of floating-point type and size 5. Meaning, it can hold 5 floating-point
values.

The size and type of arrays cannot be changed after its declaration.

Elements of an Array and How to access them?

You can access elements of an array by indices.

Suppose you declared an array mark as above. The first element is mark[0], second element is mark[1]
and so on.
Few key notes:

Arrays have 0 as the first index not 1. In this example, mark[0], If the size of an array is n, to access
the last element, (n-1) index is used. In this example, mark[4]

Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will be 2124d, address
of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.

How to initialize an array in C programming?

It's possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

Another method to initialize array during declaration:

int mark[] = {19, 10, 8, 17, 9};

Here,

mark[0] is equal to 19

mark[1] is equal to 10

mark[2] is equal to 8

mark[3] is equal to 17

mark[4] is equal to 9

How to insert and print array elements?

int mark[5] = {19, 10, 8, 17, 9}

// insert different value to third element

mark[3] = 9;

// take input from the user and insert in third element

scanf("%d", &mark[2]);

// take input from the user and insert in (i+1)th element

scanf("%d", &mark[i]);

// print first element of an array

printf("%d", mark[0]);

// print ith element of an array

printf("%d", mark[i-1]);
Example: C Arrays

// Program to find the average of n (n < 10) numbers using arrays

#include <stdio.h>

int main(){

int marks[10], i, n, sum = 0, average;

printf("Enter n: ");

scanf("%d", &n);

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

printf("Enter number%d: ",i+1);

scanf("%d", &marks[i]);

sum += marks[i];

average = sum/n;

printf("Average marks = %d", average);

return 0;

Output

Enter n: 5

Enter number1: 45

Enter number2: 35

Enter number3: 38

Enter number4: 31

Enter number5: 49

Average = 39

Important thing to remember when working with C arrays

Suppose you declared an array of 10 elements. Let's say,

int testArray[10];

You can use the array members from testArray[0] to testArray[9].

If you try to access array elements outside of its bound, let's say testArray[12], the compiler may not
show any error. However, this may cause unexpected output (undefined behavior).
Problem Set:

1. Write a C program to read and print elements of array.


2. Write a C program to print all negative elements in an array.
3. Write a C program to find sum of all array elements. - using recursion.
4. Write a C program to find second largest element in an array.
5. Write a C program to count total number of even and odd elements in an array.
6. Write a C program to copy all elements from an array to another array.
7. Write a C program to print all unique elements in the array.
8. Write a C program to delete all duplicate elements from an array.
9. Write a C program to count frequency of each element in an array.
10. Write a C program to merge two array to third array.
11. Write a C program to find reverse of an array.
12. Write a C program to put even and odd elements of array in two separate array.
13. Write a C program to sort array elements in ascending order.

You might also like