Thank you for looking at this thread. I am on a deadline for college and i can't get the kinks worked out of this program. It compiles fine, but i am having problems with the get_data and sort function. This program asks the user how large the array (quantity) and then the user enters numbers for the array as the program prompts. The program then sorts the array using selection sort and a sort array in descending order from the largest number to the lowest printing duplicate next to any numbers that repeat themselves. then the program sums the numbers and totals them. In this program i need the 5 pointers and the 2 arrays to sort.
Right now the program does not work correctly. The numbers i enter don't make it through the program (the problem is somewhere in the get_data and sort function) the numbers that the program is to print out always come out to 0.00
0.00 .....etc. They should be values not zeroes my error i'm assuming is pointers, but i will quit rambling because if i knew i could fix it.
thank you all for your help, i appreciate you.
************************************************** ********************/
#include <stdio.h> /* printf, scanf */
#include <stdlib.h>
#include <string.h>
/************************************************** ********************/
/* Symbolic Constants */
/************************************************** ********************/
#define COURSE_NUMBER "CS227" /* PCC assigned course number */
#define PROGRAM_NUMBER 2 /* Teacher assigned program number */
#define PROGRAMMER_NAME "Hipke" /* Programmer's last name */
#define DATA_ALLOC_ERR 1 /**/
#define SORT_ALLOC_ERR 2 /* */
#define MIN_DATA_VALUE 2 /**/
#define MAX_DATA_VALUE 100 /**/
#define QUIT 0 /**/
/************************************************** ********************/
/* Function Prototypes */
/************************************************** ********************/
void print_heading();
/* Print the program heading and instructions for the user */
void print_instructions();
/**/
int get_quantity();
/* */
void get_data(float *p_data_start, int quantity);
/* */
void sort_data(float *p_data_start, int quantity);
/* */
void print_data(float *p_data_start, int quantity);
/* */
float sum_data(float *p_data_start, int quantity);
/* */
void print_total(float sum_data);
/* */
/************************************************** ********************/
/* Main Function */
/************************************************** ********************/
int main()
{
int quantity;
float *p_data_start;
print_heading();
while(print_instructions(), (quantity = get_quantity()) != QUIT)
{
if((p_data_start = (float*) malloc(sizeof(*p_data_start) * quantity)) == NULL)
{
printf("\nError number %d occurred", DATA_ALLOC_ERR);
printf("\nMalloc was not able to allocate sufficient memory for Data Array.");
printf("\nThe program is now aborting!");
printf("\n\n\n");
exit(DATA_ALLOC_ERR);
}
get_data(p_data_start, quantity);
sort_data(p_data_start, quantity);
print_data(p_data_start, quantity);
print_total(sum_data(p_data_start, quantity));
free(p_data_start);
}
printf("\nGoodbye");
return 0;
}
/************************************************** ********************/
/* Print the program heading */
/************************************************** ********************/
void print_heading()
{
printf("\n\n\n\n\n\n");
printf("\n======================================== ================");
printf("\n Program Number: %d", PROGRAM_NUMBER);
printf("\n Programmer: %s", PROGRAMMER_NAME);
printf("\n PCC Course Number: %s", COURSE_NUMBER);
printf("\n======================================== ================");
return;
}
/************************************************** ********************/
/* */
/************************************************** ********************/
void print_instructions()
{
printf("\n This program processes experimental scientific data");
return;
}
/************************************************** ********************/
/* */
/************************************************** ********************/
int get_quantity()
{
int number_of_values;
do
{ printf("%d", number_of_values);
printf("\nHow many data values are there (2 to 100, 0 = quit)");
scanf("%d", &number_of_values);
}while(number_of_values < MIN_DATA_VALUE ||
number_of_values > MAX_DATA_VALUE);
return number_of_values;
}
/************************************************** ********************/
/* */
/************************************************** ********************/
void get_data(float *p_data_start, int quantity)
{
float *p_array;
for(p_array = p_data_start; (p_array - p_data_start) < quantity; p_array++)
{
printf("\nEnter data value %d:", p_array - p_data_start + 1);
scanf("%f", p_data_start);
if(*p_data_start < 0)
*p_data_start = - (*p_data_start);
}
return;
}
/************************************************** *********************/
/* */
/************************************************** *********************/
void sort_data(float *p_data_start, int quantity)
{
float *p_sort_start,
*p_data,
*p_sort,
*p_largest_value;
if((p_sort_start = (float*) malloc(sizeof(*p_sort_start) * quantity)) == NULL)
{
printf("\nError number %d occurred", SORT_ALLOC_ERR);
printf("\nMalloc was not able to allocate sufficient memory for Sort Array.");
printf("\nThe program is now aborting!");
printf("\n\n\n");
exit(SORT_ALLOC_ERR);
}
for(p_sort = p_sort_start; (p_sort - p_sort_start) < quantity; p_sort++)
{
for(p_data = p_data_start; (p_data - p_data_start) < quantity; p_data++)
{
if(*p_data > *p_largest_value)
p_largest_value = p_data;
}
*p_sort = *p_largest_value;
*p_largest_value = 0.0f;
}
memcpy(p_sort_start, p_data_start, sizeof(*p_data_start) * quantity);
free(p_sort_start);
return;
}
/************************************************** *********************/
/* */
/************************************************** *********************/
void print_data(float *p_data_start, int quantity)
{
float *p_array;
printf("\nThe date in descending order (with duplicates noted):");
printf("\n-----------------------------------------------------");
for(p_array = p_data_start; (p_array - p_data_start) < quantity; p_array++)
{
printf("\n%9.2f", *p_array);
if(*p_array != *p_data_start && *p_array == *(p_array - 1))
printf(" (Duplicate)");
}
return;
}
/************************************************** *********************/
/* */
/************************************************** *********************/
float sum_data(float *p_data_start, int quantity)
{
float *p_data,
sum = 0;
for(p_data = p_data_start; (p_data - p_data_start) < quantity; p_data++)
sum += *p_data;
return sum;
}
/************************************************** *********************/
/* */
/************************************************** *********************/
void print_total(float sum_data)
{
printf("\n---------");
printf("\n%9.2f", sum_data);
return;
}