SlideShare a Scribd company logo
1 of 12
Lecture 1
 An array is a group of same data type.
 For instance, An int array has the ements of
int types.
 A float array holds the elements of float
types.
 Let’s Consider a scenario where we need to find
out the average of 100 integer numbers entered
by user.
 In C, we have two ways to do this:
1) We can define 100 variables with int data type
and then perform 100 scanf() operations to
store the entered values in the variables and
then at last calculate the average of them.
2) We can have a single integer array to store all
the values, loop the array to store all the
entered values in array and later calculate the
average.
Surely, it is convenient to store same data types in
one single variable and later access them using
array index.
1. int num[35];
 [It means, An integer array of 35 elements]
2. char ch[10];
 [It means, an array of characters for 10
elements]
 It is possible to initialize an array during declaration. For
example,
 int mark[5] = {19, 10, 8, 17, 9};
 You can also initialize an array like this.
 int mark[] = {19, 10, 8, 17, 9};
 Here, we haven't specified the size. However, the compiler
knows its size is 5 as we are initializing it with 5 elements.
 Example 1: C Program to calculate the
average of N (N<10) using Array.
 Example 2: C Program to assign values to
one dimensional array.
 Example 3: C program to Display Largest
Element of an array
 Example 4: C program to check whether a
character is palindrome or not
 An array of arrays is known as 2D array. The
two dimensional (2D) array in C
programming is also known as matrix. A
matrix can be represented as a table of rows
and columns. Before we discuss more about
two Dimensional array lets have a look at the
following C program example.
Two dimensional (2D) arrays in C
programming with example
 #include <stdio.h>
 const int CITY = 2;
 const int WEEK = 7;
 int main()
 {
 int temperature[CITY][WEEK];
 // Using nested loop to store values in a 2d array
 for (int i = 0; i < CITY; ++i)
 {
 for (int j = 0; j < WEEK; ++j)
 {
 printf("City %d, Day %d: ", i + 1, j + 1);
 scanf("%d", &temperature[i][j]);
 }
 }
 printf("nDisplaying values: nn");
 // Using nested loop to display vlues of a 2d array
 for (int i = 0; i < CITY; ++i)
 {
 for (int j = 0; j < WEEK; ++j)
 {
 printf("City %d, Day %d = %dn", i + 1, j + 1, temperature[i][j]);
 }
 }
 return 0;
 For loop: for loop in C programming with example. A loop is used for
executing a block of statements repeatedly until a given condition
returns false.
 Structure:
 for (initialization; condition test; increment or decrement)
 {
 //Statements to be executed repeatedly
 }
 Nested loop: A nested loop is a loop within a loop. The most common
applications of loops are for matrix data (e.g., looping through the rows
and columns of a table). You can nest any type of loop inside any other
type; a for loop can be nested in a while loop.
 Structure:
 Outer-Loop
 {
 // body of outer-loop Inner-Loop
 {
 // body of inner-loop
 }
 }

More Related Content

What's hot (19)

Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
Pointers
PointersPointers
Pointers
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
C- Programming Assignment practice set 2 solutions
C- Programming Assignment practice set 2 solutionsC- Programming Assignment practice set 2 solutions
C- Programming Assignment practice set 2 solutions
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
 
Lab 1
Lab 1Lab 1
Lab 1
 
16829 memory management2
16829 memory management216829 memory management2
16829 memory management2
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 
Paper
PaperPaper
Paper
 
C - Programming Assignment 1 and 2
C - Programming Assignment 1 and 2C - Programming Assignment 1 and 2
C - Programming Assignment 1 and 2
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
Thesis PPT
Thesis PPTThesis PPT
Thesis PPT
 
R programming lab 1 - jupyter notebook
R programming lab   1 - jupyter notebookR programming lab   1 - jupyter notebook
R programming lab 1 - jupyter notebook
 
Py9 3
Py9 3Py9 3
Py9 3
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
6.array
6.array6.array
6.array
 

Similar to Arrays and Loops in C

Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programmingnmahi96
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Kumar Boro
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysMaulen Bale
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)SURBHI SAROHA
 
C programming session 05
C programming session 05C programming session 05
C programming session 05Vivek Singh
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2YOGESH SINGH
 

Similar to Arrays and Loops in C (20)

Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
C Arrays.ppt
C Arrays.pptC Arrays.ppt
C Arrays.ppt
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Unit 2
Unit 2Unit 2
Unit 2
 
Array
ArrayArray
Array
 
Array
ArrayArray
Array
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 

Recently uploaded

power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 

Recently uploaded (20)

power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 

Arrays and Loops in C

  • 2.  An array is a group of same data type.  For instance, An int array has the ements of int types.  A float array holds the elements of float types.
  • 3.  Let’s Consider a scenario where we need to find out the average of 100 integer numbers entered by user.  In C, we have two ways to do this: 1) We can define 100 variables with int data type and then perform 100 scanf() operations to store the entered values in the variables and then at last calculate the average of them. 2) We can have a single integer array to store all the values, loop the array to store all the entered values in array and later calculate the average. Surely, it is convenient to store same data types in one single variable and later access them using array index.
  • 4. 1. int num[35];  [It means, An integer array of 35 elements] 2. char ch[10];  [It means, an array of characters for 10 elements]
  • 5.
  • 6.
  • 7.  It is possible to initialize an array during declaration. For example,  int mark[5] = {19, 10, 8, 17, 9};  You can also initialize an array like this.  int mark[] = {19, 10, 8, 17, 9};  Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements.
  • 8.  Example 1: C Program to calculate the average of N (N<10) using Array.  Example 2: C Program to assign values to one dimensional array.  Example 3: C program to Display Largest Element of an array  Example 4: C program to check whether a character is palindrome or not
  • 9.  An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns. Before we discuss more about two Dimensional array lets have a look at the following C program example. Two dimensional (2D) arrays in C programming with example
  • 10.
  • 11.  #include <stdio.h>  const int CITY = 2;  const int WEEK = 7;  int main()  {  int temperature[CITY][WEEK];  // Using nested loop to store values in a 2d array  for (int i = 0; i < CITY; ++i)  {  for (int j = 0; j < WEEK; ++j)  {  printf("City %d, Day %d: ", i + 1, j + 1);  scanf("%d", &temperature[i][j]);  }  }  printf("nDisplaying values: nn");  // Using nested loop to display vlues of a 2d array  for (int i = 0; i < CITY; ++i)  {  for (int j = 0; j < WEEK; ++j)  {  printf("City %d, Day %d = %dn", i + 1, j + 1, temperature[i][j]);  }  }  return 0;
  • 12.  For loop: for loop in C programming with example. A loop is used for executing a block of statements repeatedly until a given condition returns false.  Structure:  for (initialization; condition test; increment or decrement)  {  //Statements to be executed repeatedly  }  Nested loop: A nested loop is a loop within a loop. The most common applications of loops are for matrix data (e.g., looping through the rows and columns of a table). You can nest any type of loop inside any other type; a for loop can be nested in a while loop.  Structure:  Outer-Loop  {  // body of outer-loop Inner-Loop  {  // body of inner-loop  }  }