C challenges

Lets give each other some tasks in C to solve

I will be first, dont make it too hard im just a student

Attached: a8qrrwgg6ly01.png (657x527, 48K)

Other urls found in this thread:

stackoverflow.com/questions/5958221/big-o-order-of-recursive-and-iterative-fib-functions
leetcode.com/problems/the-skyline-problem/
twitter.com/AnonBabble

I hope you aren't trolling but OK..

Here it is write a function that will print all student numbers and their gpa


struct student {
char name [20+1];
double gpa;
unsigned int student-num;
struct student *right;
struct student *left;

}
typedef struct student student_t;
student_t *head;

eh..that seems to hard user

Attached: Linux-Birdie-640x353.jpg (640x353, 18K)

>Not using char* for student name.
Enjoy your overflows.

Don't cover for OP

you're not gonna make it

dont say that who uses structures anyways...

maybe its not to late to go on the python hype train

rewrite python's struct module

Struct is just a public class you pleb. Have fun at community college next year.

>dont say that who uses structures anyways...
you're definitely not gonna make it

lol desu I as a student myself wouldnt know how to solve this one

i'm a 1st year dropout and can do this
do schools not teach or are you just dumb?

SEE
not the only one

#include
#include
int main(){
char c;
printf("What\'s your opinion on traps Senpai?\n");
printf("Are traps gay? y/n \n\n");
scanf("%c",&c);
switch (c) {
case 'y':
case 'Y':
printf("UwU. That\'s debatable Senpai.\n");
break;
case 'n':
case 'N':
printf("OwO. Are my programming socks Kawaii Senpai?\n");
break;
default:
printf("UwU. Invalid answer Senpai\n");
exit(1);
}
return 0;
}

Attached: Trap Devs.png (1200x1080, 834K)

give solution pls ?

lol idiot this doesn't even compile. You can't put the - operator in a variable name and you didn't put a ; after the struct definition.

OK that would be 5$
Yeah my bad should have been _

for free ?

C'mon, post-order tree traversal is not rocket surgery.

It seems it is to some

You'll pay for what you've done to linux

Attached: computersciencetrannies.png (3640x2140, 1.45M)

typedef struct s_student student_t;

struct s_student {
char name[20 + 1];
double gpa;
unsigned int id;
student_t *right;
student_t *left;
};

void print_students(student_t *p)
{
if (!p) return;

printf("%s (%d) - gpa=%.2f\n", p->name, p->id, p->gpa);
print_students(p->left);
print_students(p->right);
}

int main()
{
student_t s1 = { "tyrone", 3.1, 1, NULL, NULL };
student_t s2 = { "jamal", 0.8, 2, NULL, NULL };
student_t s3 = { "chad", 99.9, 3, NULL, NULL };
student_t s4 = { "stacy", 7.7, 4, NULL, NULL };

s1.left = &s3;
s1.right = &s4;
s3.left = &s2;

print_students(&s1);

return 0;
}

fork the linux kernal and improve efficiency at 0.7%

1 2 3 GO!!

Write a function that prints the contents of a single linked list in reverse order. You must do this without creating a new list or data structure and cannot have a limiting factor greater than O(log n).

Here is your starting point.

typedef struct {
Node* next;
void data;
} Node;

Node n;

recursion

int gPA(struct student* root)
{
if (root == NULL)
return 0;
return (root->gpa / gPA(root->left) / gPA(root->right));
}

>using recursion

Attached: Dr Kill.jpg (540x586, 122K)

Heh storing on the stack is a kind of data structure

tnx

see there are still good people
although racist

the rules dont say i cant

Easy, just remove the page table isolation stuff added to mitigate meltdown.

Doesn't even compile.

GB emulator

The rules don't say I have to give you a job either. Better luck next time kiddo.

Attached: slaughter.webm (960x720, 2.95M)

what if i had 2000 students

Why the hate for recursions ?

alright then, reverse the list, print it, then reverse it back
dumb question

You don't have to write everything just the functions...also incorrect

How are you going to reverse the list?

>dumb question
*pulls out two whiteboards*

Attached: Nook.png (525x698, 565K)

Wtf is that user !?

you're wasting my time
I'm applying for a different job

#include
#include
#include
struct student {
char *name;
double gpa;
unsigned int student_num;
struct student *right;
struct student *left;
};
typedef struct student student_t;

void add_student(student_t **head, char *name, double gpa, unsigned int student_num) {
student_t *stud_to_add = NULL;
if(*head == NULL) {
*head = (student_t*)calloc(1, sizeof(student_t));
stud_to_add = *head;
stud_to_add->left = NULL;
}
else {
student_t *temp = *head;
while(temp->right != NULL) {
temp = temp->right;
}
student_t *next = (student_t*)calloc(1, sizeof(student_t));
temp->right = next;
next->left = temp;
stud_to_add = next;
}
stud_to_add->name = strdup(name);
stud_to_add->gpa = gpa;
stud_to_add->student_num = student_num;
stud_to_add->right = NULL;
}

void print_students(student_t *head) {
student_t *temp = head;
while(temp != NULL) {
printf("%s %0.2f %u\n", temp->name, temp->gpa, temp->student_num);
temp = temp->right;
}
}

int main() {
student_t *head = NULL;
add_student(&head, "Thomas Anderson", 3.2, 1337);
add_student(&head, "Robert E. Lee", 4.0, 1222);
print_students(head);
return 0;
}


$ gcc test.c -Wall
$ ./a.out
Thomas Anderson 3.20 1337
Robert E. Lee 4.00 1222


I'll leave freeing the memory to you.

When I interviewed for a job I used to have, I had to do this one on a whiteboard. It was fun, but I had the advantage of learning CS back when they taught data structures properly instead of DURR YUZE DUH STANDURD LIBURYZ

Then later I was on the other side of the interview. It was pathetic. The only ones doing any good were the EE grads. (Note that we weren't caring about syntax and shit, just can you juggle the pointers properly.) CS grads who had used Java as their main language in school were the worst.

Hmm, nah, I had to do "reverse a linked list", and this one is a bit more subtle in that you apparently have to leave the list un-reversed at the end. So reverse, print, then reverse again, I guess.

>O(2n)

Attached: Special Ed Akko.png (665x574, 266K)

>casting the return value of calloc()

that's not how big O notation works

you're a dumb goalpost-moving nigger.

/* sane struct declaration */
struct node {
char *data;
struct node *next;
};

void nigger(struct node* ptr)
{
if (ptr->next != NULL)
nigger(ptr->next);
printf("%s\n", ptr->data);
}

Why do people hate recursion?

typedef struct s_student student_t;

struct s_student {
char name[20 + 1];
double gpa;
unsigned int id;
student_t *right;
student_t *left;
};

void print_students(student_t *p)
{
if (!p) return;

printf("%s (%d) - gpa=%.2f\n", p->name, p->id, p->gpa);
print_students(p->left);
print_students(p->right);
}

static size_t timestamp()
{
size_t timestamp{ 0 };

__asm {
pusha
rdtsc
mov timestamp, eax
popa
};

return timestamp;
}

static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";

student_t *new_student()
{
static int s_id = 1;
student_t *s = (student_t*)malloc(sizeof(student_t));

if (!s) return NULL;

s->id = s_id++;
s->left = NULL;
s->right = NULL;

int i = 0;
for (; i < sizeof(s->name); i++)
s->name[i] = alphabet[timestamp() % (sizeof(alphabet) - 1)];
s->name[i] = 0;

s->gpa = timestamp() % 5;

return s;
}

void add_random(student_t *root, size_t n)
{
if (!n) return;

root->left = new_student();
root->right = new_student();
add_random(root->left, n - 1);
add_random(root->right, n - 1);
}

void free_students(student_t *root)
{
if (!root) return;

free_students(root->left);
free_students(root->right);
free(root);
}

int main()
{
student_t *s = new_student();

add_random(s, 10);
print_students(s);

free_students(s);
return 0;
}

it makes some people feel dumb when they don't understand it so they insist on making everything iterative

t. sepples

honestly, does anyone these days have to write their own linked list classes? I've done it but it seems like a pointless question to ask seeing it's liable to trip you up if you haven't done it before

I was told once that it was literally designed to filter out as many pajeets as possible since their kind of the most commonly known to just follow a bunch of C++ tutorials on youtube and then suddenly call themselves a software engineer.

never used sepples in my life

It does make people feel dumb, but iteration is usually faster because recursion involves many function calls which uses more processor than iteration. With iteration the loop only counts as one function call. Check out Big O notation algorithm efficiency of recursion vs iteration on the same task. Task example could be Fibonacci sequences.

Squabbling about casting calloc, imagine being this dumb.

Big O notation on recursion vs iteration is the same. It's the stack overhead that makes it slower. That barely matters, usually it just comes down to what is easier to write out in code

> Jow Forums making threads all the time that whiteboard interviews are bs
> People not knowing how to do linked list traversal

Attached: patrick.jpg (768x768, 28K)

wrong. fib recursive is 2^n and fib iterative is log(n) source : stackoverflow.com/questions/5958221/big-o-order-of-recursive-and-iterative-fib-functions

That O(log n) was clearly to keep R-tards from using either of the two stupid ways: recursion (limit in stack space), or "traverse entire list for element N, print it" "traverse entire list for element N-1, print it" etc., which is O(N^2).

It's when you're expected to know how linked lists work, not to build the whole thing from scratch. Asking someone to do a balanced tree insert is too big of a scope, but reverse a linked list is just right for a whiteboard test.


But really, the coolest thing I did with linked lists was when I discovered, during the middle of a programming contest, that you can do bubble sort with a singly-linked list. Sure, it's not super efficient as sorts go, but it's so easy to write when coding time is most important.

So why not try that as a challenge, guys... using the node structure from , only with actual data (say, a char*) instead of the void, do a bubble sort on the linked list.

Oh, and I ran out of time on that one, which would have been 5 for 5 for our team. It was to print a polynomial after multiplication, and I forgot to suppress the zero terms.

Attached: turbo pascal.jpg (689x891, 78K)

that's a different algorithm though
the same algorithm doesn't change complexity whether it's iterative or recursive

And now you know why 99% of Jow Forums users are unemployed.

That's why I said task. And btw it is still the same algorithm. Only diff is saving you value to heap instead of the stack.

>Only diff is saving you value to heap instead of the stack.
lol no, the iterative one is just using the last result, the recursive one is calculating the whole thing each time, hence the difference in complexity

EE grad here, fucking halfwits kept trying to force Java down our throats every year and the mentor / lecturer who was championing Java as the fucking marvelous answer to all couldn't fucking teach anything.

Every god dam one of the lecturers were more concerned with how good looking the final year project was instead of how it worked or why it worked. The guy who got top marks stuck a fucking camera on a remote control car, used a kinect to wave his arms around and control the car. All prebuilt libraries, all code he could never explain how it worked.

The way to solve the problem (algorithm) is the same fundamentally. The approach between the two is different. The different approaches involve saving values to heap instead of stack (dynamic programming) and not repeating calculations. You do not know what algorithms are, apparently.

not repeating the calculation makes it a different algorithm though
and all the values are saved on the stack

no it doesn't. its the same. just like if I asked you to give all the number from 1 to 5. you could say...1,2,3,4,5.....or you could say 1. 1,2. 1,2,3. 1,2,3,4.1,2,3,4,5. See? I solved the problem the same both ways (using n+1) but one is obviously more efficient.

the longer i stare at that picture the worse it gets.

Make a game engine BENID :DDDDD

Would you guys mind explaining this? Newbie here, and still getting a hold of pointers in general.

>storing gpa in a double

char[] is char*, but with memory automatically set aside in the struct or the stack.

#define NAME_MAX 21
char name[NAME_MAX];

Name can be passed as a pointer, and used with standard string functions as long as the 21st char remains 0. Just build your loops or memory copies with NAME_MAX taken into account.

jesus christ

That doesn't work.

typedef struct node node;
struct node{
node* next;
void *data;
}


This gets around having a struct with a pointer to itself.

If you use a fixed-size array for a buffer (especially declared at the top of a struct) you're vulnerable to buffer-overflow. Essentially the function that reads into the buffer does not check and make sure you're only putting in 20 characters and you can overwrite memory. People use this to insert instructions and jump to them; this executes their own code.

>20+1
Nigger what

>char[] is char*
Sort of correct. The type system actually remembers the size of the array in the former when possible. This is used in c++ to allow for each loops on arrays in some cases. In other words, char[] is NOT char*. Only at runtime this is true. I am very familiar with how pointers work in c/c++.
Makes null char more obvious.

sure without going into too much detail, it is because types are stored in different sizes by bytes. If he did a loop with integers as the counter to iterate over a char array(c-string), there is significant risk of iterating right on past the end of that char array because chars are stored as a single byte and int is stored as 2 or 4 bytes. His pointer will continue past the end of the array he is iterating over and into "unknown memory" creating a buffer overflow. This is also a bad ass end all attack if you iterate right into something like ....say....the location of the command shell program. Then you spawn a shell and pwn them.

Struct is actually just a way of specifying a memory layout. Classes don't guaranty that members are organized in memory in the order they appear in the code.

struct dat_ass{
int size; // First four bytes are the size
int weight; // Next four bytes are the weight
char is_black; // Last byte
};
.

If you do this:
struct dat_ass booty;
u8 *pointer_thingy = (&booty)+8;
.

pointer_thingy is now a pointer to booty.is_black.

leetcode.com/problems/the-skyline-problem/

I give my applicants two hours to solve literally this exact problem, and those that can't solve it don't get passed on.

you are also wrong. char[] and char* mean the EXACT same thing. They both point to the memory space at the beginning of the array.

I understand that, however, how is using char* any different from using char[ ] to avoid overflows? Is it because once you put text into a char* variable, it goes into "read only" memory? Not trying to be condescending, just simply curious.

If you're not careful, you can use up massive amounts of stack space. Each time you jump deeper into a recursive function, the memory for the previous recursions remain until you finish and jump back out.

Java doesn't have this problem

Attached: 1531255884023.png (326x326, 174K)

I agree with that. They aren't any different when talking about overflows. He should be using string class or creating a circular buffer

you can't fool unions with classes like you can with structs. because structs have a guaranteed memory layout the behavior for casting structs is well defined. this means you can create an unholy octopus from hell with unions of unions of structs cast as smaller structs. you can create a complex, deterministic, and yet highly flexible network with minimal overhead this way.

wow, I can double my gpa that easily? Why can't I just use "triple gpa"?

>tfw you didn't learn about bst

Java also does not correspond directly to assembly instructions like MIPS or ARM which is one step above binary. Java uses a virtual machine (JVM)....and once you know how to program using JVM instructions instead of java code you can overflow the same as c....in fact the JVM is programmed with C code. If you are somebody who wants full hardware control, C is the best bet aside from binary and assembly instruction. sweet pic though

>allocating something on the heap that can easily be statically or stack-allocated with proper buffer length checks
retard detected

I like using fixed length strings whenever I can. For example, I have the concept of a dataname_t.

#define DATANAME_LEN 32

typedef struct{
u8 len;
u8 ch[DATANAME_LEN];
}dataname_t;

// Takes a null-terminated string, and converts it to dataname_t
dataname_t dataname(u8 *name)
{
unsigned int len = strlen(name);
dataname_t r = {0};

if(len > DATANAME_LEN){
r.len = DATANAME_LEN;
}else{
r.len = len;
}
memcpy(&r.ch, name, len);

return r;
}


Advantages: Can store datanames in a nice array if need be. Can look at name.len to get its length without calling strlen(). Can save to a file with a fixed amount of memory, can read out of a file with a fixed amount of memory.

Implement malloc

This is why I always tell people to learn how to fucking use Unions!

Attached: trapageddon.jpg (452x616, 99K)

lol, yeah. Why program dynamically when you can just program regular?

They don't know about implementing lazy eval/tail recursion.

Better yet, just allocate a large chunk of memory up front, then create special-purpose allocators that work within sub-sections of that memory range.

Don't bother it's one of the dumbest data structures ever conceived by men and trannies.

maybe it's just because tail recursion is useless when you could just structure your data correctly and use loops. if the overhead of the stack is really that big of a pain for you and you think you're too intelligent for loops then use goto.