$ submit 2031 lab4 q6.c q7.c
#define MAX the-rest-of-the-linewould cause the token MAX to be replace by the-rest-of-the-line in the source code before it is passed on to the next stage of compiling.
#define MAX 255
#define MIN the rest of the line
int main(void){
int xMAX = MAX;
char A[MIN];
...
return 0;
}
Output of preprocessor (Use cc -E or cpp):
int main(void){
int xMAX = 255 ;
char A[ the rest of the line ];
...
return 0;
}
Note that the #define statements have disappeared and that the preprocessor
did not object to the ... in the middle of the program.
http://www.gnu.org/software/binutils/
There is a link to documentation near the bottom of the page.
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int x;
int y;
/* some code to assign x and y values */
x = 256;
y = 257;
/* print out x and y before the swap */
printf("Before swap: x = %d y = %d\n", x, y);
/* swap values of x and y */
swap(&x, &y);
/* print out x and y after the swap */
printf(" After swap: x = %d y = %d\n", x, y);
return 0;
}
Copy, compile and run this program.
How would you have to change this program if x and y were declared
as char*** instead of int?
First consider the program with "int" replaced with "TYPE" where appropiate (4 places).
You would get
#include <stdio.h>
void swap(TYPE *a, TYPE *b)
{
TYPE temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
TYPE x;
TYPE y;
/* some code to assign x and y values */
x = 256;
y = 257;
/* print out x and y before the swap */
printf("Before swap: x = %d y = %d\n", x, y);
/* swap values of x and y */
swap(&x, &y);
/* print out x and y after the swap */
printf(" After swap: x = %d y = %d\n", x, y);
return 0;
}
Of course, you may have to change the lines that assign x and y values,
since the assignment given may not be appropriate for whatever TYPE is.
And in the printf statements you may have to replace the %d with something
else if TYPE isn't an int. (To print a pointer you use %p.)
swap(&x, &y);Finally, consider the program when you replace TYPE with char***.
#include <stdlib.h>
#include <stdio.h>
/* prototype for the qsort function */
/*
This prototype is not necessary here since stdlib.h is #included.
But it's here so you can see it and refer to it.
*/
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
/* a comparison function for sorting into non-descending order */
/*
make sure you understand how intCompare1 starts with
void * pointers p1 and p2 and gets int values for x and y from them.
*/
int intCompare1(const void * p1, const void* p2) {
int x = *((int *)p1);
int y = *((int *)p2);
if (x < y) return -1;
if (x > y) return 1;
return 0;
}
/* a comparison function for sorting into non-increasing order */
/*
intCompare2 has a formal parameter list different from the comparison
function compar in the qsort prototype. What to do ...? (Don't change it!)
*/
int intCompare2(int * p1, int* p2) {
int x = *p1;
int y = *p2;
if (x < y) return 1;
if (x > y) return -1;
return 0;
}
int main(void)
{
int i;
int A[] = {2, 4, 9, 7, 9, 6, 4, 2, 5};
int B[] = {5, 7, 8, 10, 22, 0, 2};
/* print A */
printf("A before sorting: ");
for(i = 0; i < sizeof(A)/sizeof(int); i++)
printf("%d ", A[i]);
printf("\n");
/* sort the last 4 elements of A into non-decreasing order using intCompare1*/
/* don't hardcode length of A */
/* ... */
/* print A again */
printf("A after sorting: ");
for(i = 0; i < sizeof(A)/sizeof(int); i++)
printf("%d ", A[i]);
printf("\n");
/* print B */
printf("\nB before sorting: ");
for(i = 0; i < sizeof(B)/sizeof(int); i++)
printf("%d ", B[i]);
printf("\n");
/* sort all of B into non-increasing order using intCompare2 */
/* don't hardcode length of B */
/* ... */
/* print B again */
printf("B after sorting: ");
for(i = 0; i < sizeof(B)/sizeof(int); i++)
printf("%d ", B[i]);
printf("\n");
return 0;
}