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

Arrays

Arrays allow us to store multiple values of the same data type in contiguous memory locations. Each element in an array can be accessed using its index or subscript. One-dimensional arrays are declared using syntax like data_type array_name[size]; with the size specifying the number of elements. Multidimensional arrays can also be declared. Array elements are initialized either during declaration or separately. Elements are accessed using the array name and subscript like array_name[index]. Common array operations include traversal and initialization. Strings in C++ are represented using character arrays, with a null character '\0' marking the end.

Uploaded by

Anamika T Anil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Arrays

Arrays allow us to store multiple values of the same data type in contiguous memory locations. Each element in an array can be accessed using its index or subscript. One-dimensional arrays are declared using syntax like data_type array_name[size]; with the size specifying the number of elements. Multidimensional arrays can also be declared. Array elements are initialized either during declaration or separately. Elements are accessed using the array name and subscript like array_name[index]. Common array operations include traversal and initialization. Strings in C++ are represented using character arrays, with a null character '\0' marking the end.

Uploaded by

Anamika T Anil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ Arrays

square brackets. The subscript can be a


Arrays & its needs variable, constant or an expression.
Array is a collection of variables of same Eg: int mark[20];
data type referenced by a common name. cin>>mark[0]; //reading first element
These variables are stored in consecutive / mark[2]=10; // 10 is assigned to third element
contiguous memory locations. Each element mark[3]=mark[2] + 20;
in an array can be accessed using its position for(int i=0;i<20;i++) //reading array elements
in the list called index number or subscript. cin>>mark[i];
Arrays can be one dimensional or Traversal
multidimensional. Traversal means accessing each element
Declaring arrays of an array at least once. Summation of all
The syntax of an array declaration is, elements in an array, displaying the entire
data_type array_name[size]; array elements etc. are some examples for
Here data_type is the base type of the array, traversal operation.
array_name is the name of the array String handling using arrays
(identifier) and size is a positive integer. String is a group (sequence) of characters
Eg: int a[10]; Here a is an integer array that for representing name, house name, city etc.
can hold 10 integers. Character array is used for representing string
in memory. The general form of declaring a
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] string is,
Note: In C++, the array index/subscript starts char sring_name[size];
with zero (0) Eg:- char name[20];
Memory allocation for arrays Here the string name can store 19 valid
In memory, array values are stored in characters (1 byte is reserved for the null
consecutive memory locations. Number of character „\0‟)
bytes used by an array element is according
to the data type of the array. Memory allocation for strings
Eg:- char c[5]; It allocates 5 single byte In the case of a string, memory is
consecutive location for the variable c. allocated for an array of characters and a
0 1 2 3 4 special character null character („\0‟) is added
c automatically at the last location of that array.
1000 1001 1002 1003 1004 Eg: char str[15] = “Computer”;
Note: The total bytes used by an array C o m p u t e r \0
variable = size of the data type X size of the 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

array. Initialisation of a string variable


Eg: float a[2]; Total bytes used by the array 1) char name[10] = “MANOJ”;
= 4 X 2 = 8 bytes 2) char name[ ] = “MANOJ KUMAR”;
Array Initialization In the first statement 10 memory
An array can be initialized in their locations will be allocated and the string will
declaration statements itself. be stored with null character as the delimiter.
Eg: int mark[5] = {35, 42, 41, 47, 27}; The last four bytes will be left unused. But, for
char code[6] = {„s‟, ‟a‟,‟ m‟, ‟p‟, ‟l‟, ‟e‟}; the second statement, since the size of the
float weight[4] = {45.7, 52.5, 58.1, 41.5}; array is not specified, only 12 bytes will be
Note: If the number of initial values is less allocated (11 bytes for the string and 1 for
than the size of the array, they will be stored '\0').
in the elements starting from the first position Input / Output operations on strings
and the remaining positions will be initialized We cannot input strings containing
with zero, in the case of numeric data type. white spaces using the input operator >>. This
For char array, these positions will be is because a white space is treated as a
initialized with „ „ (space). When an array is separator of data. This can be solved using
initialized with values, the size can be omitted. the function gets().
Eg: int num[ ] = {10, 20, 30, 40}; gets( ): It is a console input function used to
Accessing Elements of Arrays read a string through the keyboard and places
An array element can be accessed using it in a string variable.
the array name along with the subscript in Syntax: gets(character_array_name);

1
Eg:- char name[20]; 6. Printing all the elements of an array is an
cout<< “Enter a name :”; example for _____ operation.
gets(name); 7. Write down the output of the following code
Here the first statement declares a string segment.
variable „name‟ which can store 19 valid puts(“hello”);
characters (the remaining one character is „\0‟ puts(“friends”);
with which the string is terminated). 8. Write the initialisation statement to store the
puts( ): It is a console output function used to string "GCC".
write or display a string on the screen. The 9. Define an Array.
puts() function advances the cursor to the new 10. What does the declaration
line. int studlist[1000]; mean?
Syntax: puts(string_data); 11. How is memory allocated for a single
Eg:- char name[20]; dimensional array?
cout<< “Enter Your name :”; 12. Write C++ statements to accept an array
gets(name); of 10 elements and display the count of even
puts(“Your name is :”); and odd numbers in it.
puts(name); 13. Read the following statements:
Note: (1) gets() function can read strings char name[20];
containing white spaces. This is not possible cin>>name;
with >> operator. cout<<name;
(2) To use gets() and puts(), we have to What will be the output, if you input the
include the header file cstdio string “Sachin Tendulkar”? Justify your
(3) getline( ) is also used to read a string from answer.
the keyboard. It is used with the input file 14. Write C++ statements to accept two single
stream object cin and its header file is dimensional arrays of equal length and find
iostream. the difference between corresponding
elements.
Eg:- char name[20]; 15. Write a program to check whether a string
cin.getline(name,19); is a palindrome or not.
Sample Questions
1. The elements of an array with ten elements
are numbered from ____ to ____.
2. An array element is accessed using _____.
3. If AR is an array, which element will be
referenced using AR[7]?
4. Consider the array declaration
int a[3]={2,3,4};
What is the value of a[1]?
5. Consider the array declaration
int a[]={1,2,4};
What is the value of a[1]?

You might also like