对功能感到困惑 [英] Confused about functions

查看:55
本文介绍了对功能感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我们的任务是创建一个程序,可以找到平均值,中位数

&整数#的模式。这是我的计划:


#include< stdio.h>


int main()

{

int item [100];

int a,b,t,mode;

int median_index;

浮动中位数,平均值;


int count;

printf(你要输入多少个数字?);

scanf("%d"& count);

for(a = 0; a< count; a ++){

scanf(" %d",& item [a]);

}

for(a = 1; a< count; a ++)

for (b = count-1; b> = a; --b){

if(item [b-1]> item [b]){

t = item [b-1];

item [b-1] = item [b];

item [b] = t;

}

}

/ *中位数* /


中位数=计数/ 2;

如果(计数%2 == 1){

printf(有奇数组数字。\ n);

median = item [count / 2] ;

}


else {

median_index = count / 2;


中位数=(项目[median_index ] + item [median_index-1])/ 2.0;

}

printf(中位数是%.1f \ n,中位数);


/ *平均值* /


avg = 0;

for(a = 0;一个<计数; a ++){

avg = avg + item [a];}

printf(平均值是:%。1f \ n,平均/计数);


/ *模式* /


for(a = 0; a< count; a ++){

if (item [a] == item [a + 1]){

mode = item [a];

}

else {

mode = 0;}

}


printf(模式是:%d \ n,模式);

返回0;

}


问题是我需要有平均值,中值和&模式为3个单独的

函数。我不知道如何设置这些功能。如果有人能告诉我

怎么可能,我会非常感激。


谢谢。 :)


P.S. - 我注意到我程序的模式部分有错误。如果

用户输入2种模式(例如1,2,2,4,4,5),我的程序将只显示

。我怎么能纠正这个?

Hi. Our assignment was to creat a program that can find the average, median
& mode of a #of integers. Here''s my program:

#include<stdio.h>

int main()
{
int item[100];
int a, b, t, mode;
int median_index;
float median, avg;

int count;
printf("How Many numbers do you want to enter? ");
scanf("%d", &count);
for(a=0; a<count; a++){
scanf("%d", &item[a]);
}
for(a=1; a<count; a++)
for(b=count-1; b>=a; --b){
if(item[b-1] > item[b]){
t=item[b-1];
item[b-1] = item[b];
item[b]=t;
}
}
/*median*/

median = count/2;
if(count%2 ==1){
printf("There are odd sets of numbers.\n");
median = item[count/2];
}

else {
median_index = count/2;

median = (item[median_index]+item[median_index-1])/2.0;
}
printf("The Median is %.1f\n", median);

/*average*/

avg = 0;
for(a=0; a<count; a++){
avg = avg+item[a];}
printf("Average is: %.1f\n", avg/count);

/*mode*/

for(a=0; a<count; a++){
if(item[a]== item[a+1]){
mode=item[a];
}
else{
mode=0;}
}

printf("Mode is: %d\n", mode);
return 0;
}

The problem is that I need to have the average, median & mode as 3 separate
functions. I don''t know how to set those functions up. If anyone can show me
how this is possible, I would greatly appreciate it.

Thank You. : )

P.S. - I notice that there is a fault in my mode section of my program. If
the user enters 2 modes (ex. 1,2,2,4,4,5) my program will only display the
first. How could I correct this?

推荐答案

agentxx04写道:
agentxx04 wrote:
嗨。我们的任务是创建一个程序,可以找到平均值,中位数
&整数#的模式。这是我的程序:
#include< stdio.h>

insert

#define NUMITEMS(100)int main()
使那个int main(void){
int item [100];
制作这个NUMITEMS; int a,b,t,mode;
int median_index;
float median,avg;
C不对浮点变量的相对整数范围提供任何保证,但如果sizeof(float)== sizeof(int)

那么那里存在整数(例如来自< limits.h>的INT_MAX)

,它不能完全由浮点数表示。所以,我建议

使用双倍或长双倍的中位数和平均值。

int count;

printf("多少你要输入数字吗?");
scanf("%d",& count);
检查是否计数< = NUM​​ITEMS - 否则我会

只需尝试输入NUMITEMS + 1 ... for(a = 0; a< count; a ++){
scanf("%d",& item [a]);
scanf()返回读取的输入项的数量,因此要确保在你的情况下你应该检查scanf是否返回1。 }


排序:你基本上需要信息

_what_来排序,这是数组项和

的类型数组元素,

_how_many_元素有排序。

你期望没有这个值(a = 1; a< count; a ++)
for( b = count-1; b> = a; - b){
if(item [b-1]> item [b]){
t = item [b-1];
item [b-1] = item [b];
item [b] = t;
}
}


确定中位数:你需要一个排序的数组和

数组的大小(并且必须知道数组类型。

你期望浮点数/双/长双值输出这个./ *中位数* /

中位数=计数/ 2;
这是不必要的。如果(计数%2 == 1){
printf(&有)奇数组。数字。中心=项目[计数/ 2];
}
其他{
median_index = count / 2; <中位数=(item [median_index] + item [median_index-1])/ 2.0;
count = 3 => median_index = 1 =>你正在查看项目[0]

和项目[1]。使其为median_index + 1。 }

printf(中位数是%.1f \ n,中位数);


平均值的确定:你需要数组,数组的类型

成员和数组元素的数量。数组不一定是
排序。

你期望浮动/双/长双值。 / *平均值* /

avg = 0;
for(a = 0; a< count; a ++){
avg = avg + item [a];}
printf(平均值是:%。1f \ n,平均/计数);

模式:我至少不明白应该采用什么模式

是的,但我猜这里也是如此。看来你好像b $ b假设一个排序的数组。 / *模式* /

for(a = 0; a< count; a ++){
这里有一个一个错误:如果a == count-1

然后下一行将访问项目[count]

超出范围。让它运行一个< count-1

或从a = 1开始并查看项目[a-1]和项目[a]。 if(item [a] == item [a + 1]){
mode = item [a];
}
else {
mode = 0;}
}
这有效地将

循环模式的每次迭代设置为0或项[a]。如果

item [count-1] == item [count]则mode = item [count-1],

否则为0后循环。这根本不需要

循环。
printf(模式是:%d \ n,模式);

返回0;
}

问题是我需要有平均值,中位数和&模式为3个独立的功能。我不知道如何设置这些功能。如果有人能告诉我这是怎么可能的,我将不胜感激。

谢谢。 :)

P.S。 - 我注意到我程序的模式部分有错误。如果用户进入2种模式(例如1,2,2,4,4,5),我的程序将只显示
。我怎么能纠正这个?
Hi. Our assignment was to creat a program that can find the average, median
& mode of a #of integers. Here''s my program:

#include<stdio.h>
insert
#define NUMITEMS (100) int main() make that int main (void) {
int item[100]; make this NUMITEMS; int a, b, t, mode;
int median_index;
float median, avg; C does not give any guarantees about the relative integer ranges
of floating point variables, but if sizeof(float)==sizeof(int)
then there exist ints (for example INT_MAX from <limits.h>)
which cannot represented exactly by floats. So, I would suggest
to use either double or long double for median and avg.

int count;
printf("How Many numbers do you want to enter? ");
scanf("%d", &count); Check whether count <=NUMITEMS -- otherwise I would
just try and enter NUMITEMS+1... for(a=0; a<count; a++){
scanf("%d", &item[a]); scanf() returns the number of input items read, so to be
sure you should check whether scanf returns 1 in your case. }
Sorting: You essentially need the information
_what_ to sort, that is the array item and the type of the
array elements,
_how_many_ elements there are to sort.
You expect no value from this for(a=1; a<count; a++)
for(b=count-1; b>=a; --b){
if(item[b-1] > item[b]){
t=item[b-1];
item[b-1] = item[b];
item[b]=t;
}
}
Determination of the median: You need a sorted array and
the size of the array (and have to know the array type.
You expect a float/double/long double value out of this. /*median*/

median = count/2; This is unnecessary. if(count%2 ==1){
printf("There are odd sets of numbers.\n");
median = item[count/2];
}

else {
median_index = count/2;

median = (item[median_index]+item[median_index-1])/2.0; count = 3 => median_index = 1 => you are looking at item[0]
and item[1]. Make it median_index+1. }
printf("The Median is %.1f\n", median);

Determination of average: You need array, type of array
members and number of array elements. array has not to be
sorted.
You expect a float/double/long double value out of this. /*average*/

avg = 0;
for(a=0; a<count; a++){
avg = avg+item[a];}
printf("Average is: %.1f\n", avg/count);
Mode: I do not understand in the least what mode should
be but I guess the above holds here, too. It seems that you
assume a sorted array. /*mode*/

for(a=0; a<count; a++){ You have an off-by-one error here: if a==count-1
then the next line will access item[count] which
is out of bounds. Make it run as long a<count-1
or start at a=1 and look at item[a-1] and item[a]. if(item[a]== item[a+1]){
mode=item[a];
}
else{
mode=0;}
} This effectively sets in every iteration of the
loop mode to either 0 or item[a]. If
item[count-1]==item[count] then mode=item[count-1],
otherwise 0 after the loop. This does not require a
loop at all.
printf("Mode is: %d\n", mode);
return 0;
}

The problem is that I need to have the average, median & mode as 3 separate
functions. I don''t know how to set those functions up. If anyone can show me
how this is possible, I would greatly appreciate it.

Thank You. : )

P.S. - I notice that there is a fault in my mode section of my program. If
the user enters 2 modes (ex. 1,2,2,4,4,5) my program will only display the
first. How could I correct this?




示例:排序:

输入:数组,大小

输出:没什么

void sort_it(int size,int array [])

{

int a,b; / *循环计数器,类型大小* /

int tmp; / * temp。存储,类型为数组[0] * /


for(a = 1; a< size; a ++)

for(b = size-1; b> = a; --b){

if(array [b-1]> array [b]){

tmp = array [b-1] ;

数组[b-1] =数组[b];

数组[b] = t;

}

}

}

示例:中位数:

输入:数组,计数

输出:浮动/双/长双;为了与你的程序保持一致,

我将使用float但是不使用它。

float array_median(int size,int array [])

{

int index; / * index,类型大小* /

浮点数; / *中位数变量,类型如返回类型* /


index = size / 2;

if((size%2)== 1){

printf(有奇数组数字。\ n);

中位数=数组[指数];

}

else {

median =(array [index] + array [index + 1])/ 2.0; / *(*)马克* /

}


返回中位数;

}


休息时留给读者练习。


注意:

- 对于数组索引,最好使用size_t类型

(而不是int),因为size_t保证适用于所有

可能的索引。 (如果你有16位整数和2GB

内存并使用一个超过pow(2,15)-1元素的数组

(例如33000),那么int不能再用作索引

而size_t可以。)

- (*):如果数组[索引] +数组[索引+ 1]> INT_MAX,你运行进入

麻烦。如果您要求将两者中的一个转换为

加倍之前加倍或加倍,那么总和将为

以(长)双倍完成:

median =((double)array [index] + array [index + 1])/ 2.0;

就足够了。

- 你的排序算法是最糟糕的可能。寻找

插入排序或选择排序(略微)更好

" simple"算法和Shell排序或快速排序

更复杂的算法能够快速处理(与前者比较)与大型数组相比。

- 还有一些问题,但我建议你工作

你的代码然后回来。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



Example: Sorting:
In: array, size
Out: Nothing
void sort_it (int size, int array[])
{
int a, b; /* loop counters, type like size */
int tmp; /* temp. storage, type like array[0] */

for (a=1; a<size; a++)
for (b=size-1; b>=a; --b) {
if (array[b-1] > array[b]) {
tmp = array[b-1];
array[b-1] = array[b];
array[b] = t;
}
}
}
Example: Median:
In: array, count
Out: float/double/long double; in keeping with your program,
I will use float but disadvise its use.
float array_median (int size, int array[])
{
int index; /* index, type like size */
float median; /* median variable, type like return type */

index = size/2;
if ( (size%2) == 1 ) {
printf("There are odd sets of numbers.\n");
median = array[index];
}
else {
median = (array[index]+array[index+1])/2.0; /* (*) Mark */
}

return median;
}

Rest left to reader as exercise.

Notes:
- For array indices, it is better to use the type size_t
(instead of int), as size_t ist guaranteed to work for all
possible indices. (If you have 16-Bit integers and 2GB
memory and use an array with more than pow(2,15)-1 elements
(e.g. 33000), then int cannot used any longer as index
whereas size_t can.)
- (*): If array[index]+array[index+1]>INT_MAX, you run into
trouble. If you demand that one of the two is converted to
double or long double prior to addition then the sum will
be done in (long) double:
median = ((double)array[index]+array[index+1])/2.0;
suffices.
- Your sorting algorithm is the worst possible. Look for
insertion sort or selection sort for (marginally) better
"simple" algorithms and for Shell sort or quicksort for
more sophisticated algorithms able to deal quickly (in
comparison with the former) with large arrays.
- There are some more issues but I suggest you work on
your code and come back then.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


agentxx04写道:
agentxx04 wrote:
嗨。我们的任务是创建一个程序,可以找到平均值,中位数
&整数#的模式。这是我的程序:
Hi. Our assignment was to creat a program that can find the average, median
& mode of a #of integers. Here''s my program:




尝试这样的事情(但不要尝试将其打开,除非你能够

证明每一个line):


#include< stdio.h>

#include< stdlib.h>


int qsort_dblcmp(const void *,const void *);

int qsort_cntcmp(const void *,const void *);

double get_median(int n,double x [ n]);

double get_average(int n,double x [n]);

void get_modes(int n,double x [n],int * nm,double m [n]);


int main(无效)

{

double * item,* modes;

int ndx,nmode;

双倍中位数,平均值;


int count;

printf("你要输入多少个数字?");

scanf("%d"& count);

if(count< 1)

退出(EXIT_SUCCESS);

if(!(item = malloc(count * sizeof * item))){

fprintf(stderr," ;我无法获得%d的空间数字。\ n"

" quitting ... \ n",count);

退出(EXIT_FAILURE);

}

if(!(modes = malloc(count * sizeof * modes))){

fprintf(stderr,我无法获得%d可能模式的空间。 \ n"

" quitting ... \ n",count);

退出(EXIT_FAILURE);

}

for(ndx = 0; ndx<计数; ndx ++){

scanf("%lf",& item [ndx]);

}

qsort(item,count, sizeof * item,qsort_dblcmp);

median = get_median(count,item);

average = get_average(count,item);

printf (中位数为%g \ n,中位数);

printf(平均值为:%g \ n,平均值);

get_modes (count,item,& nmode,modes);

printf(有%d个节点。它们是:\ n,nmode);

for (ndx = 0; ndx< nmode; ndx ++)

printf("%g\ n",modes [ndx]);


免费(项目);

免费(模式);


返回0;

}


int qsort_dblcmp(const void * p1,const void * p2)

{

return *(double *)p1 - *(double *)p2;

}


double get_median(int n,double x [n])

{

if( n%2)

返回x [n / 2];

返回(x [n / 2] + x [n / 2 - 1])/ 2;

}


double get_average(int n,double x [n])

{

double sum = 0;

int i;

for(i = 0;我< N; i ++)

sum + = x [i];

返还金额/ n;

}


typedef struct

{

double v;

int n;

} Cnt;


void get_modes(int n,double x [n],int * nm,double m [n])

{

int i,这= 0;

Cnt cnt [n];

if(n< 1){

* nm = 0;

返回;

}

for(i = 0; i< n; i ++){

cnt [i] .v = 0;

cnt [i] .n = 0;

}

cnt [0] .v = x [0] ;

cnt [0] .n = 1;

for(i = 1; i< n; i ++){

if( x [i]!= cnt [this] .v){

这个++;

cnt [this] .v = x [i];

}

++ cnt [this] .n;

}

qsort(cnt,this + 1,sizeof * cnt,qsort_cntcmp );

* nm = 0;

m [0] = cnt [0] .v;

for(i = 1; i< ; =这&& cnt [i] .n == cnt [i - 1] .n; i ++){

++ * nm;

m [* nm] = cnt [i] .v;

}

++ * nm;
< br $>
}


int qsort_cntcmp(const void * p1,const void * p2)

{

return((const Cnt *)p2) - > n - ((const Cnt *)p1) - > n;

}



Try something like this (but don''t try turning this in unless you can
justify every line):

#include <stdio.h>
#include <stdlib.h>

int qsort_dblcmp(const void *, const void *);
int qsort_cntcmp(const void *, const void *);
double get_median(int n, double x[n]);
double get_average(int n, double x[n]);
void get_modes(int n, double x[n], int *nm, double m[n]);

int main(void)
{
double *item, *modes;
int ndx, nmode;
double median, average;

int count;
printf("How Many numbers do you want to enter? ");
scanf("%d", &count);
if (count < 1)
exit(EXIT_SUCCESS);
if (!(item = malloc(count * sizeof *item))) {
fprintf(stderr, "I could not get space for %d numbers.\n"
"quitting ...\n", count);
exit(EXIT_FAILURE);
}
if (!(modes = malloc(count * sizeof *modes))) {
fprintf(stderr, "I could not get space for %d possible modes.\n"
"quitting ...\n", count);
exit(EXIT_FAILURE);
}
for (ndx = 0; ndx < count; ndx++) {
scanf("%lf", &item[ndx]);
}
qsort(item, count, sizeof *item, qsort_dblcmp);
median = get_median(count, item);
average = get_average(count, item);
printf("The Median is %g\n", median);
printf("Average is: %g\n", average);
get_modes(count, item, &nmode, modes);
printf("There are %d nodes. They are: \n", nmode);
for (ndx = 0; ndx < nmode; ndx++)
printf("%g\n", modes[ndx]);

free(item);
free(modes);

return 0;
}

int qsort_dblcmp(const void *p1, const void *p2)
{
return *(double *) p1 - *(double *) p2;
}

double get_median(int n, double x[n])
{
if (n % 2)
return x[n / 2];
return (x[n / 2] + x[n / 2 - 1]) / 2;
}

double get_average(int n, double x[n])
{
double sum = 0;
int i;
for (i = 0; i < n; i++)
sum += x[i];
return sum / n;
}

typedef struct
{
double v;
int n;
} Cnt;

void get_modes(int n, double x[n], int *nm, double m[n])
{
int i, this = 0;
Cnt cnt[n];
if (n < 1) {
*nm = 0;
return;
}
for (i = 0; i < n; i++) {
cnt[i].v = 0;
cnt[i].n = 0;
}
cnt[0].v = x[0];
cnt[0].n = 1;
for (i = 1; i < n; i++) {
if (x[i] != cnt[this].v) {
this++;
cnt[this].v = x[i];
}
++cnt[this].n;
}
qsort(cnt, this + 1, sizeof *cnt, qsort_cntcmp);
*nm = 0;
m[0] = cnt[0].v;
for (i = 1; i <= this && cnt[i].n == cnt[i - 1].n; i++) {
++*nm;
m[*nm] = cnt[i].v;
}
++*nm;

}

int qsort_cntcmp(const void *p1, const void *p2)
{
return ((const Cnt *) p2)->n - ((const Cnt *) p1)->n;
}


> double get_median(int n,double x [n]);
> double get_median(int n, double x[n]);
double get_average(int n,double x [n]);
double get_average(int n, double x[n]);




关于上述函数原型,我有几个问题:


1.''x'中''n''是否与第一个参数''int相关N ''?我是

假设它不是。

2.将''n''添加到''double x [n]''意味着什么函数声明?

我假设它没有。


如果我的假设是正确的,我会认为它更多明确写''双

x []''或只是''double []''作为参数。



I had a couple questions regarding the above function prototypes:

1. Is the ''n'' in ''double x[n]'' related to the first parameter ''int n''? I''m
assuming it''s not.
2. Does adding ''n'' to ''double x[n]'' mean anything in a function declaration?
I''m assuming it doesn''t.

If my assumptions are correct, I''d think it''s more clear to write ''double
x[]'' or just ''double[]'' as your parameter.


这篇关于对功能感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆