我的函数总是等于0,同时在数组中查找唯一和重复的值

时间:2018-03-04 04:49:51

标签: c++

编辑:感谢您提供了有用的评论,我将我的count_distinct和我的count_duplicates移到了while循环中,他们应该从一开始就让我的代码看起来像这样

unique = count_distinct(data, size); // get result
cout << "There are " << unique << " unique values" << endl;
count = count_duplicates(data, size); // get result 
cout << "There are " << count << " duplicate values" << endl;

我浏览了调试器,它说我的值i等于我输入的任何大小,即使我在我的函数中设置了i=0。如果i = size,则整个函数终止,这导致我的唯一值等于0

short count_distinct(short num[], short size)   //Function to return the count of the number of unique/distinct values in the array
{
short i, j, unique=0;
    for(i=0;i<size;i++) //start 1 high to compare to previous numbers
    {
        for(j=0;j<size;j++) //nested for statement which goes through the array to compare with previous numbers
        {
            if (num[i]==num[j]) //compares the numbers to eachother, if they are equal then stop 
            {
                break; // if they are equal then dont count it
            }
        if(i==j)
            unique++;
        }
    }
    return unique;
}

问题在于:当我输出我的独特&amp;重复变量,它们只等于0。 可能是我的唯一和重复功能设置不正确。 我一直试图解决这个问题几个小时尝试了几个不同的事情,但我不知道代码有什么问题

这是调试器错误

"count & unique = Not found in current context"

这是我的代码:

    using namespace std;

void input_data(short data[], short size);
void display_data (short data[], short size);
short count_distinct(short num[], short size);  // value returning function
short count_duplicates(short num[], short size);  // value returning function

int main()
{   // declare local variables      - do I need to declare more variables??
short size, data[1000], unique, count;  
    unique = count_distinct(data,size);     
    count = count_duplicates(data,size);

cout<<"Enter a size of numbers to process\n";   // display a message to the user about the number of values to store in the array

cin>>size;
while(size>0)
{

    cout<<"Enter the numbers that need to be processed"<<endl;

    input_data(data, size);                     // Call input_data function         - Lets user input elements into array   
    cout<<"There are "<<size<<" values stored in the array."<<endl;

    display_data(data,size);                    // Call display_data function   

    count_distinct(data,size);                  // Call count_distinct function     - Shows unique values       
    cout<<"There are "<<unique<<" unique values"<<endl;

    count_duplicates(data,size);                // Call count_duplicates function   - Shows duplicate values
    cout<<"There are "<<count<<" duplicate values"<<endl;

    cout<<"To run the program again, enter the number of values to\n";
    cout<<"store in the array or 0 to terminate the program";
    cout<<"\n\n";
    cin>>size;

        }

    return 0;
    }

    // Function to input(store) data into the array
    void input_data(short data[], short size){
    short i;    
        for(i=0; i<size;i++)
        {
        cin>>data[i];
        }
    }

    // Function to display the data in the array 
    // print all values on the same line with one space between each number
    void display_data (short data[], short size){
    short i;
    cout<<"Values are : "<<endl;
        for(i=0;i<size;i++)
        {
            cout<<data[i]<<" ";
            cout<<endl;
        }

    }


    short count_distinct(short num[], short size)   //Function to return the count of the number of unique/distinct values in the array
    {
    short i, j, unique=0;
        for(i=0;i<size;i++) //start 1 high to compare to previous numbers
        {
            for(j=0;j<size;j++) //nested for statement which goes through the array to compare with previous numbers
            {
                if (num[i]==num[j]) //compares the numbers to eachother, if they are equal then stop 
                {
                    break; // if they are equal then dont count it
                }
            if(i==j)
                unique++;
            }
        }
        return unique;
    }


    short count_duplicates(short num[], short size) //Function to return the count of the number of duplicate values in the array
    {
    short i, j, count =0;
        for( i=1;i<size;i++) //start 1 high to compare to previous numbers
        {
            for(j=0;j<i;j++) //nested for statement which goes through the array to compare with previous numbers
            {
                if (num[i]==num[j]) //compares the numbers to eachother
                {
                count++;    // if values are equal they are duplicates
                break;
                }
            }

        }

        return count;
    }

1 个答案:

答案 0 :(得分:0)

主要

int main()
{ // declare local variables      - do I need to declare more variables??
    short size, data[1000], unique, count;
    unique = count_distinct(data, size);
    count = count_duplicates(data, size);
使用未初始化的参数调用

count_distinctcount_duplicates。这会调用未定义的行为,程序无效。在此之后发生的任何事情都不可信任。

大多数编译器会警告这样的错误。不要忽略编译器警告。它们通常是防止简单错误的第一道防线。

后来

    count_distinct(data, size); // Call count_distinct function     - Shows unique values
    cout << "There are " << unique << " unique values" << endl;

    count_duplicates(data, size); // Call count_duplicates function   - Shows duplicate values
    cout << "There are " << count << " duplicate values" << endl;
再次调用

count_distinctcount_duplicates,但不存储其结果,从而打印出之前调用中出现的任何无效结果。

修复:

int main()
{ // declare local variables      - do I need to declare more variables??
    short size, data[1000], unique, count;
    //unique = count_distinct(data, size); remove this
    //count = count_duplicates(data, size); remove this

    unique = count_distinct(data, size); // get result
    cout << "There are " << unique << " unique values" << endl;
    count = count_duplicates(data, size); // get result 
    cout << "There are " << count << " duplicate values" << endl;

count_distinct无法正常工作,使用调试器逐步执行程序将帮助您查看错误,我还质疑是否需要while (size > 0)循环。