Home > Enterprise >  Error in the looping that made an infinite loop of (*)
Error in the looping that made an infinite loop of (*)

Time:11-26

This is the program to find mean, median, and mode of element in array and then make histogram from it,

but the problem i have is only at the star count and print the star

#include<stdio.h>

int main()
{
    int sample[50] = {4,3,5,5,1,5,5,5,4,3,5,3,3,5,5,5,2,5,5,5,5,4,3,5,5,2,5,2,5,4,3,5,3,3,5,5,5,5,5,5,3,5,4,3,5,3,3,5,5,5,};

    int i;
    int j;
    int f1;
    int f2;
    int f3;
    int f4;
    int f5;
    int temp;
    int sum = 0;
    int count = 0;
    int median;
    float mean;

    for(i = 0; i < count; i  )
    {
    if(sample[i] == 1)
    {
        f1  ;
    }
    else if(sample[i] == 2)
    {
        f2  ;   
    }
    else if(sample[i] == 3)
    {
        f3  ;
    }
    else if(sample[i] == 4)
    {
        f4  ;
    }
    else if(sample[i] == 5)
    {
        f5  ;
    }
    }
    for(i = 0; i < 5; i  )
    {
        printf("\t%d\t", i   1);
        if(i == 0)
        {
        printf(" %d\t", f1);
        for(j = 0; j < f1; j  )
        {
            printf("*");
        }
        }
        else if(i == 1)
        {
        printf(" %d\t", f2);
        for(j = 0; j < f2; j  )
        {
            printf("*");
        }
        }
        else if(i == 2)
        {
        printf(" %d\t", f3);
        for(j = 0; j < f3; j  )
        {
        printf("*");
        }
        }
        else if(i == 3)
        {
        printf(" %d\t", f4);
        for(j = 0; j < f4; j  )
        {
            printf("*");
        }
        }
        else if(i == 4)
        {
        printf(" %d\t", f5);
        for(j = 0; j < f5; j  );
        {
            printf("*");
        }
        }
        printf("\n");
    }
return 0;
}

The output should be like this

but

My output right now is this

it seems the loop happened because the frequency of number 4 is 4205314, so the star looped until that number, but i can't find why the frequency goes that high

CodePudding user response:

Much simpler and shorter way:

#include <stdio.h>

int main(void) {
    static const int sample[50] = {4,3,5,5,1,5,5,5,4,3,5,3,3,5,5,5,2,5,5,5,5,4,3,5,5,2,5,2,5,4,3,5,3,3,5,5,5,5,5,5,3,5,4,3,5,3,3,5,5,5,};
    int count = sizeof(sample)/sizeof(*sample);    
    int histo[5] = {};
    for(int i=0; i<count;   i)
    {
        histo[sample[i]-1]  ;
    }
    
    char stars[count];
    ((char*)memset(stars, '*', count))[count-1] = '\0';
    
    printf("%-20s%-20s%-20s\n", "Response", "Frequency", "Histogram");
    for(int i=0; i<5;   i)
    {
        printf("%-20d%-20d%.*s\n", i 1, histo[i], histo[i], stars);
    }

    return 0;
}

Output

Success #stdin #stdout 0s 5500KB
Response            Frequency           Histogram           
1                   1                   *
2                   3                   ***
3                   12                  ************
4                   5                   *****
5                   29                  *****************************

CodePudding user response:

Most of your variables are uninitialized, and are likely completely random numbers. You then reference those random numbers in your for loops which is why they are not behaving how you indented them to.

You need to initialize your variables to the value you expect them to start at, for example:

int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0;
  •  Tags:  
  • c
  • Related