0% found this document useful (0 votes)
51 views

Lab 10

The document contains the code for 4 tasks that involve arrays in C++. Task 1 prints the elements of a initialized integer array. Task 2 involves inputting numbers into an array and separating the output into even and odd numbers. Task 3 generates random numbers to populate an array and copies the array to a new one. Task 4 searches an initialized integer array for a user-input number and prints the index if found.

Uploaded by

Zain Mehmood
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Lab 10

The document contains the code for 4 tasks that involve arrays in C++. Task 1 prints the elements of a initialized integer array. Task 2 involves inputting numbers into an array and separating the output into even and odd numbers. Task 3 generates random numbers to populate an array and copies the array to a new one. Task 4 searches an initialized integer array for a user-input number and prints the index if found.

Uploaded by

Zain Mehmood
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 10

Task 1:
#include <iostream>

using namespace std;

int main()
{
int num[10] = { 11,12,13,14,15,16,17,18,19,20 };
int i;
for (i = 0; i <= 9; i++)
{
cout << num[i] << endl;

}
return 0;
}

Result:

Task 2(a):
#include <iostream>

using namespace std;

int main()
{
int i;
int num;
int z[10];
cout << "Enter num = ";
cin >> num;
cout << "Enter" << num << "numbers = ";
for (i = 0; i<num; i++)
{
cin >> z[i];
}
cout << "Even numbers = ";
for (i = 0; i<num; i++) {
if (z[i] % 2 == 0)
cout << " " << z[i];
}
cout << "Odd numbers = ";
for (i = 0; i<num; i++)
{
if (z[i] % 2 == 1)
cout << " " << z[i];
}
return 0;
}
Result:

Task 2(b):
#include <iostream>

using namespace std;

int main()
{
int z[10];
cout << "Enter 10 values = ";
for (int i = 0; i <= 9; i++)
{
cin >> z[i];
}
cout << endl;
cout << "Values on even index = ";
for (int j = 0; j <= 9; j++)
{
if (j % 2 == 0)
cout << z[j] << endl;
}
cout << "Values on odd index = ";
for (int k = 0; k <= 9; k++)
{
if (k % 2 != 0)
cout << z[k] << endl;
}
return 0;
}

Result:

Task 3:
#include <iostream>
using namespace std;
int main()
{
int z;
cout << "Enter the size of array::";
cin >> z;
int Array[z];
for (int i = 0; i < z; i++)
Array[i] = rand() % 6 + 1; //Generate number between 0 to 6

cout << "\nElements of the array using random function :" << endl;

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


cout << "Array[" << i << "]=" << Array[i] << endl;
}
int arr[z];
cout << "Elements in copied array are" << endl;
for (int i = 0; i < z; i++) {
arr[i] = Array[i];
cout << "arr[" << i << "]=" << arr[i] << endl;
}
return 0;
}

Result:
Task 4:
#include<iostream>
using namespace std;
int main()
{
int num[20], count = 1, search;
for (int i = 0; i <= 19; i++)
{
num[i] = count;
count++;
}
cout << "Enter number to search : \n";
cin >> search;
int j;
for (j = 0; j <= 19; j++)
{
if (num[j] == search)
{
cout << "The given number is present on the index " << j << endl;
break;
}
}
if (num[j] != search)
cout << "-999\n";
system("pause");
return 0;
}

Result:

You might also like