C NEED HELP plz
I get no errors but i dont know how to do the sorting portion
/********************************************************************************
** CIS 15 BG-03
** Fall, 2004
***************
**
** 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 order. (Do not sort the array in
descending order.)
The program must not change the original array and must not create a
copy of it. (Hint: See figure 10-25, page 515).
Print the sorted array 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
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:
/*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#define SIZE 100
#define TRUE 1
#define FALSE 0
/* Prototype Declarations */
void welcome (void);
void getData (int *array, int *arraySize);
void sortArray (int *array, int arraySize);
void printArray (int *array, int arraySize);
void farewell (void);
int main (void)
{
/* Local Definitions */
int array[SIZE];
int arraySize;
/* Statements */
welcome ();
getData (array, &arraySize);
sortArray(array, arraySize);
printArray (array, arraySize);
printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n");
farewell ( );
return 0;
} /* main */
/* ============================== welcome ==============================
Prints a welcome message.
PRE : nothing
POST : welcome message printed
*/
void welcome (void)
{
/* Local Definitions */
/* Statements */
printf("\t\t LAB 4 - Pointer Applications\n\n");
return;
} /* welcome */
void getData (int *array, int *arraySize)
{
/* LD */
int i;
/* ST */
printf("Array Size: ");
scanf("%d", arraySize);
for (i = 0; i < *arraySize; ++i)
{
printf("\nValue: ", i, *arraySize);
scanf("%d", (array + i));
}
return;
}
void sortArray (int *array, int arraySize)
{
return;
}
void printArray (int *array, int arraySize)
{
/* LD */
FILE *fparray;
int i;
/* ST */
fparray = fopen("array.txt", "w");
if(!fparray)
{
printf("could not print to the output file\n");
}
fprintf (fparray, "========== =========== ============\n");
fprintf (fparray, " ORIGINAL ASCENDING DESCENDING\n");
fprintf (fparray, "========== =========== ============\n");
for (i = 0; i <= arraySize; i++)
{
if (i %3 != 0)
{
fprintf(fparray, "%6d %6d %6d", *(array + i));
}
else
{
fprintf(fparray, "\n");
fprintf(fparray, "%6d %6d %6d", *(array + i));
}
scanf("%d", i);
}
return;
}
/* farewell
Prints a farewell message.
PRE : nothing.
POST : farewell message printed
*/
void farewell (void)
{
printf("\n\t\tEnd of the program!"
"\n\t\tHave a great day!\n");
return;
} /* farewell */
/********************************************************************************
** CIS 15 BG-03
** Fall, 2004
***************
**
** 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 order. (Do not sort the array in
descending order.)
The program must not change the original array and must not create a
copy of it. (Hint: See figure 10-25, page 515).
Print the sorted array 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
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:
/*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#define SIZE 100
#define TRUE 1
#define FALSE 0
/* Prototype Declarations */
void welcome (void);
void getData (int *array, int *arraySize);
void sortArray (int *array, int arraySize);
void printArray (int *array, int arraySize);
void farewell (void);
int main (void)
{
/* Local Definitions */
int array[SIZE];
int arraySize;
/* Statements */
welcome ();
getData (array, &arraySize);
sortArray(array, arraySize);
printArray (array, arraySize);
printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n");
farewell ( );
return 0;
} /* main */
/* ============================== welcome ==============================
Prints a welcome message.
PRE : nothing
POST : welcome message printed
*/
void welcome (void)
{
/* Local Definitions */
/* Statements */
printf("\t\t LAB 4 - Pointer Applications\n\n");
return;
} /* welcome */
void getData (int *array, int *arraySize)
{
/* LD */
int i;
/* ST */
printf("Array Size: ");
scanf("%d", arraySize);
for (i = 0; i < *arraySize; ++i)
{
printf("\nValue: ", i, *arraySize);
scanf("%d", (array + i));
}
return;
}
void sortArray (int *array, int arraySize)
{
return;
}
void printArray (int *array, int arraySize)
{
/* LD */
FILE *fparray;
int i;
/* ST */
fparray = fopen("array.txt", "w");
if(!fparray)
{
printf("could not print to the output file\n");
}
fprintf (fparray, "========== =========== ============\n");
fprintf (fparray, " ORIGINAL ASCENDING DESCENDING\n");
fprintf (fparray, "========== =========== ============\n");
for (i = 0; i <= arraySize; i++)
{
if (i %3 != 0)
{
fprintf(fparray, "%6d %6d %6d", *(array + i));
}
else
{
fprintf(fparray, "\n");
fprintf(fparray, "%6d %6d %6d", *(array + i));
}
scanf("%d", i);
}
return;
}
/* farewell
Prints a farewell message.
PRE : nothing.
POST : farewell message printed
*/
void farewell (void)
{
printf("\n\t\tEnd of the program!"
"\n\t\tHave a great day!\n");
return;
} /* farewell */
0
Comments
thnks for ur help
/********************************************************************************
** CIS 15 BG-03
** Fall, 2004
***************
**
** 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 order. (Do not sort the array in
descending order.)
The program must not change the original array and must not create a
copy of it. (Hint: See figure 10-25, page 515).
Print the sorted array 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
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: Long Nguyen
**
** Date: 10/27/04
/*
********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>
#define SIZE 100
#define TRUE 1
#define FALSE 0
/* Prototype Declarations */
void welcome (void);
void getData (int *array, int *arraySize);
void sortArray (int *array, int arraySize);
void printArray (int *array, int arraySize);
int allocate(int **Ary, int ***arrayPointer);
void farewell (void);
int main (void)
{
/* Local Definitions */
int array[SIZE];
int arraySize;
int **Ary;
int ***arrayPointer;
/* Statements */
welcome ();
getData (array, &arraySize);
sortArray(array, arraySize);
printArray (array, arraySize);
arraySize = allocate(Ary, arrayPointer);
printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Leak\n");
farewell ( );
return 0;
} /* main */
/* ============================== welcome ==============================
Prints a welcome message.
PRE : nothing
POST : welcome message printed
*/
void welcome (void)
{
/* Local Definitions */
/* Statements */
printf("\t\t LAB 4 - Pointer Applications\n\n");
return;
} /* welcome */
void getData (int *array, int *arraySize)
{
/* LD */
int i;
/* ST */
printf("Array Size: ");
scanf("%d", arraySize);
for (i = 0; i < *arraySize; ++i)
{
printf("\nValue: ", i, *arraySize);
scanf("%d", (array + i));
}
return;
}
int allocate(int **Ary, int ***arrayPointer)
{
/* LD */
int sizearray;
/* ST */
while(sizearray <= 0);
if(!(*arrayPointer = (int **)calloc(sizearray, sizeof(int))))
printf("Memory is not available");
else
printf("Memory is available");
return sizearray;
}
void sortArray (int *array, int arraySize)
{
/* LD */
int walker;
int located;
int temp;
/* ST */
located = FALSE;
temp = array[arraySize];
for(walker = arraySize - 1; walker >= 0 && !located;)
if(temp < array[walker])
{
array[walker + 1] = array[walker];
walker--;
}
else
located = TRUE;
array [walker + 1] = temp;
return;
}
void printArray (int *array, int arraySize)
{
/* LD */
FILE *fpOut;
/* ST */
fpOut = fopen("array.txt", "w");
if(!fpOut)
{
printf("could not print to the output file\n");
}
fprintf(fpOut, "========== =========== ============\n");
fprintf(fpOut, " ORIGINAL ASCENDING DESCENDING\n");
fprintf(fpOut, "========== =========== ============\n");
fprintf(fpOut, "%6d %6d %6d", *(array));
fprintf(fpOut, "\n");
fprintf(fpOut, "%6d %6d %6d", *(array));
fclose(fpOut);
return;
}
/* ============================== farewell ==============================
Prints a farewell message.
PRE : nothing.
POST : farewell message printed
*/
void farewell (void)
{
printf("\n\t\tEnd of the program!"
"\n\t\tHave a great day!\n");
return;
} /* farewell */
Z:\BG_Lab_4\BG_Lab4.c(112) : warning C4700: local variable 'arrayPointer' used without having been initialized
Z:\BG_Lab_4\BG_Lab4.c(112) : warning C4700: local variable 'Ary' used without having been initialized
Z:\BG_Lab_4\BG_Lab4.c(159) : warning C4700: local variable 'sizearray' used without having been initialized
That is all I am gonna say.