按函数排列数组中的不同数字

时间:2013-10-09 20:07:34

标签: c++

程序将输入N个数字并计算一次发生的数组中的值的数量    函数在此程序中使用

#include<iostream>
using namespace std;

// Prototypes are placed before main
void input_data(short num[], short size);
void display_data(short num[], short size);
short frequencyOf1(short num[], short size);

int main()
{    
short num[100], size, // Declare an array of type short that has 100 elements
      number;         // Number counts the number of values that had a frequency of 1

cout<<"Enter the number of values to store in the array\n";
cin>>size;
while (size > 0)// Loop to try different data sets w/o re-executing the program
{
    input_data(num, size);   // Call statement to function to store data in array 
    display_data(num, size); // Call statement to function to print contents of array        

    cout<<"The program will count the number of values in the array that occurred 1 time\n"; 
    number = frequencyOf1(num, size);  // Call statement to function to count the number of value
                                       // That occurred 1 time; this is a value returning function          
    cout<<endl<<number<<" Values were found that occurred 1 time in the "<<size<<" element array\n";  

    cout<<"\nEnter the number of values to store in the array or 0 to terminate the program\n";
    cin>>size;
}    // End of while loop
     // Pause the program to see the results
 system("pause");
 return 0;       
}
// Function definitions are placed after main
void input_data(short num[], short size)
{
short i;
cout<<"Enter the "<<size<<" values to be used as data for this program\n";
for(i=0; i<size; i++)
   cin>>num[i];
}   

void display_data(short num[], short size)
{
short i;
cout<<"\nThere are "<<size<<" values in the array\n";
for(i=0; i<size; i++)
   cout<<num[i]<<' '; // There is one space between each number in the display
cout<<endl;           // Cursor moved to next output line 
}     
short frequencyOf1(short num[], short size)
{
// Enter code to solve the problem
// Must return a count using a 'return' statement
//?
  number=0;
  for(i=0; i<size; i++)
       {
            short sv=num[i];
            short event_flag=0;
                  for(int j=0; j<number; j++)
                      {
                        if(sv==frequencyOf1[j])
                         {
                                 event_flag=1;
                                         break;
                         }
                      }

                      if(event_flag==0)
                         {
                           frequencyOf1[number]=sv;
                           number++;
                         } 
       } 
}

这是另一个我不明白它是问同样的事情,我这样使用for循环?或其他什么它不起作用

好吧我试过这个现在它不会起作用

程序将输入N个数字并计算一次发生的数组中的值的数量    函数在此程序中使用     * /
    #包括     使用namespace std;

// Prototypes are placed before main
void input_data(short num[], short size);
void display_data(short num[], short size);
short frequencyOf1(short num[], short size);

int main()
{    
short num[100], size, // Declare an array of type short that has 100 elements
      number;         // Number counts the number of values that had a frequency of 1

cout<<"Enter the number of values to store in the array\n";
cin>>size;
while (size > 0)// Loop to try different data sets w/o re-executing the program
{
    input_data(num, size);   // Call statement to function to store data in array 
    display_data(num, size); // Call statement to function to print contents of array        

    cout<<"The program will count the number of values in the array that occurred 1 time\n"; 
    number = frequencyOf1(num, size);  // Call statement to function to count the number of value
                                       // That occurred 1 time; this is a value returning function          
    cout<<endl<<number<<" Values were found that occurred 1 time in the "<<size<<" element array\n";  

    cout<<"\nEnter the number of values to store in the array or 0 to terminate the program\n";
    cin>>size;
}    // End of while loop
     // Pause the program to see the results
 system("pause");
 return 0;       
}
// Function definitions are placed after main
void input_data(short num[], short size)
{
short i;
cout<<"Enter the "<<size<<" values to be used as data for this program\n";
for(i=0; i<size; i++)
   cin>>num[i];
}   

void display_data(short num[], short size)
{
short i;
cout<<"\nThere are "<<size<<" values in the array\n";
for(i=0; i<size; i++)
   cout<<num[i]<<' '; // There is one space between each number in the display
cout<<endl;           // Cursor moved to next output line 
}     
short frequencyOf1(short num[], short size)
{
  bool can_insert;


  for (int i = 0; i < 5; ++i)
{

    can_insert = true;


    for (int j = 0; j < size; ++j)
    {

        if (num[j] == number[i])
        {
            can_insert = false;
            break;
        }
    }


    if (can_insert)
    {
        num[size] = number[i];
        size++;
    }
}


cout << size;

return 0;
}

1 个答案:

答案 0 :(得分:0)

虽然我们不会为您回答您的作业问题,但我们可以提供一些建议。

通过将代码分解为更小的函数,您处于正确的道路上。 input_data(),display_data()和frequencyOf1()都是考虑任务的好方法。

最重要的是,你要为每个函数编写简单的小测试,以证明他们按照你的想法行事。我会单独测试每一个,如下:

void test_display_data()
{    
    short num[] = {1, 3, 7, 5, 3};
    short numSize = sizeof(num);
    display_data (num, numSize );
}

int main()
{
    test_display_data();
    return 0;
}

只是为了确保它符合您的期望。单独调试每个小例程。当您完成对一个例程的测试并知道它有效时,请更改main()中的调用以调用您自己的test_input_data()函数,然后再开始调试它。