0

私のコードに問題があるようです。以下に、私の割り当てに関する詳細と、割り当てでの試行を含めました。私の教授は、私は答えに非常に近いと言いますが、最終的にコードをコンパイルしてBAMすることができました! 端末に大量の数字を吐き出す無限ループが発生します。助けていただければ幸いです。:)

私のプログラムの要件は次のとおりです。

連続するサブシーケンスの最長部分の長さを昇順で求めるプログラムを作成してください。長さ、それが配置されている場所、およびサブシーケンス内の整数を出力します。同じ長さのサブシーケンスが複数ある場合は、最初に見つかったサブシーケンスを出力します。

入力:
入力には複数のデータ セットが含まれます。入力の各行は、データ セットを表します。データセットの最初の整数は、データセット (ライン上) に残っている整数の数です。データ セットの終わりには -1 のマークが付けられます。

以下はデータファイルの例です。

   16 45 89 41 55 59 64 80 70 12 45 70 90 94 99 23 41
   10 1 2 3 4 5 6 7 8 9 10
   16 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
   -1

出力:

  • データセット 1 ==>Longest: 6 Positions: 8-13 Sequence: 12 45 70 90 94 99
  • データセット 2 ==>Longest: 10 Positions: 0-9 Sequence: 1 2 3 4 5 6 7 8 9 10
  • データセット 3 ==>Longest: 4 Positions: 0-3 Sequence: 1 2 3 4

プログラムには、メイン関数、少なくとも 2 つの関数、および配列の使用が含まれている必要があります。配列をソートしていないことに注意してください。ただし、Histogram プログラムは参照するのに優れたプログラムです。

プログラムに参加する前に、鉛筆と紙で計画を立てることを強くお勧めします。ヒストグラムの例にある私のドキュメントを模倣してください。

これまでの私のコードは次のとおりです。

#include<stdio.h>
#include<stdlib.h>
void load(int a[],int n);
void seq(int a[],int n,int *max, int *loc);
void print(int a[],int n);
int main(void)
{
    int a[100];
    int n;
    int max;
    int loc;

    scanf("%d",&n);
    while(n!=-1){
        if(n>100){
            fprintf(stderr,"Number entered is larger than 100\n");
            exit(1);
        }
        load(a,n);
        seq(a,n,&max,&loc);
        print(a,n);
        scanf("%d",&n);
    }
    return 0;
}

void load(int a[],int n)
{
    int i;
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
}

void seq(int a[],int n,int *max, int *loc)
{
    int i;
    int length=1;
    *max=-1;

    for(i=0;i<n-1;i++){
        if(a[i]<a[i+1]){
            length++;
            if(length>*max){
                *max=length;
                *loc=i-*max+1;
            }
        }else{
            if(length>*max){
                *max=length;
                *loc=i-*max+1;
            }
            length=1;
        }
    }
}

void print(int a[],int n)
{
    int i=0;
    while(i<n){
        printf("%d ",a[i]);
        i++;
    }
    printf("\n");
}
4

2 に答える 2

3

おそらく割り当てのポイントを無効にするため、答えを提供するつもりはありません(そして、いくらかの努力のように見えます)...むしろ、取るべきいくつかのデバッグアプローチについて説明させてください。

  1. 適切な開発環境 (Visual C++ や Visual Studio Express など) で作業していると仮定して、while {}ループの開始/終了にブレークポイントを配置し、各段階で値が期待どおりかどうかを確認します。値が期待したものではないことに気付くかもしれません。これにより、問題が明らかになります。

  2. この方法でデバッグできない場合は、変数の状態を出力するコードの重要な行でコンソールにデバッグ メッセージを出力し、それらが期待どおりかどうかを確認します。while {}無限ループを回避し、問題の原因を突き止めるために、おそらく 10 回の反復後に強制的にループを中断させます。

適切な開発スタジオを備えていない場合は、Visual Studio 2013 Express for Windows をダウンロードしてください。適切なツールを使用せずにコードをデバッグしようとしても無意味であり、問​​題解決やプログラミング スキルを習得することにはなりません。

于 2013-11-12T00:17:22.800 に答える
0

ついに終わった!!!!!ご協力いただきありがとうございます!!!:) :)

/* Name: 
 * Class: CSC-1710
 * Date: 11/11/2013
 * File: 
 *
 * This program takes a set of numbers that are no larger than 100
 * elements in size and then finds a sequence within the set of numbers
 * that are in increasing order and displays the longest sequence,
 * the location of the sequence and when it ends.
 */
#include<stdio.h>
#include<stdlib.h>

/* PreCondition:
 *    Input array will be empty.
 * PostCondition:
 *    Array will be loaded with a maximum of 100 ints.
 * The function loads integers from a file.
 */

void load(int a[],int n);

/* PreCondition:
 *    Array is loaded with n integers.
 * PostCondition:
 *    Array is filtered through sequence function.
 * The sequence function takes an array and scans for
 * the longest sequence and finds the max and the location.
 */

void seq(int a[],int n,int *max, int *loc);

/* PreCondition:
 *    Array's max and location of sequence have been found.
 * PostCondition:
 *    Array is printed out with max and location and sequence.
 * The function prints the max, location of the beginning of the max,
 * and the ending of the max sequence,
 * and then uses a for loop to output the sequence that is the longest.
 */

void print(int a[],int n,int max,int loc,int cnt);

int main(void)
{
int a[100];
int n;
int max;
int loc;
int cnt=0;

scanf("%d",&n);
while(n!=-1){
   if(n>100){
      fprintf(stderr,"Number entered is larger than 100\n");
      exit(1);
   }
   load(a,n);
   seq(a,n,&max,&loc);
   cnt++;
   print(a,n,max,loc,cnt);
   scanf("%d",&n);
}
return 0;
}

void load(int a[],int n)
{
int i;
for(i=0;i<n;i++)
   scanf("%d",&a[i]);
}

void seq(int a[],int n,int *max, int *loc)
{
int i;
int length=1;
*max=-1;

for(i=0;i<n-1;i++){
   if(a[i]<a[i+1]){
      length++;
   }else{
      if(length>*max){
         *max=length;
         *loc=i-*max+1;
      }
   length=1;
   }
}
if(length>*max){
   *max=length;
   *loc=i-*max+1;
}
}

void print(int a[],int n,int max,int loc,int cnt)
{
int b=max+loc;
int i;
printf("Data Set %d ==>",cnt);
printf(" Longest: %d ",max);
printf("Positions: %d-%d ",loc,loc+max-1);
printf("Sequence: ");
for(i=loc;i<b;i++){
printf("%d ",a[i]);
}
printf("\n");
}
于 2013-11-12T19:21:41.537 に答える