Please help debug this lab! Please!
Hey Guys,
Its me..yet again! However, this time I have nearly finished my lab but need help with some debugging. It would be so so appreciated.. Thank you in advance..It starts off with what is required off the lab and then the program itself.
Hope you guys can help. Thanks
Mack
/************************************************** ******************************
** CIS 15 BG-03
** WINTER, 2002
***************
**
** Lab 4 Pointer Applications 20 Points
**
*************************************************
Write a program that reads the size of an array from the keyboard.
The array is to be dynamically allocated in the heap.
Then the program reads integers from the keyboard into the array.
Modify the straight insertion sort from Chapter 8 so that it sorts
the array into ascending and descending order.
The program must not change the original array. Do not create any other
integer arrays. (See figure 10-25, page 515).
Print the sorted arrays and the original array to a file, using the
following format:
========== =========== ============
ORIGINAL ASCENDING DESCENDING
========== =========== ============
50 20 100
70 50 70
20 50 70
100 70 70
70 70 50
50 70 50
70 100 20
Finally, determine and print the frequency histogram to screen,
as it is shown below:
20: 1 *
50: 2 **
70: 3 ***
100: 1 *
Data : Run your program twice:
Test Data Set 1:
10 here you enter the size of the array
10 10 30 30 40 50 50 50 60 60
Test Data Set 2:
25
50 30 10 90 5 20 25 90 40 35 50 30 50 30 90 5 40 50 90 30 10 5 20 10 35
NOTE: Use pointer notation throughout. Do not use an index to access the
elements in the array: use a pointer, as in the following example:
__________________________________________________ __________________
YES
int ary[10] = {10, 15, 10, 30, 40, 50, 60, 70, 80, 90 };
int *pWalk;
int *pLast;
pLast = ary + 10 - 1;
for( pWalk = ary; pWalk <= pLast; pWalk++ )
printf( "%3d", *pWalk );
__________________________________________________ ___________________
NO
int ary[10] = {10, 15, 10, 30, 40, 50, 60, 70, 80, 90 };
int i;
for( i = 0; i < 10; i++ )
printf( "%3d", *(ary + i) );
****************************************
**
** Written By:
**
** Date: 2/6/02 - 02/19/02
**
************************************************** ******************************/
/* This program builds an array with size based on the users input.
The user then fills the array with numbers from the keyboard.
The program sorts the array and print the array sorted and
unsorted using pointers. The user does this twice two set up
and fill two array. The program merges the two arrays into one
array and then prints it to file.
LAB# 5
*/
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
/* Prototype Declaration */
int *buildAry (int *size);
void fillAry (int *ptr,
int *plast);
void ascendSort(int *orgAry,
int *sortAry,
int *plast);
void descendSort(int *orgAry,
int *sortAry,
int *plast);
FILE* writeAll (sAry,
ascendSort,
descendSorty,
size);
void makeFrequency (int *pOrg,
int frequency[],
int size);
void makeHistogram(int freq[],
int range);
int main (void)
{
/* Local Declaration */
int *ptr;
int size = 0;
int *plast;
int *sAry;
int frequency[50];
int *freq;
/* Statements */
[ptr = buildAry(&size);
plast = ptr + size - 1;
fillAry (ptr, plast);
ascendSort(ptr, sAry, plast);
descendSort(ptr, sAry, plast);
writeAll (ptr, ascendSort, descendSort);
makeFrequency (sAry, frequency, size);
makeHistogram(freq, size);/]
return 0;
}/* main */
/*------------------------ buildAry ---------------------------------------
This function read size of array from users keyboard. It then
allocates memory with calloc based on the users input.
Pre pointer named prt
Post memory allocated for the array with the first element indexed to
the pointer ptr.
*/
int *buildAry(int *size)
{
/* Local Declaration */
int *ptr;
/* Statements */
printf("Please enter the number of integers in your array.");
scanf ("%d", size);
if(!(ptr = (int*) calloc( (unsigned int) (size + 1), sizeof(int))));
return ptr;
}/* buildAry */
/*------------------------ fillAry ---------------------------------------
This function fill the array with data read from the keyboard.
Pre empty ary with ptr pointing to the first element.
post filled ary from users keyboard entries.
*/
void fillAry (int *ptr, int *plast)
{
/* Local Declaration */
int *fil = ptr;
/* Statements */
while (fil <= plast)
scanf("%d", fil++);
return;
}/* fillAry */
/*------------------------ ascendSort --------------------------------------
This fuction uses pointer to sort the original array without changing
the original array.
Pre
Post
*/
void ascendSort(int *orgAry, int *sortAry, int *plast)
{
/* Local Declaration */
int *pOrg;
int *pWalk;
int located;
int temp;
/* Statements */
for (pOrg = orgAry + 1; pOrg <= plast; pOrg++)
{
located = FALSE;
temp = *pOrg;
for(pWalk = pOrg - 1; pWalk >= orgAry && !located
if(temp < *pWalk)
{
*(pWalk +1) = *pWalk;
pWalk--;
}/* if */
else
located = TRUE;
*(pWalk + 1) = temp;
}/* for pOrg */
sortAry = pOrg - 1;
return;
}/* inserSort */
/*------------------------ descendSort --------------------------------------
This fuction uses pointer to sort the original array without changing
the original array.
Pre
Post
*/
void descendSort(int *orgAry, int *sortAry, int *plast)
{
/* Local Declaration */
int *pOrg;
int *pWalk;
int located;
int temp;
/* Statements */
for (pOrg = plast; pOrg <= plast; pOrg)
{
located = FALSE;
temp = *pOrg;
for(pWalk = pOrg - 1; pWalk >= orgAry && !located
[if(temp > *pWalk)
{
*(pWalk +1) = *pWalk;
pWalk++;
}/* if */
else
located = TRUE;
*(pWalk + 1) = temp;
}/* for pOrg */
sortAry = pOrg - 1;
return;]
}/* inserSort */
/******************************writeArry*********** ***************************
Prints the arrays to a file as a table
PRE : arry - a filled array
size - number of elements in array
POST: arry printed
************************************************** ****************************/
[FILE* writeAll (ptr,
sAry,
sAry,
size)
{
/* Local Definitions */
FILE *fpnum;
int r = 0;
/* Statements */
if ((fpnum = fopen ("LAB4_OUTPUT.TXT", "w")) == NULL)
{
/* Lets user know that there is nothing being written to file */
printf("\a ERROR writing to LAB4_OUTPUT.TXT\n");
exit (100);
}
if( size == 0 )
fprintf( fpnum, "This array is empty!\n" );
else
{
fprintf(fpnum,"========== =========== ============\n");
fprintf(fpnum," ORIGINAL ASCENDING DESCENDING");
fprintf(fpnum,"========== =========== ============\n");
fprintf(fpnum,"%4d %4d %4d\n", ptr, sAry, sAry);
return fpnum;
}
}
/*------------------------- makeFrequency --------------------------------------
Analyze the data in array and builds their frequency distribution
Pre nums: array of validated data to be analyzed
last: number of elements in array
frequency: array for accumulation
range: maximum index/value for frequency
Post Frequency array has been built
*/
void makeFrequency (int *orgAry,
int frequency[],
int size)
{
/* Local Definitions */
int pWalk;
int i;
/* Statements */
/* First initialize the frequency array */
for (pWalk = 0; pWalk < size; pWalk++)
frequency[pWalk] = 0;
for (i=0; i < size; i++)
frequency[*orgAry]++;
return;
} /*makeFrequency*/
/*----------------------------makeHistogram---------------------------------------
Print a histogram representing analyzed data
Pre freq contains times each value occurred in data
size represents elements in frequency array
Post histogram has been printed
*/
void makeHistogram(int freq[],
int size)
{
/* Local Definitions */
int i;
int j;
int *pOrg;
/* Statements */
for (i=0; i < range; i++)
{
printf("%2d %2d ", pOrg, freq[i]);
for (j=1; j<= freq[i]; j++)
printf("*");
printf("\n");
} /* for i..*/
return;/]
} /* makeHistogram */