Differences

This shows you the differences between two versions of the page.

Link to this comparison view

misc_c_programs [2014/10/26 01:52] (current)
Line 1: Line 1:
 +==== Misc C++ applications ====
 +<code lang="​c++">​
 +// one.cpp
  
 +#include <​iostream>​
 +#include <​vector>​
 +#include <​time.h>​
 +
 +using namespace std;
 +
 +// function prototypes
 +int main(void);
 +void strv(char *s, int x);
 +int myStrLen(char *s);
 +void dumpArray(int d[], int x);
 +void fillArray(int array[], int len);
 +void shuffle(int array[], int len);
 +int fibonacci(int n);
 +
 +struct Node {
 + int nValue;
 + Node *pNext;
 +};
 +
 +Node * MergeSortLists(Node *listFirst, Node *listSecond);​
 +
 +
 +// ---- main() ----
 +int main() {
 +
 + cout << "Hello World" << endl;
 + char *orig = "My String";​
 +#if TEST_STRV
 + cout << "​Original:​ " << orig << endl;
 + strv(orig, myStrLen(orig)-1);​
 + cout << "​After:​ " << orig << endl;
 +#endif
 +
 +#if TEST_SHUFFLE
 + const int MAX_SIZE = 10;
 + int intArray[MAX_SIZE+1] = {0};
 + fillArray(intArray,​ MAX_SIZE);
 + shuffle(intArray,​ MAX_SIZE);
 +#endif
 +
 + return 0;
 +}
 +
 +void strv(char s[], int l) {
 + if (l == -1) { cout << endl; return; }
 + cout << s[l];
 + strv(s, l-1);
 +}
 +
 +
 +int myStrLen(char *s) {
 + int len = 0;
 + while (*s++) len++;
 + return len;
 +}
 +
 +void dumpArray(int array[], int len, int d) {
 + cout << "Dup = " << d << endl;
 + for (; len >= 0; len--) {
 + cout << "​a["​ << len << "​]="​ << array[len] << endl;
 + }
 +}
 +
 +void fillArray(int array[], int len) {
 + srand(time(0));​
 + int dupIndex = rand() % len;
 + int dupValue = 0;
 + int origLen = len;
 + for (int i=0; i < len; i++) {
 + array[i] = i+1;
 + dupValue = array[dupIndex];​
 + }
 + array[origLen] = dupValue;
 + dumpArray(array,​ origLen, dupIndex);
 +
 +}
 +
 +
 +void shuffle(int array[], int len) {
 +
 + int origLen = len;
 + for (int j=0; j<5; j++) {
 + for (int i=0; i < len; i++) {
 + int indx = rand() % len;
 + int tmp = array[i];
 + array[i] = array[indx];​
 + array[indx] = tmp;
 + // cout << indx << endl;
 + }
 + }
 + dumpArray(array,​ len, origLen);
 +}
 +
 +int fibonacci(int n) {
 + if (n <= 2) 
 + return 1;
 + else 
 + return fibonacci(n-1) + fibonacci(n-2);​
 +}
 +
 +Node *buildList(int n) {
 + Node *list = null;
 +
 + for (int i=0; i < n; i++) {
 + int val = rand() % n;
 + cout << val << endl;
 + }
 + return list;
 +}
 +
 +
 +Node * MergeSortLists(Node *listFirst, Node *listSecond) {
 + Node *returnList = NULL;
 + return returnList;
 +}
 +
 +</​code>​
 +
 +----
 +
 +  * [[cplusplus|Back to C++]]
misc_c_programs.txt ยท Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0