Array and String in C++
Array and String in C++
sequentially in memory.
• Therefore, an integer array holds some number of integers, a
character array holds some number of characters, and so on.
• The size of the array is referred to as its dimension.
• To declare an array in C++, we write as follows:
type arrayName[dimension];
• To declare an integer array named arr of four elements, we write
int arr[4];
• Suppose a class has 250 students, and we need to store the grades of all
of them. Instead of creating 250 separate variables, we can simply
create an array:
double grade[250];
• The elements of an array can be accessed by using an index into the array.
• Arrays in C++ are zero-indexed, so the first element has an index of 0.
• So, to access the third element in arr, we write arr[2]; The value returned can
then be used just like any other integer.
Initializing an Arrays
• There are several ways to initialize the array. One way is to
declare the array and then initialize some or all of the elements:
int arr[4];
arr[0] = 6;
arr[1] = 0;
arr[2] = 9;
arr[3] = 6;
• Another way is to initialize some or all of the values at the time
of declaration:
int arr[4] = { 6, 0, 9, 6 };
• Sometimes it is more convenient to leave out the size of the array
and let the compiler determine the array's size for us, based on
how many elements we give it:
int arr[] = { 6, 0, 9, 6, 2, 0, 1, 1 };
• Here, the compiler will create an integer array of dimension 8.
The array can also be initialized with values that are not known beforehand:
#include <iostream>
using namespace std;
int main( )
{
int i, arr[4];
cout <<"Please enter 4 integers:" << endl;
for(i = 0; i < 4; i++){
cin >> arr[i];
}
cout << "Values in array are now:";
for(i = 0; i < 4; i++){
cout << " " << arr[i] << endl;
}
}
#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; }
#include<iostream>
using namespace std;
int main( )
{
int i, avg, sum = 0 ;
int marks[10] ; /* array declaration */
for ( i = 0 ; i <= 9; i++ )
{
cout<<"\n Enter marks " ;
cin>>marks[i] ; /* store data in array */
}
for ( i = 0 ; i <= 9 ; i++ ) {
sum = sum + marks[i] ; /* read data from an array*/ }
avg = sum / 10 ;
cout<< "\n Average marks = "<<avg;
return 0; }
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int val_array[3], int_val=1, counter;
cout<<"Please enter three numbers that you want to multiply"<<endl;
for(counter=0; counter<3; counter++)
{
cin>>val_array[counter];
int_val = int_val*val_array[counter];
}
cout<<"The product of these three numbers is = "<<int_val;
getch();
return 0;
}
/*Write a program to get n numbers and find out sum and average of
numbers*/
#include<iostream>
using namespace std;
int main( ) {
int a[100], i, n; //array of size n
float avg, sum=0;
cout<<“Enter total number of elements : ”;
cin>>n;
for(i=0; i<n; i++) {
cout<<“Enter the number<<“endl”;
cin>>a[i];
sum = sum + a[i];
}
avg=sum/n;
cout<<“Array elements are :”<<endl;
for(i=0; i<n; i++){
cout<< a[i])<<endl; }
cout<<“Sum = “<<sum <<“Average = “<<avg;
return 0;
}
//Program to find the largest element in an
array
#include<iostream>
using namespace std;
int main() {
int a[50], size, i, big;
cout<<“\nEnter size of the array : ”;
cin>>size;
cout<<“\nEnter <<size<<“elements in to the
array : ”;
for(i=0; i<size; i++) {
cin>>a[i]; }
big=a[0];
for(i=1; i<size; i++){
if(big<a[i])
big=a[i];}
• Note that when accessing an array the index given must be a
positive integer from 0 to n-1, where n is the dimension of the
array. The index itself may be directly provided, derived from a
variable, or computed from an expression:
arr[5];
arr[i];
arr[i+3];
• Arrays can also be passed as arguments to functions.
• When declaring the function, simply specify the array as
a parameter, without a dimension. The array can then
be used as normal within the function. For example:
#include <iostream>
using namespace std;
int sum(const int array[ ], const int length) {
long sum = 0;
for(int i = 0; i < length; sum += array[i++]);
return sum;
}
int main( ) {
int arr[ ] = {1, 2, 3, 4, 5, 6, 7};
cout << "Sum: " << sum(arr, 7) << endl;
return 0;
}
• C++ supports the creation of multidimensional arrays, through the
addition of more than one set of brackets.
• Thus, a two-dimensional array may be created as follows:
type arrayName[dimension1][dimension2];
• The array will have dimension1 x dimension2 elements of the same
type and can be thought of as an array of arrays.
• The first index indicates which of dimension1 sub-arrays to access,
and then the second index accesses one of dimension2 elements
within that sub-array.
• Initialization and access are similar to the one-dimensional case:
#include <iostream> twoDimArray[1][3] = 1;
using namespace std; for(int i = 0; i < 2; i++)
int main() { {
int twoDimArray[2][4]; for(int j = 0; j < 4; j++)
twoDimArray[0][0] = 6; {
twoDimArray[0][1] = 0; cout << twoDimArray[i][j];
twoDimArray[0][2] = 9; }
twoDimArray[0][3] = 6; }
twoDimArray[1][0] = 2; cout << endl;
twoDimArray[1][1] = 0; return 0;
twoDimArray[1][2] = 1; }
The array can also be initialized at declaration in the following ways:
int twoDimArray[2][4] = { 6, 0, 9, 6, 2, 0, 1, 1 };
int twoDimArray[2][4] = { { 6, 0, 9, 6 } , { 2, 0, 1, 1 } };
Examples #include<iostream>
using namespace std;
int main( )
{
/* An array with 5 rows and 2 column */
int a[5][2] = {{0,0},{1,2},{2,4},{3,6},{4,8}};
int i, j;
/* output each array element’s value */
for(i=0; i<5; i++){
for(j=0; j<2; j++){
cout<<a[i][j]<<endl”;
}
}
return 0;
}
#include <iostream> }
using namespace std; cout << "The numbers are: " << endl;
int main() // Printing array elements
Syntax of strcat()
strcat(first_string, second_string);
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char name[10], name1[10];
//clrscr();
cout<<"Enter the First string \t";
gets(name);
cout<<"Enter the Second string \t";
gets(name1);
strcat(name,name1);
cout<<"The string after concatenations is "<<endl<<name;
return 0;
}
strrev ()
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
char name[10];
cout<<"Enter the String ";
gets(name);
strrev(name);
cout<<"The String after reverse is\n "<<name;
getch();
return 0;
}
strcmp ()
#include <iostream>
#include <string.h>
#include<conio.h>
using namespace std;
int main()
{
char *str1 = "sample", *str2 = "sample";
if(strcmp(str1, str2)==0)
cout<<"strings are equal";
else
cout<<"strings are not equal";
getch();
return 0;
}