diff --git a/1.cpp b/1.cpp index fedde2c..c2b91b8 100644 --- a/1.cpp +++ b/1.cpp @@ -1,23 +1,18 @@ #include -#include #include - +#include using namespace std; -long long *b; - long long int factorial(int n) { return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n; } -long long int *producingTheFactorialFractions() +long long int *producingTheFactorialFractions(long long int *b) { - long long b[10]; - - for (int i = 10; i >= 0; i--) + for (int i = 9; i >= 0; i--) // changed 10 to 9 { - b[i] += (int)pow(factorial(10), 2.0) / (i + 1); + b[i] += (long long int)pow((double)factorial(10), 2.0) / (i + 1); // change int to long long int } return b; } @@ -26,25 +21,22 @@ void checkZeros(long long *a) { for (int i = 9; i >= 0; i--) { - if (a[i] = 0) + if (a[i] == 0) // change = to == cout << "Zero Found" << endl; } } int main() { - - long long int *a; - a = producingTheFactorialFractions(); + long long int *a = new long long int; // use heap + a = producingTheFactorialFractions(a); checkZeros(a); for (int i = 0; i < 10; i++) { cout << a[i] << endl; } - delete a; - - cout<<"hello"; - cout<<"Bye"; - - + cout << "hello" << endl; + cout << "Bye"; + delete a; // for leak of memory + return 0; } \ No newline at end of file diff --git a/2.cpp b/2.cpp index 6a27746..5b106b7 100644 --- a/2.cpp +++ b/2.cpp @@ -4,23 +4,24 @@ 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 countAllSpecificChars(string sArr[], int arrLength, char specificChar) +{ + int count = 0; // initialize count = 0 + for (int i = 0; i < arrLength; ++i) // change <= to < + for (int j = 0; j < sArr[i].size(); ++j) // change <= to < // if the jth char of the string is the specific char - if (sArr[i][j] = specificChar) + if ((sArr[i])[j] == specificChar) // change = to == count++; return count; } -int main() { +int main() +{ string sArr[4] = { - "I am", - "in", - "ap", - "class" - }; + "I am", + "in", + "ap", + "class"}; char findIt; cin >> findIt; cout << countAllSpecificChars(sArr, 4, findIt); diff --git a/3.cpp b/3.cpp index 77eb722..edab0ff 100644 --- a/3.cpp +++ b/3.cpp @@ -1,23 +1,29 @@ -#include -#include +#include +#include #define MAX_SIZE 200 int arr[MAX_SIZE]; -typedef struct alfa * alfaptr; +typedef struct alfa *alfaptr; -struct alfa { - long long x; +struct alfa +{ + long long int x; alfaptr next; }; alfaptr rear = NULL, front = NULL; -void push(int x) +void push(long long int x) { alfaptr node; node = (alfaptr)malloc(sizeof(struct alfa)); node->x = x; + node->next = NULL; if (!front) + { front = node; - else { + rear = node; + } + else + { rear->next = node; rear = node; } @@ -31,29 +37,39 @@ void pop() else { node = front->next; + free(front); front = node; } } -void search(int x) +void search(long long int x) { alfaptr node = front; int counter = 0; while (node) + { if (node->x == x) - printf("%d", counter); - else { + { + printf("%d\n", counter); + break; + } + else if (node->next == NULL) + { printf("ERROR2"); break; } + counter++; node = node->next; + } } -void rpop() {//pop last element +void rpop() +{ // pop last element alfaptr node = front; - while (node) + while (node->next->next) node = node->next; free(rear); rear = node; + // printf("%d",rear->x); } void set() @@ -66,17 +82,26 @@ void set() int size() { alfaptr node = front; - int count; + int count = 0; while (node) - count++;node = node->next; + { + node = node->next; + count++; + } return count; } void show() { - if (!front) { - for (int i = 0; i < MAX_SIZE; i++) + if (front) + { + printf("Your linked list elements are --> [ "); + for (int i = 0; i < size(); i++) + { printf("%d ", arr[i]); + if (i == size() - 1) + printf(" ]\n"); + } } else { @@ -88,48 +113,58 @@ int average() { alfaptr node = front; - int sum = 0, count; - while (node) { + long long int sum = 0, count = 0; + while (node) + { sum += node->x; count++; node = node->next; } return sum / count; } - -void main() +void menu() +{ + printf("\nEnter the number of your choise (1 : push) (2 : pop) (3 : rpop) (4 : search) (5 : set) (6 : show) (7 : size) (8 : average) (9 : exit) \n"); +} +int main() { int cmd; long long int x; while (true) { + menu(); scanf("%d", &cmd); switch (cmd) { - case 1://push - scanf("%lld", &x); + case 1: // push + scanf("%d", &x); push(x); + system("cls"); break; - case 2://pop + case 2: // pop pop(); break; - case 3://rpop + case 3: // rpop rpop(); break; - case 4://search - scanf("%lld", &x); + case 4: // search + scanf("%d", &x); search(x); break; - case 5://set + case 5: // set set(); + system("cls"); break; - case 6://show + case 6: // show show(); break; - case 7://size - printf("%d", size()); + case 7: // size + printf("%d\n", size()); + break; + case 8: + printf("%d\n", average()); break; - case 10: + default: exit(0); } } diff --git a/4.cpp b/4.cpp index a9a32f2..9929fea 100644 --- a/4.cpp +++ b/4.cpp @@ -4,6 +4,7 @@ int main() float arr[5] = { 12.5, 10.0, 13.5, 90.5, 0.5 }; float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; - printf("%f", *ptr2 - *ptr1); + printf("%f", *ptr2 - *ptr1); // prints 78.000000 return 0; + } \ No newline at end of file diff --git a/5.cpp b/5.cpp index 1be3d10..b345a9e 100644 --- a/5.cpp +++ b/5.cpp @@ -4,7 +4,7 @@ int main() int arr[] = { 10, 20, 30, 40, 50, 60 }; int *ptr1 = arr; int *ptr2 = arr + 5; - printf("%d\n", (*ptr2 - *ptr1)); - printf("%c", (char)(*ptr2 - *ptr1)); + printf("%d\n", (*ptr2 - *ptr1)); // prints integer 50 + printf("%c", (char)(*ptr2 - *ptr1)); // prints asci code 50 -> 2 return 0; } \ No newline at end of file diff --git a/6.cpp b/6.cpp index f8b3141..dd44fe5 100644 --- a/6.cpp +++ b/6.cpp @@ -6,6 +6,6 @@ int main() x = (char *)&a; a = 512; x[0] = 1; - printf("%d\n", a); + printf("%d\n", a); // prints 513 return 0; } diff --git a/7.cpp b/7.cpp index 7b065a0..511a5d0 100644 --- a/7.cpp +++ b/7.cpp @@ -1,10 +1,10 @@ -#include +#include int main() { - int arr[] = { 1, 2, 3, 4, 5 }; + int arr[] = {1, 2, 3, 4, 5}; int *p = arr; - ++*p; + ++*p; p += 2; - printf("%d", *p); + printf("%d", *p); // prints 3 return 0; } \ No newline at end of file diff --git a/8.cpp b/8.cpp index ac3d613..c03ace7 100644 --- a/8.cpp +++ b/8.cpp @@ -7,7 +7,6 @@ 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; // I add this + // prints Be WooW }