lec 1
and other things this is mad confusing
This commit is contained in:
commit
045d7367a8
11 files changed
+349
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
.vscode
|
||||||
|
exe
|
||||||
|
drafts
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int real;
|
||||||
|
int imag;
|
||||||
|
} ldaComplex;
|
||||||
|
|
||||||
|
void print(ldaComplex* lda)
|
||||||
|
{
|
||||||
|
printf("%d + %di", lda->real, lda->imag);
|
||||||
|
}
|
||||||
|
// this is the best of my ability in c: returning string is a Fucking Mess
|
||||||
|
// i would love to do this in rust
|
||||||
|
|
||||||
|
ldaComplex add(ldaComplex a, ldaComplex b)
|
||||||
|
{
|
||||||
|
return (ldaComplex){a.real + b.real, a.imag + b.imag};
|
||||||
|
}
|
||||||
|
ldaComplex multiply(ldaComplex a, ldaComplex b)
|
||||||
|
{
|
||||||
|
return (ldaComplex){a.real * b.real - a.imag * b.imag,
|
||||||
|
a.real * b.imag + a.imag * b.real};
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char const *argv[])
|
||||||
|
{
|
||||||
|
ldaComplex lda = {1, 2};
|
||||||
|
ldaComplex lda2 = {5, 2};
|
||||||
|
ldaComplex addRes = add(lda, lda2);
|
||||||
|
ldaComplex multiRes = multiply(lda, lda2);
|
||||||
|
|
||||||
|
printf("("); print(&lda); printf(") + ("); print(&lda2); printf(") = "); print(&addRes);
|
||||||
|
printf("\n("); print(&lda); printf(") * ("); print(&lda2); printf(") = "); print(&multiRes);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// ass
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
void findMax(int *max, int a) {
|
||||||
|
if (a > *max) *max = a;
|
||||||
|
};
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
int main(){
|
||||||
|
int c = 176;
|
||||||
|
int f = 10;
|
||||||
|
findMax(&f, c);
|
||||||
|
printf("%d, %d", c, f);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+24
@@ -0,0 +1,24 @@
|
|||||||
|
# lab 1
|
||||||
|
|
||||||
|
## ex 1
|
||||||
|
|
||||||
|
Write a pseudocode and implement a program in C to swap the first and last digits
|
||||||
|
of a positive integer.
|
||||||
|
|
||||||
|
## ex 2
|
||||||
|
|
||||||
|
Complete this given function `void findMax(int *max, int a)`, which assigns a value
|
||||||
|
a to max if a > max.
|
||||||
|
|
||||||
|
## ex 3
|
||||||
|
|
||||||
|
Write a structure to represent complex numbers and complete operators: add and
|
||||||
|
multiply.
|
||||||
|
|
||||||
|
## ex 4
|
||||||
|
|
||||||
|
Write a pseudo-code by commenting in the file then implement a program in to
|
||||||
|
enter a natural number n and verify whether n is sphenic. Calculate the complexity
|
||||||
|
of your program.
|
||||||
|
Note: A sphenic number is a product of $p \times q \times r$ where $p$, $q$, and $r$ are three distinct
|
||||||
|
prime numbers. Example: $30 = 2 \times 3 \times 5$; $42 = 2 \times 3 \times 7$; $66 = 2 \times 3 \times 11$
|
||||||
+43
@@ -0,0 +1,43 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int log10intint(int n)
|
||||||
|
{
|
||||||
|
// as int goes to 2^64 we gon
|
||||||
|
if (n <= 0)
|
||||||
|
return 1 << 31;
|
||||||
|
int a = 1;
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
a *= 10;
|
||||||
|
if (n - a < 0)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
int ten_to_the(int i)
|
||||||
|
{ if (i < 0) return 0;
|
||||||
|
int ret = 1;
|
||||||
|
while (i-- > 0)
|
||||||
|
ret *= 10;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int special_swap(int number)
|
||||||
|
{
|
||||||
|
int first_number, second_number, third_number;
|
||||||
|
int this_number = ten_to_the(log10intint(number));
|
||||||
|
|
||||||
|
third_number = number % 10;
|
||||||
|
first_number = number / this_number;
|
||||||
|
second_number = number % this_number - third_number;
|
||||||
|
|
||||||
|
return second_number + third_number * this_number + first_number;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
printf("%d\n", special_swap(142425));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
bool sphenic(int i)
|
||||||
|
{
|
||||||
|
if (i < 30)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// number can be prime ^ a * prime ^ b * prime ^ c * other;
|
||||||
|
// idea straight from stack overflowing
|
||||||
|
|
||||||
|
int prime_prod = 1;
|
||||||
|
int count = 0; // handle case 3*11 for example
|
||||||
|
int sphenic = 3;
|
||||||
|
|
||||||
|
for (
|
||||||
|
int n = i, p = 2;
|
||||||
|
count < sphenic // we have 3 numbers
|
||||||
|
&& n != 1 // we have divided all the numbers
|
||||||
|
&& p <= n; // lil check
|
||||||
|
p++)
|
||||||
|
|
||||||
|
if (n % p == 0)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
prime_prod *= p;
|
||||||
|
while (n % p == 0)
|
||||||
|
n /= p; // what if you can be divided by 4?
|
||||||
|
};
|
||||||
|
|
||||||
|
return prime_prod == i // see thats what the while is for
|
||||||
|
&& count == sphenic; // what if we have 17 * 29
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int N = 200;
|
||||||
|
|
||||||
|
for (int i = 1; i < N; i++)
|
||||||
|
if (sphenic(i))
|
||||||
|
printf("%d ", i);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
# lab 2
|
||||||
|
|
||||||
|
## ex 1
|
||||||
|
|
||||||
|
Suppose that we use a List data structure to represent unbounded
|
||||||
|
integers.
|
||||||
|
|
||||||
|
- the first element refers to the sign: 1 is positive and -1 is negative.
|
||||||
|
- each element in the list represents one digit of these integers.
|
||||||
|
|
||||||
|
Implement and test the program in C/C++ using an Array-based List (Static or Dynamic) to manage this unbounded integers:
|
||||||
|
|
||||||
|
- add new digits at any place in an integer.
|
||||||
|
- remove a digit at a specfic position in an integer.
|
||||||
|
- calculate the sum of all digits from an unbounded integer.
|
||||||
|
- display the whole number on the screen.
|
||||||
|
|
||||||
|
## ex 2
|
||||||
|
|
||||||
|
Assume that a railway train has N railroad cars attached together such as car1, car2, car3, ..., carN . A train can be considered as a list and each car corresponds to a node in this list.
|
||||||
|
|
||||||
|
- Each car carries a maximum capacity of passengers (int type) and has an id (char type). Both variables are user-defined values.
|
||||||
|
- Each car is reserved by a number of passengers (int type) (which should be less than its capacity). If there are any cars that don’t have any passengers, they should be removed from the train.
|
||||||
|
- It is possible to add new cars to the train.
|
||||||
|
- A function is available to display all cars’ information or the length of the train.
|
||||||
|
|
||||||
|
Implement a program in C/C++ using Linked List to manage the train and test all functions.
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include "../common/arraylist.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
int main(int argc, char const *argv[])
|
||||||
|
{
|
||||||
|
ldaArrayList lda;
|
||||||
|
ldalistinit(&lda);
|
||||||
|
ldalistpush(&lda, 1);
|
||||||
|
ldalistpush(&lda, 2);
|
||||||
|
ldalistpush(&lda, 5);
|
||||||
|
ldalistpush(&lda, 10);
|
||||||
|
ldalistpush(&lda, 20);
|
||||||
|
ldalistpush(&lda, 30);
|
||||||
|
ldalistadd(&lda, 4, 50); // correct af
|
||||||
|
ldalistprint(&lda);
|
||||||
|
int g;
|
||||||
|
ldalistpop(&lda, &g);
|
||||||
|
int f;
|
||||||
|
ldalistremove(&lda, 6, &f);
|
||||||
|
ldalistprint(&lda);
|
||||||
|
printf("%d %d",g, f);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
// for printf
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "arraylist.h"
|
||||||
|
// stack list
|
||||||
|
void ldalistinit(ldaArrayList *l)
|
||||||
|
{
|
||||||
|
l->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ldalistlen(ldaArrayList *l)
|
||||||
|
{
|
||||||
|
return l->size;
|
||||||
|
}
|
||||||
|
int ldalistcheck(ldaArrayList *l)
|
||||||
|
{
|
||||||
|
if (ldalistlen(l) >= 100)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ldalistpush(ldaArrayList *l, int value)
|
||||||
|
{
|
||||||
|
if (ldalistcheck(l) != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
l->list[l->size++] = value;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int ldalistadd(ldaArrayList *l, int at, int value)
|
||||||
|
{
|
||||||
|
if (ldalistcheck(l) != 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int size = ldalistlen(l);
|
||||||
|
if (at > size)
|
||||||
|
return -1;
|
||||||
|
else if (at == size)
|
||||||
|
{
|
||||||
|
return ldalistpush(l, value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = size; i > at; i--)
|
||||||
|
{
|
||||||
|
l->list[i] = l->list[i - 1];
|
||||||
|
};
|
||||||
|
l->list[at] = value;
|
||||||
|
l->size += 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ldalistpop(ldaArrayList *l, int *put_here)
|
||||||
|
{
|
||||||
|
if (ldalistlen(l) <= 0)
|
||||||
|
return -1;
|
||||||
|
*put_here = l->list[l->size--];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ldalistremove(ldaArrayList *l, int at, int *put_here)
|
||||||
|
{
|
||||||
|
if (ldalistlen(l) <= 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int size = ldalistlen(l);
|
||||||
|
if (at > size)
|
||||||
|
return -1;
|
||||||
|
else if (at == size)
|
||||||
|
{
|
||||||
|
return ldalistpop(l, put_here);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*put_here = l->list[at];
|
||||||
|
for (int i = at; i++ > size;)
|
||||||
|
{
|
||||||
|
l->list[i] = l->list[i - 1];
|
||||||
|
};
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ldalistlookup(ldaArrayList *l, int at, int *copy_here)
|
||||||
|
{
|
||||||
|
if (at > ldalistlen(l))
|
||||||
|
return -1;
|
||||||
|
*copy_here = l->list[at];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
void ldalistprint(ldaArrayList *l)
|
||||||
|
{
|
||||||
|
printf("[");
|
||||||
|
for (int i = 0,
|
||||||
|
cur,
|
||||||
|
_ = ldalistlookup(l, i, &cur) || printf("%d", cur);
|
||||||
|
++i < ldalistlen(l) && printf(", ");
|
||||||
|
_ = ldalistlookup(l, i, &cur) || printf("%d", cur))
|
||||||
|
;
|
||||||
|
printf("]");
|
||||||
|
};
|
||||||
|
|
||||||
|
// int main(int argc, char const *argv[])
|
||||||
|
// {
|
||||||
|
// /* code */
|
||||||
|
// return 0;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// for printf
|
||||||
|
// stack list
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef COMMON_LDAARRAYLIST_H
|
||||||
|
#define COMMON_LDAARRAYLIST_H
|
||||||
|
typedef struct LdaArrayList
|
||||||
|
{
|
||||||
|
int size;
|
||||||
|
int list[100];
|
||||||
|
} ldaArrayList;
|
||||||
|
|
||||||
|
void ldalistinit(ldaArrayList *l);
|
||||||
|
int ldalistlen(ldaArrayList *l);
|
||||||
|
int ldalistcheck(ldaArrayList *l);
|
||||||
|
int ldalistpush(ldaArrayList *l, int value);
|
||||||
|
int ldalistadd(ldaArrayList *l, int at, int value);
|
||||||
|
int ldalistpop(ldaArrayList *l, int *put_here);
|
||||||
|
int ldalistremove(ldaArrayList *l, int at, int *put_here);
|
||||||
|
|
||||||
|
int ldalistlookup(ldaArrayList *l, int at, int *copy_here);
|
||||||
|
|
||||||
|
void ldalistprint(ldaArrayList *l);
|
||||||
|
#endif
|
||||||
Reference in new issue
Block a user