0

I'm trying to solve this task: "Write a bool sparse function (polynomial p) that takes polynomial p and returns true if 50% or more of the coefficients of polynomial p equals 0."

I use header file from my university.

#include <iostream>
#include "polinomATP.h"     // header file
using namespace std;

bool sparse(polinom p);

int main(){

    polinom p;
    int d;
    cout << "Upisi najveci stupanj polinoma: "<<endl;     // enter poly degree
    cin>>d;
    for(int i=0; i<=d; i++){
        cout<<"Upisi " << i <<". koeficijent: "<<endl;    //enter coef.
        int k;
        cin>>k;
        Attach(&p, i, k);
    }

    cout<<sparse(p);

    system("pause");
}

bool sparse(polinom p){

    int sum=0;
    for(int i=0; i<=Degree(p); i++){
        if(Coef(p, i)==0){
            sum++;
        }
    }
    if(sum>Degree(p)/2){
        cout<<"yes";
        return true;
    }
}

I'm not really good with bool functions so I guess I'm doing something wrong there. Thank you!

leonheess
  • 16,068
  • 14
  • 77
  • 112
ivanm43
  • 9
  • 3
  • 3
    You forgot to return something when the result is false. A decent compiler will warn you about this. – molbdnilo Apr 18 '20 at 17:34
  • 1
    You also forgot to mention what the problem is. "I guess I'm doing something wrong" doesn't say much. – molbdnilo Apr 18 '20 at 17:35
  • It works now, so simple. Thank you. I'm still new on Stack so tried to explain as best as I could. – ivanm43 Apr 18 '20 at 17:41

0 Answers0