Skip to content
Open

AP #40

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions 1.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
#include <iostream>
#include <string>
#include <math.h>

#include <cstdlib>
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;
}
Expand All @@ -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;
}
23 changes: 12 additions & 11 deletions 2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
97 changes: 66 additions & 31 deletions 3.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
#include<stdio.h>
#include<stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}
Expand All @@ -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()
Expand All @@ -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
{
Expand All @@ -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);
}
}
Expand Down
3 changes: 2 additions & 1 deletion 4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
4 changes: 2 additions & 2 deletions 5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion 6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions 7.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include<stdio.h>
#include <stdio.h>
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;
}
5 changes: 2 additions & 3 deletions 8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}