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

Programming Fundamentals 10

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Programming Fundamentals 10

Uploaded by

Asmara Minhas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

COMPUTER

PROGRAMMING
ARRAYS
ARRAY

• It is a group of variables of similar data types referred to by a single element.


• Its elements are stored in a contiguous memory location.
• The size of the array should be mentioned while declaring it.
• Array elements are always counted from zero (0) onward.
• Array elements can be accessed using the position of the element in the array.
• The array can have one or more dimensions.
ARRAYS

• Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
• To declare an array, define the variable type, specify the name of the array followed
by square brackets and specify the number of elements it should store:
• string cars[4];
• We have now declared a variable that holds an array of four strings. To insert values to it,
we can use an array literal - place the values in a comma-separated list, inside curly
braces:

• string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};


ARRAY DECLARATION IN C/C++:
ACCESS THE ELEMENTS OF AN ARRAY

• You access an array element by referring to the index number inside square brackets [].

• This statement accesses the value of the first element in cars:

• Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
// Outputs Volvo
LOOP THROUGH AN ARRAY

• You can loop through the array elements with the for loop.

• The following example outputs all elements in the cars array:

• Example
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
EXAMPLE

int myNumbers[5] = {10, 20, 30, 40, 50};


for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}
EXAMPLE : TAKE INPUTS FROM USER AND
STORE THEM IN AN ARRAY
#include <iostream>

using namespace std;

int main() {

int numbers[5];

cout << "Enter 5 numbers: " << endl;

// store input from user to array

for (int i = 0; i < 5; ++i) {

cin >> numbers[i];

cout << "The numbers are: ";

// print array elements

for (int n = 0; n < 5; ++n) {

cout << numbers[n] << " ";

return 0;

}
PROGRAM WHICH PRINTS THE SUM OF ALL THE
ELEMENTS IN AN ARRAY:
#include <iostream>

include <string>

using namespace std;

int main()

int myarray[5] = {10, 20,30,40,50};

int sum = 0;

for(int i = 0;i<5;i++)

sum += myarray[i];

cout<<"Sum of elements in myarray:\n "<<sum;

}
ADVANTAGES OF AN ARRAY IN C/C++:

• Random access of elements using the array index.


• Use of fewer lines of code as it creates a single array of multiple elements.
• Easy access to all the elements.
• Traversal through the array becomes easy using a single loop.
• Sorting becomes easy as it can be accomplished by writing fewer lines of code.
DISADVANTAGES OF AN ARRAY IN C/C++:

• Allows a fixed number of elements to be entered which is decided at the time of


declaration. Unlike a linked list, an array in C is not dynamic.
• Insertion and deletion of elements can be costly since the elements are needed to be
managed in accordance with the new memory allocation.

You might also like