1D Arrays-Creation and Insertion
1D Arrays-Creation and Insertion
1D ARRAY-
CREATION&INSERTION
Session –
CREATED BY K. VICTOR BABU
AIM OF THE SESSION
To familiarize students with the concept of Arrays .
INSTRUCTIONAL OBJECTIVES
LEARNING OUTCOMES
• Compared to the basic data type (int, float) it is an aggregate or derived data
type.
2D Array
Multidimensional
Array
Syntax:
data_type Array_Name[size];
data_type: the type of each element in the array.
Array_Name: any valid C identifier name
size : defines how many elements the array will hold
Note: size should be an integer value
Examples : int marks[10];
float temp[4];
float temp[4];
A sample 1D Array:
0 1 2 3
temp 23.5 35.6 40.5 52.5
0 1 2 3 4
a 5 10 15 20 25
0 1 2 3
temp 23.5 35.6 40.5 52.5
• At run time
Syntax
Data_type array_name[size] = {a_list_of_value};
Example
int _idNum[7] = {1, 2, 3, 4, 5, 6, 7};
char vowels[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
int main() 0 1 2 3
{
int a[4],i; a
for(i=0;i<4;i++)
{
scanf(“%d”,&a[i]);
} 10 20 30 45
} a
int main()
{
int a[4],i;
for(i=0;i<4;i++)
{
scanf(“%d”,&a[i]);
printf(“%d\t”,a[i]);
}
} Output: 10 20 30 45
int Arr[]={1,3,5,6,8};
printf(“%d\t%d\n”,Arr[1],Arr[3]);
Output: 3 6
CREATED BY K. VICTOR BABU
Example Program for 1D Array
Take 10 integer input from user and store then in an array and find the sum
of all numbers stored in array.
#include<stdio.h>
int main()
{
int a[6],i,sum=0;
for(i=0;i<6;i++)
{ 0 1 2 3 4 5
scanf(“%d”,&a[i]); 11 22 33 44 55 66
}
for(i=0;i<6;i++)
{ sum=sum+a[i]; }
printf(the sum is %d\n”,sum);
}
CREATED BY K. VICTOR BABU
1D Array Applications
1. write a program in C to find the smallest index of the largest element in an Array: If
the array has multiple elements with the same largest value, find the smallest index of
such an element. Suppose the array myList is {1, 5, 3, 4, 5, 5}. So, the largest element is
5 and the smallest index for 5 is 1.
2. Given an array of integer values, return true if 6 appears as either the first or last
element in the array.
Team – CTSD