<< 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.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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