Module 1
Arrays
Topics
• Basics of Array
• Single Dimensional Array
• Multi Dimensional Array
• Manipulating Arrays
Note : Upon entry on this topic, the students are EXPECTED to have PRIOR
KNOWLEDGE in Programming
Arrays
Single
Dimensional
Array
Learning Objectives
• Explain the significance of declaring data types, and arrays
• Implement simple programs using single dimensional &
multidimensional arrays
• Implement programmer defined data types involving arrays using
proper codings and standards and testing and debugging techniques.
Arrays An array is a fundamental data structure in C that allows you to store a collection of elements
of the same type. Arrays provide a convenient way to organize and manipulate data as a contiguous block of
memory. This guide introduces the concept of arrays, explains their purpose, covers the declaration, initialization,
and accessing of array elements, and highlights the importance of arrays in programming.
Purpose of an Array
• Grouping related data: Arrays allow you to group • Iteration and processing: Arrays are commonly used
related data elements of the same type into a single in loops to iterate over the elements and perform
entity. For example, you can use an array to store a list
of integers, a sequence of characters, or a collection
operations on them. By accessing array elements
of floating-point numbers. sequentially, you can process large amounts of data
efficiently.
• Efficient storage and retrieval: Arrays provide efficient
storage and retrieval of elements. The elements of an Syntax
array are stored in contiguous memory locations, enabling
quick access to individual elements based on their indices.
type arrayName[arraySize];
Declaring & Initializing Array
type arrayName[arraySize];
int scores [5];
char names [10];
int scores [5] = {1, 15, 4, 5, 8;
float grades [4];
Navigating & Accessing Array
Arrayname[index]; int scores [5] = {1, 15, 4, 5, 8};
Int value = scores[2];
SUM = scores[1] + scores[3];
•Efficient storage and access: Arrays
•provide efficient storage and retrieval of elements. They enable direct access to elements using their indices,
allowing for fast and efficient data manipulation.
•Data organization: Arrays allow you to organize related data elements into a single entity, making it easier to
work with collections of data.
•Iteration and processing: Arrays are commonly used in loops to iterate over elements and perform operations
on them. They enable you to process large amounts of data efficiently.
•Versatility: Arrays can store elements of any data type, including integers, characters, floating-point numbers,
and even user-defined types.
•Flexibility: Arrays can be resized dynamically using dynamic memory allocation techniques such
as malloc and realloc, enabling you to handle varying amounts of data.
•Widely used: Arrays are a foundational concept used in many algorithms, data structures, and programming
techniques. Understanding arrays is essential for becoming proficient in programming.
Quiz No. 1
1. What is the purpose of arrays in programming.?
a. Grouping Data
b. Efficient storage and retrieval
c. Iteration and processing
d. Data encryption
2. How are array elements accessed?
a. Using indices
b. Using keys
c. Using pointers
d. Using addresses
Quiz No. 1
3. What advantage do array offer in terms of data organization?
a. Facilitating the organization of related data items into a coherent
data structure
b. Enabling efficient data manipulation
c. Supporting diverse data types
d. Allowing direct access of elements via indices
4. Ho do arrays enhance iteration and processing?
a. By streamlining iterative processes and efficiently handling sizable
data sets
b. By offering direct access to elements via indices
c. By supporting dynamic memory allocation techniques
d. By facilitating data organization
Quiz No. 1
5. What is the flexibility of Arrays?
a. Dynamic memory allocation techniques enable resizing to cater varying data volumes
b. Arrays support diverse data types, enhancing their applicability
c. Array allows efficient data manipulation by allowing direct access to elements
d. Arrays streamline iterative processes and efficiently handle sizable data sets
6. What is the index range for the first and last element of an array?
a. 0 – arraysize – 1
b. 1 to arraysize
c. Arraysize to 0
d. 0 to arraysize
Quiz No. 1
7. What is the importance of arrays in loops and iterations?
a. Facilitating iteration and operation execution
b. Enabling dynamic memory allocation
c. Efficient data organization
d. Supportive diverse data types
8. Why are arrays foundational in algorithms, data structures, and
programming practices?
a. They facilitate efficient data storage
b. They support dynamic memory allocation
c. They enhance data organization
d. They handle diverse data tyoes
Quiz No. 1
9. Which among the following is correct in array declaration?
a. Int numbers = array[5]
b. Int numbers[5] = 0;
c. Int numbers[5];
d. Int numbers = 5;
10 Which of the following is correct if you want to access the 2nd element
of an array numbers?
a. Score = numbers[2];
b. Scores = numbers[1];
c. Scores[2] = numbers;
d. None of the above
One Dimensional Array
Type _ Arrayname[array size;
int scores[5] = {2, 5, 7, 1, 15}; int 1stscores = scores[0];
Scores[3] = 15;
One Dimensional Array
Type _ Arrayname[array size;
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
#include <stdio.h>
int main()
{
int numbers[5]; // Declaration of an integer array
numbers[0] = 10; // Assigning a value to the first element
numbers[1] = 20; // Assigning a value to the second element
int sum = numbers[0] + numbers[1]; // Accessing and performing operations on array elements
printf("Sum: %d\n", sum); // Output the sum of the first and second elements
return 0;
}
Programming Exercises (100 points)
1. An array with 10 elements is initialized in the code editor.
Add the second, fifth, and ninth element and print the sum on
the next line (15 pts).
#include<stdio.h>
int main(void)
{ int x[] = {4, 5, 1, -10, 3, 5, 2, 1, 22, 94};
int sum;
2. Write a program that creates an integer array based on the
user-inputted size and list of elements (one input per line).
After creating the array, ask for another integer input and print
the element at that index on the next line (25 pts).
Enter array size: 5
3. Write a program using
Enter elements of the array:
1
array that accepts 5 scores
2 and output the highest
3
4 score (60 points)
5
Enter an index: 4
5