Arrays in C/C++

One-dimensional array IN C /C++ /C++

Array is a best way of storing the same type of data in consecutive memory location. In this tutorial we learn how we declare and Initialize an array in c/c++ language.
In C/C++ we are used two type of array:
1- One-dimensional arrays
2- Multidimensional arrays (will be discussed in next Tutorial)

If we want to store the marks of 10 student the there is no need to declare the separate variable for each student marks. W can declare an array of 10 student.
For example:


#include<stdio.h>
#include<conio.h>     
void main(void)

 {
int marks[5];

marks[0]=78;

marks[1]=99;

marks[2]=59;

marks[3]=67;

marks[4]=78;

marks[5]=67;

marks[6]=87;

marks[7]=56;

marks[8]=88;

marks[9]=68;

}


We can also use for loop for input and output purpose.
For example


#include<stdio.h>
#include<conio.h>

#include<iostream.h>     
void main(void)

 {

int marks[10];
for(i=0;i<=9;i++)

{

cin>>marks[i];   // this statement will get the marks[0], marks[1] and up to son for i=0 to 9.

}

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

{

cout<<"marks[i]; // this statement will display the marks[0], marks[1] and up to son for i=0 to 9.

}

getch( ) ;

}

Note :- Important thing to remember when working with arrays in C/C++ language suppose you declared an array of 10 elements. Let's say, int testArray[10]; You can use the array members from testArray[0] to testArray[9].