Note: This code should be appropriately commented.
printOdd.c #include#include #define MAX 1000 int scanN(); void printOdd(int n); int main() { int n = scanN(); //int n = getCharN(); printOdd(n); return 0; } int scanN() { int n; scanf("%d", &n); return n; } void printOdd(int n) { int i; // i = 1 : we want to start printing from 1 // i <= n : the last thing we possibly want to print is n // i += 2 : we're printing odd numbers, which are spaced two apart from each other for (i = 1; i <= n; i+=2) { printf("%d\n", i); } }
tri.c // Copied & modified from t1 directory // printTRTri offers a different approach to this problem // // Dong. #include// Bottom Left Triangle (i.e. right angle at bottom left) void printBLTri(int num) { int i, j; // Prints the rows (lowerlim;upperlim;inc) for (i = 0; i < num; i++) { int blTri = i + 1; // Prints the characters of each row for (j = 0; j < blTri; j++) { printf("*"); } printf("\n"); } } // Top Left Triangle (i.e. right angle at top left) void printTLTri(int num) { int i, j; // Prints the rows (lowerlim;upperlim;inc) for (i = 0; i < num; i++) { int tlTri = num - i; // Prints the characters of each row for (j = 0; j < tlTri; j++) { printf("*"); } printf("\n"); } } // Top Right Triangle (i.e. right angle at top right) void printTRTri(int num) { int i, j; for (i = 0; i < num; i++) { for (j = 0; j < num; j++) { if (j >= i) { printf("*"); } else { printf(" "); } } printf("\n"); } } // Bottom Right Triangle (i.e. right angle at bottom right) void printBRTri(int num) { int i, j; // Prints the rows (lowerlim;upperlim;inc) for (i = 0; i < num; i++) { int trTri = num - i; //int brTri = i + 1; // Prints the characters of each row for (j = 0; j < trTri - 1; j++) { printf(" "); } for (; j < num; j++) { printf("*"); } printf("\n"); } } int main() { int num; printf("Enter n : "); scanf("%d", &num); printf(" === Bottom Left Triangle ===\n"); printBLTri(num); printf(" === Bottom Right Triangle ===\n"); printBRTri(num); printf(" === Top Left Triangle ===\n"); printTLTri(num); printf(" === Top Right Triangle ===\n"); printTRTri(num); return 0; }
pow2.c #includeint isPow2(int n) { // while n is even, divide by 2 while (n % 2 == 0) { n = n/2; } if (n == 1) { return 1; } return 0; } int main(int argc, char** argv) { int n; scanf("%d", &n); if (isPow2(n)) { printf("%d is a power of 2\n", n); } else { printf("%d is not a power of 2\n", n); } return 0; }
freqNum.c // README // Counts from stdin until EOF is found // To test, type in characters and finish with "CTRL-D" // // Credits to Ka Hei :) #include// Scans user input, counts into the count void scanNums(int * count) { char c; while ((c = getchar()) != EOF) { if (c >= '0' && c <= '9') { int num = c - '0'; count[num] = count[num] + 1; } } } void printFreq(int * count, int n) { int i = 0; while (i < n) { printf("%d %d\n", i, count[i]); i++; } } int main() { int count[10] = {0}; scanNums(count); printf("\n"); printFreq(count, 10); return 0; }
average.c // Credits to Ka Hei :) #includedouble average(int len, int a[]) { // Returns the average of the array a double sum = 0; int i = 0; while (i < len) { sum = sum + a[i]; i++; } double avg = sum/len; return avg; } int main() { int a[] = {20, 35, -63, 42, 57}; int len = 5; double avg = average(len, a); printf("%lf\n", avg); return 0; }