8/28/2019 CSE 1001 Department of CSE 1
Objectives
To learn and appreciate the following concepts:
• Declare, initialize and access 1D array.
• Write programs using common data
structures namely arrays and strings and
solve problems.
8/28/2019 CSE 1001 Department of CSE 2
Session outcome
• At the end of session student will be able to
Declare, initialize and access 1D array
Write programs using 1D array
8/28/2019 CSE 1001 Department of CSE 3
Arrays
An array is a group of related data items that share
a common name.
The array elements are placed in contiguous memory
locations.
A particular value in an array is indicated by writing an
integer number called index number or subscript in
square brackets after the array name.
The least value that an index can take in array is 0..
8/28/2019 CSE 1001 Department of CSE 4
Arrays
Array Declaration:
data-type name [size];
where data-type is a valid data type (like int, float, char...)
name is a valid identifier
size specifies how many elements the array has to contain.
size field is always enclosed in square brackets [ ] and
takes static values.
For example an array salary containing 5 elements is declared
as follows int salary [5];
8/28/2019 CSE 1001 Department of CSE 5
Arrays - One Dimensional
A linear list of fixed number of data items of same type.
These items are accessed using the same name using a single
subscript. E.g. roll[0], roll[1]…. or salary [1], salary [4]
A list of items can be given one variable name using only one
subscript and such a variable is called a single-subscripted variable
or a one- dimensional array.
8/28/2019 CSE 1001 Department of CSE 6
Arrays - 1D
Total size:
The Total memory that can be allocated to 1Darray is
computed as
Total size =size *(sizeof(data_type));
where size number of elements in 1-D array
data_type basic data type.
sizeof() is an unary operator which returns the size
of data type in bytes.
8/28/2019 CSE 1001 Department of CSE 7
Arrays - 1D
If the values of array are 3, 2, 6, 1, 9 then these values are
stored in array arr as follows.
int main()
{
int arr[50],n; // declaration of ‘arr’
printf(" enter value of n\n“); // no of elements
scanf(“%d”, &n); // reading the limit into n
for(int i=0;i<n;i++)
{
scanf(“%d”,&arr[i]); // reading n elements
}
for(int j=0; j<n;j++) //displaying n elements
{ printf(“%d”,arr[i]);
printf(“\t”);}
return 0;
}
8/28/2019 CSE 1001 Department of CSE 8
Initializing one-dimensional array
int number[3] ={0,0,0}; or {0} ;
declares the variable number as an array of size 3
and will assign 0 to each element.
int age[ ] ={16,25,32,48,52,65};
declares the age array to contain 6 elements with
initial values 16, 25, 32, 48, 52, 65 respectively
8/28/2019 CSE 1001 Department of CSE 9
Initializing one-dimensional array with zeros
Initialize all the elements of an integer array
‘values’ to zero
int values[20];
Begin for loop
Initialize counter
Set limit for counter for ( int i=0; i<20; i++)
Increment counter
Initialize element in array ‘values’
values[i]=0;
8/28/2019 CSE 1001 Department of CSE 10
Printing one-dimensional array
For example
int x[3] = {9,11,13};
printf(“%d\n”,x[0]); Output:
9
printf(“%d\n”,x[1]);
11
printf(“%d\n”,x[2]); 13
int x[3] = {9,11,13};
for (int i = 0; i<3; i++)
printf(“%d\n”,x[i]);
8/28/2019 CSE 1001 Department of CSE 11
Program to read n elements into an array and
print it int a[10], i, n;
printf("enter no of numbers“); Output:
scanf(“%d”,&n); enter no of numbers
3
printf(“enter n numbers \n”);
enter n numbers
for(i=0;i<n;i++) 9
scanf(“%d\n”,&x[i]); 11
13
Numbers entered are:
printf(“\nNumbers entered are:\n”); 9
for(i=0;i<n;i++) 11
printf(“%d\n”,a[i]); 13
8/28/2019 CSE 1001 Department of CSE 12
Program to add two array elements and store the
corresponding elements sum in another array
int a[10], b[10], c[10],n, m, i;
printf("enter no. of numbers in if(m==n)
first array\n“); {
for(i=0;i<m;i++)
scanf(“%d”,&n);
c[i]=a[i]+b[i];
//first array
for(i=0;i<n;i++) printf(“Sum of given array
scanf(“%d”,&a[i]); elements\n”);
printf("enter no of numbers in
for(i=0;i<n;i++)
second array\n“);
printf(“%d\n”,c[i]);
scanf(“%d”,&m); }
for(i=0;i<m;i++) //second array else
scanf(“%d”,&b[i]); printf("cannot add“);
}
8/28/2019 CSE 1001 Department of CSE 13
Displaying elements of an array in reverse order.
int a[10], n, i;
Example : a[ ]={1, 2, 3, 4, 5}
printf(“Enter values\n“); Enter values
n=5
for(i=0;i<n;i++) 12345
Reverse printing of array
scanf(“%d”,&a[i]); 5 4 3 2 1
Array before Array after
printf(“\nReverse order printing a[0]=1 a[0]=1
a[1]=2 a[1]=2
of array\n”); a[2]=3 a[2]=3
a[3]=4 a[3]=4
for(i=n-1;i>=0;i--) // reverse loop a[4]=5 a[4]=5
printf(“%d\n”,a[i]);
8/28/2019 CSE 1001 Department of CSE 14
Write a program to reverse an array using only
one array
Example : a[ ]={1, 2, 3, 4, 5}
int a[20], i, j, n, temp;
Enter values
printf("enter n \n“); n=5
12345
scanf(“%d”,&n); Reversed array
5 4 3 2 1
printf("\n Enter values for an array“); Array Reversed
array
for(i=0;i<n;i++) a[0]=1 a[0]=5
a[1]=2 a[1]=4
scanf(“%d”,&a[i]); a[2]=3 a[2]=3
a[3]=4 a[3]=2
Contd… a[4]=5 a[4]=1
8/28/2019 CSE 1001 Department of CSE 15
Reversing an array
for(i=0, j=n-1; i<n/2; i++, j--) Example :
a[ ]={1, 2, 3, 4, 5}
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
printf("\n Reversed array: \n“); Output:
Enter values for an array
for(i=0;i<n;i++)
n=5
printf(“%d\t”,a[i]); 12345
Reversed array
} 5 4 3 2 1
8/28/2019 CSE 1001 Department of CSE 16
WAP to insert an element to an array at a given position
int a[100], n,i, pos,ele;
scanf(“%d”,&n); // number of elements Example : insert 9 at 2nd position
printf("\nEnter the elements of array:“); a[ ]={1, 2, 3, 4, 5}
for(i=0;i<n;i++)
New array after inserting 9 :
scanf(“%d”,&a[i]); a[ ]={1, 9, 2, 3, 4, 5}
printf("\nEnter the element and position of insertion:“);
scanf(“%d %d”,&ele,&pos);
for(i=n; i>=pos; i--) //shift the elements to right
a[i]=a[i-1];
a[pos-1] = ele;//ele is inserted at the specified pos.
n = n + 1; // increment the count of no of elements
printf("\nThe array after insertion is:“);
for(i=0;i<n; i++) printf(“%d\n”,a[i]);
8/28/2019 CSE 1001 Department of CSE 17
WAP to delete an element from an array
printf("enter no of numbers“); Example : delete ele at 2nd position
a[ ]={1, 2, 3, 4, 5}
scanf(“%d”,&n);
printf("enter n numbers \n“); New array after deleting 2:
a[ ]={1, 3, 4, 5}
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf("enter the position at which the element to be deleted“);
scanf(“%d”,&pos);
for(i=pos-1; i<n-1; i++)
a[i] =a[i+1]; //shift the elements to left
n = n-1;//decrement the count of no of elements
for(i=0;i<n;i++)
8/28/2019 printf(“%d”,a[i]); CSE 1001 Department of CSE 18
Insert an element into a sorted array
Read array elements (in sorted order) & element ‘ele’ to be
inserted
Example: insert 3 into the array
a[ ] = {1, 2, 4, 5,6}
/ / f indin g p o s it ion
for(i=0;i<n;i++) New array after inserting 3 :
a[ ] = {1, 2, 3, 4, 5,6}
if (ele<a[i]) break;
pos = i+1; //position of insertion
for(i=n; i>=pos; i--) //shift the elements to right
a[i]=a[i-1];
a[pos-1] = ele;//ele is inserted at the specified pos.
n = n + 1; // increment the count of no of elements
8/28/2019 CSE 1001 Department of CSE 19
Summary
• Arrays
• 1 Dimensional arrays (lists)
• Problems on 1D arrays
8/28/2019 CSE 1001 Department of CSE 20
Syntax
1D Array:
Syntax: type array_name[size];
Memory Requirement:
Total size =size *(sizeof(data_type));
Initialization:
type array-name [size]={list of values}
Write and Read:
for(i=0;i<n;i++) for(i=0;i<n;i++)
scanf(“%d”,&a[i]); prinft(“%d\n”,a[i]);
8/28/2019 CSE 1001 Department of CSE 21
Tutorials on Array
• Write a program to find average of an 1-D array.
• Write a program to find second largest element in an array.
• Write a program to find union and intersection of two arrays.
8/28/2019 CSE 1001 Department of CSE 22