-1

プログラムは N 個の数字を入力し、配列内で 1 回発生した値の数をカウントします 関数はこのプログラムで使用されます

#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 個の数字を入力し、配列内で 1 回発生した値の数をカウントします このプログラムでは関数が使用されます */
#include 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)
{
  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;
}
4

1 に答える 1

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;
}

期待どおりに機能することを確認するだけです。各小さなルーチンを個別にデバッグします。1 つのルーチンのテストが完了し、それが機能することを確認したら、呼び出しを変更しmain()て独自のtest_input_data()関数を呼び出し、次にデバッグを開始します。

于 2013-10-09T20:56:17.300 に答える