Skip to content

Commit

Permalink
Array practise question
Browse files Browse the repository at this point in the history
  • Loading branch information
Warishayat committed Jan 29, 2024
1 parent 6fa07a3 commit 976c1c9
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions day9.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
//array use dto store same type of data. which is contigious
int arr[5];
int size= sizeof(arr)/sizeof(arr[0]);
for (int i=0;i<size;i++)
{
cin>>arr[i];
}
//to print the output
for (int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"//find the sum of the given array:"<<endl;
int arr[6];
int sum=0;
cout<<"Enter manually number:"<<endl;
int size=sizeof(arr)/sizeof(arr[0]);
for (int i=0;i<size;i++)
{
//takeinput
cin>>arr[i];
}
cout<<endl;
//for output
for (int i=0;i<size;i++)
{
sum=sum+arr[i];
}
cout<<"The sum of the array is:"<<sum<<endl;

cout<<"//Question: calculate the maximum element from the array:"<<endl;
cout<<"Enter number manually by keypad:"<<endl;
int max[5];
int m=arr[0];
for (int i=0;i<5;i++)
{
cin>>max[i]; //take the input
}

for (int i=0;i<5;i++)
{
if (max[i]>m)
{
m=max[i];
}
}
cout<<"The maximum element in the array is:"<<m<<endl;


cout<<"Questoion: Search a given element in the array if it present return idx else -1:"<<endl;
//take input
int arrr[5];
int ans=-1;
cout<<"Enter the number manually by keypard:"<<endl;
for(int i=0;i<5;i++)
{
cin>>arrr[i];
}

//
int key;
cout<<"take input number that you want to search:"<<endl;
cin>>key;


//for itertae the loop to find key
for (int i=0;i<5;i++)
{
if (arrr[i]==key)
{
ans=i;
}
}
cout<<"Element at:"<<ans<<endl;
}

0 comments on commit 976c1c9

Please sign in to comment.