0% found this document useful (0 votes)
28 views35 pages

Unit 3 - C1 (Arrays, One Dimentional Array)

The document introduces arrays and one-dimensional arrays. [1] Arrays allow storing multiple values of the same data type. [2] An array is declared with the data type, array name, and size. [3] Values can be assigned to arrays through initialization, direct assignment, or input functions. The document provides examples to illustrate array declaration and value assignment.

Uploaded by

bala krishna
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)
28 views35 pages

Unit 3 - C1 (Arrays, One Dimentional Array)

The document introduces arrays and one-dimensional arrays. [1] Arrays allow storing multiple values of the same data type. [2] An array is declared with the data type, array name, and size. [3] Values can be assigned to arrays through initialization, direct assignment, or input functions. The document provides examples to illustrate array declaration and value assignment.

Uploaded by

bala krishna
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
You are on page 1/ 35

INTRODUCTION TO PROGRAMMING

RGM College of Engineering & Technology (Autonomous)


Department of Electronics & Communication Engineering
Academic Year : 2023-2024
Presented By
K.NAGENDRAKUMAR
Unit –III
Arrays and Strings

Topics Covered:
1. Introduction to Arrays
2. One - dimensional arrays

Dept. of ECE, RGMCET(Autonomous), Nandyal 2


Introduction to Arrays

 The variables we declare using the data types like int, char, float, double

and so on will store only one value in them at any given point of time.

 Therefore, they can be used only to handle limited amount of data.

 If we need to store more values means we have to declare more number of

variables in the program which is very difficult to manage in the C program.

Dept. of ECE, RGMCET(Autonomous), Nandyal 3


 To store more number of values or data items when the program is executing, we can

use a derived data type provided by the C language. This derived data type is called

array.
 A data structure is the way data is stored in the machine and the functions used to
access that data.
 An array is a data structure that is a collection of variables of one type that are accessed
through a common name.

 An array is a contiguous memory locations of same type of elements, and is given one

name to all these locations (Memory cells).

 Each memory cell of an array is accessed by an index value that uniquely identifies it.

Dept. of ECE, RGMCET(Autonomous), Nandyal 4


 The values we store in an array must be of same data type. So, an array is also known as

a homogeneous data type.

 An array can be declared as int or float or char or other data types.

 First, array has to be declared of some size and of a data type before we use array in our

statements of the program.

 Array is also called as subscripted data type. This is because the values of the array are

accessed using subscripts or index values.

Dept. of ECE, RGMCET(Autonomous), Nandyal 5


Properties of an Array:

 All the elements of an array belong to same data type.

 The type of an array is the data type of its elements.

 The location of an array is location of its first element.

 The length of an array is the number of data elements in the array.

 The size of an array is the length of the array times the size of an element.

Dept. of ECE, RGMCET(Autonomous), Nandyal 6


 We can use arrays to represent not only simple lists of values but also to

represent tables of data in two, three or more dimensions.

Types of arrays:

One-dimensional arrays

Two-dimensional arrays

Multi-dimensional arrays

Dept. of ECE, RGMCET(Autonomous), Nandyal 7


Note:

 Array whose element are specified by one subscript are called single

subscripted or one dimensional array.

 Similarly, array whose elements are specified by two and three subscripts

are called two-dimensional or double subscripted and three-dimensional or

triple-subscripted arrays respectively.

Dept. of ECE, RGMCET(Autonomous), Nandyal 8


One - dimensional arrays

 Like any other variable, First we have to declare an array before we use it

to store and access values of it.

 The syntax for declaring an array is shown below.


Syntax : Array Declaration

data_type array_name[size];

Where,

 data_type is any of the data types like int, float, char, double,.. All
Dept. of ECE, RGMCET(Autonomous), Nandyal 9
 array_name is any valid name that we want to give to the array.

 size is a positive integer value specifying the size of the array, i.e., number of values the

array stores. This size is not the memory size occupied by the array.

Examples:
Here the array name is age
int age[10];
Array age stores 10 integer values.
Here the array name is height
float height[15];
Array height stores 15 float values.
Here the array name is gender
char gender[8];
Array gender stores 8 character values.
Dept. of ECE, RGMCET(Autonomous), Nandyal 10
Giving values to an Array:

 After an array is declared, its elements must be initialized. Otherwise, they will

contain “garbage” values.

 Values to an array can be assigned in many ways depending on the need.

i)Through Array Initialization

ii)Through Direct Assignment

iii) Through standard input functions

Dept. of ECE, RGMCET(Autonomous), Nandyal 11


i) Through Array Initialization

 Arrays can also be initialized with values at the declaration time itself.

The general form of initialization of array is:


Syntax : Array Initialization

data_type array_name[size]= { list of values};

 The values in the list are separated by commas.

Dept. of ECE, RGMCET(Autonomous), Nandyal 12


Example 1:

int number [3] = { 0,0,0 };

will declare the variable number as an array of size 3 and will assign zero to

each element.

Dept. of ECE, RGMCET(Autonomous), Nandyal 13


 If the number of values in the list is less than the number of elements, then only that

many elements will be initialized. The remaining elements will be set zero

automatically.

For example,

float total[5] = { 0.0 , 15.75 , -10 };

will initialize the first three elements 0.0, 15.75 and -10.0 and the remaining two elements

to zero.

Dept. of ECE, RGMCET(Autonomous), Nandyal 14


 The Size may be omitted. In such cases, the compiler allocates enough space for all

initialized elements.

For example,

int counter[ ] = {1,1,1,1 };

will declare the counter array to contain four elements with initial values 1.

 This approach works fine as long as we initialize every element in the array.

Dept. of ECE, RGMCET(Autonomous), Nandyal 15


Example 2:

int VAL[5] = {10,20,30,40,-20};

 In the above example, VAL is the name of the array storing 5 integer values in it.

 The values given in the flower braces {…} are assigned to the array and this can be

visualized as below.

Array name  VAL 10 20 30 40 -20


Subscript  0 1 2 3 4

Dept. of ECE, RGMCET(Autonomous), Nandyal 16


 The subscript (or) index value of an array will give individual value of the array elements.

 Always the array subscript starts from 0 (zero).

So,

VAL[0] will store value 10

VAL[1] will store value 20

VAL[2] will store value 30

VAL[3] will store value 40

VAL[4] will store value -20


Dept. of ECE, RGMCET(Autonomous), Nandyal 17
ii) Through Direct Assignment:

 Arrays can also be assigned with values at any time after the declaration of it.

Here are the some examples of it.

VAL[0] = 55;

VAL[1] =22;

Now, array has values like this:


Array name  VAL 55 22
Subscript  0 1 2 3 4

Dept. of ECE, RGMCET(Autonomous), Nandyal 18


 We can also assign the values into an array as shown below:

VAL[2] = VAL[1] + VAL[0];

Now, array has values like this:


Array name  VAL 55 22 77
Subscript  0 1 2 3 4

Dept. of ECE, RGMCET(Autonomous), Nandyal 19


iii) Through standard input functions:

 Arrays can also be assigned with values by using the standard input function scanf().

Here are the some examples of it.

scanf(“%d”,&VAL[3]);

 Assume that, user entered a value of 231 for the above input function. Then array is,

Array name  VAL


55 22 77 231

Subscript  0 1 2 3 4

Dept. of ECE, RGMCET(Autonomous), Nandyal 20


Similarly, one can give values to array variables as per the need in the program while

solving a specific problem.

NOTE:

 The array cell index starts with ‘zero’ and not by ‘one’.

 So, if the size of a array is ‘5’, then we can subscript the array up to a value of ‘4’,

only, since, it starts with zero (0).

Dept. of ECE, RGMCET(Autonomous), Nandyal 21


 Some more examples of one-dimensional array declarations:

float PRICE[2];

int QTY[30];

char NAME[25]; and so on..

Remember the following points:


 Any reference to the arrays outside the declared limits would not necessarily cause an
error. Rather, it might result in unpredictable program results.
 The size should be either a numeric constant or a symbolic constant.

Dept. of ECE, RGMCET(Autonomous), Nandyal 22


Dept. of ECE, RGMCET(Autonomous), Nandyal 23
/*Program to find the average of n numbers using arrays*/
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0;
double average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i < n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i]; // adding integers entered by the user to the sum variable
}
average = (double) sum / n; // then calculate average
printf("Average = %.2lf", average);
return 0;
}
Dept. of ECE, RGMCET(Autonomous), Nandyal 24
• Output:
Enter number of elements: 3
Enter number1: 10
Enter number2: 10
Enter number3: 10
Average = 10.00

Dept. of ECE, RGMCET(Autonomous), Nandyal 25


C Program to Print the Alternate Elements in an Array
#include <stdio.h> Output: enter the element of an
array
void main()
3345678456
{
Alternate elements of a given
int array[10];
array
int i, j, temp;
3
printf("enter the element of an array \n");
4
for (i=0;i<10;i++)
6
scanf("%d", &array[i]);
8
printf("Alternate elements of a given
5
array \n");
for (i=0;i<10;i+=2)
printf("%d\n",array[i]) ;
} Dept. of ECE, RGMCET(Autonomous), Nandyal 26
• C Program to Increment every Element of the Array by one & Print
Incremented Array.
#include <stdio.h>
void main()
{
int i;
int array[4] = {10, 20, 30, 40};
for (i = 0; i < 4; i++)
array[i]++;
for (i = 0; i < 4; i++)
printf("%d\t", array[i]);
}
Output: 11 21 31 41
Dept. of ECE, RGMCET(Autonomous), Nandyal 27
• int a[5]={9,8,7,6,5};
int b[5];
b=a; //not valid
• we have to copy all the elements by using for loop.
for(a=i; i<5; i++)
b[i]=a[i];
Processing:
• For processing arrays we mostly use for loop. The total no. of
passes is equal to the no.of elements present in the array and in
each pass one element is processed.
Dept. of ECE, RGMCET(Autonomous), Nandyal 28
Example 2:
Write a C program which copies contents of one array into another
array.

Example 3:
Write a C program which reads the values into array from user and
separately stores the even and odd values into two different arrays.
Dept. of ECE, RGMCET(Autonomous), Nandyal 29
Example 4:

Write a C program which finds the maximum and minimum elements of the

given array.

Example program 5:

Write a C program which finds whether the given element is available or not in

the given array:

Dept. of ECE, RGMCET(Autonomous), Nandyal 30


Example 6:

Write a C program which sorts the elements of given array in ascending order.

Dept. of ECE, RGMCET(Autonomous), Nandyal 31


Write a C program which copies contents of one array into another array.
#include <stdio.h>
int main()
{
int arr1[] = {1, 2, 3, 4, 5}; //Initialize array
int length = sizeof(arr1)/sizeof(arr1[0]); //Calculate length of array arr1
int arr2[length]; //Create another array arr2 with the size of arr1.
for (int i = 0; i < length; i++) //Copying all elements of one array into another
{
arr2[i] = arr1[i];
}
printf("Elements of original array: \n"); //Displaying elements of array arr1

Dept. of ECE, RGMCET(Autonomous), Nandyal 32


printf("Elements of original array: \n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr1[i]);
}
printf("\n"); //Displaying elements of array arr2
printf("Elements of new array: \n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr2[i]);
}
return 0;
Dept. of ECE, RGMCET(Autonomous), Nandyal 33
Output: Elements of original array:
12345
Elements of new array:
12345

Dept. of ECE, RGMCET(Autonomous), Nandyal 34


Thank You
Dept. of ECE, RGMCET(Autonomous), Nandyal 35

You might also like