MCS-201 · Unit 4

Arrays and Strings

Section 1 of 9

Block 1 · Unit 4Arrays and Strings8 min · Section 1 of 9

4.1Introduction to Arrays and Array Declaration

Covers IGNOU MCS-201 §4.2, 4.2.1, 4.2.2

Storing the marks of 100 students in 100 separate variables is unmanageable: you cannot loop over variables named m1, m2, … m100. An array solves this by giving one name to a whole row of values. This section explains what an array is and how to declare one.

What an array is

Key properties of an array:

  • Homogeneous: every element has the same type.
  • Contiguous: the elements sit next to each other in memory.
  • Indexed from 0: an array of n elements has indices 0 to n − 1.
  • Fixed size: the size is set when the array is created and cannot grow.
  • Random access: any element can be reached directly by its index.

Declaring an array

Syntax
data-type array-name[size];
DeclarationMeaningMemory on a typical 64-bit system
int marks[100];100 integers, marks[0] to marks[99]100 × 4 = 400 bytes
float temp[7];7 floats7 × 4 = 28 bytes
char name[40];40 characters40 × 1 = 40 bytes

The memory needed is

bytes=number of elements×size of one element\text{bytes} = \text{number of elements} \times \text{size of one element}

and sizeof reports it: sizeof marks is 400, and sizeof marks / sizeof marks[0] gives the number of elements, 100.

Size specification with a symbolic constant

Define the size once with #define and use the name everywhere: in the declaration and in every loop. To change the size later, you edit one line.

Key takeaways

  • An array stores many values of one type under one name, in consecutive memory.
  • type name[size]; creates elements with indices 0 to size − 1.
  • Memory = number of elements × size of one element.
  • Use a #define constant for the size.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What are the valid indices of float t[8];?

Show answer

Answer: B. 0 to 7

Indices start at 0 and end at size − 1, so 0 to 7. Counting from 1 is the common mistake.

Question 2 · multiple choice

On a system where int is 4 bytes, how many bytes does int a[25]; occupy?

Show answer

Answer: C. 100

25 elements × 4 bytes = 100 bytes. 50 assumes the old 2-byte int.

Question 3 · multiple choice

Why is an array called a homogeneous data structure?

Show answer

Answer: A. All its elements have the same data type

Homogeneous means “of the same kind”: one type for every element. The values can all differ.

Question 4 · multiple choice

Which declaration is valid?

Show answer

Answer: D. int list[10];

The form is type, name, then size in brackets. char is a keyword and cannot be a name.

Question 5 · multiple choice

What does sizeof a / sizeof a[0] give for double a[12];?

Show answer

Answer: C. 12

Total bytes (96) divided by the bytes of one element (8) is the number of elements, 12.

Question 6 · short answer

Explain two advantages of using #define SIZE 50 with int marks[SIZE]; instead of writing int marks[50];.

Show worked answer

First, readability: the name SIZE tells the reader what the number means wherever it appears, such as in for (i = 0; i < SIZE; i++). Second, maintainability: the array size is usually needed in several loops as well as the declaration; with a symbolic constant, changing the class size from 50 to 60 means editing one line, and the preprocessor updates every use, so no loop is left with an outdated limit.

↑ ↓ move↵ openTab switch type