Post your dumbass code

Ok so I feel stupid as fuck right now. I'm trying to print a simple sinusoidal function in a console in c++.

Am I even going about it the right way? Can I ever learn to code?

Pick related.

Other urls found in this thread:

tldp.org/HOWTO/NCURSES-Programming-HOWTO/
twitter.com/NSFWRedditGif

dude
check this out
tldp.org/HOWTO/NCURSES-Programming-HOWTO/
they even have a man page
`man ncurses`

Are you trying to do something like one of these? #include #include #include #include using namespace std;constexpr int sz = 20, sz2 = sz / 2;void version1(){ char draw_buffer[sz * sz]; fill(begin(draw_buffer), end(draw_buffer), ' '); for (double res = 0; res < 30; res += 0.1){ int approx_sin = sz2 * sin(res) + sz2, approx_cos = sz2 * cos(res - sz2) + sz2; draw_buffer[approx_sin * sz + approx_cos] = 'X'; } for (int i = 0, ij = 0; i < sz; ++i, cout

i was playing this this in the challenge thread. runs fine, no compile errors.
#include #include int main(void){ char *a=malloc(1); char *b=malloc(1); a[0]='a'; a[1]='b'; //b-e haven't been allocated a[2]='c'; a[3]='d'; a[4]='e'; a[5]='f'; b[0]='d'; // why doesn't this overwrite a b[1]='i'; b[2]='n'; b[3]='d'; b[4]='u'; b[507]='n'; b[508]='u'; b[509]='f'; b[510]='f'; b[511]='i'; b[512]='n'; printf("a:%s\n",a); printf("b:%s%s\n",b,&b[7]); return 0;}

output:
$./a.outa:abcdefb:dindunuffin

malloc size is a placebo

forgot to change
printf("b:%s%s\n",b,&b[7]);
should be
printf("b:%s%s\n",b,&b[507]);

malloc implementations usually allocate from blocks of a uniform size, for efficiency reasons. So malloc(1) is really asking for a block of at least one byte.

you're almost certainly getting a smaller block than the 513 bytes you access, but the rest of it is still mapped into memory, because the malloc implementation expands the heap in large increments

Yeah, holy fuck I don't think I'll ever get to this level. Maybe I should just change majors or kms fml.

You don't deserve those trips. The reason why this works was already explained to you in the challenge thread. In explicit detail at that. What the fuck is wrong with you?

Maybe you just need practice; e.g. the easy challenges of or some programming challenge site like hackerrank.

Nah. Don't quit yet. Identify whether the problem is that you lack a sufficient conceptual understanding of the problem at hand, or that you understand the problem, but you're having difficulty creating a solution with the tool at hand (in this case, C++ and the features it provides).

If the former, you're going to have to work harder to ensure that you have a solid understanding of the problem you're trying to solve before you attempt to tackle it in code. What is it, exactly, that you're trying to do? You may even need to get paper and pencil and work it out visually, by sketching or otherwise. I was once trying to work out a problem that had to do with a cube, so I actually made a physical cube out of paper to help me visualize.

If the latter, you're going to have to work to understand how to translate your understanding of the problem into something amenable to solution with the tool at hand (C++). Try to identify what's going wrong. Have you made incorrect assumptions about the way a C++ feature is working? Break the problem down into smaller parts (if possible) and try to solve them individually.

If you have both problems, well, then you have even more hard work in front of you. But it's not impossible. Try to get enough rest, but spend as much time as reasonably possible working toward a breakthrough.

One final piece of advice, though it's not always applicable. C++ is a high-level language, but many people don't find it as easy as, say, Python or Ruby. Sometimes, it can be beneficial to prototype something in Python before tackling it in C++ (or C, or another compiled language). I was once trying to implement an algorithm I read about in a CS journal in Java, and I thought I had it, but something was totally fucked up. The program compiled and ran, but the results I was getting when I ran the algorithm on a known dataset were totally wrong, and I couldn't tell whether the problem was with my understanding of the algorithm, or my understanding of Java (which I didn't know well). So I tried it in Python (which I was much more familiar with), and quite quickly had the algorithm implemented and got the correct results. It was helpful, because it told me that I understood how the algorithm worked, but something about my understanding of Java was fucked up, and I needed to go back and test and figure out where my understanding of what I was doing in Java had gone wrong. This was ~10 years ago, and I forget what it was, but I did eventually figure it out. I probably saved more time with my Python detour than I would have just staring at the Java code with no idea of how/where I fucked up.

Work hard, and in the end, you'll probably get it. If you enjoy the hard work (or, at least, if the result it worth it), you're good. If you hate it, consider whether you hate it more than you would doing something else with your life. If not, stick with it. Most people hate their jobs.

Good luck.

...

what did he mean by this?

1. It looks like you are passing degrees instead of radians into sin/cos
2. Swap your x and y variable names. When you go down a line you would expect y to change, not x.
3. Scrap the for loop stuff. You can either replace it with ncurses or use escape codes to move the cursor around in your terminal. For the later approach you print out some blank lines then move the cursor to where the x needs to go and then print it

There's probably a better way to do this, but without knowing the specifications for what you want to do, I can't give a good suggestion.
eg. A cleaner way to do it would be to iterate over all the (x, y) and compute whether it needs to be filled or not.

Unless you're one of the "achtually it doesn't extend C at all" autists, C++ is nothing more than additions to C (hence, plus plus) and nearly all of that is firmly in high level language territory. Templates are even functional programming.

Protip: add printf("a = %p - b = %p\n", a, b); at the end of your code.


It is definitely not as high-level as Haskell or Python, but I wouldn't call a language with templates, lambdas, smart pointers, or even classes strictly "low-level". It ultimately depends of your definitions of low-level and high-level, but I would call it a high-level language with low-level facilities.
Anyway it's an useless semantic debate.

i think i have a basic understanding of why it's not fucking up. i posted it in the "dumbass code" thread because i can imagine fucking up this way in a much more subtle accidental way down the line with an off by 1 or something, and it's going to be fun to try to debug when there's no warnings or errors and it'll work fine until it doesn't. i'm sure the shit assigned in the 500+ range could get overwritten at some point, but it's a could, not a guarantee, so if I understand this right if it does get overwritten and fuck everything up, it's not going to be repeatable every time.

#include #include #include const gchar *str = "We must secure the existence of our people and a future for white children. ";gint direction = 1;gint cx = 0;gint slen = 0;GtkLabel *label;voidchange_dir(GtkLayout *o, gpointer d) { direction = -direction;}gchar *rotateby(const gchar* str, gint start, gint len) { gint i, j, iter_len = len; gchar *newstr = calloc(len+1, 1); for (i = start, j = 0; iter_len > 0; iter_len--, i = (i + 1) % len, j++) newstr[j] = str[i]; newstr[len] = 0; return newstr;}gbooleanscroll_it(gpointer data) { if ( direction > 0 ) cx = (cx + 1) % slen; else cx = (cx + slen - 1 ) % slen; gchar *scrolled = rotateby(str, cx, slen); gtk_label_set_text(label, scrolled); free(scrolled); return TRUE;}intmain(int argc, char **argv) { GtkWidget *win; GtkButton *button; PangoFontDescription *pd; gtk_init(&argc, &argv); win = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(win), "卍 HEIL HITLER 1488 卍"); g_signal_connect(G_OBJECT(win), "delete-event", gtk_main_quit, NULL); label = (GtkLabel *)gtk_label_new(str); // since we shift a whole character per time, it's better to use // a monospace font, so that the shifting seems done at the same pace pd = pango_font_description_new(); pango_font_description_set_family(pd, "monospace"); gtk_widget_modify_font(GTK_WIDGET(label), pd); button = (GtkButton *) gtk_button_new(); gtk_container_add(GTK_CONTAINER(button), GTK_WIDGET(label)); gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(button)); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(change_dir), NULL); slen = strlen(str); g_timeout_add(125, scroll_it, NULL); gtk_widget_show_all(GTK_WIDGET(win)); gtk_main(); return 0;}
gcc $(pkg-config --libs --cflags gtk+-2.0 pango) -o animtext animtext.c

bad code time?

...

My generic makefile. Is there a simpler way to do PGO? Using recursive make forces me to seperate user and makefile set C/LDFLAGS (painfully).

TARGET := '$(shell basename -- $(shell pwd))'CC ?= cc_CFLAGS := -D_DEFAULT_SOURCE -std=c99 -pedantic -Wall -Wextra \ -fdiagnostics-color=autoCPPFLAGS := -MMD -MP_LDFLAGS :=# If using a ~/local library prefix# PREFIX := "$$HOME/local"# CPPFLAGS := -I$(PREFIX)/include -MMD -MP# _LDFLAGS := -Wl,-rpath $(PREFIX)/lib -L$(PREFIX)/libLDLIBS :=CONFIG ?= Debugifeq ($(CONFIG),Debug) _CFLAGS += -Og -g3 -fsanitize=address,undefined _LDFLAGS += -fsanitize=address,undefined -rdynamicelse ifeq ($(CONFIG),Profile) _CFLAGS += -Og -g3 -DNDEBUGelse ifeq ($(CONFIG),Release) _CFLAGS += -O3 -DNDEBUGelse$(error CONFIG invalid value; must be "Debug", "Profile" or "Release")endifSRC := $(wildcard *.c)OBJ := $(SRC:%.c=%.o)DEP := $(OBJ:%.o=%.d).PHONY: all clean pgo testall: $(TARGET)$(TARGET): $(OBJ) $(CC) $(_LDFLAGS) $(LDFLAGS) $^ -o $@ $(LDLIBS)%.o: %.c $(CC) $(CPPFLAGS) $(_CFLAGS) $(CFLAGS) -c $< -o $@clean: rm -rf -- $(TARGET) $(OBJ) $(DEP)pgo: $(MAKE) clean $(MAKE) CONFIG=$(CONFIG) CFLAGS="$(CFLAGS) -fprofile-generate" \ LDFLAGS="$(LDFLAGS) -fprofile-generate" $(MAKE) test $(MAKE) clean $(MAKE) CONFIG=$(CONFIG) CFLAGS="$(CFLAGS) -fprofile-use" LDFLAGS=$(LDFLAGS)test: ./$(TARGET)-include $(DEP)

I use something simpler, without dependency generation.
CC?=gccCFLAGS?=-O2CPPFLAGS?=LDFLAGS?=LIBS?=PREFIX?=/usr/localBINDIR?=$(PREFIX)/binOUT_PROG?=my_progDEBUG?=0ifeq ($(DEBUG),1) CPPFLAGS+= -DDEBUGendifinclude src/libshit/Objects.mkinclude src/main/Objects.mkOBJECTS:=\ $(LIBSHIT_OBJECTS)\ $(MAIN_OBJECTS)\ src/main.o.PHONY: all install uninstall cleanall: src/$(OUT_PROG) @echo " DONE $(OUT_PROG)"src/$(OUT_PROG): $(OBJECTS) @echo " LD $(OUT_PROG)" @$(CC) $^ -o $@ $(LDFLAGS) $(LIBS)%.o: %.c @echo " CC $(OUT_PROG)" @$(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS)install: all @cp -v src/$(OUT_PROG) $(BINDIR)/ @echo " INSTALL $(OUT_PROG)"uninstall: @rm -fv $(BINDIR)/$(OUT_PROG) @echo " UNINSTALL $(OUT_PROG)"clean: @rm -fv $(OBJECTS) $(OUT_PROG) @echo " CLEAN $(OUT_PROG)"

What benefits does dependency/makefile generation have?

PGO is always going to be messy, it's just how it is.
Recursive make is usually how it's done. Hacking on variables should really be done as a configure step so you don't end up with horror. The meme of avoiding autotools is a meme.