Why is my code not submitting when i have done it in right way?
#include
using namespace std;
int maxarray(long int a[],long int n)
{
int key=a[0],index=0;
for(int i=1;i<n;i++)
{
if(a[i]>key)
index=i;
}
return index;
}
int main()
{
long int b[10];
long int size;
cout<<"Enter the number of elements: ";
cin>>size;
for(int i=0;i<size;i++)
{
cin>>b[i];
}
int i=maxarray(b,size);
cout<<“maximum element is:”<<b[i];
return 0;
}
About the code we submit
Hey @sharma.shubham
There’s a fault in your logic,
Here’s how you need to solve this question:
Let’s consider an index variable, which stores the ans(which is the maximum element), initialise it to min possible element(viz INT_MIN), then update it whenever a greater element comes, this way you’ll get max element after the loop completes, here’s your code after correction
#include<bits/stdc++.h>
using namespace std;
int maxarray(long int a[],long int n)
{
int index=INT_MIN;
for(int i=0;i<n;i++)
{
if(a[i]>index)
index=a[i];
}
return index;
}
int main()
{
long int size;
cout<<"Enter the number of elements: ";
cin>>size;
long int b[size];
for(int i=0;i<size;i++)
{
cin>>b[i];
}
int i=maxarray(b,size);
cout<<“maximum element is:”<<i;
return 0;
}
thanks for the correction. I have copied this code into the ide but it is not submitting as i am clicking on the submit button it is not working? Is it because i have copy pasted the code?
When am i getting my answer??
nothing it seems like if the button is not working because i am clicking on the button and there is no response
nope, now i have given up all hopes for this problem I have unlocked editorial, downloaded test cases but nothing is helping. But yes now my friend has given me his code which is running perfectly even though he has used the same logic his code is running not mine, so now it is ok. THANKS FOR YOUR HELP