C Programming/Exercise solutions

Into main {       Into ans; Ans=a*a*a; Printf("%d",c) ; Return 0; }
 * 1) include
 * 2) define a5+2

Naming
3. Give an example of a C variable name that would not work. Why doesn't it work?


 * No, the name of a variable must begin with a letter (lowercase or uppercase), or an underscore.
 * 1) Only the underscore can be used.
 * 2) for example, #nm*rt is not allowed because # and * are not the valid characters for the name of a variable.

main {   int a, b, c, max; clrscr; printf("\n enter three numbers "); scanf(" %d %d %d ",&a,&b,&c); max = a;   if(max < b)        max = b;    if(max < c)        max = c;    printf("\n largest=%d \n",max); getch; }
 * 1) include 

Data Types
1.1. On your computer, how much memory does each require?
 * 3 data types : long int, short int,float.
 * On my computer :
 * long int : 4 bytes
 * short int : 2 bytes
 * float : 4 bytes
 * we can not use 'int' or 'float' as a variable's name.

Assignment

 * The standard way of assigning 3.14 to pi is:
 * Since pi is a constant, good programming convention dictates to make it unchangeable during runtime. Extra credit if you use one of the following two lines:
 * Yes, for example :
 * Yes, but a cast is necessary and the double is truncated:

Referencing

 * 1) The reverse,   is a valid C statement if   is not a constant and   is initialized.
 * 2) a.  b. The reverse:   is not valid since it is impossible to assign a value to a literal.
 * 1) a.  b. The reverse:   is not valid since it is impossible to assign a value to a literal.

String manipulation
One possible solution could be:

Loops
One possible solution:

One possible solution:

One possible solution:

or like this (all math)

One possible solution:

Another solution:

Math
Another better solution that doesn't need to include math.h and faster than the one above.

Merge sort
One possible solution, after reading online descriptions of recursive merge sort, e.g. Dasgupta :

Binary heaps

 * 10, 4, 6, 3, 5, 11 -> 10
 * 4, 6,3, 5, 11 -> 10, 4  : 4 is end-added, no swap-parent because 4 < 10.
 * 6, 3, 5, 11 ->  10, 4, 6  : 6 is end-added, no swap-parent because 6 < 10.
 * 3, 5, 11 -> 10, 4, 6, 3 : 3 is end-added, 3 is position 4, divide by 2 = 2, 4 at position 2, no swap-parent because 4 > 3.
 * 5, 11 -> 10, 4, 6, 3 , 5 : 5 is end-added , 5 is position 5, divided by 2 = 2, 4 at position 2, swap-parent as 4 < 5; 5 at position 2, no swap-parent because 5 < 10 at position 1.

- 10, 5, 6, 3, 4


 * 11 -> 10, 5, 6, 3, 4, 11 : 11 is end-added, 11 is position 6, divide by 2 = 3, swap 6 with 11, 11 is position 3, swap 11 with 10, stop as no parent.

- 11, 5, 10, 3, 4, 6

- 11 has children 5, 10 ; 5 has children 3 and 4 ; 10 has child 6. Parent always > child.


 * 11 leaves *, 5, 10, 3, 4, 6 -> 6 , 5, 10, 3, 4 -> sift-down -> choose greater child 5 (2*n+0) or 10 ( 2*n+1) -> is 6 > 10 ? no -> swap 10 and 6 ->

- 10, 5, *6, 3, 4 -> 4 is greatest child as no +1 child. is 6 > 4 ? yes, stop.


 * 10 leaves *, 5 , 6 , 3, 4 -> *4, 5, 6, 3 -> is left(0) or right(+1) child greater -> +1 is greater; is 4 > +1 child ? no , swap

- 6,5, *4, 3  -> *4 has no children so stop.

is
 * 6 leaves *, 5, 4, 3 -> *3, 5, 4 -> +0 child is greater -> is 3 > 5 ? no, so swap -> 5, *3, 4 , *3 has no child so stop.
 * 5 leaves so 3, 4 -> *4, 3 -> +0 child greatest as no right child  -> is 4 > 3 ? no, so exit


 * 4 leaves 3.


 * 3 leaves *.


 * numbers extracted in descending order 11, 10, 6, 5, 4, 3.

Quick sort
One possible solution, can be to adapt this word sorting use of quicksort to sort integers. Otherwise, an exercise would be to re-write non-generic qsort functions of qsortsimp, partition, and swap for integers.