Fedora Linux Support Community & Resources Center
  #1  
Old 10th October 2008, 01:55 AM
northernfalcon Offline
Registered User
 
Join Date: Sep 2008
Posts: 8
Please Help--Sorting of Arrays using Pointers (C language)

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;
}
Reply With Quote
  #2  
Old 10th October 2008, 06:00 AM
nick.stumpos's Avatar
nick.stumpos Offline
Registered User
 
Join Date: Feb 2005
Location: Lansing, Mi
Age: 28
Posts: 2,222
ill give it a look if i get some time later
__________________
As always
Love, Life, Loyalty, Wisdom, Knowledge, And Understanding
FC6: Common Questions answered
Reply With Quote
  #3  
Old 10th October 2008, 08:03 PM
nick.stumpos's Avatar
nick.stumpos Offline
Registered User
 
Join Date: Feb 2005
Location: Lansing, Mi
Age: 28
Posts: 2,222
i have no idea why you would implement this like you did, but fixing a ton of errors, and simplifing some stuff gives
Code:
#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();
  print_instructions();
  quantity = get_quantity();
  while(quantity != QUIT)
  {
    if( (p_data_start = (float*) malloc( (sizeof(float)*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);
    }
    *p_data_start=0.0f;
    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));
    print_instructions();	
    quantity = get_quantity();
  }
  free(p_data_start);
  printf("\nGoodbye\n");
  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);
    if(number_of_values==0)break;
  }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;
  p_array=p_data_start;
  int i;
  for(i=1;i <= quantity; i++)
  {
     printf("\nEnter data value %d:", i);
     scanf("%f", p_array);
     if(*p_array < 0.0f) *p_array = - (*p_array);
     p_array=p_array+sizeof(float);
  }
  return;
}

/************************************************** *********************/
/* */
/************************************************** *********************/
void sort_data(float *p_data_start, int quantity)
{
  float *p_sort_start, *p_data, *p_sort, * p_largest_value;
  int i,j;
  float temp;
  p_data=p_data_start;
  for(i=0; i < quantity-1; i++)
  {
    p_largest_value=p_data;
    p_sort=p_data+sizeof(float);
    for(j=i+1; j < quantity; j++)
    {
      if((*p_sort) > (*p_largest_value)){
        temp=*p_largest_value;
        *p_largest_value=*p_sort;
	*p_sort=temp;
      }
      p_sort=p_sort + sizeof(float);
    }
    p_data=p_data + sizeof(float);
  }
  return;
}

/***********************************************************************/
/* */
/***********************************************************************/
void print_data(float *p_data_start, int quantity)
{
  float *p_array;
  p_array=p_data_start;
  printf("\nThe date in descending order (with duplicates noted):");
  printf("\n-----------------------------------------------------");
  int i;
  for(i=0; i< quantity; i++)
  {
     printf("\n%9.2f", *p_array);
     if(*p_array != *p_data_start && *p_array == *(p_array - sizeof(float)))
     printf(" (Duplicate)");
     p_array=p_array+sizeof(float);
  }
return;
}

/************************************************** *********************/
/* */
/************************************************** *********************/
float sum_data(float *p_data_start, int quantity)
{
  float *p_data,
  sum = 0;
  p_data=p_data_start;
  int i;
  for(i=0; i < quantity; i++){
    sum += *p_data;
    p_data=p_data+sizeof(float);
  }
  return sum;
}

/************************************************** *********************/
/* */
/************************************************** *********************/
void print_total(float sum_data)
{
  printf("\n---------");
  printf("\n%9.2f", sum_data);
  return;
}
does this work for you?
__________________
As always
Love, Life, Loyalty, Wisdom, Knowledge, And Understanding
FC6: Common Questions answered

Last edited by nick.stumpos; 10th October 2008 at 08:06 PM. Reason: weird spaceing
Reply With Quote
  #4  
Old 23rd October 2008, 07:45 PM
Aaron222 Offline
Registered User
 
Join Date: Oct 2008
Posts: 41
Pointers used to be my weekest point in school and implemnting linked list was the horrible dream... Now, I live pointers...

Linux Archive

Last edited by Aaron222; 9th November 2008 at 10:31 AM.
Reply With Quote
Reply

Tags
arrays, helpsorting, language, pointers

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP arrays... fnord Programming & Packaging 12 5th December 2005 09:59 PM
Arrays in C eggsy85 Programming & Packaging 14 23rd April 2005 05:36 AM
Semaphore Arrays LackeyLad Programming & Packaging 2 5th November 2004 05:51 AM


Current GMT-time: 21:53 (Saturday, 25-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat