Lab 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Page 1 of 5

TASK #1:
What is difference between size declarator and a subscript?
SOLUTION:
The size declarator is used in a definition of an array to indicate the number of elements the array will
have. A subscript is used to access a specific element in an array.
Task #2:
How do you define an array without providing a size declarator?
SOLUTION:
By providing an initialization list. The array is sized to hold the number of values in the list.

TASK #3:
Assuming that array1 and array2 are both arrays, why is it not possible to assign the
contents of array2 to array1 with the following statement?
array1=array2
SOLUTION:
Because an array name without brackets and a subscript represents the array's beginning memory
address.

TASK #4:
Write a program in C++ that take array elements from user and then display those
elements. The size of the array should be 5.
SOLUTION:
#include <iostream>
using namespace std;

int main()
{
int numbers[5];

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


{
cout<<"Enter Number "<<i+1<<": ";
cin >> numbers[i];
}

cout << "\nThe numbers are: ";


for (int n = 0; n < 5; ++n)
cout << numbers[n] << " ";
}
Page 2 of 5

TASK #5:
Write a program in C++ that take the array elements from the user and tell the highest
value among those values. The size of the array should be 5.
SOLUTION:
#include <iostream>
using namespace std;

int main()
{
int arr[5];

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


{
cout << "Enter Number " << i+1 <<": ";
cin >> arr[i];
}
int max = arr[0];
cout << "\nThe highest number is: ";
for (int n = 0; n < 5; ++n)
if(arr[n]>max)
max=arr[n];
cout<<max;
}
Page 3 of 5

TASK #6:
Write a program in C++ that take the array elements from the user and search the specific
element in the array. The specific element should be entered from the user which is to be
searched. The size of the array should be 5.
SOLUTION:
#include <iostream>
using namespace std;

int main()
{
int arr[5], find, X=0;
for (int i=0; i<5; ++i)
{
cout << "Enter Number " << i+1 <<": ";
cin >> arr[i];
}
cout<<"Which number you want to search: ";
cin>>find;
for (int n = 0; n < 5; ++n)
if(arr[n]==find)
{
X++;
break;
}
if(X==1)
cout<<"Founded your desired number!";
else
cout<<"Not founded your desired number!";
}
Page 4 of 5

TASK #7:
Write a program in C++ that take the array elements from the user and display the array
elements in ascending order. The size of the array must be user defined.
SOLUTION:
#include <iostream>
#define MAX 500
using namespace std;

int main()
{
int arr[MAX], n, i, j, temp;
cout<<"Enter total number of elements to read: ";
cin>>n;

for(i=0;i<n;i++)
{
cout<<"Enter element ["<<i+1<<"] ";
cin>>arr[i];
}

cout<<"Unsorted Array elements:"<<endl;


for(i=0;i<n;i++)
cout<<arr[i]<<"\t";

cout<<endl;

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

cout<<"Sorted (Ascending Order) Array elements:"<<endl;


for(i=0;i<n;i++)
cout<<arr[i]<<"\t";

cout<<endl;
}
Page 5 of 5

Prepared By: Khawar Khalil

You might also like