Professional Documents
Culture Documents
Lab Manual # 08: Title: Arrays in C++ One D Array
Lab Manual # 08: Title: Arrays in C++ One D Array
Lab Manual # 08
CLO: CLO-1
Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results
No of Checks
SUB TOTAL
TOTAL SCORE
______________________
Course Instructor / Lab Engineer
An array is a series of elements of the same type placed in contiguous memory locations that
can be individually referenced by adding an index to a unique identifier.
That means that, for example, five values of type int can be declared as an array without having
to declare 5 different variables (each with its own identifier). Instead, using an array, the
five int values are stored in contiguous memory locations, and all five can be accessed using the
same identifier, with the proper index.
To declare an array in C++, the programmer specifies the type of the elements and the number
of elements required by an array as follows:
double balance[10];
You can initialize C++ array elements either one by one or using a single statement as follows:
The number of values between braces { } can not be larger than the number of elements that
we declare for the array between square brackets [ ]. Following is an example to assign a single
element of the array:
If you omit the size of the array, an array just big enough to hold the initialization is created.
Therefore, if you write:
You will create exactly the same array as you did in the previous example.
balance[4] = 50.0;
An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example:
Example : 1
#include<iostream>
#include<iomanip>
usingnamespace std;
int main ()
{
int n[10];
for(int i =0; i <10; i++)
{
n[ i ]= i +100;
}
cout<<"Element"<<setw(13)<<"Value"<<endl;
Example :2
#include<iostream>
using namespace std;
int main()
{
intArr[10],n,i;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0;i<n;i++)
{
cout<<Arr[i]<<endl;
}
system("pause");
return 0;
}
P-1: Write a C++ program to find the sum and average of one dimensional integer array.
#include<iostream>
int main()
{
int n;
cin>>n;
float arr1[n],sum=0,avg;
for(int i = 0;i<n;i++){
cin>>arr1[i];
for(int i = 0;i<n;i++)
cout<<arr1[i]<<"\t";
avg= sum/5;
return 0;
P-2 Write a Program that will add two arrays and store the sumin the third array. Print them all
out to the screen. As given below
Your Code:
#include<iostream>
int main()
{
int first[20], second[20], sum[20], c, n;
#include<iostream>
using namespace std;
int arr[num];
for(int i = 0;i<num ;i++)
cin>>arr[i];
}
cout<<"\nElements of an array after Reverse :: \n";
for(int i = 0;i<num ;i++)
cout<<arr[i]<"\t";
return 0;
}
Your Code:
#include<iostream>
using namespace std;
int main ()
{
int arr[10], n, i, max, min;
cout << "Enter the size of the array : ";
cin >> n;
cout << "Enter the elements of the array : ";
for (i = 0; i < n; i++)
Your Code:
#include <iostream>
using namespace std;
int main()
//check bound
if(n<0 || n>MAX)
{
cout<<"Input valid range!!!"<<endl;
return -1;
}
//read n elements
for(i=0;i<n;i++)
{
cout<<"Enter element ["<<i+1<<"] ";
cin>>arr[i];
}
return 0;
P-6 Write a program to swap 1st and last element of an integer 1-d array
Your Code:
#include<iostream>
using namespace std;
int main()
{
int n, temp;
cout<<"Enter Number of elements you want to enter :: ";
cin>>n;
int arr[n];
for(int i = 0;i<n;i++){
cin>>arr[i];
}
temp = arr[0];
Comments:
In this lab we learn about array , 2-D array ,1-D array and how to program sort by assending
and desending order.