Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

c - I can't see the problem with my program's output even after 6 hours of programming

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.

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.

This program is supposed to output like so

========================================================
                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

But it is outputting like 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):
- - - - - - - - - - - - - - - - - - - - - - - - - - -
                           0.00
                           0.00 (duplicate)
                           0.00 (duplicate)
                           0.00 (duplicate)
                     ---------
                         0.00 total

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("





");
   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("
Error %d in main.", DATA_ALLOC_ERR);
         printf("
Cannot allocate memory for the data.");
         printf("
The 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("
Thanks for your processing data. Have a nice day!");
   printf("




");

   return 0;
}

/**********************************************************************/
/*                   Prints the program instructions                  */
/**********************************************************************/
void print_instructions()
{
   printf("
This program processes experimental scientific data.");
   printf("
- - - - - - - - - - - - - - - - - - - - - - - - - - ");

   return;
}

/**********************************************************************/
/*                      Retrieves  data quantity                      */
/**********************************************************************/
int retrive_quantity()
{
   int quantity; /* Quantity of data values                           */

   do
   {
      printf("
How 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("
  Enter data value %d:    ", (int)(p_data - p_data_start)
         + 1);
      scanf(" %f", p_data);
      if(*p_data < 0.0f)
      {
         printf("
Negative %.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("
Error %d in main.", DATA_ALLOC_ERR);
      printf("
Cannot allocate memory for the data.");
      printf("
The 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("

The data in descending order (wiht duplicates noted):");
   printf("
- - - - - - - - - - - - - - - - - - - - - - - - - - -");

   for(p_data = p_data_start; (p_data - p_data_start) < quantity;
      p_data++)
   {
      printf("
                      %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("
                     ---------");
   printf("
                    %9.2f total", sum);

   return;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
for(p_sort = p_data_start; (p_sort - p_sort_start) < quantity;
      p_sort++)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...