Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views9 pages

C++ Array Programs for Integer Operations

The document contains 7 programming assignments involving arrays in C++. The assignments include: 1) Finding the largest element in an array. 2) Finding the second largest element in an array. 3) Finding the smallest and second smallest elements in an array. 4) Separating even and odd numbers in an array. 5) Checking if a given number is present in an array. 6) Copying elements from one array to another in reverse order. 7) Taking integer inputs, storing in an array, and counting the positive, negative, even, odd and zero elements.

Uploaded by

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

C++ Array Programs for Integer Operations

The document contains 7 programming assignments involving arrays in C++. The assignments include: 1) Finding the largest element in an array. 2) Finding the second largest element in an array. 3) Finding the smallest and second smallest elements in an array. 4) Separating even and odd numbers in an array. 5) Checking if a given number is present in an array. 6) Copying elements from one array to another in reverse order. 7) Taking integer inputs, storing in an array, and counting the positive, negative, even, odd and zero elements.

Uploaded by

Salman Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name :Aaqib mahmood

Roll# 5
Assignment C++ Array

1. Write a C++ program to find the largest element of a given array of


integers

#include<iostream>

using namespace std;

int main()

int arr[5], i ,lrg;

for(i=0; i<=4; i++)

cout<<”Enter elements in array“;

cin>>arr[i];

lrg=arr[0];

for(i=1; i<=4; i++)

If(lrg<arr[i])

lrg=arr[i];

cout<<”Largest element is =”<<lrg;


return 0;

2. Write a C++ program to find second largest element in an given array of


Integers

#include<iostream>

using namespace std;

int main()

int arr[10],lrg, scndlrg,i;

for(i=0; i<=9; i++)

cout<<”Enter elements in array “;

cin>>arr[i];

Scndlrg=lrg=arr [0];

for (i=1; i<=9; i++)

if(lrg<arr[i])

lrg=arr[i];

for (i=1; i<=9; i++)

if (scndlrg<arr[i] && lrg>arr[i])


scndlrg=arr[i];

cout<<”Secondlargest elements are :”<<scndlrg ;

return 0;

3. Write a C++ program to find the smallest and second smallest elements
in a given array of integers

#include<iostream>

using namespace std;

int main()

int arr[10],sml, scndsml,i;

for(i=0; i<=9; i++)

cout<<”Enter any elements “;

cin>>arr[i];

sml=arr[o];

for(i=1; i<=9; i++)

If(sml>arr[i])

sml=arr[i];

For(i=1; i<=9; i++)


{

If(sml>arr[i] && sml<arr[i])

scndsml=arr[i];

cout<<”Second Small elements are :”<<scndsml;

return 0;

4. Write a C++ program to seprate even and odd numbers of an array of

[Link] all even nmb first and then odd nmb.

#inlude<iostream>

using namespace std ;

int main()

Int arr[10],i

for(i=0; i<=9; i++)

cout<<”Enter elements in array “;

cin>>arr[i];

}
cout<<”All even number:\n”;

for(i=0; i<=9; i++)


If(n%2==0)

Cout<<arr[i]<<” “<<nmb is even;

If(n%2!=0)

Cout<<arr[i]<<” “ <<nmb is odd;

return 0;

5. Take 10 integer inputs from user and store them in an array. Again ask
user to give a number. Now, tell user whether that number is present in
array or not.

#inlude<iostream>

using namespace std ;

int main()

int arr[10],i,num,p=0;

cout<<” Enter elements in array “ ;

for(i=0;i<=9;i++)

cin>>arr[i];

cout<<” enter any element for checking “;

cin>>num;

for(i=0;i<=9;i++)
{
if(arr[i]==num)

p=1

if(p==1)

cout<<” elemet not present in array”;

else

cout<<” Element Present in array “<<num;

return0;

6. Take 10 integer inputs from user and store them in an array. Now, copy
all the elements in another array but in reverse order

#include<iostream>

using namespace std ;

int main()

int arr1[10],arr2[10],i;

cout<<” Enter elements in array “;

for(i=0;i<=9;i++)

cin>>arr1[i];

for(i=9;i>=0;i--)
{

arr2[i]=arr1[i];

cout<<” elements are copy in second array but in reverse


order”<<arr2[i];

return0;

7. Take 20 integer inputs from user and print the following:


number of positive numbers
number of negative numbers
number of odd numbers
number of even numbers number of 0.

#inlude<iostream>

using namespace std ;

int main()

int arr[20],i, odd ,poss ,negi, even ,zero;

odd=poss=negi=even=zero=0;

cout<<” Enter elements in array “ ;

for(i=0;i<=19;i++)

cin>>arr[i];

}
for(i=0;i<=19;i++)

if(arr[i]%2==0)

even++;

cout<<” Numbers of even number”<<even;

for(i=0;i<=19;i++)

if(arr[i]%2!=0)

odd++;

cout<<” Numbers of odd number “<<odd;

for(i=0;i<=19;i++)

if(arr[i]<0)

negi++;

cout<<” Numbers of Negatives Numbers “<<negi;

for (i=0;i<=19;i++)

if(arr[i]>0)
poss++;
}
cout<<”Numbers of Positive Numbers”<<poss;
for (i=0;i<=19;i++)
{
if(arr[i]==0)
zero++;
}
cout<<” Numbers of Zero number”<<zero;
return0;
}

You might also like