ソートされたランダムな配列から 1 番目と 3 番目の四分位数を出力するコードを記述する必要があります。私はそれを回避する方法がわかりません。何か助けはありますか?コードについて説明が必要な場合は、お問い合わせください。ありがとう!
median() 関数が機能しないバグを見つけていただければ幸いです。無限ループに陥っていると思います。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define TABLE_SIZE 20
#define HISTOGRAM_SIZE 9
int tablica[TABLE_SIZE];
int histogram[HISTOGRAM_SIZE];
void wylosuj_tablice() //creates and randomizes the array
{
int i;
srand(time(0));
for(i=0;i<TABLE_SIZE;i++)
{
tablica[i]=(rand()%HISTOGRAM_SIZE);
}
}
void czysc_histogram() //cleans histogram
{
int i;
for(i=0; i<HISTOGRAM_SIZE; i++) {
histogram[i] = 0;
}
}
void tworz_histogram() //counts from 0 to 8
{
int i;
czysc_histogram();
for(i=0; i<TABLE_SIZE; i++) {
histogram[tablica[i]]++;
}
}
void wyswietl_histogram() // shows histogram from -4 to 4
{
int i;
for(i=0; i<HISTOGRAM_SIZE; i++) {
printf("Liczba %d: %d\n", i-4 /* <== */, histogram[i]);
}
}
void wyswietl_tablice() //shows the sorted array from -4 to 4
{
int i;
for(i=0; i<TABLE_SIZE; i++) {
printf ("tablica[%d]=%d\n", i, tablica[i]-4 /* <== */);
}
}
float median() {
int temp;
int i, j;
for (i=0; i<HISTOGRAM_SIZE-1; j++) {
for(j=i+1; j<HISTOGRAM_SIZE; j++) {
if(tablica[j] < tablica[i]) {
//replaces elements
temp = tablica[i];
tablica[i] = tablica[j];
tablica[j] = temp;
}
}
}
if (HISTOGRAM_SIZE%2==0) {
//if there are 2 even numbers then it return mean from them
int wynik1 = ( (tablica[HISTOGRAM_SIZE/2] + tablica[HISTOGRAM_SIZE/2 -1]) /2.0);
printf("%d", wynik1);
} else {
//if there isn't then it gives the middle one
int wynik2 = tablica[HISTOGRAM_SIZE/2];
printf("%d", wynik2);
}
}
int main()
{
wylosuj_tablice(); //executes voids
wyswietl_tablice();
tworz_histogram();
wyswietl_histogram();
median();
return 0;
}