SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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 a 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
 

Último

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
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
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
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
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
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
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 

Último (20)

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
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
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
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
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
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
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 

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