diff --git a/1.cpp b/1.cpp index fedde2c..9397ac0 100644 --- a/1.cpp +++ b/1.cpp @@ -13,20 +13,20 @@ long long int factorial(int n) long long int *producingTheFactorialFractions() { - long long b[10]; + long long *b[10]; - for (int i = 10; i >= 0; i--) + for (int i = 9; i >= 0; i--) { b[i] += (int)pow(factorial(10), 2.0) / (i + 1); } - return b; + return *b; } void checkZeros(long long *a) { for (int i = 9; i >= 0; i--) { - if (a[i] = 0) + if (a[i] == 0) cout << "Zero Found" << endl; } } @@ -46,5 +46,5 @@ int main() cout<<"hello"; cout<<"Bye"; - + return 0; } \ No newline at end of file diff --git a/2.cpp b/2.cpp index 6a27746..23cea79 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 77eb722..e525c50 100644 --- a/3.cpp +++ b/3.cpp @@ -16,10 +16,22 @@ void push(int x) node = (alfaptr)malloc(sizeof(struct alfa)); node->x = x; if (!front) + { front = node; + front->next = NULL; + } + else { - rear->next = node; rear = node; + rear->next = NULL; + if (front->next == NULL) + front->next = rear; + else { + alfaptr temp = front; + while (temp->next != NULL) + temp = temp->next; + temp->next = rear; + } } } @@ -27,7 +39,7 @@ void pop() { alfaptr node; if (!front) - printf("ERROR1"); + printf("%s", "ERROR1"); else { node = front->next; @@ -39,56 +51,73 @@ void search(int x) alfaptr node = front; int counter = 0; while (node) + { if (node->x == x) + { printf("%d", counter); - else { - printf("ERROR2"); break; } + else { + printf("%s", "ERROR2"); + } node = node->next; + } } void rpop() {//pop last element alfaptr node = front; - while (node) + while (node->next->next != NULL) node = node->next; free(rear); rear = node; + rear->next = NULL; } void set() { alfaptr node = front; - for (int i = 0; i < MAX_SIZE && node; i++, node = node->next) + for (int i = 0; i < MAX_SIZE; i++) + arr[i] = node->x; + node = front; + + for (int i = 0; i < MAX_SIZE && node; i++) + { arr[i] = node->x; + node = node->next; + } } int size() { alfaptr node = front; - int count; + int count = 0; while (node) - count++;node = node->next; + { + count++; + node = node->next; + } + return count; } void show() { - if (!front) { + if (front) { for (int i = 0; i < MAX_SIZE; i++) printf("%d ", arr[i]); } else { - printf("ERROR3"); + printf("%s", "ERROR3"); } + printf("\n"); } int average() { alfaptr node = front; - int sum = 0, count; + int sum = 0, count = 0; while (node) { sum += node->x; count++; @@ -97,7 +126,7 @@ int average() return sum / count; } -void main() +int main() { int cmd; long long int x; @@ -127,10 +156,11 @@ void main() show(); break; case 7://size - printf("%d", size()); + printf("%d\n", size()); break; case 10: exit(0); } } + return 0; } \ No newline at end of file diff --git a/4.cpp b/4.cpp index a9a32f2..ec33abb 100644 --- a/4.cpp +++ b/4.cpp @@ -5,5 +5,6 @@ int main() float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; printf("%f", *ptr2 - *ptr1); + // result of the run: 90.5 - 12.5 = 78.0000 return 0; } \ No newline at end of file diff --git a/5.cpp b/5.cpp index 1be3d10..d58984a 100644 --- a/5.cpp +++ b/5.cpp @@ -6,5 +6,6 @@ int main() int *ptr2 = arr + 5; printf("%d\n", (*ptr2 - *ptr1)); printf("%c", (char)(*ptr2 - *ptr1)); + // result of the run: line 7 = 50 , line 8 = 2 (ascii code for number 2 is 50) return 0; } \ No newline at end of file diff --git a/6.cpp b/6.cpp index f8b3141..f3bd900 100644 --- a/6.cpp +++ b/6.cpp @@ -7,5 +7,6 @@ int main() a = 512; x[0] = 1; printf("%d\n", a); + // result of the run: 513 return 0; } diff --git a/7.cpp b/7.cpp index 7b065a0..50398b5 100644 --- a/7.cpp +++ b/7.cpp @@ -6,5 +6,7 @@ int main() ++*p; p += 2; printf("%d", *p); + /* result of the run: 3 because line 7 will change the part that pointer is pointing to + the third data of the array which is 3 */ return 0; } \ No newline at end of file diff --git a/8.cpp b/8.cpp index ac3d613..2c0fc30 100644 --- a/8.cpp +++ b/8.cpp @@ -8,6 +8,6 @@ int main() { 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)); - - + // the result of the run: line 8 = Be , line 9 = WooW + return 0; }