0% found this document useful (0 votes)
3 views

array

The document explains the concept of arrays in C++, detailing their declaration, indexing, and usage for storing multiple values of the same data type. It provides examples of declaring arrays, accessing and modifying elements, and iterating through arrays using loops. Key points include the fixed size of arrays, zero-based indexing, and the ability to store various data types.

Uploaded by

sundiidot123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

array

The document explains the concept of arrays in C++, detailing their declaration, indexing, and usage for storing multiple values of the same data type. It provides examples of declaring arrays, accessing and modifying elements, and iterating through arrays using loops. Key points include the fixed size of arrays, zero-based indexing, and the ability to store various data types.

Uploaded by

sundiidot123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

An array in C++ is a collection of elements of the same data type, stored in contiguous memory

locations. Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

Basic Concepts of Arrays:

 Array Declaration: The syntax for declaring an array in C++ is:

dataType arrayName[arraySize];

Where:

o dataType is the type of elements (e.g., int, float, char).


o arrayName is the name of the array.
o arraySize is the number of elements the array can hold.
 Array Indexing: Arrays are zero-indexed, meaning the first element is at index 0, the
second at index 1, and so on.

Example 1: Simple Array Declaration and Access

Let’s create a simple array of integers:

#include <iostream>
using namespace std;

int main() {
// Declare an array of 5 integers
int numbers[5] = {10, 20, 30, 40, 50};

// Accessing array elements


cout << "First element: " << numbers[0] << endl;
cout << "Second element: " << numbers[1] << endl;

// Modify array elements


numbers[2] = 100; // Changing the third element
cout << "Modified third element: " << numbers[2] << endl;

return 0;
}

Explanation:

1. Declaration: int numbers[5] = {10, 20, 30, 40, 50}; declares an integer array
named numbers with 5 elements, initialized with values.
2. Accessing Elements: numbers[0] refers to the first element of the array (which is 10).
Similarly, numbers[1] refers to the second element (which is 20).
3. Modifying Elements: You can change any element by assigning a new value.
numbers[2] = 100; changes the value of the third element to 100.
Example 2: Looping Through an Array

You can use loops to iterate through arrays, which is especially useful when working with larger
arrays.

#include <iostream>
using namespace std;

int main() {
// Declare and initialize an array
int numbers[5] = {5, 10, 15, 20, 25};

// Loop through the array and print each element


cout << "Array elements are:" << endl;
for(int i = 0; i < 5; i++) {
cout << numbers[i] << " ";
}
cout << endl;

return 0;
}

Explanation:

 For Loop: The loop for(int i = 0; i < 5; i++) runs from i = 0 to i = 4 (because
array indexing starts from 0), and in each iteration, numbers[i] gives the element at
index i.
 The output will be: 5 10 15 20 25

Key Points to Remember:

1. Arrays have a fixed size; once you declare an array with a size, you can't change the size
later.
2. Array elements are accessed using their index.
3. You can initialize arrays at the time of declaration.
4. Arrays can store elements of any data type, like int, float, char, etc.

Example 3: Array of Characters (String Representation)

In C++, a character array can be used to store strings (though it's more common to use
std::string).

#include <iostream>
using namespace std;

int main() {
// Declare a character array
char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Null-terminated string

// Print the string


cout << "Character Array as String: " << name << endl;

return 0;
}

Explanation:

 The array char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'} holds a string
"Hello".
 The \0 (null character) marks the end of the string in character arrays.

Key Takeaways:

 Arrays are useful for storing a collection of elements of the same type.
 They have a fixed size, and the elements can be accessed and modified using indices.
 Loops are often used to process arrays efficiently.

a simple way for students to understand.

What is an Array?

An array is a collection of variables that are stored in a single name. Instead of declaring multiple
variables to store a list of values, you can use an array. All the elements in an array are of the
same data type, like int, float, char, etc.

Example:

Imagine you have 5 students and you want to store their marks. Instead of declaring 5 different
variables, you can create an array.

Without Arrays:

int mark1 = 85;


int mark2 = 90;
int mark3 = 78;
int mark4 = 88;
int mark5 = 92;

With Arrays:

int marks[5] = {85, 90, 78, 88, 92};

In this array:

 marks[0] = 85
 marks[1] = 90
 marks[2] = 78
 marks[3] = 88
 marks[4] = 92

Array Declaration:

To declare an array, you need to specify the type, the name, and the size.

Syntax:

data_type array_name[size];

Example:

int numbers[5]; // Declares an array of 5 integers.

Accessing Array Elements:

Array elements are accessed using their index, starting from 0.

Example:

int marks[3] = {50, 60, 70};


cout << marks[0]; // Output: 50
cout << marks[1]; // Output: 60
cout << marks[2]; // Output: 70

Looping through Arrays:

You can use loops like for or while to go through all elements in an array.

Example:

#include<iostream>
using namespace std;

int main() {
int marks[5] = {85, 90, 78, 88, 92};

// Looping through the array


for (int i = 0; i < 5; i++) {
cout << "Marks of student " << i + 1 << " are: " << marks[i] << endl;
}

return 0;
}

Output:
Marks of student 1 are: 85
Marks of student 2 are: 90
Marks of student 3 are: 78
Marks of student 4 are: 88
Marks of student 5 are: 92

Key Points:

1. Array Size: The size of an array is fixed when you declare it.
2. Indexes: Array indexes start at 0.
3. Access: You can access any element using its index.

#include<iostream>

using namespace std;

void sum(int x, int y)

int z;

z=x+y;

cout<<"the sum is "<<z<<endl;

int sum(int x, float y)

int z;

z=x+y;

cout<<"the sum is "<<z<<endl;

}
int main()

int a=5;

int b=10;

float c=3.5;

sum(a,b);

sum(a,c);

You might also like