Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please Help Fix my Code

0 votes
asked Oct 24, 2020 by Alvin (130 points)
I am trying to make a code that takes inputs and finds the frequency of the inputs. What I want is the output to be something like

"the number 5 has occurred 3 times"

but whenever I run the code the output is

"the number 5 has occurred 3 times"

"the number 5 has occurred 2 times"

"the number 5 has occurred 1 times"

Please help me fix this my code is below

#include <stdio.h>

#define MIN_SCORE 0

#define MAX_SCORE 450

#define MIN_PLAYERS 0

#define MAX_PLAYERS 2000

int main (){

int topScore [MAX_PLAYERS];

int numPlayers;

    float sum;

    int placeHolder1;

    int frequnecy [100];

    printf ("Please enter how many players there are\n");

    do{

        scanf ("%i",&numPlayers);

        if (numPlayers > MAX_PLAYERS || numPlayers < MIN_PLAYERS){

            printf ("Invalid please try again\n");

        }

        else printf ("There are %i players\n",numPlayers);

    }

    while (numPlayers > MAX_PLAYERS || numPlayers < MIN_PLAYERS);

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

printf ("Enter score betweem 0 and 450 for person %i\n", i+1);

do{

scanf ("%i", &topScore [i]);

if (topScore [i] > 450 || topScore [i] < 0){

    printf ("Invalid please enter a number between 0 and 450 for person %i\n", i+1);

        frequnecy[i] = -1;

}}

while (topScore [i]> MAX_SCORE || topScore [i]< MIN_SCORE);

}

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

     printf("Player %i scored %d\n",i+1, topScore [i]);

  }

    sum = 0;

    

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

        sum = sum + topScore [i];

    }

    

    printf ("The average top score was %.1f \n\n", sum/numPlayers);

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

    placeHolder1 = 1;

    for (int j = i+1; j < numPlayers; j++)

    {

    if (topScore [i] == topScore [j])

    {

        placeHolder1++;

        frequnecy[j] = 0;

    }

}

    if (frequnecy != 0 ){

        frequnecy[i] = placeHolder1;

    }

}

    

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

        if (frequnecy[i] != 0){

        printf ("%d occurs %d times\n", topScore[i], frequnecy[i]);

        }

    }    

    printf ("%5s", "Top Score");

    printf ("%20s", "Frequnecy");

    

}

2 Answers

0 votes
answered Oct 26, 2020 by Muhammad Awais Raza (190 points)
A lot of mistake in this program look again properly line by line you will find your mistake...
0 votes
answered Oct 26, 2020 by xDELLx (6,200 points)

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/

#include <stdio.h>
#define MIN_SCORE 0

#define MAX_SCORE 450
#define MIN_PLAYERS 0
#define MAX_PLAYERS 2000
    int
main ()
{
    int topScore[MAX_PLAYERS];
    int numPlayers;
    float sum;
    int placeHolder1;
    int frequnecy[MAX_PLAYERS];

   //Corrections below in 2 for loops to init all data

   for (int i = 0; i < MAX_PLAYERS; i++)
    {
        frequnecy[i] = 0;
    }
    for (int i = 0; i < MAX_PLAYERS; i++)
    {
        topScore[i] = 0;
    }

//Corrections below in 2 for loops to init all data -- END
    printf ("Please enter how many players there are\n");
    do
    {
        scanf ("%i", &numPlayers);
        if (numPlayers > MAX_PLAYERS || numPlayers < MIN_PLAYERS)
            printf ("Invalid please try again\n");
        else
            printf ("There are %i players\n", numPlayers);
    }while (numPlayers > MAX_PLAYERS || numPlayers < MIN_PLAYERS);

    for (int i = 0; i < numPlayers; i++)
    {
        printf ("Enter score betweem 0 and 450 for person %i\n", i + 1);
        do
        {
            scanf ("%i", &topScore[i]);
            if (topScore[i] > 450 || topScore[i] < 0)
            {
                printf("Invalid please enter a number between 0 and 450 for person %i\n", i + 1);
                frequnecy[i] = -1; //?? n0t reqd here , i think
            }
        } while (topScore[i] > MAX_SCORE || topScore[i] < MIN_SCORE);
    }
#if 1
    for (int i = 0; i < numPlayers; i++)
    {
        printf ("Player %i scored %d\n", i + 1, topScore[i]);
    }
    sum = 0;

    for (int i = 0; i < numPlayers; i++)
    {
        sum = sum + topScore[i];

    }

    printf ("The average top score was %.1f \n\n", sum / numPlayers);
    for (int i = 0; i < numPlayers; i++)
    {
        placeHolder1 = 1;
        for (int j = i + 1; frequnecy[topScore[i]] ==0 && j < numPlayers; j++){
            if (topScore[i] == topScore[j])
            {
                placeHolder1++;
                //frequnecy[j] = 0;
            }
        }
        if ( frequnecy[topScore[i]] ==0 )
        {
            frequnecy[topScore[i]] = placeHolder1;
        }
    }

    printf ("%5s", "Top Score");
    printf ("%20s", "Frequnecy\n");
    
    for (int i = 0; i < MAX_PLAYERS; i++)
    {
        if (frequnecy[i] != 0)
        {
            printf ("%d occurs %d times\n", i, frequnecy[i]);
        }
    }

#endif
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...