#include <stdio.h> // for printf(), scanf(), putchar()
void printd(int); // print int in decimal
int main()
{
int n;
printf("Type an integer, [+-]?[0-9]+\n");
scanf("%d", &n);
if (n < 0) {putchar('-');}
printd(n);
putchar('\n');
return 0;
}
void printd(int n) // print n in decimal
{
int sign = 1;
if (n < 0) {sign = -1;}
if (n / 10) // if (n / 10 != 0)
{printd(n / 10);} // recursive call
putchar((n % 10) * sign + '0');
}
/*
gcc printd.c -o printd
./printd
Type an integer, [+-]?[0-9]+
0
0
./printd
Type an integer, [+-]?[0-9]+
+0
0
./printd
Type an integer, [+-]?[0-9]+
-0
0
./printd
Type an integer, [+-]?[0-9]+
123
123
./printd
Type an integer, [+-]?[0-9]+
+123
123
./printd
Type an integer, [+-]?[0-9]+
-123
-123
./printd
Type an integer, [+-]?[0-9]+
-1
-1
./printd
Type an integer, [+-]?[0-9]+
2147483647 // INT_MAX // limits.h
2147483647
./printd
Type an integer, [+-]?[0-9]+
-2147483648 // INT_MIN
-2147483648
./printd
Type an integer, [+-]?[0-9]+
2147483648 // INT_MAX + 1 (overflow)
-2147483648 // INT_MIN // modulo 2 arithmetic
./printd
Type an integer, [+-]?[0-9]+
2147483649 // INT_MAX + 2 (overflow)
-2147483647 // INT_MIN + 1
./printd
Type an integer, [+-]?[0-9]+
-2147483649 // INT_MIN - 1 (underflow)
2147483647 // INT_MAX
./printd
Type an integer, [+-]?[0-9]+
-2147483650 // INT_MIN - 2 (underflow)
2147483646 // INT_MAX-1
*/
We print the minus sign in main() to be able to use negative numbers inside printd() or else we could not treat the case of overflow (and print the integer limit INT_MIN). See also Exercise_3-4.
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), putchar()
#include <stdlib.h> // for srand(), rand()
#include <time.h> // for time_t, time()
#define SIZE 100 // array size
// sort v[left] ... v[right] into increasing order:
void qSort (int v[], int left, int right); // qsort() in declared in stdlib.h
int main()
{
int array[SIZE];
int i;
time_t t;
srand((unsigned) time(&t)); // seed for rand()
for (i = 0; i < SIZE; i++)
{array[i] = rand() % 1000;} // initialize array[]
printf("Unsorted:\n");
for (i = 0; i < SIZE; i++)
{
printf("%d%c", array[i], (i % 10) == 9 ? '\n' : ' ');
}
putchar('\n');
qSort(array, 0, SIZE-1);
printf("Sorted:\n");
for (i = 0; i < SIZE; i++)
{
printf("%d%c", array[i], (i % 10) == 9 ? '\n' : ' ');
}
return 0;
}
void swap(int v[], int i, int j); // swap v[i] and v[j]
// sort v[left] ... v[right] into increasing order:
void qSort (int v[], int left, int right)
{
int i, last;
if (left >= right) // do nothing if array contains fewer that
{return;} // two elements (stopping condition for recursion)
swap(v, left, (left + right) / 2); // move partition element to v[left]
last = left; // v[left] is now in the middle of v[left] ... v[right]
for (i = left+1; i <= right; i++)
{ // value of `left' does not change here
if (v[i] < v[left]) // compare v[left] ... v[right] to partition element
{swap(v, ++last, i);} // smaller to the left, bigger to the right
}
swap(v, left, last); // restore partition element
qSort(v, left, last-1); // recursive call (sort first half)
qSort(v, last+1, right); // recursive call (sort last half)
}
void swap(int v[], int i, int j) // swap v[i] and v[j]
{
int temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
/*
gcc qsort.c -o qsort
./qsort
Unsorted:
454 449 499 215 79 630 447 413 455 926
794 606 308 651 272 157 564 34 613 649
472 824 563 278 883 472 50 100 849 39
217 655 488 716 222 567 699 669 333 154
947 127 112 256 130 736 765 46 770 378
696 243 202 611 873 86 83 924 186 284
963 755 939 803 824 513 722 523 535 407
29 482 534 493 90 17 230 856 63 0
234 111 595 789 722 469 227 805 745 413
441 60 520 732 215 344 246 289 219 781
Sorted:
0 17 29 34 39 46 50 60 63 79
83 86 90 100 111 112 127 130 154 157
186 202 215 215 217 219 222 227 230 234
243 246 256 272 278 284 289 308 333 344
378 407 413 413 441 447 449 454 455 469
472 472 482 488 493 499 513 520 523 534
535 563 564 567 595 606 611 613 630 649
651 655 669 696 699 716 722 722 732 736
745 755 765 770 781 789 794 803 805 824
824 849 856 873 883 924 926 939 947 963
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 4-12. Adapt the ideas of printd (see printd.c above) to write a recursive version of itoa (Exercise_3-4); that is, convert an integer into a string by calling a recursive routine.
#include <stdio.h> // for printf(), scanf()
#define SIZE 100 // array size (no of digits)
// recursive itoa():
int itoa(int , char []); // int to string (char array), return length
int main()
{
int i, n;
char array[SIZE];
printf("Type an integer, [+-]?[0-9]+\n");
scanf("%d", &n);
if (n < 0) {array[0] = '-';}
i = itoa(n, array);
array[i] = '\0';
printf("%s\n", array);
return 0;
}
// int n to string (char array), recursive itoa():
int itoa(int n, char s[]) // start writing at position pos in array s[]
{ // return last position written (length of s[])
int sign = 1;
int pos = 0; // position
if (n < 0)
{
sign = -1;
pos = 1;
}
if (n / 10) // if (n / 10 != 0)
{pos = itoa(n / 10, s);} // recursive call
s[pos++] = (n % 10) * sign + '0';
return pos;
}
/*
gcc itoar.c -o itoar
./itoar
Type an integer, [+-]?[0-9]+
0
0
./itoar
Type an integer, [+-]?[0-9]+
+0
0
./itoar
Type an integer, [+-]?[0-9]+
-0
0
./itoar
Type an integer, [+-]?[0-9]+
123
123
./itoar
Type an integer, [+-]?[0-9]+
+123
123
./itoar
Type an integer, [+-]?[0-9]+
-123
-123
./itoar
Type an integer, [+-]?[0-9]+
-2147483648 // INT_MIN // limits.h
-2147483648
./itoar
Type an integer, [+-]?[0-9]+
-2147483649 // INT_MIN-1 (underflow)
2147483647 // INT_MAX
./itoar
Type an integer, [+-]?[0-9]+
2147483647 // INT_MAX
2147483647
./itoar
Type an integer, [+-]?[0-9]+
2147483648 // INT_MAX+1 (overflow)
-2147483648 // INT_MIN
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strlen()
#define SIZE 100 // array size
void reverse(char s[], int i, int j); // reverse a string from s[i] to s[j]
int main()
{
char array[SIZE];
int len; // array length
printf("Type a string:\t");
scanf("%s", array);
len = strlen(array);
reverse(array, 0, len-1);
printf("Reversed:\t%s\n", array);
return 0;
}
void reverse(char s[], int i, int j) // reverse a string from s[i] to s[j]
{
int temp;
if (i < j)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
reverse(s, i, j);
}
}
/*
gcc reverse.c -o reverse
./reverse
Type a string: a
Reversed: a
./reverse
Type a string: ab
Reversed: ba
./reverse
Type a string: abc
Reversed: cba
./reverse
Type a string: 123456
Reversed: 654321
*/