即使经过6个小时的编程,我也看不到程序输出的问题 [英] I can't see the problem with my program's output even after 6 hours of programming

查看:74
本文介绍了即使经过6个小时的编程,我也看不到程序输出的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此程序通过首先获取数据量及其值,然后按降序对其进行排序,最后求和,从而处理实验科学数据"(实际上只是整数).

This program processes "experimental scientific data" (it is really just integers) by first getting the quantity of data and their values, sort them in descending order, and finally summing.

问题是由于某种原因,输出中的数据为零(我无法弄清楚).我确实认为问题出在sort_data函数中.但是我可能是错的.我丝毫不知道为什么要这么做.

The problem is for some reason the data is being zero(ed) out in the output I haven't been able to figure it out. I do believe the issue is in the sort_data function. But I could be wrong. I haven't the slightest idea why it is doing this.

该程序应该这样输出

========================================================
                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点.我失去理智.请帮助我快死了.

I can not figure this out I have been trying for hours on end its 4 AM for me. I am loosing sanity. Please help I am dying.

/**********************************************************************/
/*                                                                    */
/* 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;
}

推荐答案

for(p_sort = p_data_start; (p_sort - p_sort_start) < quantity;
      p_sort++)

p_data_start指向的数组与p_sort_start指向的数组不同,这就是错误.而且您从未真正将数据从main()复制到p_sort_start数组中.另外,您可以在排序循环中将内容设置为零,这也没有道理.

p_data_start points at a different array than p_sort_start so that's the bug. And you never actually copied the data from main() into the p_sort_start array. And additionally, you set the contents to zero inside the sorting loop, which doesn't make sense either.

我不确定为什么首先需要分配第二个缓冲区,只需就地对数组进行排序即可.

I'm not sure why you need to allocate a second buffer in the first place, simply sort the array in-place.

这篇关于即使经过6个小时的编程,我也看不到程序输出的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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