C语言中将0-10范围直方图修改为-5到5范围的技术求助
Hey there! I see you’ve been stuck adjusting your histogram from a 0-10 range to -5 to 5—let’s work through this together. The core issue here is that when you subtract 5 from your original 0-10 values, you end up with negative numbers, but array indices can’t be negative. We just need a small offset to map those negative values to valid positions in your histogram array.
Here are the exact changes you need to make:
1. Update the Histogram Creation (tworz_histogram)
Right now, you’re directly using the array’s 0-10 values as histogram indices. We first subtract 5 to shift values into the -5 to 5 range, then add 5 again to convert those negative/zero/positive values into valid 0-10 indices for the histogram array:
void tworz_histogram() // Now handles -5 to 5 range { int i; czysc_histogram(); for(i=0; i<TABLE_SIZE; i++) { int adjusted_value = tablica[i] - 5; // Convert original 0-10 to -5 to 5 histogram[adjusted_value + 5]++; // Shift to valid 0-10 index (-5+5=0, 5+5=10) } }
2. Update the Histogram Display (wyswietl_histogram)
Your current code prints 0-10 as the value labels, but we need to show -5 to 5 instead. Just subtract 5 from the loop index i to get the correct label for each histogram bin:
void wyswietl_histogram() // Print histogram with -5 to 5 labels { int i; for(i=0; i<HISTOGRAM_SIZE; i++) { printf("Liczba %d: %d\n", i - 5, histogram[i]); // i ranges 0-10, so i-5 is -5 to 5 } }
Full Modified Code
Here’s the complete code with these changes applied:
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<math.h> #define TABLE_SIZE 30 #define HISTOGRAM_SIZE 11 // Still 11 values (-5 to 5 inclusive) 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); // Still generates 0-10, we adjust later } } void czysc_histogram() //clean histogram { int i; for(i=0; i<HISTOGRAM_SIZE; i++) { histogram[i] = 0; } } void tworz_histogram() // Now handles -5 to 5 range { int i; czysc_histogram(); for(i=0; i<TABLE_SIZE; i++) { int adjusted_value = tablica[i] - 5; // Convert original 0-10 to -5 to5 histogram[adjusted_value + 5]++; // Map to valid histogram index (0-10) } } void wyswietl_histogram() // Print histogram with -5 to5 labels { int i; for(i=0; i<HISTOGRAM_SIZE; i++) { printf("Liczba %d: %d\n", i - 5, histogram[i]); } } void wyswietl_tablice() //Print the array (shows original 0-10 values) { int i; for(i=0; i<TABLE_SIZE; i++) { printf ("tablica[%d]=%d\n", i, tablica[i]); } } int main() { wylosuj_tablice(); wyswietl_tablice(); tworz_histogram(); wyswietl_histogram(); return 0; }
Quick Explanation
- We keep generating 0-10 values in
wylosuj_tabliceas you did before, then subtract 5 intworz_histogramto get the desired -5 to 5 range. - Adding 5 to the adjusted value fixes the negative index problem:
-5 +5 =0,0+5=5,5+5=10—perfectly matching your existing histogram array size. - When printing, we reverse the offset by subtracting 5 from the loop index to show the correct -5 to 5 labels.
This should give you exactly the histogram you need!
内容的提问来源于stack exchange,提问作者matxi

