0

compiler error :[Error] invalid conversion from 'int' to 'int*' [-fpermissive] could someone please help me , and tell me why my programme is giving me this error ?

the faulty code :

cout<<"Mode score: "<<printModeValues(scorePtr,size,modeFrequency(scorePtr,size));

my code: Main:

#include <iostream>
#include <string>
#include "processScores.h"

int main()  {
    int * scorePtr;
    string * namePtr, scoresFileName;

    cout<<"Enter the file name: ";
    cin>>scoresFileName;

    unsigned size=getRecordsNumber(scoresFileName);
    scorePtr = new int[size];
    namePtr = new string[size];

    readRecords(scorePtr,namePtr,scoresFileName);
    sort(scorePtr,namePtr,size);

    cout<<"The records in ascending order of surnames are: \n";
    cout<<"Name          Score\n";
    cout<<"---------------------"<<endl;
    printScores(scorePtr,namePtr,size);

    cout<<endl;
    cout<<"Highest score: "<<highest(scorePtr,size)<<"\t";
    printFoundNames(scorePtr,namePtr,size,highest(scorePtr,size));
    cout<<"Lowest score: "<<lowest(scorePtr,size)<<"\t";    
    printFoundNames(scorePtr,namePtr,size,lowest(scorePtr,size));
    cout<<"Mean score: "<<mean(scorePtr,size)<<endl;
    cout<<"Mode score: "<<printModeValues(scorePtr,size,modeFrequency(scorePtr,size));
    cout<<"Modal value occurrences is "<<modeFrequency(scorePtr,size)<<" time\n"<<endl;
    cout<<"Median score: "<<median(scorePtr,size)<<endl; 
    delete [] scorePtr;
    delete [] namePtr;
}

the header file:

void printModeValues(const int *, size_t, int[]);

function prototype:

//**** MODE FREQUENCY ****
int modeFrequency(const int * scores, size_t size)
{
    int y[size] , modes[size];//Sets all arrays equal to 0
    int i,j,k,m,a,cnt,count=0,max=0,no_mode=0,mode_cnt=0;
    double num;

    for(k=0; k<size; k++)//Loop to count an array from left to right
    {
        cnt=0;
        num=scores[k];//Num will equal the value of array x[k]

        for(i=k; i<size; i++)//Nested loop to search for a value equal to x[k]
        {
            if(num==scores[i])
                 cnt++;//if a number is found that is equal to x[k] count will go up by one

        }

        y[k]=cnt;//The array y[k] is initialized the value of whatever count is after the nested loop

        if(cnt>=2)//If cnt is greater or equal to two then there must be atleast one mode, so no_mode goes up by one
        {
            no_mode++;
        }
    }

if(no_mode==0)//after the for loops have excuted and still no_mode hasn't been incremented, there mustn't be a mode
{
    //Print there in no mode and return control to main
    modes[1]=-1;
   // return modes;
}
    for(j=0; j<size; j++)
//A loop to find the highest number in the array
    {   
        if(y[j]>max)
        max=y[j];
    }
 for(m=0; m<size; m++)//This loop finds how many modes there are in the data set
{
    //If the max is equal to y[m] then that is a mode and mode_cnt is incremeted by one
    if(max==y[m])
        mode_cnt++;
}
//cout<<"This data set has "<<mode_cnt<<" mode(s)"<<endl;//Prints out how many modes there are
    for(m=0; m<size; m++)
    {
        if(max==y[m])//If max is equal to y[m] then the same sub set of array x[] is the actual mode
        {

            cout<<"The value "<<scores[m]<<" appeared "<<y[m]<<" times in the data set\n"<<endl;
            modes[count]=scores[m];
            count++;
        }
    }
return *modes;
}
//=====================================================================================
//**** PRINT MODE VALUE ****
void printModeValues(const int *scores, size_t size, int *mostAppearance)
{
    if (mostAppearance[0]== -1)
    {
        cout<<"-1 Modal value occurance is one time "<<endl;
    }
    else
    {
        for (int a=0 ; a< sizeof(mostAppearance); a++)
        {
            cout<<mostAppearance[a]<<" ";
        }
        cout<<endl; 
    }
}
2
  • Why do you need to pass a pointer, when only a value would be sufficient? Commented May 23, 2015 at 18:51
  • modeFrequency returns int but you are passing it to a function which should give an array! hence that error is given, I think you are giving the parameters in a wrong order Commented May 23, 2015 at 18:52

2 Answers 2

1

Function printModeValues is declared the following way

void printModeValues(const int *, size_t, int[]);

As you see its third parameter is declared like int[] that is adjusted to int *

In this statement

cout<<"Mode score: "<<printModeValues(scorePtr,size,modeFrequency(scorePtr,size));

you call the function like

printModeValues(scorePtr,size,modeFrequency(scorePtr,size))

that is it uses as the third argument the value returned by function modeFrequency.

However this function has return type int instead of int * that is exprected by function printModeValues for its third parameter

int modeFrequency(const int * scores, size_t size);
^^^^

This is the reason for the error.

There are other errors in your program. For example in this function

void printModeValues(const int *scores, size_t size, int *mostAppearance)
{
    if (mostAppearance[0]== -1)
    {
        cout<<"-1 Modal value occurance is one time "<<endl;
    }
    else
    {
        for (int a=0 ; a< sizeof(mostAppearance); a++)
        {
            cout<<mostAppearance[a]<<" ";
        }
        cout<<endl; 
    }
}

the condition in this loop statement

        for (int a=0 ; a< sizeof(mostAppearance); a++)

does not make sense because operator sizeof(mostAppearance) yields the size of the pointer itself (usually 4 or 8 bytes). It is not the same as the number of elements in the array the first element of which is pointed to by this pointer.

It seems you was going to return a pointer from function modeFrequency that is you wanted to declare the function like

int * modeFrequency(const int * scores, size_t size);

and was going to return the pointer to the first element of array modes

int * modeFrequency(const int * scores, size_t size)
{
    int y[size] , modes[size];//

    //...

    return modes;
}

However even in this case the function will be invalid because it returns pointer to a local object of the function because array modes is a local array. Moreover the C++ Standard does not allow to use variable length arrays. So this declaration

    int y[size] , modes[size];//

is not C++ compliant.

I would advice to use standard class std::vector instead of arrays in your program. Or you have to allocate arrays dynamically yourself.

Sign up to request clarification or add additional context in comments.

Comments

0

modeFrequency is returning int

int modeFrequency(const int * scores, size_t size)

but your printModeValues expecting an integer array.

void printModeValues(const int *, size_t, int[]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.