May 25, 2022

# One dimensional array- declaration & operations

Hey guys, what is up. In this particular post, we will be talking about a one dimensional array. Declaration of the 1-D array in c++ and operations performed on the 1-D array.

Updated on 29/9/2021

## One dimensional array:

It is a linear list of all elements stored by a common name with the same data type.

## Declaration of the one dimensional array in c++:

#include<iostream.h>

int main()

{

const Max=10;

int A[Max], n;

}

const means constant. This is used to fix the value of variable max. So that value of max remains constant through the program. N is used to take the size of the array. Here A is an array that is defined using int. This determines that the array is an integer type. Only integers can be stored in this array. Here one condition arrives that is Max should always be greater than or equal to n. So to fulfill this condition always initialize the value of max with a bigger number between the range of 20-30 in normal programs.

## Question: To take input of 10 array elements from user

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

const max=20;

int A[max], n, i;

n=10; //giving value to variable

//n is the size of A

cout<<“ENTER ARRAY ELEMENTS= “;

for (i=0;i<n;i++)  //loop to take the input

{

cout<<“A[“<<i<<“]= “;

cin>>A[i]; //taking input

}

return 0;

}

## Traversal:

This operation includes accessing each element once and then processing it. In simple words, it means printing the whole array.

## For example:

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

const max=20;

int A[max], n, i;

//n should be less than value of max

cout<<“ENTER SIZE OF ARRAY= “;

cin>>n;

//loop for taking input

cout<<“ENTER ELEMENTS IN AN ARRAY= “;

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

{

cout<<“A[“<<i<<“]= “;

cin>>A[i];

}

//loop for printing of array elements

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

{

cout<<endl<<“Element A[“<<i<<“]= “;

cout<<A[i];

}

return 0;

}

## Searching:

Searching for an element in a 1-D array.

There are two types of searching:

1. Linear Search
2. Binary search

## Question: Write a program to count the number of marks of those students scoring marks more than 75 in a list of marks stored in an array marks[n].

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

const max=20;

int A[max],n, i, j;

cout<<“ENTER VALUE OF N<20= “;

cin>>n;

j=0;

//j is variable for counting

//first loop for taking input

cout<<“ENTER ARRAY ELEMENTS= “;

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

{

cout<<“A[“<<i<<“]= “;

cin>>A[i];

}

//second loop to check how many students scored marks more than 75

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

{

if (A[i] > 75)

{

j++;

}

}

cout<<“NUMBER OF STUDENTS SCORING MARKS MORE THAN 75= “<<j;

return 0;

}

## Question: Write a program to increase the marks of those students who are scoring between 40-60 by 10 and decrease the marks of those students scoring more than 90 by 5.

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

const max=20;

int marks[max],n,i;

cout<<“ENTER VALUE OF N<20= “;

cin>>n;

cout<<endl<<“Enter marks of students= “;

//loop for taking input

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

{

cout<<” Marks [“<<i<<“]= “;

cin>>marks[i];

}

i=0;

//loop for changing the score of students

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

{

if( marks[i] > 90)

{

marks[i]=marks[i] – 5;

}

else if( marks[i]>40 && marks[i]<60)

{

marks[i]= marks[i]+10;

}

}

//loop for printing new array

i=0;

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

{

cout<<endl<<” Marks [“<<i<<“]= “;

cout<<marks[i];

}

return 0;

}

## Question: Write a program to find the least marks in a list of 20 marks given in an array Marks[n]

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

const Max=20;

int Marks[Max], n, i, least;

cout<<“ENTER VALUE OF N= “;

cin>>n;

//input taking loop

cout<<“ENTER ARRAY ELEMENTS= “;

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

{

cout<<“Marks[“<<i<<“] = “;

cin>>Marks[i];

}

//least is initialized with value of array element 0

i=0;

least=Marks[0];

//loop for finding the least marks

for(i=1; i<n; i++)

{

if ( least > Marks[i] )

{

least=Marks[i];

}

}

cout<<“LEAST MARKS OBTAINED = “;

cout<<least;

return 0;

}

In the upcoming post, you will be getting information related to types of sensors and Binary and linear search. If you haven’t seen our previous post that was based on an array in c programming then go and read this. Also, you can read previous posts by scrolling downwards. Also, stay tuned for the upcoming post.

#### Sneha Shukla

View all posts by Sneha Shukla →