codescracker


c++

C++ One Dimensional Array



« Previous Tutorial Next Tutorial »


A one-dimensional array is a group of elements having the same datatype and same name. Individual elements are referred to using common name and unique index of the elements.

The simplest form of an array is one-dimensional-array. The array itself is given name and its elements are referred to by their subscripts. In C++, an array is denoted as follows:

array_name[array_size]

where size specifies the number of elements in the array and the subscript (also called index) value ranges from 0 through size-1.

Declare One Dimensional Array in C++

Here is the general form to declare one dimensional array in C++

data_type array_name[array_size];

Here, data_type is any valid C++ data type, array_name is the name of the array, and array_size is the size of array. Here is an example, declaring an array named arr of int type, having maximum element size of 10 elements

int arr[10];

Initialize One Dimensional Array in C++

Here is the general form to initialize values to one dimensional array in C++

data_type array_name[array_size] = {comma_separated_element_list};

Here is an example, declaring and initializing values to the array name arr of type int, containing 10 elements

int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

C++ One Dimensional Array Example

Here are some example program, demonstrating one dimensional array in C++

/* C++ One Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[5] = {1, 2, 3, 4, 5};
	int i;
	for(i=0; i<5; i++)
	{
		cout<<"arr["<<i<<"] = "<<arr[i]<<"\n";
	}
	getch();
}

Here is the sample output of this C++ program:

c++ one dimensional array

Here is another C++ example, also demonstrating one dimension array in C++

/* C++ One Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[10];
	int i;
	int sum=0, avg=0;
	cout<<"Enter 10 array elements: ";
	for(i=0; i<10; i++)
	{
		cin>>arr[i];
		sum = sum + arr[i];
	}
	cout<<"\nThe array elements are: \n";
	for(i=0; i<10; i++)
	{
		cout<<arr[i]<<"  ";
	}
	cout<<"\n\nSum of all elements is: "<<sum;
	avg = sum/10;
	cout<<"\nAnd average is: "<<avg;
	getch();
}

Here is the sample run of the above C++ program:

c++ single dimensional array

Below is another program on one-dimensional array in C++

/* C++ One Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[5];
	int i, position, index;
	cout<<"Enter 5 array elements: ";
	for(i=0; i<5; i++)
	{
		cin>>arr[i];
	}
	cout<<"\nIndex\t\tPosition";
	for(i=0; i<5; i++)
	{
		cout<<"\n";
		cout<<i<<" = "<<arr[i]<<"\t\t"<<i+1<<" = "<<arr[i+1];
	}
	cout<<" (Garbage value/not of array)";
	cout<<"\n\nImportant - Array element can only be accessed by indexing the array\n";
	cout<<"Note - Array index always starts from 0";
	getch();
}

Below is the sample run of this C++ program:

c++ 1d array

Let's take one more C++ program, on single or one dimensional array

/* C++ One Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[10];
	int i, position, index;
	cout<<"Enter 10 array elements: ";
	for(i=0; i<10; i++)
	{
		cin>>arr[i];
	}
	cout<<"Accessing element at position...Enter position...";
	cin>>position;
	cout<<"\nElement present at position "<<position<<" is "<<arr[position+1];
	cout<<"\n\nAccessing element at index..Enter index..";
	cin>>index;
	cout<<"\nElement present at index "<<index<<" is "<<arr[index];

	getch();
}

Here is the sample run of the above C++ program:

one dimensional array

More Examples

Here are some more C++ programs listed on one dimensional array, that you may like:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test