Array lab practice
1. Displaying Array Elements
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "\nThe numbers are: ";
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}
Output…….
2. Take Inputs from User and Store Them in an Array
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl;
// store input from user to array
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}
return 0;
}
Output……..
1
3. Accessing Array element
#include <iostream>
using namespace std;
int main() {
int a[] = {25, 50, 75, 100};
cout << a[0] << '\n';
cout << a[1] << '\n';
cout << a[2] << '\n';
cout << a[3] << '\n';
return 0;
}
Output……
4. Accessing Array element
#include <iostream>
using namespace std;
int main()
{
int age[5] = { 19, 18, 21, 20, 17 };
for (int x = 0; x < 5; x++)
{
cout <<age[x]<<"\n";
}
return 0;
}
Output ……
#include <iostream>
2
using namespace std;
int main()
{
char name[6] = { 'w','o','r','k','i','e' };
for (int x = 0; x < 6; x++)
{
name[5]='w';
cout <<name[x]<<"\n";
}
return 0;
}
5. Display Sum of Array Elements Using for Loop
#include <iostream>
using namespace std;
int foo [] = {16, 2, 77, 40, 12071};
int result=0;
int main ()
{
for ( int i=0 ; i<5 ; i++ )
{
result += foo[i];
}
cout << result;
return 0;
}
Output ……..
3
6. Display Sum and Average of Array Elements Using for Loop
#include <iostream>
using namespace std;
int main() {
// Initialize the array
int numbers[] = {10, 20, 30, 40, 50};
int length = sizeof(numbers) / sizeof(numbers[0]); // Get the length of the array
// Initialize sum variable
int total_sum = 0;
// Calculate the sum using a for loop
for (int i = 0; i < length; i++) {
total_sum += numbers[i];
}
// Calculate the average
double average =total_sum / length;
// Display the results
cout << "Sum of array elements: " << total_sum << endl;
cout << "Average of array elements: " << average << endl;
return 0;
}
Output….
7. Store and accessing array element
#include<iostream>
using namespace std;
int main()
{
int arr[5];
cout<<"Enter any five numbers: ";
4
for(int i=0; i<5; i++)
{
cin>>arr[i];
}
cout<<"\nYou entered: ";
for(int i=0; i<5; i++)
{
cout<<arr[i]<<endl;
}
cout<<endl;
return 0;
}
Output ………..
8. #include<iostream>
using namespace std;
int main()
{
int arr[5];
cout<<"Enter any five numbers:\n";
for(int i=0; i<5; i++)
cin>>arr[i];
cout<<"\nThe array \"arr\" with their indexing is as follows:\n";
for(int i=0; i<5; i++)
cout<<"arr["<<i<<"] = "<<arr[i]<<endl;
cout<<endl;
return 0;
}
Output …….
9. #include<iostream>
using namespace std;
int main()
{
int mat[4][3], i, j;
cout<<"Enter 12 elements for the array: ";
for(i=0; i<4; i++)
{
for(j=0; j<3; j++)
{
cin>>mat[i][j];
5
}
}
cout<<"\n\nThe given 2-D array should look like:\n";
for(i=0; i<4; i++)
{
for(j=0; j<3; j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<endl;
}
cout<<endl;
return 0;
}
Output ……
10. Write a C++ program to find the sum and average of one dimensional
integer array
#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,i,sum=0;
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++)
sum+=Arr[i];
cout<<"\nThe sum of Array is :"<<sum;
cout<<"\nThe average of Array is :"<<sum/i;
return 0;
}
Output…….
22. program to print the value of each element along with its indexes.
#include<iostream>
6
using namespace std;
int main()
{
int i, arr[5] = {10, 20, 23, 43, 50};
for(i=0; i<5; i++)
cout<<"arr["<<i<<"] = "<<arr[i]<<endl;
cout<<endl;
return 0;
}
Output……