2016-06-14 2 views
1

プログラミングクラスの紹介の2週間後に、回答があまりにも狂ってしまう前に、私は言及する必要があります。一例として、この配列を使用してCの配列からデータを保存する

int scores[30] = {90,85,100,50,50,85,60,70,55,55,80,95,70,60,95, 
    80,100,75,70,95,90,90,70,95,50,65,85,95,100,65} 

私は後で使用するために2つの新しい並列配列を作成し、それを解析しようとしています。アイデアは、「スコア」を保持する配列と各スコアの「発生」を保持する配列を作ることです。しかし、コンパイル時にはエラーは発生しませんが、実行時にクラッシュします。

void frequency(int scores[], int max){ 
    int i, x=0, temp=0, count=0, sum=0, mode=0; 
    int score[sum]; //unknown length of array, sum gets added after the while loop 
    int freq[sum]; 
    printf("score\tfrequency\n"); 
    printf("-----\t---------\n"); 
    fprintf(fp, "score\tfrequency\n"); 
    fprintf(fp, "-----\t---------\n"); 
    for (i = 0; i < max; ++i){ 
     while (scores[i]==scores[x]){ 
      x++; 
      count++; 
      sum++; 
      temp = x-1; 
      if(scores[i] != scores[x]){ 
       //printf(" %d\t  %d\n",scores[i], count); 
       freq[i] = count; 
       score[i] = scores[i]; 
       count=0; 

       i=temp; 
       x=temp+1; 
       sum++; 
       printf("%d\t%d", score[i], freq[i]); 
       fprintf(fp, "%d\t%d", score[i], freq[i]); 
      } 
     } 
    } 
} 
+0

クラッシュが発生した場所をあなたのデバッガはあなたを教えてくれる、 –

答えて

2

この部分:

int i, x=0, temp=0, count=0, sum=0, mode=0; 
int score[sum]; 
int freq[sum]; 

が間違って見えます。

sumをゼロに設定し、それを配列次元として使用します。あなたは何をするもしかして:

sum = max; 
+0

私はint型のスコアを使用し、[合計。 ] whileループの後になるまでの長さを知らずに配列にその長さを与える方法として。 – grobot

+1

@grobot - あなたはそれをすることはできません。アレイを使用する前に、アレイに十分なスペースを確保する必要があります。あなたがそれをやらなければあなたのプログラムはクラッシュします。 – 4386427

+0

私はそれを難し​​い方法で考え出しました。新しい配列を作成せずにこのデータを保存するにはどのような方法がありますか? – grobot

1

私はそれがクラッシュし、実行時にただし、エラーなしでコンパイルしてしまいます。

理由

あなたがfrequency()機能

void frequency(int scores[], int max){ 
int i, x=0, temp=0, count=0, sum=0, mode=0; 
int score[sum]; 
int freq[sum]; 

ソリューションをint使用のアレイに十分なメモリを割り当てられていないため、プログラムがクラッシュする理由:

コンパイル時に要件に応じて実行時にメモリを提供したり、ブロックのメモリサイズを変更したりする方法はありますか?

あなたのコードでfrequency()機能、私はあなたが送信するすべての整数配列のために作品を提供してきました関数に固定された配列を送ってもはい、それは.... Dynamic memory allocationが使用されている理由は非常に理由です。..


ここで私は

  • 1つのアレイに格納すべてのユニークなスコア

  • とOTHコードを提供してきましたERの配列は、それぞれの出現数は、私はこの使用して動的なメモリ割り当てを行ってきた

を獲得..私はあなたが動的なメモリ割り当て関数の基本的な理解を持っている場合、それは理解しやすいと思う店...あなたが疑問を持っている場合は、コメントを通じて私に尋ねる:)と私はあることを、あなたの主な機能を想定してきた方法によって:

int main() 
{ 
    int scores[30] = {90,85,100,50,50,85,60,70,55,55,80,95,70,60,95, 
     80,100,75,70,95,90,90,70,95,50,65,85,95,100,65}; 
    frequency(scores,30); 
    return 0; 
} 

コード:

#include <stdio.h> 
#include <stdlib.h> 

void frequency(int scores[], int max); 

int main() 
{ 
    int scores[30] = {90,85,100,50,50,85,60,70,55,55,80,95,70,60,95, 
     80,100,75,70,95,90,90,70,95,50,65,85,95,100,65}; 
    frequency(scores,30); 
    return 0; 
} 

void frequency(int scores[], int max) 
{ 
    int i,j,count=0,flag=0,occur=0; 
    int *score=malloc(sizeof(int)); 
    if(malloc==NULL) 
    { 
     printf("memory allocation failed"); 
     exit(1); 
//it's good to check if memory allocated was successful or not 
//I've avoided it for further allocations,to decrease the size of post :) 
    } 
    int *freq=malloc(sizeof(int)); 
    printf("score\tfrequency\n"); 
    printf("-----\t---------\n"); 


    //building array which has only scores 
    for(i=0;i<max;i++) 
    { 
     if(count==0) //first time 
     { 
      score=realloc(score,(count+1)*sizeof(int)); 
      //increasing size of array by 1*sizeof(int) 
      score[count]=scores[i]; 
      count++; 
     }//first one requires no checking whether it's repeated or not 
     else 
     { 
      flag=0; //resetting flag value 
      for(j=0;j<count;j++) 
      { 
       if(scores[i]==score[j]) 
       { 
        flag=1; // 
        break; 
       } 
      } 

      if(flag==0) // if not repeated need to add new element 
      { 
       score=realloc(score,(count+1)*sizeof(int)); 
       score[count]=scores[i]; 
       count++; 
      } 

     } 
    } 

    //allocating memory for frequency array 
    freq=realloc(freq,count*sizeof(int)); 

    //building array which has frequency of each score 
    for(i=0;i<count;i++) 
    { 
     occur=0; 
     for(j=0;j<max;j++) 
     { 
      if(score[i]==scores[j]) 
       occur++; 
     } 
     freq[i]=occur; 
    } 

    for(i=0;i<count;i++) //printing output 
     printf("\n %d\t %d\n",score[i],freq[i]); 

    free(score); //freeing the blocks 
    free(freq); 
} 

私のアプローチ非常に理解しやすいです

  1. 最初に私は余分なメモリを作成するscoreを作成します一意の要素を見つけてそこに格納するときはいつでも
  2. の要素のそれぞれについて、score配列の配列をscores配列にチェックし、それらをfreq配列に格納します。

出力:

score frequency 
----- --------- 

    90  3 

    85  3 

    100  3 

    50  3 

    60  2 

    70  4 

    55  2 

    80  2 

    95  5 

    75  1 

    65  2 

私は、これはあなたが達成しようとしていたものであると思います:)

+0

私の友人は、まさに私が学ばそうとしていたものです。これにかなり新しいので、reallocとmallocに関するドキュメントを読むことは混乱していました。私は実際に何が起こっていたかを見ることができました。ありがとうございました! – grobot

+0

...あなたを助けてうれしいです:) @grobot – Cherubim

関連する問題