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};
 // 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

String in c
String in cString in c
String in c
Suneel Dogra
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
Emertxe Information Technologies Pvt Ltd
 
Array within a class
Array within a classArray within a class
Array within a class
AAKASH KUMAR
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Arrays
ArraysArrays
Python : Dictionaries
Python : DictionariesPython : Dictionaries
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 & shorting
argusacademy
 
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
vikram 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 strings
Rai 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 strings
Rai 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 strings
Rai University
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
GC University Faisalabad
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima 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 strings
Rai 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

Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
chanchal ghosh
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
YOGESH SINGH
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
Nishant Munjal
 
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
JawadTanvir
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
Vikram Nandini
 
Array
ArrayArray
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
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
rushabhshah600
 
Array.pptx
Array.pptxArray.pptx
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
mohd_mizan
 
Array in C.pdf
Array in C.pdfArray in C.pdf
Array in C.pdf
georgejustymirobi1
 
Array.pdf
Array.pdfArray.pdf
Array.pdf
mounikanarra3
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
C Language Unit-3
C Language Unit-3C Language Unit-3
C Language Unit-3
kasaragadda srinivasrao
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfilesmrecedu
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
Array
ArrayArray
Array
Anil Dutt
 

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

Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 

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