Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

Histogram from -5 to 5 in C

so basically I have an assignment to change my previous code that I wrote that generates and prints out the histogram (mine does 0 to 10) from 30 element board. I can't seem to make it do -5 to 5 so I'd want it to -5 every number it generates before printing it out. I've been thinking for 2 days already and I can't seem to find the soludion. I'm attaching the raw code. I'm new to C.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define TABLE_SIZE 30
#define HISTOGRAM_SIZE 11

int tablica[TABLE_SIZE];
int histogram[HISTOGRAM_SIZE];


void wylosuj_tablice() //generate an array
{
    int i;
    srand(time(0));
    for(i=0;i<TABLE_SIZE;i++)
    {
        tablica[i]=(rand()%HISTOGRAM_SIZE);
    }
}

void czysc_histogram() //clean histogram
{
    int i;
    for(i=0; i<HISTOGRAM_SIZE; i++) {
        histogram[i] = 0;
    }
}

void tworz_histogram() //basically now it does 0 to 10 but I want it to -5 to 5, so every int would be -5
{
    int i;
    czysc_histogram();
    for(i=0; i<TABLE_SIZE; i++) {
        histogram[tablica[i]]++;
    }

}

void wyswietl_histogram() // print the histogram
{
{
    int i;
    for(i=0; i<HISTOGRAM_SIZE; i++) {
        printf("Liczba %d: %d
", i, histogram[i]);
    }
}
}
void wyswietl_tablice() //Print a sorted table
{
    int i;
    for(i=0; i<TABLE_SIZE; i++) {
        printf ("tablica[%d]=%d
", i, tablica[i]);
    }
}

int main()
{
    wylosuj_tablice();
    wyswietl_tablice();
    tworz_histogram();
    wyswietl_histogram();
    return 0;
}
question from:https://stackoverflow.com/questions/65643247/histogram-from-5-to-5-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is it what you're looking for ?

void wyswietl_histogram() // print the histogram
{
    int i;
    for(i=0; i<HISTOGRAM_SIZE; i++) {
        printf("Liczba %d: %d
", i-5 /* <== */, histogram[i]); 
    }
}

void wyswietl_tablice() //Print a sorted table
{
    int i;
    for(i=0; i<TABLE_SIZE; i++) {
        printf ("tablica[%d]=%d
", i, tablica[i]-5 /* <== */); 
    }
}

Output :

tablica[0]=-5
tablica[1]=0
tablica[2]=-4
tablica[3]=-2
tablica[4]=5
tablica[5]=3
tablica[6]=-2
tablica[7]=-2
tablica[8]=-3
tablica[9]=1
tablica[10]=3
tablica[11]=4
tablica[12]=4
tablica[13]=1
tablica[14]=5
tablica[15]=2
tablica[16]=-2
tablica[17]=-3
tablica[18]=5
tablica[19]=-4
tablica[20]=2
tablica[21]=4
tablica[22]=0
tablica[23]=-2
tablica[24]=3
tablica[25]=-1
tablica[26]=2
tablica[27]=2
tablica[28]=-3
tablica[29]=1
Liczba -5: 1
Liczba -4: 2
Liczba -3: 3
Liczba -2: 5
Liczba -1: 1
Liczba 0: 2
Liczba 1: 3
Liczba 2: 4
Liczba 3: 3
Liczba 4: 3
Liczba 5: 3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...