Arrays Part1
Arrays Part1
Introduction
• Arrays
– Structures of related data items
– Static entity – same size throughout program
2
Name of array
Arrays (Note that all
elements of this
• Array array have the
same name, c)
– Group of consecutive memory locations
– Same name and type c[0] -45
c[1] 6
• To refer to an element, specify c[2] 0
c[3] 72
– Array name c[4] 1543
– Position number c[5] -89
c[6] 0
• Format: c[7] 62
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
Position number
of the element
within array c
1
07-11-2022
Arrays
• Array elements are like normal variables
c[ 0 ] = 3;
printf( "%d", c[ 0 ] );
– Perform operations in subscript. If x equals 3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]
Declaring Arrays
• When declaring arrays, specify
– Name
– Type of array
– Number of elements
arrayType arrayName[ numberOfElements ];
– Examples:
int c[ 10 ];
float myArray[ 3284 ];
• Declaring multiple arrays of same type
– Format similar to regular variables
– Example:
int b[ 100 ], x[ 27 ];
2
07-11-2022
3
07-11-2022
7
Element Value Histogram
0 19 *******************
1 3 ***
2 15 ***************
Program Output
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *
4
07-11-2022
9
#include <stdio.h>
void main()
{
int i,n,a[100];
10
5
07-11-2022
11
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
12
6
07-11-2022
13
#include <stdio.h>
void main()
{
int arr1[100];
int i, mx, mn, n;
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
}
14
7
07-11-2022
15
#include <stdio.h>
void main()
{
int i,n,sum=0;
float avg;
printf("Input the 10 numbers : \n");
for (i=1;i<=10;i++)
{
printf("Number-%d :",i);
scanf("%d",&n);
sum +=n;
}
avg=sum/10.0;
printf("The sum of 10 no is : %d\nThe Average is :
%f\n",sum,avg);
16
8
07-11-2022
17
#include <stdio.h>
void main()
{
scanf("%d", &num);
printf("Enter the elements of the array \n");
18