我需要编写代码来打印排序和随机数组中的第一个和第三个四分位数。我不知道如何绕过它。有什么帮助吗?如果您需要有关代码的任何说明,请询问。谢谢!
如果你能找到一个 bug,为什么 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;
}