0% found this document useful (0 votes)
9 views

1D Arrays-Creation and Insertion

1d array

Uploaded by

hasnainwaris14
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)
9 views

1D Arrays-Creation and Insertion

1d array

Uploaded by

hasnainwaris14
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/ 20

Department of BES-I

COURSE: COMPUTATIONAL THINKING


FOR STRUCTURAL DESIGN

COURSE CODE: 23SC1202


Topic:

1D ARRAY-
CREATION&INSERTION
Session –
CREATED BY K. VICTOR BABU
AIM OF THE SESSION
To familiarize students with the concept of Arrays .

INSTRUCTIONAL OBJECTIVES

This Session is designed to:


1. Demonstrate the concept of 1D Arrays.
2. To implement 1D arrays in C.
3. To create a 1D Array in C and perform operations like insertion.

LEARNING OUTCOMES

At the end of this session, you should be able to:


1. Understand the concept and importance of 1D Array.
2. To create a 1D Array and perform operations such as insertion.

CREATED BY K. VICTOR BABU


Arrays INTRODUCTION

• An Array is collection of elements of similar data type stored contiguously


and referenced by a common name.

• Compared to the basic data type (int, float) it is an aggregate or derived data
type.

• All the elements of an array occupy a set of contiguous memory locations.

CREATED BY K. VICTOR BABU


Array types
1D Array

2D Array

Multidimensional
Array

CREATED BY K. VICTOR BABU


Why Arrays?

Consider the Scenario


We need to store Assignment marks of 1000 students
Assume that marks are of an integer type.

Without Arrays: Declaration using the basic data type (int)


int studMark0, studMark1, ...studMark999

Solution: 1 Dimensional Arrays

CREATED BY K. VICTOR BABU


1D Array Creation

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];

CREATED BY K. VICTOR BABU


1D Array Creation

Consider the Declaration:

float temp[4];

A sample 1D Array:
0 1 2 3
temp 23.5 35.6 40.5 52.5

1000 1004 1008 1012


CREATED BY K. VICTOR BABU
Array

0 1 2 3 4

a 5 10 15 20 25

2000 2004 2008 2012 2016

a[0]=5 is stored in location 2000


a[1]=10 is stored in location 2004
a[2]=15 is stored in location 2008
a[3]= 20 is stored in location 2012
a[4]=25 is stored in location 2016

CREATED BY K. VICTOR BABU


1D Array Creation

0 1 2 3
temp 23.5 35.6 40.5 52.5

1000 1004 1008 1012

temp[0]=23.5 address: 1000


temp[1]=35.6 address: 1004
temp[2]=40.5 address: 1008
temp[3]=52.5 address: 1012

CREATED BY K. VICTOR BABU


Input to a 1D Array

Input to an Array can be done in the following ways:

• At compile time (Initializing)

• At run time

CREATED BY K. VICTOR BABU


Initializing a 1D Array

Initializing a 1D Array can be done in the following ways:

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'};

CREATED BY K. VICTOR BABU


Input to a 1D Array at run time

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

CREATED BY K. VICTOR BABU


1000 1004 1008 1012
Output to a 1D Array at run time

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

CREATED BY K. VICTOR BABU


Retrieveing specific Array elements

In order to retrieve specific element from an array we need to


specify the array or variable name and the index number of
interest.

For example: consider the following code snippet

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

• To find the position of an element.


• To search for a particular data or element
• To arrange the data in a specific order
• To find the maximum or minimum element

CREATED BY K. VICTOR BABU


Self Assessment Questions

1. What is the highest index for an array with 10 elements in C?


2. What is the maximum number of elements that can be stored
in an array?
3. How do you access the last element of an array arr with n
elements in C.

CREATED BY K. VICTOR BABU


Practice Questions

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.

firstLast6([1, 2, 6]) → true

firstLast6([6, 1, 2, 3]) → true

firstLast6([13, 6, 1, 2, 3]) → false

CREATED BY K. VICTOR BABU


References

"C Programming Absolute Beginner's Guide" by Greg Perry and Dean


Miller
"The C Programming Language" by Brian W. Kernighan and Dennis M.
Ritchie
"C Programming: A Modern Approach" by K.N. King
"C Programming for the Absolute Beginner" by Michael Vine
"Learn C the Hard Way" by Zed A. Shaw

CREATED BY K. VICTOR BABU


THANK YOU

Team – CTSD

CREATED BY K. VICTOR BABU

You might also like