Expanded_Arrays_in_C121323133223423423.pptx

KamranKiyani5 5 views 11 slides Sep 01, 2025
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

wewe


Slide Content

Arrays in C Principles of Computer Science

What is an Array? • A collection of elements of the same type stored in contiguous memory. • Example: int A[5];

Why Use Arrays? • Easier to manage large data sets. • Access elements using index. • Saves code repetition.

Declaring Arrays Syntax: type arrayName[size]; Example: int marks[10];

Initializing Arrays • int A[5] = {1, 2, 3, 4, 5}; • int A[5] = {1, 2}; // others default to 0

Accessing Array Elements • Access with index: A[0], A[1] • Example with loop: for(int i=0;i<5;i++) printf("%d", A[i]);

Example Program #include <stdio.h> int main(){ int marks[5]={90,80,70,85,75}; int sum=0; for(int i=0;i<5;i++) sum+=marks[i]; printf("Total = %d", sum); return 0; }

2D Arrays • Example: int M[3][3]; • Access: M[i][j]

Strings as Character Arrays • char name[10] = "John"; • Strings are arrays of characters ending with '\0'.

Applications of Arrays • Store student marks • Represent matrices • Implement lookup tables • Store strings

Limitations of Arrays • Fixed size • Memory wastage if unused • Cannot dynamically resize
Tags