• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Pointer » Single-Dimensional Arrays in c++
Next →
← Prev

Single-Dimensional Arrays in c++

By Dinesh Thakur

A single-dimensional array is the simplest form of an array that requires only one subscript to access an array element. Like an ordinary variable, an array must have been declared before it is used in the program. The syntax for declaring a single-dimensional array is

data_type array_name [size] ;

For example, an array marks of type int and size five can be declared using this statement.

int marks [5];

Initialization of Single- Dimensional Array: Once an array is declared, the next step is to initialize each array element with a valid and appropriate value. Note that unless an array is initialized, all the array elements contain garbage values. An array can be initialized at the time of its declaration. The syntax for initializing an array at the time of its declaration is

data_type array_name [size] ={value 1,value 2,…………… ,value n};

The values are assigned to the array elements in the order in which they are listed.

That is, value1, value 2 and value n are assigned to the first, second and nth element of the array, respectively.

For example, the array marks can be initialized while declaring using this statement.

int marks[5]={51,62,43,74,55};

If an array is declared and initialized simultaneously, then specifying its size is optional. For example, the statement int marks [] = {51, 62,43,74,55} is also valid.

Accessing Single-Dimensional Array Elements: Once an array is declared and initialized, the values stored in the array can be accessed any time. Each individual array element can be accessed using the name of the array and the subscript value. Every element in an array is associated with a unique subscript value, starting from 0 to size-l (where, size refers to the maximum number of elements that can be stored in the array). The syntax for accessing the values stored in a single dimensional array is

array_name [subscript]

For examp1e, the elements of the array marks can be referred to as marks [0], marks [1],

Marks [2], marks [3] and marks [4], respectively.

Single-dimensional arrays are always allocated contiguous blocks of memory. This implies that all the elements in an array are always stored next to each other. The memory representation of the array marks is shown in Figure. As each element is of the type int (that is, 2 bytes long), the array marks occupies ten contiguous bytes in memory and these bytes are reserved in the memory at the compile-time.

Memory Representation of marks

Manipulation of Single-Dimensional Array Elements: Once the array elements are accessed, a number of operations can be performed on them. These operations include finding the sum, average, maximum or minimum, sorting and searching of the array elements, etc.

To understand the concept of single-dimensional arrays, consider this example.

Example: A program to sort the array elements

#include<iostream>
#include<iomanip> //for setw()
using namespace std;
int main () {
  const int size = 5; //declaring size of array
  int num[size],temp; //declaring an array
  cout<<”Enter five elements of array”<<”\n";
  for (int i=0; i<size; i++) //reading array elements
       cin>>num[i];
       for(int i=0;i<size-1;i++) //sorting array elements {
         for(int j=i+l;j<size;j++) {
           if(num[i]>num[j]) {
             temp = num[i];
             num[i] = num[j];
             num[j] = temp;
           }
        }
}
cout<<”Elements of sorted array are\n";
    for(int i=0 ; i<size ; i++)
      cout<<num[i]<<setw(4) ;
      return 0;
}

The output of the program is

Enter five elements of array

67 34 12 98 26

Elements of sorted array are

12 26 34 67 98

Strings-Array of Characters: In addition to integer and floating point elements, an array can have characters as its elements. An array containing a group of characters is known as a character array. For example, an array name of type char is declared and initialized with five characters using this statement.

char name[5]={, a' , ' b' , 'c' , ,d' , 'e' };

An array of characters in which the last character is always a null character (‘ \ 0’) represents a string. Thus, while declaring a character array that holds a string, the size is always declared one more than the number of characters in the string. This type of string representation is also referred to as C-string or null-terminated string. Note that a string not terminated by ‘\ 0’ is treated merely as a collection of characters and not a string. Besides C-strings, C++ also defines a string class, named string to offer object oriented approach to string handling.

Initializing Strings: A string is declared and initialized in the same way as a character array. The syntax for initializing a string at the time of its declaration is

char string_name[size]=”value”;

To understand the concept of string initialization while declaring, consider this statement.

char str[]=”Express”;

In this statement, the string str is initialized with a string constant Express. Note that in this type of initialization, the null character ‘\ 0’ is not specified, the compiler inserts the null character automatically. However, the string str can also be initialized element-wise using this statement.

char str[]={'E', 'x', 'p', 'r', 'e', 's', 's', '\0'};

In this type of initialization, the null character is specified explicitly to denote the end of string. Figure shows memory representation of the string str, where each character occupies one byte in the memory.

Memory Representation of str

Manipulating Strings: In C++, strings can be manipulated in different ways. There is a set of pre-defined functions, which are used to manipulate strings in different manners. These pre-defined functions, which are declared in the string. h (or cstring header in new C++ compilers) header file, are listed in Table.

String FunctionTo understand the concept of strings and string operations, consider this example.

Example : A program to demonstrate string functions

#include<iostream>
#include<cstring> // for string functions
using namespace std;
int main () {
 const int max=80; // maximum size of string
 char str1 [max], str2 [max];
 cout<<”Enter first string: “;
 cin>>str1; // reading first string
 cout<<”Enter second string: “;
 cin>>str2; // reading second string
 cout<<”Length of first string: “<<strlen(str1) <<"\n";
 cout<<”Length of second string: “<<strlen(str2) <<"\n";
 if(!strcmp(str1,str2)) // comparing strings
   cout<<”Strings are equal “<<"\n";
 else
   cout<<”Strings are not equal”<<”\n”;
   cout<<” Concatenated string: "<<strcat(str1,str2);
   return 0;
}

The output of the program is

Enter first string : United

Enter second string : Kingdom

Length of first string: 6

Length of second string: 7

Strings are not equal

Concatenated string: United Kingdom

In this example, two strings str1 and str2 are read and different string functions are performed on them. The strlen () determines the lengths of strings, strcmp () compares the strings and strcat () concatenates the strings.

Note that, in the statement cin>>str1 the extraction operator >> considers a space to be a terminating character, thus it is unable to read strings embedded with blanks and multiple lines. For such strings, member functions of the standard input stream (cin) are required.

You’ll also like:

  1. Multi-Dimensional Arrays in c++
  2. C++ Declaration of One Dimensional Arrays
  3. Three-Dimensional Arrays in C
  4. Two-dimensional Arrays or Matrices in C
  5. Pointer to Three-Dimensional Arrays in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW