RR2001 RR2001 - 7 months ago 177
C++ Question

Pivot element - c++/c

Program to find the pivot element in an array where all the elements are non zero and unique. *An element in an array is a pivot element if the sum of all the elements in the list to its left is equal to the sum of all the elements to its right. EX : {1,2,3,7,6} here 7 is pivot element as 1+2+3 = 6 *
Coming to the question, I will be sharing my code for this question. Problem is it isn't working. I consulted some resources but all are mentioning it is inefficient. Can some one please check my code and tell me where i went wrong. I want to develop this code and requesting all to find errors in this code rather than suggest a new code. PS : my first question on SO so in case you aren't satisfied with my method of posting a question please excuse.
Lastly the problem seems to be with my pivot function. Requesting experts to skip other lines of code and validate the pivot function.


My code:

#include<iostream>
using namespace std;

void initarray(int a[],int n)
{
for(int i =0;i<n;i++)
{
a[i]=0;
}
}

void acceptarray(int a[],int n)
{
for(int i =0;i<n;i++)
{
cout<<"Enter element"<<i+1;cin>>a[i];
}
}

int pivotelement(int a[],int n)
{
int s1 =0; int s2=0;
for(int i =0;i<n-1;i++)
{
for(int k =0;k<=i;k++)
{
s1 +=a[k];
}
for(int j = i+2;j<n;j++)
{
s2 +=a[j];
}

if(s1 == s2)
{
cout<<"Pivot element is"<<" "<<a[i+1];break;
}
else if(s1 != s2)
{
continue;
}

else if(i == n-2)
{
cout<<"0(no pivot element)";break;
}

}
}

int main(void)
{
int a[100];
int n =0;
cout<<"Enter the number of elements in the array";cin>>n;
initarray(a,n);
acceptarray(a,n);
pivotelement(a,n);
return 0;

}

Answer Source
#include<iostream>
using namespace std;

void initarray(int a[], int n)
{
    for(int i = 0; i<n; i++)
    {
        a[i]=0;
    }
}

void acceptarray(int a[], int n)
{
    for(int i = 0; i<n; i++)
    {
        cin >> a[i];
    }
}

int pivotelement(int a[], int n)   //function has return type int so return the index of pivot element
{
    int s1 = 0;
    int s2 = 0;

    for(int i = 0; i < n-1; i++)
    {
        if(i==0 || i==n-1) /* this condition added */
            continue;

        s1=0, s2=0; /* make s1 s2 zero inside for loop */

        for(int k = 0; k<i; k++) /* k<i not k<=i */
        {
            s1 += a[k];
        }
        for(int j = i+1; j<n; j++) /* j=i+1 not j=i+2 */
        {
            s2 +=a[j];
        }
        if(s1 == s2)
        {
            return i;               //returning the index of pivot element
        }

    }
    return -1;         //return -1 if pivot not found
}

int main(void)
{
    int a[100];
    int n =0;
    cin>>n;
    initarray(a,n);
    acceptarray(a,n);
    int x=pivotelement(a,n);         //call pivot element function and take the value returned

    if(x==-1)
        cout<<"no pivot";
    else
        cout<<"pivot element "<<a[x];


    return 0;
}

I have pointed out your errors in comments. This code is working fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download