PROGRAMMING FUNDAMENTALS LAB
PROGRAMMING FUNDAMENTALS LAB
FUNDAMENTALS LAB
(CL1002)
LABORATORY MANUAL
Spring 2025
LAB 02
ARRAYS
______________________________________
Task no 1:
Program:
#include <iostream>
using namespace std;
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int index;
cout << "Enter the element number (1 to " << size << ") to print: ";
cin >> index;
while (index < 1 || index > size)
{
cout << "Invalid input! Please enter a number between 1 and " << size << ": ";
cin >> index;
}
cout << "Element at position " << index << " is: " << arr[index - 1] << endl;
return 0;
}
Out put:
Task No 3:
Program:
#include <iostream>
using namespace std;
int main()
{
int arr[10];
cout << "Enter 10 elements of the array:" << endl;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
int element;
cout << "Enter the element to find its frequency: ";
cin >> element;
int frequency = 0;
for (int i = 0; i < 10; i++)
{
if (arr[i] == element)
{
frequency++;
}
}
cout << "Occurrence of " << element << ": " << frequency << endl;
return 0;
}
Output:
Task no 3:
Program:
#include<iostream>
using namespace std;
int main()
{
int n;
int a[n];
int q;
int i,j,k;
cout<<"Enter number of elements in odd:"<<endl;
cin>>n;
if(n%2!=0)
{
cout<<"Array:"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
q=((n+1)/2);
for(int i=0;i<q;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
for(i=q;i<n;i++)
{
cout<<a[i]<<" ";
}
}
return 0;
}
Output:
Task no 4