Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Lab # 8 – Programming Fundamentals (CPE-121)

Lab Manual # 08

Title: Arrays in C++ One D array

CLO: CLO-1

Student Name: M. Usama saghar Class Roll: 2019-CPE-27

Subject Teacher : Engr. Muhammad Baqer


Lab Engineer : Engr. Abdul Rehman
Lab Performed In : Networking Lab (CPED)

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

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

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)
Arrays

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.

For example, an array containing 5 integer values of type int called A could be represented as:

Declaring 1-D Arrays:

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:

type arrayName [ arraySize ];

This is called a single-dimension array. The arraySize must be an integer constant greater than


zero and type can be any valid C++ data type. For example, to declare a 10-element array called
balance of type double, use this statement:

double balance[10];

Initializing 1-D Arrays:

You can initialize C++ array elements either one by one or using a single statement as follows:

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

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:

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous example.

balance[4] = 50.0;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th
index will be 5th, i.e., last element because all arrays have 0 as the index of their first element
which is also called base index. Following is the pictorial representaion of the same array we
discussed above:

Accessing Array Elements:

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:

double salary = balance[9];

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;

for(int j =0; j <10; j++)


{
cout<<setw(7)<< j <<setw(13)<< n[ j ]<<endl;
}
System(“pause”);
return0;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

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.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)
Your Code:

#include<iostream>

using namespace std;

int main()

{
int n;

cout<<"Enter Number of elements you want to enter :: ";

cin>>n;

float arr1[n],sum=0,avg;

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

cin>>arr1[i];

sum = sum + arr1[i];

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

cout<<arr1[i]<<"\t";

avg= sum/5;

cout<<"\nSum ="<<sum<<"\nPercentage = "<<avg;

return 0;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

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>

using namespace std;

int main()

{
int first[20], second[20], sum[20], c, n;

cout << "Enter the number of elements in the array ";


cin >> n;

cout << "Enter elements of first array" << endl;

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


cin >> first[c];

cout << "Enter elements of second array" << endl;

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


cin >> second[c];

cout << "Sum of elements of the arrays:" << endl;

for (c = 0; c < n; c++) {


sum[c] = first[c] + second[c];
cout << sum[c] << endl;
}
return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

P-3 Write a C++ program to reverse element of an integer array


Your Code:

#include<iostream>
using namespace std;

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)
int main()
{
// Reverse of an array
int num, temp;
cin>>num;

int arr[num];
for(int i = 0;i<num ;i++)
cin>>arr[i];

for(int i = 0, j=num-1; i<j;i++,j--)


{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

}
cout<<"\nElements of an array after Reverse :: \n";
for(int i = 0;i<num ;i++)
cout<<arr[i]<"\t";

return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

Paste your output here

P-4 Write a program to find largest and smallest element of an array.

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++)

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];
}
cout << "Largest element : " << max;
cout << "\nSmallest element : " << min;
return 0;
}

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

Paste your output here

P-5 Write a program to sort an array in ascending order .

Your Code:

#include <iostream>
using namespace std;

#define MAX 100

int main()

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)
{
//array declaration
int arr[MAX];
int n,i,j;
int temp;

//read total number of elements to read


cout<<"Enter total number of elements to read: ";
cin>>n;

//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];
}

//print input elements


cout<<"Unsorted Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;

//sorting - ASCENDING ORDER


for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}

//print sorted array elements

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)
cout<<"Sorted (Ascending Order) Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;

return 0;

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

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];

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)
arr[0] = arr[n-1];
arr[n-1] = temp;
cout<<"Array after swapping first and last elements "<<endl;
for(int i = 0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 8 – Programming Fundamentals (CPE-121)

Comments:
In this lab we learn about array , 2-D array ,1-D array and how to program sort by assending
and desending order.

Version 1.0 Department of Computer Engineering UCE&T BZU Multan

You might also like