diff --git a/1.cpp b/1.cpp index 190c209..5855efb 100644 --- a/1.cpp +++ b/1.cpp @@ -15,11 +15,14 @@ class vector :public container { int call_num; public: - explicit vector(int l) :len(l),size(1 * 100){ + explicit vector(int l) :len(l),container(100){ p = new float(); } + + explicit vector(const container c): container{c} + {} int len; - int& getlen() const { + int& getlen(){ call_num ++; return len; } @@ -29,12 +32,12 @@ class vector :public container { int main() { container c1(100); - vector v1 = c1; + vector v1{c1}; container& r1 = v1; container c2 = 100; - c2.getsize() = 20; + c2.getsize(); cout << c2.getsize(); - vector v2 = 100; - v2.getlen = 40; + vector v2(100); + v2.getlen(); cout << v2.getlen(); } \ No newline at end of file diff --git a/2.cpp b/2.cpp index 6a27746..8de7df1 100644 --- a/2.cpp +++ b/2.cpp @@ -5,23 +5,20 @@ using namespace std; // count all the specific char in the whole array of strings int countAllSpecificChars(string sArr[], int arrLength, char specificChar) { - int count; - for (int i = 0; i <= arrLength; ++i) - for (int j = 0; j <= sArr[i].size(); ++j) + int count = 0; + for (int i = 0; i < arrLength; ++i) + for (int j = 0; j < sArr[i].size(); ++j) // if the jth char of the string is the specific char - if (sArr[i][j] = specificChar) + if (sArr[i][j] == specificChar) count++; return count; } int main() { - string sArr[4] = { - "I am", - "in", - "ap", - "class" - }; + string sArr[4] = {"I am","in","ap","class"}; char findIt; cin >> findIt; cout << countAllSpecificChars(sArr, 4, findIt); -} \ No newline at end of file +} + +// return the number of occurence of char findIt in the sArr \ No newline at end of file diff --git a/3.cpp b/3.cpp index c8732ef..845db37 100644 --- a/3.cpp +++ b/3.cpp @@ -9,17 +9,25 @@ struct alfa { long long x; alfaptr next; }; + alfaptr rear = NULL, front = NULL; + + void push(int x) { alfaptr node; node = (alfaptr)malloc(sizeof(struct alfa)); node->x = x; if (!front) + { front = node; - else { + front->next = NULL; + } + else + { rear->next = node; rear = node; + rear->next = NULL; } } @@ -31,6 +39,7 @@ void pop() else { node = front->next; + delete front; front = node; } } @@ -39,18 +48,24 @@ void search(int x) alfaptr node = front; int counter = 0; while (node) + { if (node->x == x) + { printf("%d", counter); - else { - printf("ERROR2"); - break; - } + return; + } node = node->next; + } + + printf("ERROR2"); + + } void rpop() {//pop last element + alfaptr node = front; - while (node) + while (node->next->next) node = node->next; free(rear); rear = node; @@ -66,9 +81,13 @@ void set() int size() { alfaptr node = front; - int count; + int count = 0; while (node) - count++;node = node->next; + { + count++; + node = node->next; + } + return count; } @@ -88,16 +107,20 @@ int average() { alfaptr node = front; - int sum = 0, count; + int sum = 0, count = 0; while (node) { sum += node->x; count++; node = node->next; } + if(count == 0) + { + return 0; + } return sum / count; } -void main() +int main() { int cmd; long long int x; diff --git a/4.cpp b/4.cpp index a9a32f2..9f3f0fd 100644 --- a/4.cpp +++ b/4.cpp @@ -6,4 +6,6 @@ int main() float *ptr2 = ptr1 + 3; printf("%f", *ptr2 - *ptr1); return 0; -} \ No newline at end of file +} + +// print 90.5 - 12.5 which is 78.000000 \ No newline at end of file diff --git a/5.cpp b/5.cpp index e9a1737..33ab105 100644 --- a/5.cpp +++ b/5.cpp @@ -1,10 +1,11 @@ #include int main() { - int arr[] = { 10, 20, 30, 40, 50, 60 }; + int arr[] = { 10, 20, 30, 40, 50, 60 }; // 5 int *ptr1 = arr; int *ptr2 = arr + 5; printf("%d\n", (*ptr2 - *ptr1)); printf("%c", (char)(*ptr2 - *ptr1)); return 0; -} \ No newline at end of file +} +// print (60 - 10) and character ('2') \ No newline at end of file diff --git a/6.cpp b/6.cpp index bb721c3..25cb007 100644 --- a/6.cpp +++ b/6.cpp @@ -9,3 +9,5 @@ int main() printf("%d\n", a); return 0; } + +// print 513 diff --git a/7.cpp b/7.cpp index 7b065a0..84495bd 100644 --- a/7.cpp +++ b/7.cpp @@ -3,8 +3,8 @@ int main() { int arr[] = { 1, 2, 3, 4, 5 }; int *p = arr; - ++*p; - p += 2; - printf("%d", *p); + ++*p; // 1++ -> 2 (first arr element) + p += 2; // go to index 2 + printf("%d", *p); // print 3 return 0; } \ No newline at end of file diff --git a/8.cpp b/8.cpp index 76b870c..4aaac2d 100644 --- a/8.cpp +++ b/8.cpp @@ -11,3 +11,5 @@ int main() { } + +// print "Be WooW"