此程序通过首先获取数据量及其值,然后按降序对其进行排序,最后求和,从而处理“实验科学数据”(实际上只是整数)。
问题是由于某种原因,输出中的数据为零(我无法弄清楚)。我确实认为问题出在sort_data函数中。但是我可能是错的。我丝毫不知道为什么要这么做。
该程序应该这样输出
========================================================
Program Number: 3
Programmer:
PCC Course Number: CS227
========================================================
Miscellaneous operations on your two whole numbers
This program processes experimental scientific data.
- - - - - - - - - - - - - - - - - - - - - - - - - -
How many data values are there (2 to 100, 0 = quit): 4
Enter data value 1: 3
Enter data value 2: 5
Enter data value 3: 4
Enter data value 4: 8
The data in descending order (with duplicates noted):
- - - - - - - - - - - - - - - - - - - - - - - - - - -
3.00
4.00 (duplicate)
5.00 (duplicate)
8.00 (duplicate)
---------
20.00 total
但是它正在输出
========================================================
Program Number: 3
Programmer:
PCC Course Number: CS227
========================================================
Miscellaneous operations on your two whole numbers
This program processes experimental scientific data.
- - - - - - - - - - - - - - - - - - - - - - - - - -
How many data values are there (2 to 100, 0 = quit): 4
Enter data value 1: 3
Enter data value 2: 5
Enter data value 3: 4
Enter data value 4: 8
The data in descending order (with duplicates noted):
- - - - - - - - - - - - - - - - - - - - - - - - - - -
0.00
0.00 (duplicate)
0.00 (duplicate)
0.00 (duplicate)
---------
0.00 total
我无法弄清楚这个问题,我已经尝试了几个小时才结束。我失去理智。请帮助我快死了。
/**********************************************************************/
/* */
/* This program processes experimental scientific data by first */
/* getting the quantity of data and their values, sort them in */
/* descending order, and finally summing. */
/* */
/**********************************************************************/
#include <stdio.h> /* printf, scanf */
#include <stdlib.h> /* malloc, free, exit(0) */
#include <string.h> /* memcpy */
/**********************************************************************/
/* Symbolic Constants */
/**********************************************************************/
#define COURSE_NUMBER "CS227" /* PCC assigned course number */
#define PROGRAM_NUMBER 3 /* Teacher assigned program number */
#define LAST_NAME "Lokey" /* The Programmer's last name */
#define MAX_CHOICE 100 /* Max choice */
#define MIN_CHOICE 2 /* Minimum choice */
#define DATA_ALLOC_ERR 1 /* Cannot allocate data memory */
#define DATA_SORT_ERR 2 /* Cannot allocate sort memory */
#define QUIT 0 /* Program value to quit */
/**********************************************************************/
/* Function Prototypes */
/**********************************************************************/
void print_heading(); /* Print the program heading */
void print_instructions(); /* Prints program instructions */
int retrive_quantity(); /* Get data quantity */
void get_data(float *p_data_start, int quantity);
/* Get data values */
void sort_data(float *p_data_start, int quantity);
/* Sorts data in order */
void prints_data(float *p_data_start, int quantity);
/* Prints the data */
float sum_data(float *p_data_start, int quantity);
/* Sums the data */
void print_sum(float sum); /* Prints data's sum */
/**********************************************************************/
/* Main Function */
/**********************************************************************/
int main()
{
float *p_data; /* Points to the data */
int quantity; /* Quantity of data values */
/* Prints program heading */
printf("\n\n\n\n\n\n");
print_heading();
/* Loops processing data until user quits */
while(print_instructions(), (quantity = retrive_quantity()) != QUIT)
{
/* Allocate memory for the data and then aborts */
/* program with errors if memory could not be allocated */
if((p_data = (float *)malloc(sizeof(*p_data) * quantity)) == NULL)
{
printf("\nError %d in main.", DATA_ALLOC_ERR);
printf("\nCannot allocate memory for the data.");
printf("\nThe program is aborting.");
exit (DATA_SORT_ERR);
}
/* Retrieves, sorts, and sums the data */
get_data (p_data, quantity);
sort_data (p_data, quantity);
prints_data (p_data, quantity);
print_sum (sum_data(p_data, quantity));
/* Releases the data */
free(p_data);
}
/* Thanks and says goodbye to the user */
printf("\nThanks for your processing data. Have a nice day!");
printf("\n\n\n\n\n");
return 0;
}
/**********************************************************************/
/* Prints the program instructions */
/**********************************************************************/
void print_instructions()
{
printf("\nThis program processes experimental scientific data.");
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - ");
return;
}
/**********************************************************************/
/* Retrieves data quantity */
/**********************************************************************/
int retrive_quantity()
{
int quantity; /* Quantity of data values */
do
{
printf("\nHow many data values are there (%d to %d, %d = quit): ",
MIN_CHOICE, MAX_CHOICE, QUIT);
scanf(" %d", &quantity);
}
while((quantity < MIN_CHOICE || quantity > MAX_CHOICE) && quantity
!= QUIT);
return quantity;
}
/**********************************************************************/
/* Retrieves data values */
/**********************************************************************/
void get_data(float *p_data_start, int quantity)
{
float *p_data; /* Points to every data value */
for (p_data = p_data_start; (p_data - p_data_start) < quantity;
p_data++)
{
printf("\n Enter data value %d: ", (int)(p_data - p_data_start)
+ 1);
scanf(" %f", p_data);
if(*p_data < 0.0f)
{
printf("\nNegative %.2f ", *p_data);
*p_data = -*p_data;
printf("converted to positive is %.2f", *p_data);
}
}
return;
}
/**********************************************************************/
/* Sorts the data into descending order */
/**********************************************************************/
void sort_data(float *p_data_start, int quantity)
{
float *p_data, /*Points to the data */
*p_greatest, /*Points to greatest data */
*p_sort, /* Points to sorted data */
*p_sort_start; /* Points to start of data */
if((p_sort_start = (float *)malloc(sizeof(*p_data) * quantity))
== NULL)
{
printf("\nError %d in main.", DATA_ALLOC_ERR);
printf("\nCannot allocate memory for the data.");
printf("\nThe program is aborting.");
exit (DATA_SORT_ERR);
}
for(p_sort = p_data_start; (p_sort - p_sort_start) < quantity;
p_sort++)
{
*p_sort = 0.0f;
for(p_data = p_data_start; (p_data - p_data_start) < quantity;
p_data++)
{
if(*p_data > *p_sort)
{
*p_sort = *p_data;
p_greatest = p_data;
}
}
*p_greatest = 0.0f;
}
memcpy(p_data_start, p_sort_start, sizeof(*p_data) * quantity);
free(p_sort_start); /* Release the memory allocated to the data */
return;
}
/**********************************************************************/
/* Print all data values */
/**********************************************************************/
void prints_data(float *p_data_start, int quantity)
{
float *p_data; /* Points to the data */
printf("\n\nThe data in descending order (wiht duplicates noted):");
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - -");
for(p_data = p_data_start; (p_data - p_data_start) < quantity;
p_data++)
{
printf("\n %9.2f", *p_data);
if(p_data != p_data_start)
if(*p_data == *(p_data - 1))
printf(" (duplicate)");
}
return;
}
/**********************************************************************/
/* Sum the data */
/**********************************************************************/
float sum_data(float *p_data_start, int quantity)
{
float *p_data, /* Points to the data */
sum = 0.0f; /* Sum of all data */
for(p_data = p_data_start; (p_data - p_data_start) < quantity;
p_data++)
sum += *p_data;
return sum;
}
/**********************************************************************/
/* Prints the data sum */
/**********************************************************************/
void print_sum(float sum)
{
printf("\n ---------");
printf("\n %9.2f total", sum);
return;
}
答案 0 :(得分:2)
for(p_sort = p_data_start; (p_sort - p_sort_start) < quantity;
p_sort++)
p_data_start
指向与p_sort_start
不同的数组,这就是错误。而且您从未真正将数据从main()复制到p_sort_start
数组中。另外,您可以在排序循环中将内容设置为零,这也没有道理。
我不确定为什么首先需要分配第二个缓冲区,只需就地对数组进行排序即可。
答案 1 :(得分:1)
如其他答案所指出的那样,您的排序功能不必要地复杂且容易出错。
您只需按如下所示进行排序即可。
void sort_data(float *p_data_start, int quantity)
{
float temp;
size_t i=0,j=0;
for( i=0; i < quantity; i++)
{
for(j=0; j < quantity-i-1; j++)
{
if(p_data_start[j] > p_data_start[j+1])
{
temp = p_data_start[j];
p_data_start[j] = p_data_start[j+1];
p_data_start[j+1] = temp;
}
}
}
return;
}