SlideShare a Scribd company logo
Structured Programming Language
1 D Array
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Array
• An array is a group of like-typed variables that are
referred to by a common name.
• Arrays of any type (int, char, float, double etc.) can be
created and may have one or more dimensions.
• Arrays form a convenient way to handle groups of
related data.
Array Element
• An individual variable in the array is called an array element.
• All the array elements must be of the same type and same
storage class.
• Each array element is referred to by specifying the array name
followed by one or more subscripts, with each subscript
enclosed in square brackets.
• Each subscript must be expressed as a nonnegative integer.
• The value of each subscript can be expressed as an integer
constant, an integer variable or a more complex integer
expression.
1 Dimensional Array
• A one-dimensional array is a list of variables that are
all of the same type and are accessed through a
common name.
• C stores one-dimensional arrays in one contiguous
memory location with the first element at the lowest
address.
Declaring 1 D Array
type var_name[size];
• type is a valid C data type.
• var_name is the name of the array.
• size specifies the number of elements in
the array.
Declaring 1 D Array
type var_name[size];
• type is a valid C data type.
• var_name is the name of the array.
• size specifies the number of elements in
the array.
int x[100]; ///x is a 100 element integer array
char text[80]; ///text is an 80 element character array
#define SIZE 20
…….
float myarray[SIZE]; ///myarray is a 20 element floating-point array
1 D Array Elements
• 1st element : x[0]
• 2nd element : x[1]
• 3rd element : x[2]
• 5th element : x[4]
int x[5];
⋮
⋮
x[0] x[1] x[2] x[3] x[4]
1 D Array Elements
• 1st element : x[0]
• 2nd element : x[1]
• 3rd element : x[2]
• 5th element : x[4]
int x[5];
⋮
⋮
Element No i Index No i-1
x[0] x[1] x[2] x[3] x[4]
1 D Array Initialization
int x[5] = { 1, 2, 3, 4, 5};
Similarly,
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
float arr[6]={0, 0.25, 0, -0.50, 0, 0};
char color[ ]={'R', 'G', 'B'};
• The array size need not be specified explicitly when initial
values are included as a part of an array definition.
• The array size will automatically be set equal to the
number of initial values included within the definition.
Assigning Values to Array Elements
int x[5] = { 1, 2, 3, 4, 5};
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
x[2] = 7;
Assigning Values to Array Elements
int x[5] = { 1, 2, 3, 4, 5};
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
x[0] x[1] x[2] x[3] x[4]
1 2 7 4 5
x[2] = 7;
Printing Array Elements
int x[5] = { 1, 2, 3, 4, 5};
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
printf (“ % d “ , x[2] ) ; ///3
Problem
• Write a program that will declare a 10 elements array
and saves the value of squares from 1 to 10 to those
10 array elements.
Problem
• Write a program that will declare a 10 elements array
and saves the value of squares from 1 to 10 to those
10 array elements.
x[0] x[1] x[2] x[3] x[4]
1 4 9 16 25
x[5] x[6] x[7] x[8] x[9]
36 49 64 81 100
Solution
#include<stdio.h>
int main()
{
return 0;
}
Solution
#include<stdio.h>
#define SIZE 10
int main()
{
int sqrs[SIZE], i;
return 0;
}
Solution
#include<stdio.h>
#define SIZE 10
int main()
{
int sqrs[SIZE], i;
///assigning values to array elements
for(i=0; i<=SIZE-1; i++){
sqrs[i]=(i+1)*(i+1);
}
return 0;
}
Solution
#include<stdio.h>
#define SIZE 10
int main()
{
int sqrs[SIZE], i;
///assigning values to array elements
for(i=0; i<=SIZE-1; i++){
sqrs[i]=(i+1)*(i+1);
}
///showing the values of array elements
for(i=0; i<=SIZE-1; i++){
printf("%d th element: %dn", i, sqrs[i]);
}
return 0;
}
scanf for 1 D Array
• Using scanf you can give input values to specific array
elements.
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
scanf("%d", &x[2]); ///let, user gives input 100
x[0] x[1] x[2] x[3] x[4]
1 2 100 4 5
Sample code
#include<stdio.h>
int main()
{
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int i;
for(i=0;i<10;i+=2){
scanf("%d", &arr[i] );
}
for(i=0;i<10;i++){
printf("%d ", arr[i] );
}
return 0;
}
Note
• Single operations
which involve entire
arrays are not
permitted in C.
• So in C, you can’t
assign one entire
array to another.
#include<stdio.h>
int main()
{
int a1[10],a2[10];
int i;
for(i=0;i<10;i++){
scanf("%d", &a1[i] );
}
a2=a1; ///error
return 0;
}
Sample code
///sample code to copy one array into another
#include<stdio.h>
int main()
{
int a1[5],a2[5];
int i;
for(i=0;i<5;i++){
scanf("%d", &a1[i] );
}
///can’t write a2=a1;
for(i=0;i<5;i++){
a2[i]=a1[i];
}
///showing outputs
for(i=0;i<5;i++){
printf("%d ",a2[i]);
}
return 0;
}
Problem
• Write a program to search an element from array
elements.
Sample code
#include<stdio.h>
#define SIZE 1000
int main()
{
int arr[SIZE];
int sz,element;
printf("Please enter the number of elements of the array: ");
scanf("%d",&sz);
int i;
for(i=0;i<sz;i++){
scanf("%d",&arr[i]);
}
printf("Please enter the element to search for: ");
scanf("%d", &element);
for(i=0;i<sz;i++){
if(arr[i]==element){
printf("Yes");
break;
}
}
if(i==sz) printf("No");
return 0;
}
Problem
• Write a program to find the maximum element of an
array containing positive numbers.
Sample code
#include<stdio.h>
#define SIZE 1000
int main()
{
int arr[SIZE];
int sz, max_el=0;
printf("Please enter the number of elements of the array: ");
scanf("%d",&sz);
int i;
for(i=0;i<sz;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<sz;i++){
if(arr[i]>max_el){
max_el=arr[i];
}
}
printf("The maximum value is %dn", max_el);
return 0;
}
Problem
• Write a program to save an array in reverse order
into another array.
Sample code
#include<stdio.h>
int main()
{
int arr[5]={1,2,3,4,5};
int rev_arr[5];
int i;
for(i=0;i<5;i++){
rev_arr[i]=arr[5-i-1];
}
for(i=0;i<5;i++) printf("%d ",rev_arr[i]);
return 0;
}
Problem
• Write a program to calculate the sum of two 1 D
array (element wise) of exactly same size.
Sample code
#include<stdio.h>
int main()
{
int a1[5]={1,2,3,4,5};
int a2[5]={5,4,3,2,1};
int sum[5];
int i;
for(i=0;i<5;i++){
sum[i]=a1[i]+a2[i];
}
for(i=0;i<5;i++) printf("%d ",sum[i]);
return 0;
}
Problem
• Write a program to right rotate an array. Take input
the amount to rotate right.
Sample code
#include<stdio.h>
#define SIZE 7
int main()
{
int arr[SIZE]={1,2,3,4,5,6,7};
int r_rotate;
printf("Enter the amount to rotate right: ");
scanf("%d",&r_rotate);
int i;
for(i=SIZE-1;i>=r_rotate;i--){
arr[i]=arr[i-r_rotate];
}
for(i=0;i<r_rotate;i++){
arr[i]=0;
}
for(i=0;i<SIZE;i++) printf("%d ",arr[i]);
return 0;
}
Problem
• Write a program to separate the even and odd
elements of an array into two other arrays.
Sample code
#include<stdio.h>
#define SIZE 7
int main()
{
int arr[SIZE]={1, 2, 3, 4, 5, 6, 7};
int even[SIZE],odd[SIZE];
int i,even_sz=0,odd_sz=0;
for(i=0;i<SIZE;i++){
if(arr[i]%2==0){
even[even_sz]=arr[i];
even_sz++;
}
else{
odd[odd_sz]=arr[i];
odd_sz++;
}
}
printf("Even Array: "); ///showing the even numbers
for(i=0;i<even_sz;i++) printf("%d ",even[i]);
printf("nOdd Array: "); ///showing the odd numbers
for(i=0;i<odd_sz;i++) printf("%d ",odd[i]);
return 0;
}
References:
• Teach Yourself C, 3rd edition
Section: 5.1
• Schaum’s Outline,3rd edition
Section: 9.1, 9.2

More Related Content

What's hot

Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
Way2itech
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
Neel Mungra
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Arrays
ArraysArrays
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
Mahender Boda
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
Operators
OperatorsOperators
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
kash95
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
Aditya Vaishampayan
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
Sangani Ankur
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 

What's hot (20)

Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Strings
StringsStrings
Strings
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Arrays
ArraysArrays
Arrays
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Operators
OperatorsOperators
Operators
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 

Similar to SPL 10 | One Dimensional Array in C

Arrays
ArraysArrays
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Array
ArrayArray
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
HimanshuKansal22
 
Array-part1
Array-part1Array-part1
Array-part1
AbishaiAsir
 
Array
ArrayArray
Arrays
ArraysArrays
Array
ArrayArray
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
aarockiaabinsAPIICSE
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
Hemantha Kulathilake
 
C data types, arrays and structs
C data types, arrays and structsC data types, arrays and structs
C data types, arrays and structs
Saad Sheikh
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
 
1D Array
1D Array1D Array
1D Array
A. S. M. Shafi
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE

Similar to SPL 10 | One Dimensional Array in C (20)

Arrays
ArraysArrays
Arrays
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array
ArrayArray
Array
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Array-part1
Array-part1Array-part1
Array-part1
 
Array
ArrayArray
Array
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
02 arrays
02 arrays02 arrays
02 arrays
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
C data types, arrays and structs
C data types, arrays and structsC data types, arrays and structs
C data types, arrays and structs
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
1D Array
1D Array1D Array
1D Array
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Arrays
ArraysArrays
Arrays
 

More from Mohammad Imam Hossain

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
Mohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
Mohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 

Recently uploaded (20)

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 

SPL 10 | One Dimensional Array in C

  • 1. Structured Programming Language 1 D Array Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2. Array • An array is a group of like-typed variables that are referred to by a common name. • Arrays of any type (int, char, float, double etc.) can be created and may have one or more dimensions. • Arrays form a convenient way to handle groups of related data.
  • 3. Array Element • An individual variable in the array is called an array element. • All the array elements must be of the same type and same storage class. • Each array element is referred to by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets. • Each subscript must be expressed as a nonnegative integer. • The value of each subscript can be expressed as an integer constant, an integer variable or a more complex integer expression.
  • 4. 1 Dimensional Array • A one-dimensional array is a list of variables that are all of the same type and are accessed through a common name. • C stores one-dimensional arrays in one contiguous memory location with the first element at the lowest address.
  • 5. Declaring 1 D Array type var_name[size]; • type is a valid C data type. • var_name is the name of the array. • size specifies the number of elements in the array.
  • 6. Declaring 1 D Array type var_name[size]; • type is a valid C data type. • var_name is the name of the array. • size specifies the number of elements in the array. int x[100]; ///x is a 100 element integer array char text[80]; ///text is an 80 element character array #define SIZE 20 ……. float myarray[SIZE]; ///myarray is a 20 element floating-point array
  • 7. 1 D Array Elements • 1st element : x[0] • 2nd element : x[1] • 3rd element : x[2] • 5th element : x[4] int x[5]; ⋮ ⋮ x[0] x[1] x[2] x[3] x[4]
  • 8. 1 D Array Elements • 1st element : x[0] • 2nd element : x[1] • 3rd element : x[2] • 5th element : x[4] int x[5]; ⋮ ⋮ Element No i Index No i-1 x[0] x[1] x[2] x[3] x[4]
  • 9. 1 D Array Initialization int x[5] = { 1, 2, 3, 4, 5}; Similarly, x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 float arr[6]={0, 0.25, 0, -0.50, 0, 0}; char color[ ]={'R', 'G', 'B'}; • The array size need not be specified explicitly when initial values are included as a part of an array definition. • The array size will automatically be set equal to the number of initial values included within the definition.
  • 10. Assigning Values to Array Elements int x[5] = { 1, 2, 3, 4, 5}; x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 x[2] = 7;
  • 11. Assigning Values to Array Elements int x[5] = { 1, 2, 3, 4, 5}; x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 x[0] x[1] x[2] x[3] x[4] 1 2 7 4 5 x[2] = 7;
  • 12. Printing Array Elements int x[5] = { 1, 2, 3, 4, 5}; x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 printf (“ % d “ , x[2] ) ; ///3
  • 13. Problem • Write a program that will declare a 10 elements array and saves the value of squares from 1 to 10 to those 10 array elements.
  • 14. Problem • Write a program that will declare a 10 elements array and saves the value of squares from 1 to 10 to those 10 array elements. x[0] x[1] x[2] x[3] x[4] 1 4 9 16 25 x[5] x[6] x[7] x[8] x[9] 36 49 64 81 100
  • 16. Solution #include<stdio.h> #define SIZE 10 int main() { int sqrs[SIZE], i; return 0; }
  • 17. Solution #include<stdio.h> #define SIZE 10 int main() { int sqrs[SIZE], i; ///assigning values to array elements for(i=0; i<=SIZE-1; i++){ sqrs[i]=(i+1)*(i+1); } return 0; }
  • 18. Solution #include<stdio.h> #define SIZE 10 int main() { int sqrs[SIZE], i; ///assigning values to array elements for(i=0; i<=SIZE-1; i++){ sqrs[i]=(i+1)*(i+1); } ///showing the values of array elements for(i=0; i<=SIZE-1; i++){ printf("%d th element: %dn", i, sqrs[i]); } return 0; }
  • 19. scanf for 1 D Array • Using scanf you can give input values to specific array elements. x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 scanf("%d", &x[2]); ///let, user gives input 100 x[0] x[1] x[2] x[3] x[4] 1 2 100 4 5
  • 20. Sample code #include<stdio.h> int main() { int arr[10]={1,2,3,4,5,6,7,8,9,10}; int i; for(i=0;i<10;i+=2){ scanf("%d", &arr[i] ); } for(i=0;i<10;i++){ printf("%d ", arr[i] ); } return 0; }
  • 21. Note • Single operations which involve entire arrays are not permitted in C. • So in C, you can’t assign one entire array to another. #include<stdio.h> int main() { int a1[10],a2[10]; int i; for(i=0;i<10;i++){ scanf("%d", &a1[i] ); } a2=a1; ///error return 0; }
  • 22. Sample code ///sample code to copy one array into another #include<stdio.h> int main() { int a1[5],a2[5]; int i; for(i=0;i<5;i++){ scanf("%d", &a1[i] ); } ///can’t write a2=a1; for(i=0;i<5;i++){ a2[i]=a1[i]; } ///showing outputs for(i=0;i<5;i++){ printf("%d ",a2[i]); } return 0; }
  • 23. Problem • Write a program to search an element from array elements.
  • 24. Sample code #include<stdio.h> #define SIZE 1000 int main() { int arr[SIZE]; int sz,element; printf("Please enter the number of elements of the array: "); scanf("%d",&sz); int i; for(i=0;i<sz;i++){ scanf("%d",&arr[i]); } printf("Please enter the element to search for: "); scanf("%d", &element); for(i=0;i<sz;i++){ if(arr[i]==element){ printf("Yes"); break; } } if(i==sz) printf("No"); return 0; }
  • 25. Problem • Write a program to find the maximum element of an array containing positive numbers.
  • 26. Sample code #include<stdio.h> #define SIZE 1000 int main() { int arr[SIZE]; int sz, max_el=0; printf("Please enter the number of elements of the array: "); scanf("%d",&sz); int i; for(i=0;i<sz;i++){ scanf("%d",&arr[i]); } for(i=0;i<sz;i++){ if(arr[i]>max_el){ max_el=arr[i]; } } printf("The maximum value is %dn", max_el); return 0; }
  • 27. Problem • Write a program to save an array in reverse order into another array.
  • 28. Sample code #include<stdio.h> int main() { int arr[5]={1,2,3,4,5}; int rev_arr[5]; int i; for(i=0;i<5;i++){ rev_arr[i]=arr[5-i-1]; } for(i=0;i<5;i++) printf("%d ",rev_arr[i]); return 0; }
  • 29. Problem • Write a program to calculate the sum of two 1 D array (element wise) of exactly same size.
  • 30. Sample code #include<stdio.h> int main() { int a1[5]={1,2,3,4,5}; int a2[5]={5,4,3,2,1}; int sum[5]; int i; for(i=0;i<5;i++){ sum[i]=a1[i]+a2[i]; } for(i=0;i<5;i++) printf("%d ",sum[i]); return 0; }
  • 31. Problem • Write a program to right rotate an array. Take input the amount to rotate right.
  • 32. Sample code #include<stdio.h> #define SIZE 7 int main() { int arr[SIZE]={1,2,3,4,5,6,7}; int r_rotate; printf("Enter the amount to rotate right: "); scanf("%d",&r_rotate); int i; for(i=SIZE-1;i>=r_rotate;i--){ arr[i]=arr[i-r_rotate]; } for(i=0;i<r_rotate;i++){ arr[i]=0; } for(i=0;i<SIZE;i++) printf("%d ",arr[i]); return 0; }
  • 33. Problem • Write a program to separate the even and odd elements of an array into two other arrays.
  • 34. Sample code #include<stdio.h> #define SIZE 7 int main() { int arr[SIZE]={1, 2, 3, 4, 5, 6, 7}; int even[SIZE],odd[SIZE]; int i,even_sz=0,odd_sz=0; for(i=0;i<SIZE;i++){ if(arr[i]%2==0){ even[even_sz]=arr[i]; even_sz++; } else{ odd[odd_sz]=arr[i]; odd_sz++; } } printf("Even Array: "); ///showing the even numbers for(i=0;i<even_sz;i++) printf("%d ",even[i]); printf("nOdd Array: "); ///showing the odd numbers for(i=0;i<odd_sz;i++) printf("%d ",odd[i]); return 0; }
  • 35. References: • Teach Yourself C, 3rd edition Section: 5.1 • Schaum’s Outline,3rd edition Section: 9.1, 9.2