SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Arrays
What is an array?
• An array is a variable that can store multiple values.
• More formally, it is defined as the collection of similar type of data items stored
at contiguous memory locations.
• Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc. It also has the capability
to store the collection of derived data types, such as pointers, structure, etc.
• The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
Why need array?
• C array is beneficial if you have to store similar elements. For example, if we want
to store the marks of a student in 6 subjects, then we don't need to define
different variables for the marks in the different subject. Instead of that, we can
define an array which can store the marks in each subject at the contiguous
memory locations.
• By using the array, we can access the elements easily. Only a few lines of code are
required to access the elements of the array.
How to declare an array?
For example,
Here, we declared an array, mark, of floating-point type. And its size is 5.
Meaning, it can hold 5 floating-point values.
It's important to note that the size and type of an array cannot be changed
once it is declared.
dataType arrayName[arraySize];
float mark[5];
Access Array Elements
• You can access elements of an array by indices.
• Suppose you declared an array mark as above. The first element is mark[0], the
second element is mark[1] and so on.
Keynotes:
• Arrays have 0 as the first index, not 1. In this example, mark[0] is the first
element.
• If the size of an array is n, to access the last element, the n-1 index is used. In this
example, mark[4]
• Suppose the starting address of mark[0] is 2120d. Then, the address of the
mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on.
• This is because the size of a float is 4 bytes.
How to initialize an array?
• It is possible to initialize an array during declaration. For example,
• You can also initialize an array like this,
• Here, we haven't specified the size. However, the compiler knows its size
is 5 as we are initializing it with 5 elements.
int mark[5] = {19, 10, 8, 17, 9};
int mark[] = {19, 10, 8, 17, 9};
Change Value of Array elements
int mark[5] = {19, 10, 8, 17, 9}
// make the value of the third element to -1
mark[2] = -1;
// make the value of the fifth element to 0
mark[4] = 0;
Input and Output Array Elements
// take input and store it in the 3rd element
scanf("%d %d", &mark[2], &mark[3]);
// take input and store it in the ith element
scanf("%d", &mark[i-1]);
// print the first element of the array
printf("%d", mark[0]);
// print the third element of the array
printf("%d", mark[2]);
// print ith element of the array
printf("%d", mark[i-1]);
0; i<= 4; i++)
”, &mark[i]);
Access elements out of its bound!
Suppose you declared an array of 10 elements. Let's say,
You can access the array elements from testArray[0] to testArray[9].
Now let's say if you try to access testArray[12]. The element is not available. This
may cause unexpected output (undefined behavior). Sometimes you might get an
error and some other time your program may run correctly.
Hence, you should never access elements of an array outside of its bound.
int testArray[10];
Important things to know about Arrays
• Array indexes always begin with 0. Hence when we say array of size 10, array has
elements from index 0 to 9. If we specify or use array as intArr[10], intArr[11],
intArr[200], the C compiler will not show any error, but we will get run time errors
while executing the program.
• Arrays are supported by primitive datatypes, non-primitive types like structures,
unions, pointers etc.
Advantages of C
• In an array, accessing an element is very easy by using the index number.
• The search process can be applied to an array easily.
• 2D Array is used to represent matrices.
• For any reason a user wishes to store multiple values of similar type then the
Array can be used and utilized efficiently.
Disadvantages of C
• It allows us to enter only fixed number of elements into it. We cannot alter the size
of the array once array is declared. Hence if we need to insert more number of
records than declared then it is not possible. We should know array size at the
compile time itself.
• Inserting and deleting the records from the array would be costly since we add /
delete the elements from the array, we need to manage memory space too.
• It does not verify the indexes while compiling the array. In case there is any indexes
pointed which is more than the dimension specified, then we will get run time errors
rather than identifying them at compile time.
• Array size is fixed: The array is static, which means its size is always fixed. The
memory which is allocated to it cannot be increased or decreased.
• Explanation: In the above program the array of size 10 is declared and the value
is assigned at a particular index. But when the value at index 11 is printed then it
prints the garbage value because the array was accessed out of the bound index.
In some compiler, it gives error as “Array Index Out Of Bound.”
• Array is homogeneous: The array is homogeneous, i.e., only one type of value
can be store in the array. For example, if an array type “int“, can only store integer
elements and cannot allow the elements of other types such as double, float,
char so on.
• Explanation: The above code gives “Compilation Error” as an integer type array is
assigned value to a string and float type.
• Array is Contiguous blocks of memory: The array stores data in contiguous(one
by one) memory location.
5 7 12 9 28
0 1 2 4
3
• Insertion and deletion are not easy in Array: The operation insertion and
deletion over an array are problematic as to insert or delete anywhere in the
array, it is necessary to traverse the array and then shift the remaining elements
as per the operation. This operation cost is more.
C Program Exercises
• Write a C program to perform input/output of all basic data types.
• Write a C program to enter two numbers and find their sum.
• Write a C program to enter two numbers and perform all arithmetic operations.
• Write a C program to enter length and breadth of a rectangle and find its
perimeter.
• Write a C program to enter length and breadth of a rectangle and find its area.
• Write a C program to enter radius of a circle and find its diameter, circumference
and area.
• Write a C program to enter length in centimeter and convert it into meter and
kilometer.
• Write a C program to enter temperature in Celsius and convert it into Fahrenheit.
• Write a C program to enter temperature in Fahrenheit and convert to Celsius
• Write a C program to enter marks of five subjects and calculate total, average and
percentage.
• Write a C program to reverse a number.
• Write a C program to find maximum between two numbers.
• Write a C program to find maximum between three numbers.
• Write a C program to check whether a number is negative, positive or zero.
• Write a C program to check whether a number is divisible by 5 and 11 or not.
• Write a C program to check whether a number is even or odd.
• Write a C program to check whether a year is leap year or not.
• Write a C program to input electricity unit charges and calculate total
electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill

Weitere ähnliche Inhalte

Ähnlich wie Arrays.pptx

c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxJanineCallangan
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in Carshpreetkaur07
 
C# Array.pptx
C# Array.pptxC# Array.pptx
C# Array.pptxZanyarRzgar
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++Maliha Mehr
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptxRohithK65
 
Array & Exception Handling in C# (CSharp)
Array & Exception Handling in C# (CSharp)Array & Exception Handling in C# (CSharp)
Array & Exception Handling in C# (CSharp)Sohanur63
 
12.6-12.9.pptx
12.6-12.9.pptx12.6-12.9.pptx
12.6-12.9.pptxWinterSnow16
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfajajkhan16
 
Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C LanguageSurbhi Yadav
 
(2) collections algorithms
(2) collections algorithms(2) collections algorithms
(2) collections algorithmsNico Ludwig
 
Unit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx forUnit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx forpattinsonhenry524
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017Tanmay Modi
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256Arraysincv109102017 180831194256
Arraysincv109102017 180831194256ABHAY9616302301
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c languagetanmaymodi4
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckseidounsemel
 

Ähnlich wie Arrays.pptx (20)

c++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptxc++ arrays and pointers grade 9 STEP curriculum.pptx
c++ arrays and pointers grade 9 STEP curriculum.pptx
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
C# Array.pptx
C# Array.pptxC# Array.pptx
C# Array.pptx
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Ch8 Arrays
Ch8 ArraysCh8 Arrays
Ch8 Arrays
 
Introduction to R.pptx
Introduction to R.pptxIntroduction to R.pptx
Introduction to R.pptx
 
Array & Exception Handling in C# (CSharp)
Array & Exception Handling in C# (CSharp)Array & Exception Handling in C# (CSharp)
Array & Exception Handling in C# (CSharp)
 
12.6-12.9.pptx
12.6-12.9.pptx12.6-12.9.pptx
12.6-12.9.pptx
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C Language
 
(2) collections algorithms
(2) collections algorithms(2) collections algorithms
(2) collections algorithms
 
Unit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx forUnit-4 (Scope Rules and Arrays).pptx for
Unit-4 (Scope Rules and Arrays).pptx for
 
Data structure
Data structureData structure
Data structure
 
Arrays in c v1 09102017
Arrays in c v1 09102017Arrays in c v1 09102017
Arrays in c v1 09102017
 
Arraysincv109102017 180831194256
Arraysincv109102017 180831194256Arraysincv109102017 180831194256
Arraysincv109102017 180831194256
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Lamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ckLamborghini Veneno Allegheri #2004@f**ck
Lamborghini Veneno Allegheri #2004@f**ck
 
Arrays
ArraysArrays
Arrays
 

KĂźrzlich hochgeladen

Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsArindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilVinayVitekari
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxNadaHaitham1
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 

KĂźrzlich hochgeladen (20)

Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 

Arrays.pptx

  • 2. What is an array? • An array is a variable that can store multiple values. • More formally, it is defined as the collection of similar type of data items stored at contiguous memory locations. • Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. • The array is the simplest data structure where each data element can be randomly accessed by using its index number.
  • 3. Why need array? • C array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations. • By using the array, we can access the elements easily. Only a few lines of code are required to access the elements of the array.
  • 4. How to declare an array? For example, Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can hold 5 floating-point values. It's important to note that the size and type of an array cannot be changed once it is declared. dataType arrayName[arraySize]; float mark[5];
  • 5. Access Array Elements • You can access elements of an array by indices. • Suppose you declared an array mark as above. The first element is mark[0], the second element is mark[1] and so on.
  • 6. Keynotes: • Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element. • If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4] • Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on. • This is because the size of a float is 4 bytes.
  • 7. How to initialize an array? • It is possible to initialize an array during declaration. For example, • You can also initialize an array like this, • Here, we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements. int mark[5] = {19, 10, 8, 17, 9}; int mark[] = {19, 10, 8, 17, 9};
  • 8. Change Value of Array elements int mark[5] = {19, 10, 8, 17, 9} // make the value of the third element to -1 mark[2] = -1; // make the value of the fifth element to 0 mark[4] = 0;
  • 9. Input and Output Array Elements // take input and store it in the 3rd element scanf("%d %d", &mark[2], &mark[3]); // take input and store it in the ith element scanf("%d", &mark[i-1]); // print the first element of the array printf("%d", mark[0]); // print the third element of the array printf("%d", mark[2]); // print ith element of the array printf("%d", mark[i-1]); 0; i<= 4; i++) ”, &mark[i]);
  • 10.
  • 11.
  • 12. Access elements out of its bound! Suppose you declared an array of 10 elements. Let's say, You can access the array elements from testArray[0] to testArray[9]. Now let's say if you try to access testArray[12]. The element is not available. This may cause unexpected output (undefined behavior). Sometimes you might get an error and some other time your program may run correctly. Hence, you should never access elements of an array outside of its bound. int testArray[10];
  • 13. Important things to know about Arrays • Array indexes always begin with 0. Hence when we say array of size 10, array has elements from index 0 to 9. If we specify or use array as intArr[10], intArr[11], intArr[200], the C compiler will not show any error, but we will get run time errors while executing the program. • Arrays are supported by primitive datatypes, non-primitive types like structures, unions, pointers etc.
  • 14. Advantages of C • In an array, accessing an element is very easy by using the index number. • The search process can be applied to an array easily. • 2D Array is used to represent matrices. • For any reason a user wishes to store multiple values of similar type then the Array can be used and utilized efficiently.
  • 15. Disadvantages of C • It allows us to enter only fixed number of elements into it. We cannot alter the size of the array once array is declared. Hence if we need to insert more number of records than declared then it is not possible. We should know array size at the compile time itself. • Inserting and deleting the records from the array would be costly since we add / delete the elements from the array, we need to manage memory space too. • It does not verify the indexes while compiling the array. In case there is any indexes pointed which is more than the dimension specified, then we will get run time errors rather than identifying them at compile time.
  • 16. • Array size is fixed: The array is static, which means its size is always fixed. The memory which is allocated to it cannot be increased or decreased.
  • 17. • Explanation: In the above program the array of size 10 is declared and the value is assigned at a particular index. But when the value at index 11 is printed then it prints the garbage value because the array was accessed out of the bound index. In some compiler, it gives error as “Array Index Out Of Bound.”
  • 18. • Array is homogeneous: The array is homogeneous, i.e., only one type of value can be store in the array. For example, if an array type “int“, can only store integer elements and cannot allow the elements of other types such as double, float, char so on.
  • 19. • Explanation: The above code gives “Compilation Error” as an integer type array is assigned value to a string and float type.
  • 20. • Array is Contiguous blocks of memory: The array stores data in contiguous(one by one) memory location. 5 7 12 9 28 0 1 2 4 3
  • 21. • Insertion and deletion are not easy in Array: The operation insertion and deletion over an array are problematic as to insert or delete anywhere in the array, it is necessary to traverse the array and then shift the remaining elements as per the operation. This operation cost is more.
  • 22. C Program Exercises • Write a C program to perform input/output of all basic data types. • Write a C program to enter two numbers and find their sum. • Write a C program to enter two numbers and perform all arithmetic operations. • Write a C program to enter length and breadth of a rectangle and find its perimeter. • Write a C program to enter length and breadth of a rectangle and find its area. • Write a C program to enter radius of a circle and find its diameter, circumference and area. • Write a C program to enter length in centimeter and convert it into meter and kilometer. • Write a C program to enter temperature in Celsius and convert it into Fahrenheit. • Write a C program to enter temperature in Fahrenheit and convert to Celsius • Write a C program to enter marks of five subjects and calculate total, average and percentage.
  • 23. • Write a C program to reverse a number. • Write a C program to find maximum between two numbers. • Write a C program to find maximum between three numbers. • Write a C program to check whether a number is negative, positive or zero. • Write a C program to check whether a number is divisible by 5 and 11 or not. • Write a C program to check whether a number is even or odd. • Write a C program to check whether a year is leap year or not. • Write a C program to input electricity unit charges and calculate total electricity bill according to the given condition: For first 50 units Rs. 0.50/unit For next 100 units Rs. 0.75/unit For next 100 units Rs. 1.20/unit For unit above 250 Rs. 1.50/unit An additional surcharge of 20% is added to the bill