SlideShare a Scribd company logo
1 of 23
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};
 // 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

What's hot

Array within a class
Array within a classArray within a class
Array within a classAAKASH KUMAR
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++Fahim Adil
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shortingargusacademy
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2vikram mahendra
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsRai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-aneebkmct
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsRai University
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsRai University
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and StringTasnima Hamid
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and stringsRai University
 

What's hot (20)

String in c
String in cString in c
String in c
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Array within a class
Array within a classArray within a class
Array within a class
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Arrays
ArraysArrays
Arrays
 
14 strings
14 strings14 strings
14 strings
 
C programming slide c05
C programming slide c05C programming slide c05
C programming slide c05
 
Python : Dictionaries
Python : DictionariesPython : Dictionaries
Python : Dictionaries
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage 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
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs 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
ArrayArray
Array
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 

Similar to Arrays (20)

Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Array
ArrayArray
Array
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
Arrays
ArraysArrays
Arrays
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Array in C.pdf
Array in C.pdfArray in C.pdf
Array in C.pdf
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
 
02 arrays
02 arrays02 arrays
02 arrays
 
C Language Unit-3
C Language Unit-3C Language Unit-3
C Language Unit-3
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfiles
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Array
ArrayArray
Array
 

Recently uploaded

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
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
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...Call Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 

Recently uploaded (20)

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
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
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
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
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
High Profile Call Girls Nashik Megha 7001305949 Independent Escort Service Na...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 

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};
  • 6.
  • 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