Эта программа обрабатывает «экспериментальные научные данные» (на самом деле это просто целые числа), сначала получая количество данных и их значения, сортируя их в порядке убывания и, наконец, суммируя.
Проблема в том, что по какой-то причине данные обнуляются (выводятся) в выводе, который я не смог выяснить.Я верю, что проблема в функции 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
Я не могу понять это, я пытался часами подряд его 4 утра для меня.Я теряю здравомыслие.Пожалуйста, помогите, я умираю.
/**********************************************************************/
/* */
/* 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;
}