SlideShare a Scribd company logo
Arrays Session 7
Objectives Explain array elements and indices Define an array Explain array handling in C Explain how an array is initialized Explain string / character arrays Explain two dimensional arrays Explain   initialization of two dimensional arrays
Array Elements & Indices Each member of an array is identified by unique index or subscript assigned to it The dimension of an array is determined by the number of indices needed to uniquely identify each element An index is a positive integer enclosed in [ ] placed immediately after the array name An index holds integer values starting with zero An array with 11 elements will look like - Player[0], player[1], player[2],…. Player[10]
Defining an Array-1 An array has some particular characteristics and has to be defined with them These characteristics include – Storage Class Data Types of the elements in the Array Array Name   Which indicates the location of the first    member of the array Array Size   a constant evaluating to a +ve value .
Defining an Array-2 An array is defined in the same way as a variable is defined. The only change is that  the array name is followed by one or more  expressions, enclosed within square brackets [], specifying the array dimension. Storage_Class  data_types  array_name[size] int player[11];
Norms with Arrays All elements of an array are of the same type Each element of an array can be used wherever a variable is allowed or required Each element of an array can be referenced using a variable or an integer expression Arrays can have their data types like  int, char, float or double
Array Handling in C-1 An array is treated differently from a variable in C Two arrays, even if they are of the same type and size cannot be tested for equality It  is not possible to assign one array directly to another Values cannot be assigned to an array on the whole, instead values are assigned to the elements of the array
Array Handling in C-2 /* Input values are accepted from the user into the array ary[10]*/ #include <stdio.h> void main() { int ary[10]; int i, total, high; for(i=0; i<10; i++) { printf(“\n Enter value: %d : ”, i+1); scanf(“%d”,&ary[i]); } /* Displays highest of the entered values */ high = ary[0]; for(i=1; i<10; i++) { if(ary[i] > high) high = ary[i]; } printf(“\nHighest value entered was %d”, high); /* prints average of values entered for ary[10] */ for(i=0,total=0; i<10; i++) total = total + ary[i]; printf(“\nThe average of the elements of ary is %d”,total/i); }
Array Initialization Each element of an Automatic array needs to be initialized separately In the following example the array elements have been assigned valued using the  for  loop In case of extern and static arrays, the elements are automatically initialized to zero #include <stdio.h> void main() {   char alpha[26];   int i, j;   for(i=65,j=0; i<91; i++,j++)   {   alpha[j] = i; printf(“The character now assigned is %c \n”, alpha[j]);   }   getchar(); }
Strings/Character Arrays-1 A string can be defined as a character type array, which is terminated by a null character Each character in a string occupies one byte and the last character of a string is “\0” (Backslash zero) Example #include <stdio.h> void main() { char ary[5]; int i; printf(“\n Enter string : “); scanf(“%s”,ary);  printf(“\n The string is %s \n\n”, ary); for (i=0; i<5; i++) printf(“\t%d”, ary[i]); }
Strings/Character Arrays-2 Output - The input for the above is of 4 characters and the 5 th  character is the null character The above output is for an input of 5 characters
String Functions Wide range of string functions, which are found in the standard header file <string.h>
Two-Dimensional Arrays The simplest and the most commonly used multi-dimensional array is the two - dimensional array A two-dimensional array can be thought of as an array of two single dimensional arrays A two-dimensional array looks like a railway time-table consisting of rows and columns A two–dimensional array is declared as - int temp[4][3];
Initialization of Multidimensional Arrays-1 int ary[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; The result of the above assignment will be as follows :
int ary[3][4]= { {1,2,3}, {4,5,6}, {7,8,3}   }; Initialization of Multidimensional Arrays-2
Initialization of Multidimensional Arrays-3 The result of the assignment will be as follows : A two - dimensional string array is declared in the following manner : char str_ary[25][80];
Two-Dimensional Array-1 #include <stdio.h> #include <string.h> void main () { int i, n = 0;  int item; char x[10][12]; char temp[12]; clrscr(); printf(“Enter each string on a separate line\n\n”); printf(“Type ‘END’ when over \n\n”); /* read in the list of strings */ do { printf(“String %d : ”, n+1); scanf(“%s”, x[n]); } while (strcmp(x[n++], “END”)); /*reorder the list of strings */   contd…. Example
Two-Dimensional Array-2 n = n – 1; for(item=0; item<n-1; ++item) { /* find lowest of remaining strings */ for(i=item+1; i<n; ++i) { if(strcmp (x[item], x[i]) > 0) { /*interchange two stings */ strcpy (temp, x[item]); strcpy (x[item], x[i]); strcpy (x[i], temp);   }   } } /* Display the arranged list of strings */ printf(“Recorded list of strings : \n”); for(i = 0; i < n ; ++i) { printf(&quot;\nString %d is %s&quot;, i+1, x[i]); } } Example

More Related Content

PPTX
Arrays 1D and 2D , and multi dimensional
PDF
Array
PDF
Arrays-Computer programming
PPT
Array Presentation
PPTX
C++ programming (Array)
PPTX
Arrays in c language
PPT
C++ Arrays
Arrays 1D and 2D , and multi dimensional
Array
Arrays-Computer programming
Array Presentation
C++ programming (Array)
Arrays in c language
C++ Arrays

What's hot (20)

PPT
One dimensional 2
PPT
Array
PPT
Array in c
PPT
Chap09
PPTX
Arrays
PDF
PPTX
Arrays in C++
PPTX
2CPP06 - Arrays and Pointers
PPT
PPTX
COM1407: Arrays
PPTX
C# Arrays
PPTX
arrays in c
PPTX
Array Of Pointers
PPTX
Two-dimensional array in java
PDF
Control statements-Computer programming
PPTX
PPTX
ARRAY
PPT
02 c++ Array Pointer
One dimensional 2
Array
Array in c
Chap09
Arrays
Arrays in C++
2CPP06 - Arrays and Pointers
COM1407: Arrays
C# Arrays
arrays in c
Array Of Pointers
Two-dimensional array in java
Control statements-Computer programming
ARRAY
02 c++ Array Pointer
Ad

Viewers also liked (20)

PPTX
Eccenca linked data_101 (en)
PDF
Ossiannilsson langoer 26mars2015
KEY
Should I be on Twitter?
PPT
cutting
PPT
Innovative Interfaces
PPT
Erik Forsberg, Uppsala Bio X
PPT
Session 19
PPTX
Ossiannilsson130311 openedweek
PDF
Things That Make You Go Hmmmm
PPTX
Presentation d4 l-aalborgdk2015
PDF
Ossiannilsson medfak sida_lu2015
PPTX
Ngl14 workshop ossiannilsson_creelman_uhlin
PPT
Taste of Technology
PPTX
Ossiannilsson 140428 icde_langoer_nordicoer
PPT
פלאפל נוי שמילה
PPT
PPTX
Jim Sandrock Resume
PDF
Agile Project management For Drupal Web Development Projects
PPTX
Ossiannilsson ec et2020_wgdol_ljubliana_moo_cs_oer15_09_15
PPT
101 Cold Call Tips
Eccenca linked data_101 (en)
Ossiannilsson langoer 26mars2015
Should I be on Twitter?
cutting
Innovative Interfaces
Erik Forsberg, Uppsala Bio X
Session 19
Ossiannilsson130311 openedweek
Things That Make You Go Hmmmm
Presentation d4 l-aalborgdk2015
Ossiannilsson medfak sida_lu2015
Ngl14 workshop ossiannilsson_creelman_uhlin
Taste of Technology
Ossiannilsson 140428 icde_langoer_nordicoer
פלאפל נוי שמילה
Jim Sandrock Resume
Agile Project management For Drupal Web Development Projects
Ossiannilsson ec et2020_wgdol_ljubliana_moo_cs_oer15_09_15
101 Cold Call Tips
Ad

Similar to Session 7 En (20)

PDF
Array&amp;string
PDF
Introduction to Arrays in C
PDF
Unit ii data structure-converted
PDF
02 arrays
DOC
Arrays and Strings
PPTX
C (PPS)Programming for problem solving.pptx
PDF
Arrays and library functions
PDF
VIT351 Software Development VI Unit2
PDF
SlideSet_4_Arraysnew.pdf
DOCX
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
PPTX
Module_3_Arrays - Updated.pptx............
PDF
ARRAYS
PPTX
Chapter 13.pptx
DOCX
Array assignment
PPT
presentation_arrays_1443553113_140676.ppt
PPTX
Unit 6. Arrays
PPTX
arrays in c programming - example programs
PDF
array-191103180006.pdf
PDF
11. Programming(BS-phy6)-Lecture11+12 .pdf
Array&amp;string
Introduction to Arrays in C
Unit ii data structure-converted
02 arrays
Arrays and Strings
C (PPS)Programming for problem solving.pptx
Arrays and library functions
VIT351 Software Development VI Unit2
SlideSet_4_Arraysnew.pdf
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
Module_3_Arrays - Updated.pptx............
ARRAYS
Chapter 13.pptx
Array assignment
presentation_arrays_1443553113_140676.ppt
Unit 6. Arrays
arrays in c programming - example programs
array-191103180006.pdf
11. Programming(BS-phy6)-Lecture11+12 .pdf

More from guest91d2b3 (10)

PPT
Session 11
PPT
Session 01
PPT
Session 04 Sua
PPT
Session 02
PPT
Session 17
PPT
Session 06
PPT
Session 09
PPT
Session 13
PPT
Session 15
PPT
Session 21
Session 11
Session 01
Session 04 Sua
Session 02
Session 17
Session 06
Session 09
Session 13
Session 15
Session 21

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Advanced IT Governance
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Modernizing your data center with Dell and AMD
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Approach and Philosophy of On baking technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Cloud computing and distributed systems.
Teaching material agriculture food technology
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Advanced IT Governance
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
CIFDAQ's Market Insight: SEC Turns Pro Crypto
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Modernizing your data center with Dell and AMD
The AUB Centre for AI in Media Proposal.docx
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Approach and Philosophy of On baking technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

Session 7 En

  • 2. Objectives Explain array elements and indices Define an array Explain array handling in C Explain how an array is initialized Explain string / character arrays Explain two dimensional arrays Explain initialization of two dimensional arrays
  • 3. Array Elements & Indices Each member of an array is identified by unique index or subscript assigned to it The dimension of an array is determined by the number of indices needed to uniquely identify each element An index is a positive integer enclosed in [ ] placed immediately after the array name An index holds integer values starting with zero An array with 11 elements will look like - Player[0], player[1], player[2],…. Player[10]
  • 4. Defining an Array-1 An array has some particular characteristics and has to be defined with them These characteristics include – Storage Class Data Types of the elements in the Array Array Name Which indicates the location of the first member of the array Array Size a constant evaluating to a +ve value .
  • 5. Defining an Array-2 An array is defined in the same way as a variable is defined. The only change is that the array name is followed by one or more expressions, enclosed within square brackets [], specifying the array dimension. Storage_Class data_types array_name[size] int player[11];
  • 6. Norms with Arrays All elements of an array are of the same type Each element of an array can be used wherever a variable is allowed or required Each element of an array can be referenced using a variable or an integer expression Arrays can have their data types like int, char, float or double
  • 7. Array Handling in C-1 An array is treated differently from a variable in C Two arrays, even if they are of the same type and size cannot be tested for equality It is not possible to assign one array directly to another Values cannot be assigned to an array on the whole, instead values are assigned to the elements of the array
  • 8. Array Handling in C-2 /* Input values are accepted from the user into the array ary[10]*/ #include <stdio.h> void main() { int ary[10]; int i, total, high; for(i=0; i<10; i++) { printf(“\n Enter value: %d : ”, i+1); scanf(“%d”,&ary[i]); } /* Displays highest of the entered values */ high = ary[0]; for(i=1; i<10; i++) { if(ary[i] > high) high = ary[i]; } printf(“\nHighest value entered was %d”, high); /* prints average of values entered for ary[10] */ for(i=0,total=0; i<10; i++) total = total + ary[i]; printf(“\nThe average of the elements of ary is %d”,total/i); }
  • 9. Array Initialization Each element of an Automatic array needs to be initialized separately In the following example the array elements have been assigned valued using the for loop In case of extern and static arrays, the elements are automatically initialized to zero #include <stdio.h> void main() { char alpha[26]; int i, j; for(i=65,j=0; i<91; i++,j++) { alpha[j] = i; printf(“The character now assigned is %c \n”, alpha[j]); } getchar(); }
  • 10. Strings/Character Arrays-1 A string can be defined as a character type array, which is terminated by a null character Each character in a string occupies one byte and the last character of a string is “\0” (Backslash zero) Example #include <stdio.h> void main() { char ary[5]; int i; printf(“\n Enter string : “); scanf(“%s”,ary); printf(“\n The string is %s \n\n”, ary); for (i=0; i<5; i++) printf(“\t%d”, ary[i]); }
  • 11. Strings/Character Arrays-2 Output - The input for the above is of 4 characters and the 5 th character is the null character The above output is for an input of 5 characters
  • 12. String Functions Wide range of string functions, which are found in the standard header file <string.h>
  • 13. Two-Dimensional Arrays The simplest and the most commonly used multi-dimensional array is the two - dimensional array A two-dimensional array can be thought of as an array of two single dimensional arrays A two-dimensional array looks like a railway time-table consisting of rows and columns A two–dimensional array is declared as - int temp[4][3];
  • 14. Initialization of Multidimensional Arrays-1 int ary[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; The result of the above assignment will be as follows :
  • 15. int ary[3][4]= { {1,2,3}, {4,5,6}, {7,8,3} }; Initialization of Multidimensional Arrays-2
  • 16. Initialization of Multidimensional Arrays-3 The result of the assignment will be as follows : A two - dimensional string array is declared in the following manner : char str_ary[25][80];
  • 17. Two-Dimensional Array-1 #include <stdio.h> #include <string.h> void main () { int i, n = 0; int item; char x[10][12]; char temp[12]; clrscr(); printf(“Enter each string on a separate line\n\n”); printf(“Type ‘END’ when over \n\n”); /* read in the list of strings */ do { printf(“String %d : ”, n+1); scanf(“%s”, x[n]); } while (strcmp(x[n++], “END”)); /*reorder the list of strings */ contd…. Example
  • 18. Two-Dimensional Array-2 n = n – 1; for(item=0; item<n-1; ++item) { /* find lowest of remaining strings */ for(i=item+1; i<n; ++i) { if(strcmp (x[item], x[i]) > 0) { /*interchange two stings */ strcpy (temp, x[item]); strcpy (x[item], x[i]); strcpy (x[i], temp); } } } /* Display the arranged list of strings */ printf(“Recorded list of strings : \n”); for(i = 0; i < n ; ++i) { printf(&quot;\nString %d is %s&quot;, i+1, x[i]); } } Example