1

I should mention that I am in my 1st 2 weeks of an intro to programming class before people get too crazy with answers.

Using this array as an example,

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}

I am trying to parse through it to create 2 new parallel arrays to use later. The idea is to make one array that holds the "scores" and one that holds the "occurrences" of each score. I end up compiling with no errors however during run time it crashes.

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]);
            }
        }
    }
}
1
  • YOur debugger will tell you where the crash happens., Commented Jun 14, 2016 at 6:50

2 Answers 2

2

This part:

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

looks wrong.

You set sumto zero and then use it for the array dimension. Did you mean to do:

sum = max;
Sign up to request clarification or add additional context in comments.

6 Comments

I used int score[sum] as a way to give the array its length without knowing what the length would be until after the while loop.
@grobot - you can't do that. You need to reserve sufficient space in the arrays before using them. Your program crashes because you fail to do that.
I figured that out the hard way. What method is there for saving this data with out creating new arrays?
@grobot - I assume max is the number of elements in the input array. For array dimensions the worse case scenario is that every element in the input are different, i.e. all elements have frequency 1. In that case you'll also need max elements in the result arrays. In other words as a simple fix just use maxfor the result arrays.
That is what I ended up doing. I was going for better than that, but it works until I learn how to make it better. It's why I'm learning. Thanks for the help.
|
2

I end up compiling with no errors however during run time it crashes.

Reason:

The reason why your program crashes is because you have not allocated sufficient memory to the arrays that you use int the frequency() function

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

Solution:

So, is there a way to provide memory during run time according to requirements or change memory size of blocks during compile time?

Yes, that's the very reason why Dynamic memory allocation is used.... though you send a fixed array to the frequency() function in your code, the function I've provided works for any integer array you send..


Here I've provided code in which

  • one array stores all the unique scores

  • and other array stores number of occurrences of each score

I've done this using dynamic memory allocation.. I think it's easy to understand if you have basic understanding of dynamic memory allocation functions.. if you have any doubts, ask me through the comments :) and by the way I've assumed your main function to be :

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

Code:

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

My approach is quite simple to understand

  1. first I create array score which creates extra memory whenever it encounters unique elements and stores in it
  2. and then I check occurrences for each of the element of score array in the scores array and store them in freq array.

Output:

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

  90      3

  85      3

  100     3

  50      3

  60      2

  70      4

  55      2

  80      2

  95      5

  75      1

  65      2

I hope this is what you were trying to achieve :)

1 Comment

That my friend, is exactly what I was trying to learn. Being fairly new to this, reading the docs on realloc and malloc were confusing with out a an example I could see what was really happening. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.