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

Arrays Lab Tasks Complete

The document contains 8 tasks to write C++ programs that perform various operations on arrays such as initializing an array, computing the sum of elements, printing elements in reverse order, finding a given value in an array, printing odd elements, finding the second last element and sum/product of last two elements, finding the smallest element and its index, and passing an array as a function argument. Each task provides the code to solve it along with explanations.

Uploaded by

Hira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Arrays Lab Tasks Complete

The document contains 8 tasks to write C++ programs that perform various operations on arrays such as initializing an array, computing the sum of elements, printing elements in reverse order, finding a given value in an array, printing odd elements, finding the second last element and sum/product of last two elements, finding the smallest element and its index, and passing an array as a function argument. Each task provides the code to solve it along with explanations.

Uploaded by

Hira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Hira

Roll.no:22sw076

Arrays-Lab-Tasks
Task # 1: Write a C++ program that creates and initializes an array of type integer as :int
array[]={5,10,15,20,25}; and computes the SUM of the elements in an array
#include<iostream>

using namespace std;

int main()

int a[]={5,10,15,20,25};

int sum=0;

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

cout <<”a[“<<i<<”]=”<<a[i]<<endl;

sum+=a[i];

cout <<”sum=”<<sum;

return 0;

Task #2 Consider the array in Taskno.2 . Write a C++ program that print the all elements of the
array in reverse order, As 25,20, …5.
#include<iostream>
using namespace std;

int main()
{
int a[]={5,10,15,20,25};
for(int i=4;i>=0;i--){
cout <<a[i]<<”,”;

}
return 0;
}
Task#3 Consider the following interger array of size 10.
int array[10]={1,2,3,5,8,10,12,23,28,15};
Write a c++ program that takes an integer value from the user at runtime and finds that value in
the given array . If the value is found display Message “Number is Found “ else display
Message “Number does not Found “.
#include<iostream>

using namespace std;

int main()

int a[10]={1,2,3,5,8,10,12,23,28,15};

int a,x;

cin>>a;

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

if(a==a[i]){

x=a;

if(x==a){

cout<<”number is found”;

}
else

cout<<”number is not found!”;

return 0;

Task #4 Consider the array in Taskno.3 . Write a C++ program that finds and prints all odd
numbers in the given array.
#include<iostream>

using namespace std;

int main()

int a[10]={1,2,3,5,8,10,12,23,28,15};

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

if(a[i]%2!=0){

cout<<a[i]<<” “;

return 0;

Task #5 : Write a C++ program that prints the second last element in the array created in
Taskno. 2. And also displays the sum and product of last two elements of the array.
#include<iostream>
Using namespace std;

int main()
{
int a[]={5,10,15,20,25};

cout<<”second last element = “<<a[3];


cout<<”\nproduct of last two numbers = “<<a[3]*a[4];
return 0;
}
Task #6 : Write a C++ program that finds the smallest element in the array and also its index.
#include<iostream>
using namespace std;

int main()
{
int a[]={5,8,75,0,9},m,i,n;
for ( i=1;i<5;i++){
if(a[0]>a[i]){
m=a[i];
n=i;}
}
cout <<”smallest element :”<<m;
cout <<endl<<”index: “<<n;

return 0;
}
Task #8 : Write a C++ program that passes an array (of your choice) as an argument to a
function.
#include <iostream>
using namespace std;
void fun(int arry[5])
{

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


cout <<” “<< arry[i];
}
int main()
{
int arry[] = {10,20,30,40,50};
fun(arry);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy