<< Chapter < Page Chapter >> Page >

Some examples of array declarations

int a[1000]; // a one-dimensional array

int b[3][5]; // a two-dimensional array

int c[7][9][2]; // a three-dimensional array

In these above example, b has 3 X 5 elements, and c has 7 X 9 X 2 elements. Starting at the base address of the array, all the array elements are stored contiguously in memory.

For the array b, we can think of the array elements arranged as follows:

Multi-dimensional array

Example

This program checks if a matrix is symmetric or not.

#include<iostream.h>

const int N = 3;

int main()

{

int i, j;

int a[N][N];

bool symmetr = true;

for(i= 0; i<N; i++)

for (j = 0; j<N; j++)

cin>>a[i][j];

for(i= 0; i<N; i++)

for (j = 0; j<N; j++)

cout<<a[i][j]<<endl;

for(i= 0; i<N; i++){

for (j = 0; j<N; j++)

if(a[i][j]!= a[j][i]){

symmetr = false;

break;

}

if(!symmetr)

break;

}

if(symmetr)

cout<<"\nThe matrix is symmetric"<<endl;

else

cout<<"\nThe matrix is not symmetric"<<endl;

return 0;

}

Strings and string built-in functions

In C++ we often use character arrays to represent strings. A string is an array of characters ending in a null character (‘\0’). A string may be assigned in a declaration to a character array. The declaration

char strg[] = “C++”;

initializes a variable to the string “C++”. The declaration creates a 4-element array strg containing the characters ‘C’, ‘+’, ‘+’ and ‘\0’. The null character (\0) marks the end of the text string. The declaration determines the size of the array automatically based on the number of initializers provided in the initializer list.

C++ does not provide built-in operations for strings. In C++, you must use a string built-in functions to manipulate char variables. Some commonly used string functions are listed below.

String functions

The strcpy() function copies a literal string or the contents of a char variable into another char variable using the syntax:

strcpy(destination, source);

where destination represents the char variable to which you want to assign a new value to and the source variable represents a literal string or the char variable contains the string you want to assign to the destination.

The strcat() function combines two strings using the syntax:

strcat(destination, source);

where destination represents the char variable whose string you want to combine with another string. When you execute strcat(), the string represented by the source argument is appended to the string contained in the destination variable.

Example:

char FirstName[25];

char LastName[25];

char FullName[50];

strcpy(FirstName, “Mike”);

strcpy(LastName, “Thomson”);

strcpy(FullName, FirstName);

strcat(FullName, “ “);

strcat(FullName, LastName);

Two strings may be compared for equality using the strcmp() function. When two strings are compared, their individual characters are compared a pair at a time. If no differences are found, the strings are equal; if a difference is found, the string with the first lower character is considered the smaller string.

The functions listed in Figure 2 are contained in the string.h header file. To use the functions, you must add the statement #include<string.h>to your program.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask