SlideShare a Scribd company logo
Arrays
 An array is a collection of data that holds
fixed number of values of same type. float
marks[100].
 The size and type of arrays cannot be
changed after its declaration.
Arrays are of two types:
 One-dimensional rrays
 Multidimensional Arrays
 data_type array_name[array_size]; For
example,
 float mark[5];Here, we declared an
array, mark, of floating-point type and size
5. Meaning, it can hold 5 floating-point
values.
 You can access elements of an array by indices.
 Few key notes:
 Arrays have 0 as the first index not 1. In this
example, mark[0]
 If the size of an array is n, to access the last
element, (n-1) index is used. In this
example, mark[4]
 Suppose the starting address of mark[0] is 2120d.
Then, the next address, a[1], will be 2124d, address
of a[2] will be 2128d and so on. It's because the size
of a float is 4 bytes.
 It's possible to initialize an array during
declaration. For example,
 int mark[5] = {19, 10, 8, 17, 9};
OR
 int mark[] = {19, 10, 8, 17, 9};
Arrays
 // Program to find the average of n (n < 10) numbers using arrays
 #include <stdio.h>
 int main()
 { int marks[10], i, n, sum = 0, average;
 printf("Enter n: ");
 scanf("%d", &n);
 for(i=0; i<n; ++i)
 { printf("Enter number%d: ",i+1);
 scanf("%d", &marks[i]);
 sum += marks[i]; }
 average = sum/n;
 printf("Average = %d", average);
 return 0;
 }
 Enter n: 5
 Enter number1: 45
 Enter number2: 35
 Enter number3: 38
 Enter number4: 31
 Enter number5: 49
 Average = 39
 In C programming, a single array element or
an entire array can be passed to a function.
 This can be done for both one-dimensional
array or a multi-dimensional array.
 #include <stdio.h>
 void display(int age)
 {
 printf("%d", age);
 }
 int main()
 {
 int ageArray[] = { 2, 3, 4 };
 display(ageArray[2]); //Passing array element ageArray[2]
only.
 return 0;
 }
 Output
 4
 While passing arrays as arguments to the
function, only the name of the array is
passed (,i.e, starting address of memory area
is passed as argument).
 #include <stdio.h>
 float average(float age[]);
 main()​
 { float avg, age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };
 avg = average(age); /* Only name of array is passed
as argument. */
 printf("Average age=%.2f", avg); }
 float average(float age[])
 { int i;
 float avg, sum = 0.0;
 for (i = 0; i < 6; ++i) {
 sum += age[i]; }
 avg = (sum / 6);
 return avg;
 }
 void displayNumbers(int num[2][2]);
 main()
 { int num[2][2], i, j;
 printf("Enter 4 numbers:n");
 for (i = 0; i < 2; ++i)
 for (j = 0; j < 2; ++j)
 scanf("%d", &num[i][j]);
 // passing multi-dimensional array to displayNumbers function
 displayNumbers(num); }
 void displayNumbers(int num[2][2])
 { int i, j;
 printf("Displaying:n");
 for (i = 0; i < 2; ++i)
 for (j = 0; j < 2; ++j)
 printf("%dn", num[i][j]);
 }
 main()
 {
 int mat[4][4],trans[4][4],row,col;
 printf("enter elements od an array");
 filling Matrix
 for(row=0;row<4;row++)
 for(col=0;col<4;col++)
 scanf("%d",&mat[row][col]);

 Converting rows into cols
 for(row=0;row<4;row++)
 for(col=0;col<4;col++)
 trans[col][row]=mat[row][col];

 //displaying original matrix
 printf("original matrix n");
 for(row=0;row<4;row++ )
 { for(col=0;col<4;col++)
 { printf("t %d",mat[row][col]);
 }
 printf("n");
 }
 //displaying transpose matrix
 printf("Transpose matrix n ");
 for(row=0;row<4;row++)
 {
 for(col=0;col<4;col++)
 { printf("t %d",trans[row][col]);
 }
 printf("n") ;
 }
 }
 In C programming, array of characters is
called a string. A string is terminated by a
null character /0. For example:
 char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; OR
 char greeting[] = "Hello";
 When, compiler encounter strings, it appends
a null character /0 at the end of string.
 Using arrays
 char c[] = "abcd";
OR
char c[50] = "abcd";
OR,
char c[] = {'a', 'b', 'c', 'd', '0'};
OR,
char c[5] = {'a', 'b', 'c', 'd', '0'};
 The given string is initialized and stored in
the form of arrays as above.
 Write a C program to illustrate how to read string from terminal.
 #include <stdio.h>
 int main()
 {
 char name[20];
 printf("Enter name: ");
 scanf("%s", name);
 printf("Your name is %s.", name);
 return 0;
 }
 Output
 Enter name: Dennis Ritchie
 Your name is Dennis.
 Here, program ignores Ritchie because, scanf() function takes
only a single string before the white space, i.e. Dennis.
 Example #2: Using getchar() to read a line of text
 1. C program to read line of text character by character.
 #include <stdio.h>
 int main()
 { char name[30], ch;
 int i = 0;
 printf("Enter name: ");
 while(ch != 'n') // terminates if user hit enter
 { ch = getchar();
 name[i] = ch;
 i++;
 }
 name[i] = '0'; // inserting null character at end
 printf("Name: %s", name);
 return 0;
 }
 #include <stdio.h>
 int main()
 { char name[30];
 printf("Enter name: ");
 gets(name); //Function to read string from user.
 printf("Name: ");
 puts(name); //Function to display string.
 return 0;}
 Output
 Enter name: Dennis Ritchie
 Name: Dennis Ritchie
Sr.No. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
 #include <string.h>
 int main () {
 char str1[12] = "Hello";
 char str2[12] = "World";
 char str3[12];
 int len ;
 /* copy str1 into str3 */
 strcpy(str3, str1);
 printf("strcpy( str3, str1) : %sn", str3 );
 /* concatenates str1 and str2 */
 strcat( str1, str2);
 printf("strcat( str1, str2): %sn", str1 );
 /* total lenghth of str1 after concatenation */
 len = strlen(str1);
 printf("strlen(str1) : %dn", len );
 return 0;
 strcpy( str3, str1) : Hello
 strcat( str1, str2): HelloWorld
 strlen(str1) : 10

More Related Content

PPTX
function, storage class and array and strings
PPTX
handling input output and control statements
PDF
Unit 2
PDF
C Prog - Array
PPT
PPT
Arrays
PPT
Arrays
DOC
String in c
function, storage class and array and strings
handling input output and control statements
Unit 2
C Prog - Array
Arrays
Arrays
String in c

What's hot (20)

PDF
Python programming : Standard Input and Output
PPTX
Array within a class
PPTX
Templates in C++
PPT
PPT
14 strings
PPTX
C programming slide c05
PPTX
String Handling in c++
PPSX
C programming array & shorting
PPTX
USE OF PRINT IN PYTHON PART 2
PPTX
Mcai pic u 4 function, storage class and array and strings
PDF
Functions torage class and array and strings-
PPTX
Btech i pic u-4 function, storage class and array and strings
PPTX
Bsc cs i pic u-4 function, storage class and array and strings
DOCX
Array
PDF
Arrays and strings in c++
PDF
Character Array and String
PDF
Programming Fundamentals Arrays and Strings
PPTX
Diploma ii cfpc u-4 function, storage class and array and strings
PPTX
USER DEFINE FUNCTIONS IN PYTHON
Python programming : Standard Input and Output
Array within a class
Templates in C++
14 strings
C programming slide c05
String Handling in c++
C programming array & shorting
USE OF PRINT IN PYTHON PART 2
Mcai pic u 4 function, storage class and array and strings
Functions torage class and array and strings-
Btech i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
Array
Arrays and strings in c++
Character Array and String
Programming Fundamentals Arrays and Strings
Diploma ii cfpc u-4 function, storage class and array and strings
USER DEFINE FUNCTIONS IN PYTHON
Ad

Similar to Arrays (20)

PPT
Array THE DATA STRUCTURE. ITS THE STRUCT
PDF
Arrays and function basic c programming notes
PPTX
C-Arrays & Strings (computer programming).pptx
PPTX
Lecture 1 mte 407
PPTX
Lecture 1 mte 407
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
PPTX
ARRAY's in C Programming Language PPTX.
PPT
PPT
array.ppt
PPTX
Unit 3
PPTX
C (PPS)Programming for problem solving.pptx
PPTX
MY STRING AND ARRAY PROGRAM for all student
PDF
CSEG1001Unit 3 Arrays and Strings
PPTX
Module 4- Arrays and Strings
PPTX
Array
PPT
Basics of Data structure using C describing basics concepts
PPTX
Unit 3
PDF
Unit 2
PDF
SPL 10 | One Dimensional Array in C
Array THE DATA STRUCTURE. ITS THE STRUCT
Arrays and function basic c programming notes
C-Arrays & Strings (computer programming).pptx
Lecture 1 mte 407
Lecture 1 mte 407
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
ARRAY's in C Programming Language PPTX.
array.ppt
Unit 3
C (PPS)Programming for problem solving.pptx
MY STRING AND ARRAY PROGRAM for all student
CSEG1001Unit 3 Arrays and Strings
Module 4- Arrays and Strings
Array
Basics of Data structure using C describing basics concepts
Unit 3
Unit 2
SPL 10 | One Dimensional Array in C
Ad

Recently uploaded (20)

PDF
Abrasive, erosive and cavitation wear.pdf
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
Feature types and data preprocessing steps
PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Design Guidelines and solutions for Plastics parts
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
737-MAX_SRG.pdf student reference guides
PPTX
Artificial Intelligence
PPTX
Management Information system : MIS-e-Business Systems.pptx
Abrasive, erosive and cavitation wear.pdf
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Feature types and data preprocessing steps
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
R24 SURVEYING LAB MANUAL for civil enggi
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Design Guidelines and solutions for Plastics parts
Exploratory_Data_Analysis_Fundamentals.pdf
Categorization of Factors Affecting Classification Algorithms Selection
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Fundamentals of Mechanical Engineering.pptx
III.4.1.2_The_Space_Environment.p pdffdf
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
737-MAX_SRG.pdf student reference guides
Artificial Intelligence
Management Information system : MIS-e-Business Systems.pptx

Arrays

  • 2.  An array is a collection of data that holds fixed number of values of same type. float marks[100].  The size and type of arrays cannot be changed after its declaration. Arrays are of two types:  One-dimensional rrays  Multidimensional Arrays
  • 3.  data_type array_name[array_size]; For example,  float mark[5];Here, we declared an array, mark, of floating-point type and size 5. Meaning, it can hold 5 floating-point values.
  • 4.  You can access elements of an array by indices.  Few key notes:  Arrays have 0 as the first index not 1. In this example, mark[0]  If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark[4]  Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will be 2124d, address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.
  • 5.  It's possible to initialize an array during declaration. For example,  int mark[5] = {19, 10, 8, 17, 9}; OR  int mark[] = {19, 10, 8, 17, 9};
  • 7.  // Program to find the average of n (n < 10) numbers using arrays  #include <stdio.h>  int main()  { int marks[10], i, n, sum = 0, average;  printf("Enter n: ");  scanf("%d", &n);  for(i=0; i<n; ++i)  { printf("Enter number%d: ",i+1);  scanf("%d", &marks[i]);  sum += marks[i]; }  average = sum/n;  printf("Average = %d", average);  return 0;  }
  • 8.  Enter n: 5  Enter number1: 45  Enter number2: 35  Enter number3: 38  Enter number4: 31  Enter number5: 49  Average = 39
  • 9.  In C programming, a single array element or an entire array can be passed to a function.  This can be done for both one-dimensional array or a multi-dimensional array.
  • 10.  #include <stdio.h>  void display(int age)  {  printf("%d", age);  }  int main()  {  int ageArray[] = { 2, 3, 4 };  display(ageArray[2]); //Passing array element ageArray[2] only.  return 0;  }  Output  4
  • 11.  While passing arrays as arguments to the function, only the name of the array is passed (,i.e, starting address of memory area is passed as argument).
  • 12.  #include <stdio.h>  float average(float age[]);  main()​  { float avg, age[] = { 23.4, 55, 22.6, 3, 40.5, 18 };  avg = average(age); /* Only name of array is passed as argument. */  printf("Average age=%.2f", avg); }  float average(float age[])  { int i;  float avg, sum = 0.0;  for (i = 0; i < 6; ++i) {  sum += age[i]; }  avg = (sum / 6);  return avg;  }
  • 13.  void displayNumbers(int num[2][2]);  main()  { int num[2][2], i, j;  printf("Enter 4 numbers:n");  for (i = 0; i < 2; ++i)  for (j = 0; j < 2; ++j)  scanf("%d", &num[i][j]);  // passing multi-dimensional array to displayNumbers function  displayNumbers(num); }  void displayNumbers(int num[2][2])  { int i, j;  printf("Displaying:n");  for (i = 0; i < 2; ++i)  for (j = 0; j < 2; ++j)  printf("%dn", num[i][j]);  }
  • 14.  main()  {  int mat[4][4],trans[4][4],row,col;  printf("enter elements od an array");  filling Matrix  for(row=0;row<4;row++)  for(col=0;col<4;col++)  scanf("%d",&mat[row][col]);   Converting rows into cols  for(row=0;row<4;row++)  for(col=0;col<4;col++)  trans[col][row]=mat[row][col]; 
  • 15.  //displaying original matrix  printf("original matrix n");  for(row=0;row<4;row++ )  { for(col=0;col<4;col++)  { printf("t %d",mat[row][col]);  }  printf("n");  }  //displaying transpose matrix  printf("Transpose matrix n ");  for(row=0;row<4;row++)  {  for(col=0;col<4;col++)  { printf("t %d",trans[row][col]);  }  printf("n") ;  }  }
  • 16.  In C programming, array of characters is called a string. A string is terminated by a null character /0. For example:  char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; OR  char greeting[] = "Hello";  When, compiler encounter strings, it appends a null character /0 at the end of string.
  • 17.  Using arrays  char c[] = "abcd"; OR char c[50] = "abcd"; OR, char c[] = {'a', 'b', 'c', 'd', '0'}; OR, char c[5] = {'a', 'b', 'c', 'd', '0'};  The given string is initialized and stored in the form of arrays as above.
  • 18.  Write a C program to illustrate how to read string from terminal.  #include <stdio.h>  int main()  {  char name[20];  printf("Enter name: ");  scanf("%s", name);  printf("Your name is %s.", name);  return 0;  }  Output  Enter name: Dennis Ritchie  Your name is Dennis.  Here, program ignores Ritchie because, scanf() function takes only a single string before the white space, i.e. Dennis.
  • 19.  Example #2: Using getchar() to read a line of text  1. C program to read line of text character by character.  #include <stdio.h>  int main()  { char name[30], ch;  int i = 0;  printf("Enter name: ");  while(ch != 'n') // terminates if user hit enter  { ch = getchar();  name[i] = ch;  i++;  }  name[i] = '0'; // inserting null character at end  printf("Name: %s", name);  return 0;  }
  • 20.  #include <stdio.h>  int main()  { char name[30];  printf("Enter name: ");  gets(name); //Function to read string from user.  printf("Name: ");  puts(name); //Function to display string.  return 0;}  Output  Enter name: Dennis Ritchie  Name: Dennis Ritchie
  • 21. Sr.No. Function & Purpose 1 strcpy(s1, s2); Copies string s2 into string s1. 2 strcat(s1, s2); Concatenates string s2 onto the end of string s1. 3 strlen(s1); Returns the length of string s1. 4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. 5 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1. 6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
  • 22.  #include <string.h>  int main () {  char str1[12] = "Hello";  char str2[12] = "World";  char str3[12];  int len ;  /* copy str1 into str3 */  strcpy(str3, str1);  printf("strcpy( str3, str1) : %sn", str3 );  /* concatenates str1 and str2 */  strcat( str1, str2);  printf("strcat( str1, str2): %sn", str1 );  /* total lenghth of str1 after concatenation */  len = strlen(str1);  printf("strlen(str1) : %dn", len );  return 0;
  • 23.  strcpy( str3, str1) : Hello  strcat( str1, str2): HelloWorld  strlen(str1) : 10