I added some code to print the values in the array month into a random access file. Again when you enter in any values, it returns rubbish to the screen, and to the file.
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
void bubble(int *, const int, int (*)(int, int)); /* bubble sort */
void swap(int *, int *);
int sortAscending(const int, const int);/* sorting items in ascending order*/
int main(void)
{
int months[SIZE];
//= {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
int counter;
FILE *cfPtr;
if((cfPtr = fopen("phonebill.dat", "w")) == NULL)
printf("The file could not be opened!\n");
else {
printf("Enter your monthly phone bills for each month of the year\n");
scanf("%d", &months);
bubble(months, SIZE, sortAscending);
for (counter = 0; counter <= SIZE - 1; counter++)
{
fprintf(cfPtr, "%4d\n", months[counter]);
}
fclose(cfPtr);
/* prints out the first value */
for (counter = 0; counter <= 1 - 1; counter++)
{
printf("%4d\n", months[counter]);
}
/* prints out the last value */
for (counter = 0; counter <= SIZE; counter++)
if (counter == SIZE -1)
{
printf("%4d", counter[months]);
}
printf("\n");
}
return 0;
}
/* sorts the array months */
void bubble(int *work, const int size, int (*compare)(int, int))
{
int pass, count;
void swapt(int *, int *);
for (pass = 1; pass <= size - 1; pass++)
for (count = 0; count <= size - 2; count++)
if ((*compare) (work[count], work[count + 1]))
swap(&work[count], & work[count + 1]);
}
void swap(int *element1Ptr, int *element2Ptr)
{
int temp;
temp = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = temp;
}
/* sorts the array months into ascending order (smallest to largest) */
int sortAscending(const int a, const int b)
{
return b < a;
}