SlideShare a Scribd company logo
1 of 8
Arrays in C
Dr.G.Jasmine Beulah,
Dept. Computer Science, Kristu Jayanti College,
Bengaluru.
An array is defined as the collection of similar type of data
items stored at contiguous memory locations.
//Array Initialization and Traversal
#include<stdio.h>
int main()
{
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d n",marks[i]);
}//end of for loop
return 0;
}
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%dn", values[i]);
}
return 0;
}
// Program to find the average of n numbers using arrays
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
// adding integers entered by the user to the sum variable
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
}
Sorting an array
#include<stdio.h>
void main ()
{ int i, j, temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] > a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
} } }
printf("Printing Sorted Element List ...n");
for(i = 0; i<10; i++)
{ printf("%dn",a[i]);
}
Program to print the largest and second
largest element of the array.
#include<stdio.h>
void main ()
{
int arr[100],i,n,largest,sec_largest;
printf("Enter the size of the array?");
scanf("%d",&n);
printf("Enter the elements of the array?");
for(i = 0; i<n; i++)
{
scanf("%d",&arr[i]);
}
#include<stdio.h>
void main ()
{
int arr[100],i,n,largest,sec_largest;
printf("Enter the size of the array?");
scanf("%d",&n);
printf("Enter the elements of the array?");
for(i = 0; i<n; i++)
{
scanf("%d",&arr[i]);
}

More Related Content

What's hot

What's hot (20)

C programming structure & pointer
C  programming structure & pointerC  programming structure & pointer
C programming structure & pointer
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Structures
StructuresStructures
Structures
 
Structure & Union in C++
Structure & Union in C++Structure & Union in C++
Structure & Union in C++
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
Basic of Structure,Structure members,Accessing Structure member,Nested Struct...
 
Structure in c sharp
Structure in c sharpStructure in c sharp
Structure in c sharp
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structures
StructuresStructures
Structures
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
C Structure and Union in C
C Structure and Union in CC Structure and Union in C
C Structure and Union in C
 
Structures
StructuresStructures
Structures
 
OO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual FunctionOO-like C Programming: Struct Inheritance and Virtual Function
OO-like C Programming: Struct Inheritance and Virtual Function
 
Foundations of Programming Part II
Foundations of Programming Part IIFoundations of Programming Part II
Foundations of Programming Part II
 

Similar to Arrays

C-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfC-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdf
herminaherman
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.ppt
NamakkalPasanga
 

Similar to Arrays (20)

data_structure (1).pptx
data_structure (1).pptxdata_structure (1).pptx
data_structure (1).pptx
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Data structures Lecture 5
Data structures Lecture 5Data structures Lecture 5
Data structures Lecture 5
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Array
ArrayArray
Array
 
C-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdfC-Program Custom Library, Header File, and Implementation FilesI .pdf
C-Program Custom Library, Header File, and Implementation FilesI .pdf
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Unit 3
Unit 3 Unit 3
Unit 3
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.ppt
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
C arrays
C arraysC arrays
C arrays
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Arrays Fundamentals Unit II
Arrays  Fundamentals Unit IIArrays  Fundamentals Unit II
Arrays Fundamentals Unit II
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
C_Arrays.pptx
C_Arrays.pptxC_Arrays.pptx
C_Arrays.pptx
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 

More from DrJasmineBeulahG (8)

File Handling in C.pptx
File Handling in C.pptxFile Handling in C.pptx
File Handling in C.pptx
 
Constants and Unformatted Input Output Functions.pptx
Constants and Unformatted Input Output Functions.pptxConstants and Unformatted Input Output Functions.pptx
Constants and Unformatted Input Output Functions.pptx
 
Software Testing.pptx
Software Testing.pptxSoftware Testing.pptx
Software Testing.pptx
 
Software Process Model.ppt
Software Process Model.pptSoftware Process Model.ppt
Software Process Model.ppt
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Exception Handling in Python
Exception Handling in PythonException Handling in Python
Exception Handling in Python
 
STUDENT DETAILS DATABASE.pptx
STUDENT DETAILS DATABASE.pptxSTUDENT DETAILS DATABASE.pptx
STUDENT DETAILS DATABASE.pptx
 
Selection Sort.pptx
Selection Sort.pptxSelection Sort.pptx
Selection Sort.pptx
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

Arrays

  • 1. Arrays in C Dr.G.Jasmine Beulah, Dept. Computer Science, Kristu Jayanti College, Bengaluru.
  • 2. An array is defined as the collection of similar type of data items stored at contiguous memory locations.
  • 3. //Array Initialization and Traversal #include<stdio.h> int main() { int i=0; int marks[5];//declaration of array marks[0]=80;//initialization of array marks[1]=60; marks[2]=70; marks[3]=85; marks[4]=75; //traversal of array for(i=0;i<5;i++){ printf("%d n",marks[i]); }//end of for loop return 0; }
  • 4. // Program to take 5 values from the user and store them in an array // Print the elements stored in the array #include <stdio.h> int main() { int values[5]; printf("Enter 5 integers: "); // taking input and storing it in an array for(int i = 0; i < 5; ++i) { scanf("%d", &values[i]); } printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < 5; ++i) { printf("%dn", values[i]); } return 0; }
  • 5. // Program to find the average of n numbers using arrays #include <stdio.h> int main() { int marks[10], i, n, sum = 0, average; printf("Enter number of elements: "); scanf("%d", &n); for(i=0; i<n; ++i) { printf("Enter number%d: ",i+1); scanf("%d", &marks[i]); // adding integers entered by the user to the sum variable sum += marks[i]; } average = sum/n; printf("Average = %d", average); return 0; }
  • 6. Sorting an array #include<stdio.h> void main () { int i, j, temp; int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; for(i = 0; i<10; i++) { for(j = i+1; j<10; j++) { if(a[j] > a[i]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } printf("Printing Sorted Element List ...n"); for(i = 0; i<10; i++) { printf("%dn",a[i]); }
  • 7. Program to print the largest and second largest element of the array. #include<stdio.h> void main () { int arr[100],i,n,largest,sec_largest; printf("Enter the size of the array?"); scanf("%d",&n); printf("Enter the elements of the array?"); for(i = 0; i<n; i++) { scanf("%d",&arr[i]); }
  • 8. #include<stdio.h> void main () { int arr[100],i,n,largest,sec_largest; printf("Enter the size of the array?"); scanf("%d",&n); printf("Enter the elements of the array?"); for(i = 0; i<n; i++) { scanf("%d",&arr[i]); }