From fa3209f2fb64de88ef6d6ab519389dfd829ac80a Mon Sep 17 00:00:00 2001 From: Behdad Date: Wed, 17 May 2023 11:07:22 +0330 Subject: [PATCH 1/2] The code has been fixed --- 1.cpp | 23 +++++++++++++++++------ 3.cpp | 5 +++-- 8.cpp | 8 +++++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/1.cpp b/1.cpp index 190c209..f9bd497 100644 --- a/1.cpp +++ b/1.cpp @@ -6,8 +6,12 @@ class container { int size; public: float* p; + container() { + size=0; + p=nullptr; + } container(int s) :size(s){} - const int& getsize() { return size;} + int& getsize() { return size;} }; @@ -15,11 +19,14 @@ class vector :public container { int call_num; public: - explicit vector(int l) :len(l),size(1 * 100){ + vector(container& obj) { + this->getsize() = obj.getsize(); + } + explicit vector(int l) :len(l),container(l * 100){ p = new float(); } int len; - int& getlen() const { + int& getlen() { call_num ++; return len; } @@ -34,7 +41,11 @@ int main() { container c2 = 100; c2.getsize() = 20; cout << c2.getsize(); - vector v2 = 100; - v2.getlen = 40; + vector v2(100); + v2.getlen() = 40; cout << v2.getlen(); -} \ No newline at end of file +} +/* +output: +2040 +*/ \ No newline at end of file diff --git a/3.cpp b/3.cpp index c8732ef..c36fefb 100644 --- a/3.cpp +++ b/3.cpp @@ -1,5 +1,6 @@ #include #include +#pragma warning(disable:4996) #define MAX_SIZE 200 int arr[MAX_SIZE]; @@ -66,7 +67,7 @@ void set() int size() { alfaptr node = front; - int count; + int count=0; while (node) count++;node = node->next; return count; @@ -88,7 +89,7 @@ int average() { alfaptr node = front; - int sum = 0, count; + int sum = 0, count=0; while (node) { sum += node->x; count++; diff --git a/8.cpp b/8.cpp index 76b870c..6bf2618 100644 --- a/8.cpp +++ b/8.cpp @@ -7,7 +7,9 @@ int main() { const char * str[] = { "Wish","You","Best",":D" }; printf("%c%c ", *f(str), *(f(str) + 1)); printf("%c%c%c%c\n", **str, *(*(str + 1) + 1), *((str + 2)[-1] + 1), **&*(&str[-1] + 1)); - - - + return 0; } +/* +output: +Be WooW +*/ From d350d14f5b662153eb5fc2c1964179907044bdd8 Mon Sep 17 00:00:00 2001 From: Behdad Date: Wed, 17 May 2023 11:39:20 +0330 Subject: [PATCH 2/2] The code has been fixed --- 2.cpp | 8 ++++---- 4.cpp | 6 +++++- 5.cpp | 7 ++++++- 6.cpp | 5 +++++ 7.cpp | 6 +++++- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/2.cpp b/2.cpp index 6a27746..58e302a 100644 --- a/2.cpp +++ b/2.cpp @@ -5,11 +5,11 @@ 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; } diff --git a/4.cpp b/4.cpp index a9a32f2..c5df3ac 100644 --- a/4.cpp +++ b/4.cpp @@ -6,4 +6,8 @@ int main() float *ptr2 = ptr1 + 3; printf("%f", *ptr2 - *ptr1); return 0; -} \ No newline at end of file +} +/* +output: +78.000000 +*/ \ No newline at end of file diff --git a/5.cpp b/5.cpp index e9a1737..fee843f 100644 --- a/5.cpp +++ b/5.cpp @@ -7,4 +7,9 @@ int main() printf("%d\n", (*ptr2 - *ptr1)); printf("%c", (char)(*ptr2 - *ptr1)); return 0; -} \ No newline at end of file +} +/* +output: +50 +2 +*/ \ No newline at end of file diff --git a/6.cpp b/6.cpp index bb721c3..fba260a 100644 --- a/6.cpp +++ b/6.cpp @@ -9,3 +9,8 @@ int main() printf("%d\n", a); return 0; } +/* +output: +513 +*/ + diff --git a/7.cpp b/7.cpp index 7b065a0..cf7194f 100644 --- a/7.cpp +++ b/7.cpp @@ -7,4 +7,8 @@ int main() p += 2; printf("%d", *p); return 0; -} \ No newline at end of file +} +/* +output: +3 +*/ \ No newline at end of file