From 2492f6a8668f8b6638e7ed2dee50f2dfa9c42169 Mon Sep 17 00:00:00 2001 From: asal homayouni Date: Wed, 17 May 2023 10:56:22 +0330 Subject: [PATCH 1/2] bugs have been fixed --- 1.cpp | 2 +- 2.cpp | 8 ++++---- 3.cpp | 14 +++++++------- 4.cpp | 5 +++-- 5.cpp | 8 +++++--- 6.cpp | 1 + 7.cpp | 5 +++-- 8.cpp | 8 ++++---- 8 files changed, 28 insertions(+), 23 deletions(-) diff --git a/1.cpp b/1.cpp index 190c209..b367233 100644 --- a/1.cpp +++ b/1.cpp @@ -37,4 +37,4 @@ int main() { vector v2 = 100; v2.getlen = 40; cout << v2.getlen(); -} \ No newline at end of file +} 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/3.cpp b/3.cpp index c8732ef..9bc79d9 100644 --- a/3.cpp +++ b/3.cpp @@ -1,9 +1,9 @@ -#include +#include #include #define MAX_SIZE 200 int arr[MAX_SIZE]; - -typedef struct alfa * alfaptr; +#pragma warning (disable:4996) +typedef struct alfa* alfaptr; struct alfa { long long x; @@ -45,7 +45,7 @@ void search(int x) printf("ERROR2"); break; } - node = node->next; + node = node->next; } void rpop() {//pop last element @@ -66,9 +66,9 @@ 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,7 +88,7 @@ int average() { alfaptr node = front; - int sum = 0, count; + int sum = 0, count = 0; while (node) { sum += node->x; count++; diff --git a/4.cpp b/4.cpp index a9a32f2..be3483a 100644 --- a/4.cpp +++ b/4.cpp @@ -2,8 +2,9 @@ int main() { float arr[5] = { 12.5, 10.0, 13.5, 90.5, 0.5 }; - float *ptr1 = &arr[0]; - float *ptr2 = ptr1 + 3; + float* ptr1 = &arr[0]; + float* ptr2 = ptr1 + 3; printf("%f", *ptr2 - *ptr1); return 0; + //78.000 } \ No newline at end of file diff --git a/5.cpp b/5.cpp index e9a1737..91d2ec8 100644 --- a/5.cpp +++ b/5.cpp @@ -1,10 +1,12 @@ -#include +#include int main() { int arr[] = { 10, 20, 30, 40, 50, 60 }; - int *ptr1 = arr; - int *ptr2 = arr + 5; + int* ptr1 = arr; + int* ptr2 = arr + 5; printf("%d\n", (*ptr2 - *ptr1)); printf("%c", (char)(*ptr2 - *ptr1)); return 0; + //50 + //2 } \ No newline at end of file diff --git a/6.cpp b/6.cpp index bb721c3..519df24 100644 --- a/6.cpp +++ b/6.cpp @@ -8,4 +8,5 @@ int main() x[0] = 1; printf("%d\n", a); return 0; + //513 } diff --git a/7.cpp b/7.cpp index 7b065a0..d707f93 100644 --- a/7.cpp +++ b/7.cpp @@ -2,9 +2,10 @@ int main() { int arr[] = { 1, 2, 3, 4, 5 }; - int *p = arr; - ++*p; + int* p = arr; + ++* p; p += 2; printf("%d", *p); return 0; + //3 } \ No newline at end of file diff --git a/8.cpp b/8.cpp index 76b870c..2d264b7 100644 --- a/8.cpp +++ b/8.cpp @@ -1,13 +1,13 @@ -#include -const char * f(const char **p) { +#include +const char* f(const char** p) { auto q = (p + sizeof(char))[1]; return q; } int main() { - const char * str[] = { "Wish","You","Best",":D" }; + 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)); + //Be WooW - } From e8c05c8e113cd63961b392108db7d0bd31f8561d Mon Sep 17 00:00:00 2001 From: asal homayouni Date: Wed, 17 May 2023 11:13:07 +0330 Subject: [PATCH 2/2] fixed --- 1.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/1.cpp b/1.cpp index b367233..eaaed17 100644 --- a/1.cpp +++ b/1.cpp @@ -6,8 +6,9 @@ class container { int size; public: float* p; - container(int s) :size(s){} - const int& getsize() { return size;} + container():size(0){} + container(int s) :size(s) {} + int& getsize() { return size; } }; @@ -15,12 +16,16 @@ 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(); } + vector(container& c) { + call_num = 0; + len = 0; + } int len; - int& getlen() const { - call_num ++; + int& getlen() { + call_num++; return len; } ~vector() = default; @@ -34,7 +39,7 @@ int main() { container c2 = 100; c2.getsize() = 20; cout << c2.getsize(); - vector v2 = 100; - v2.getlen = 40; + vector v2 = vector(100); + v2.getlen() = 40; cout << v2.getlen(); }