PDA

View Full Version : C NEED HELP plz


neyugn
28 Oct 2004, 6:46pm
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 */

Kwitko
28 Oct 2004, 7:00pm
We can't do your homework for you, dude.

Justin
28 Oct 2004, 8:37pm
C:/Format C...... j/k, I have no idea what all that is...

TheBaron
28 Oct 2004, 9:01pm
dude it looks like you know what you're doing, the majority of your code looks good. figure the problem out yourself, else you aren't really learning anything

neyugn
28 Oct 2004, 9:33pm
nah i thought i wasnt doin it right and i told my friend to check it and he said i was doing it wrong so ya.
thnks for ur help

neyugn
29 Oct 2004, 1:24am
IM gettin 3 warnings and when it prints into the txt file its junk can ne one help me on this plz?

/********************************************************************************
** 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 */

neyugn
29 Oct 2004, 1:27am
the warnings are
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

Straight_Man
29 Oct 2004, 3:21am
Variable names are extremely case sensitive , especially if you are using a STRICT compiler setting (should be for learning, it will enforce the rules so that they become INGRAINED) and variables of type array need specific intialization as arrays. Arrays cannot be initialized as simple variables and then used multidimensionally, either. They need to be initialized using the number of dimensions they will have in use. IF the above were to be not checked, you could have a program memory leak and violate the spaces assigned then it runs, crashing another app. For a 3D array, initialize in three dimensions, etc.

That is all I am gonna say.

neyugn
29 Oct 2004, 4:19am
thnks for ur help