Noob learns to code in 3 months

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Noob learns to code in 3 months

Post by albinopapa » June 13th, 2017, 6:19 pm

Yumtard wrote:Notes from part 1:

- pointers are stored in hexadecimal
Spoiler:
Image
lol, just reading this and looking at your avatar makes it better.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » June 14th, 2017, 1:13 am

Yumtard wrote:Study update

- Watched 4.1 4.2 and 4.3 of beginner at 1.25 speed

- Turned on notifications on youtube for chili. Want that bonus video! :D

- Answered questions from wiki

- Watched part 0 and 1 of intermediate (hype)
albinopapa wrote:Congratulations and good luck.
ty sir :) have a few decent back up plans if i don't get accepted but I really hope I won't have to use them. Would suck to get this far and not get in.
Glad to see you're digging into the Intermediate stuff. Bonus video probably isn't gonna help you out too much, a lot of the advice I have for people is stuff you're already doing ;)
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 14th, 2017, 2:25 am

@albinopapa

:D I love that pic

@chili

Yeah it's about time :p First vid was very interesting btw. Some of the stuff is kinda hard to wrap my head around tho.

Hype for pointers today!

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 14th, 2017, 8:06 am

Study update

- Watched 2.1 and 2.2

- did homework for both

2.1 was some great repetition.
I basically already knew this stuff even though at times, chili had me looking like
Image

Repetition was much needed.

Here's how my hw for 2.1 came out

Code: Select all

int add(int *arr, int size)
{
	int sum = 0;
	for (int i = 0; i < size; ++i)
	{
		sum += arr[i];
	}

	return sum;
}

int main()
{
	const int arrSize = 4;
	int myArray[arrSize] = { 911, 69, 420, 666 };

	cout << add(myArray, arrSize) << endl;


	return 0;
}

So basically the same as in the hw solution video
Image


Moving on to 2.2
Bit more new stuff here, enjoyed it but got a bit confusing when chili went all
https://www.youtube.com/watch?v=8amtq5aXRL8

I was happy to hear pee pees aren't really used much

hw for 2.2

Code: Select all

int add(int *arr, int size)
{
	int sum = 0;
	for (int *e = arr + size; arr < e; arr++)
	{
		sum += *arr;
	}

	return sum;
}
and here's second task. This one actually took me a while to figure out.

Code: Select all

void Reverse(int *arr, int size)
{
	for (int *e = arr + size - 1; arr < e; arr++)
	{
		const int temp = *arr;
		*arr = *e;
		*e = temp;
		e--;
	}
}

And that worked. Pretty close to what chili did but he used a std::swap.
I was also unaware you could increment/decrement multiple variables inside the parenthesis of the for loop.

I will rewatch these 2 videos and write some notes before moving on in hopes of making some of the more confusing parts less confusing

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 15th, 2017, 10:39 am

Study update

Been doing about 5 different super simple console apps (mostly for fun)
Latest one was after watching a youtube video where the question was: What is the average distance between 2 points inside a square with the length of 1?

Code: Select all

#include <random>
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	double x1, x2, y1, y2;
	std::random_device rd;
	std::mt19937 rng(rd());
	uniform_real_distribution<double>dist(0, 1);
	int sample;
	double total = 0;
	double distance;

	cout << "Samples: ";
	cin >> sample;

	for (int i = 0; i < sample; i++)
	{
		x1 = dist(rng);
		x2 = dist(rng);
		y1 = dist(rng);
		y2 = dist(rng);

		distance = sqrt(((x1-x2) * (x1-x2)) + ((y1-y2) * (y1-y2)));
		total += distance;
	}
	
	cout << "Average distance = " << total / sample << endl;

	return 0;
}
Edit: Can't post the other ones because I delete them when making a new one so I don't have to make a new project. They're all just as simple and dumb tho

Now I'm gonna rewatch parts of the pointer vids

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 15th, 2017, 12:01 pm

Notes

int *p;

^ Declares a pointer that can point to an int

int n = 5;
p = &n

^ use the addressof operator to make the pointer point to the address in memory where n lives.

*p = *p + 64;

^ dereferencing the pointer with * gives it access to the value stored at the address it's pointing to.
For example:
int n = 5;
int *p;
p = &n;

cout << p << endl;
cout << *p << endl;

first line will cout the address of n, where in memory n is stored.
Second line will cout 5, the value stored in that address.

The difference between references and pointers is that a pointer can be retargeted
int m = 3;
int &refN = n; // reference to n
int *p = &n; // pointer to n
p = &m; // retargets p to point to m;
refN = m; // NOT allowed

Pointers also can be declared without being init.
int* p;
like so, while references has to be initilized.
int& ref; //Doesn't work

Always prefer to use variables directly if possible.
If not possible then prefer to use references over pointers if possible (if retargeting isn't needed)

non static member functions has a pointer variable automatically defined for them, called "this"
it points to the object on which the member function was called.

int arr[3] = {1, 2, 3};
int* pArr = arr;

^ pArr points to the first element in the array.
I can also write:
int* pArr = &arr[0];

now if I change
pArr[2] = 69 // it will change the element 2 over to 69

if I make a function that gets an array as argument and need to loop through it.

void loop(const int* arr, int size)
{
for (int i = 0; i < size; i++)
{
cout << arr << endl;
}

// I can also do it like this

for (const int* e = arr + size; arr < e; arr++)
//init *e to point to the address size over where arr is pointing
// as long as arr is smaller than e I will increment arr with one int in memory
{
cout << *arr << endl;
//Keep in mind if I do not dereference here, it will print out the addresses of the ints
}
}

when I increment the pointer, jumps to the next int in memory. An int is 4 bytes so incrementing would add 4 to the address

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob learns to code in 3 months

Post by chili » June 15th, 2017, 1:13 pm

Looks like you got basically 100% of the shit about pointers man. Good stuff.

It's true that you don't need to worry much about int** pp = &p; bullshit. It is used very seldomly, the less it is used the better TBH.

Have you checked out any of the Red Pill videos yet? :)
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 15th, 2017, 4:28 pm

Study update

Watched part 3.
A bit tedious when you know there's easier ways to get the same thing done (someone else has already done it for you)
But I still thought it was interesting to find out how this shit actually worked.
The tutorial was great and easy to understand how it worked. Don't think I'd be able to do half the shit on my own without cheating though.

HW

After a lot of thinking and drawing blanks I ended up making a Fib function that I didn't like.
The function seemed ugly and confusing and I knew there should be a better way of doing it. Was trying a few times with recursion but I suck at that and just failed

Here's my dumb fib function:

Code: Select all

int Fib(int n)
{
	int first = 0;
	int second = 1;
	int sum;

	if (n == first || n == second)
	{
		return n;
	}
	else
	{
		for (int i = 2; i <= n; i++)
		{
			sum = first + second;
			first = second;
			second = sum;
		}
		return sum;
	}
}
Also realized I need a function to convert ints to strings. Had some trouble with this too and I aso had to peak for the already made functions (print, read, int2str)
So now it's been a couple of hours and I got annoyed so I'm just gonna check out the solution video :p

Will rewatch the tutorial some time this week.


chili wrote:Looks like you got basically 100% of the shit about pointers man. Good stuff.

It's true that you don't need to worry much about int** pp = &p; bullshit. It is used very seldomly, the less it is used the better TBH.

Have you checked out any of the Red Pill videos yet? :)
Yay :D
I haven't but I'm planning to :)
My schedule looks like this:
friday: part 4 and possibly red pill pointers
saturday: red pill the stack (and pointers if I got stuck on part 4 on friday)
sunday: Repetition (rewatching parts of the vids from this week)

Then next week I'll finally dive into my c++ book, reading as much as possible and doing as many assignments from it as possible (will post them here, there's no solutions for the assignments in the book)
And take a break from the book whenever a new video is out.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 15th, 2017, 4:33 pm

Code: Select all

int fib(int n)
if (n < 2)
{
return n;
}
return fib(n - 1) + fib(n - 2);
Well that was a lot prettier :D

Edit:
Alright I've watched the HW video now. I wouldn't have figured this shit out anyways so I'm fine with the desicion to cheat.
Rewatching part 3 is def on the schedule for sunday

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Noob learns to code in 3 months

Post by Yumtard » June 16th, 2017, 1:36 pm

Decided to steal zedthos idea and make a simon says game. I still suck at planning games and always feel back on square one when i start a new project so it will be good practice
Will update on progress later

Post Reply