lainchan / λ
The Beginner's General
λx.x · 2020-01-02 18:19
This is the Beginner's General for beginners' questions.
If you have a simple question and a suitable thread doesn't already exist, just post it here and someone will probably try to answer it for you.
Remember to do some research before asking your question. No one wants to answer a question that a simple search can already resolve.
Backus · 2020-01-03 18:20
Does anyone have a recommendation of gui framework to use for C? Recently started learning and would like to make something more than cli programs
setq · 2020-01-03 20:28
>>16423
GTK, SDL, HTTP. And I would consider ncurses to be in a tier apart from "cli programs".
You could also pull a "Ray Tracing in One Weekend" and emit media files, like the very easy PPM.
aliased · 2020-01-03 20:54
>HTTP
Jim Hilger · 2020-01-03 22:45
>>16426
>HTTP
Wait, what? I assume you mean writing one of those web GUIs via web assembly, right?
>>16423
I'd recommend learning a curses implementation of your choice. Being able to write applications usable on a terminal is a definite plus.
synchronized · 2020-01-03 22:56
>>16432
>see: "HTTP"
>think: "electron?"
>think: "web assembly?"
I mean to use a browser as your interface. The easiest way to do this is just to have an existing web server call your C programs with a CGI handler. This has been a completely reasonable UI option for over two decades now, and a nice change from "boring cli programs". and it's quite easy if you understand the technology, which you should since you're knee deep in it.
It's obviously not a good option for a lot of purposes. I expect you to be able to figure out when it's not.
Cypher · 2020-01-04 01:01
>>16423
https://github.com/vurtun/nuklear
Try this one.
Milner · 2020-01-04 01:01
>>16423
Unless creating own from scratch, virtually everybody uses one of the frameworks off a top-10 list.... Qt, GTK, wxWidgets, TCL/Tk, maybe Agar or Flutter or one of the others, and of course Windows API if on Windows.
You can't do GUI programming at all without a video display of some sort. I wouldn't know how to do web GUIs but interesting idea.
The Plague · 2020-01-05 19:07
>>16438
Flutter has some new experimental support for desktop Linux. The bad news is it's very much alpha right now, and doesn't even work half the time. The good news is that it's NOT doing the RAM/CPU-slaughtering Electron-style "ship a whole fuarrrking web browser" thing, but actually compiles to small and efficient native binaries. All it really needs is another year or so of development.
I've been picking up some Dart in preparation for desktop Linux flutter development when it's ready for primetime, and testing using the web device. I'm liking the language so far. It feels like a less-braindamaged version of C#.
>>16432
>Being able to write applications usable on a terminal is a definite plus.
I've been playing with tview (https://github.com/rivo/tview) in Go a lot recently. It's really easy to set up a layout, since it's basically just straight-up copying CSS's flex and grid functionality. It's dead simple to set up a complex but automatically-responsive text GUI in just a hundred lines of code or so.
Nameless · 2020-01-07 05:16
my program is crashing when it tries to assign a value to a pointer.
Line 63
/*
PROJECT: C Primer Plus exercise 10 - 4
PURPOSE: "Write a function that returns the index of the larges value stored in
an array-of-double. Test the function in a simple program."
DATES: 20 dic '19, md -
*/
#include <stdio.h>
#define SIZE 8
void * Largestf (double Numbers[], double * Final, int * Scroll);
/*
ask the user for input
in a loop capture each individual number to a single slot of the array
call Largestf
printf result
*/
int main (void)
{
double Array[SIZE] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0}, * Result;
int List = 0;
printf("Give me some numbers, one per line:");
for (List = 0; List <= SIZE - 1; List++)
{
scanf("%lf", & Array[List]);
}
Largestf(Array, Result, & List);
printf("\nThe largest number there was: %lf", * Result);
printf(", the %d th on the group",List);
return 0;
}
/*
in a loop, compare all the numbers to Limit
if limit is the higest go down and keep going down until limit is equal to
a number in the array
If the array scroll goes too far set it back to zero
assign the value to the Final result and return it
*/
void * Largestf (double Numbers[], double * Final, int * Scroll)
{
double Top = 9.9;
int Step = * Scroll;
while (Top >= Numbers[Step])
{
Step++;
Scroll++;
if (Step >= SIZE)
{
Scroll = 0;
Step = 0;
Top = Top - 1.0;
}
}
* Final = Numbers[Step];
}
/*update all prose descriptions once done*/ What is the correct/safe way to do this? Are there any other issues with the program I'm not seeing?
Sipho Nkosi · 2020-01-07 09:09
>>16456
The specific problem causing the crash here is that you never initialized the pointer (Result), so when you try to set the value of "*Final" (Final coming from Result), it's going to point to some unexpected part in memory (causing the failure), because you never told what "Final" i.e. Result is supposed to point to, but since you just need to change a single value here, I'd suggest making the "Result" just a plain old double, and then calling Largestf(Array, &Result, &List);
I changed your code that way, and it gave the right number, not the right index though.
Aside from that one bug, I'd suggest changing Largestf entirely here, for one, you only need to return the index here, not the number itself (according to the exercise), so this entire pointer struggle you're fighting is unnecessary, but let's say you want to return both, using pointer arguments, there's still a lot of problems in the function here.
For one, you seem to be using Step and Scroll in a way that doesn't seem to work, and that I can't even understand tbf.
I don't know why they are both used to count the array index when you only need one, and you use while to make this weird "indented loop without an indented loop", also while I get what you're trying to do with the "Top" value and the double loop, going through the array with Top decreasing every time, this isn't what you want to do either, not only is it slow, it's often gonna give the wrong result. This isn't how you find the maximum of an array, instead you should do the following:
1. Take the "Maximum value", set to be lower than anything that could be in your array (maybe DBL_MIN, the smallest possible double)
2. Go through the array in a loop, if you find a member larger than your current "Maximum", set the maximum to be that value
3. Once you've walked through the whole array JUST ONCE, you'll have your maximum.
Here's how I'd rewrite the function:
void * Largestf (double Numbers[], double * Final, int * FinalIndex)
{
* Final = -DBL_MAX;
int index = 0;
while (index < SIZE)
{
if (Numbers[index] > *Final)
{
*Final = Numbers[index];
*FinalIndex = index;
}
index++;
}
} Note, you will need to include <float.h> to get the DBL_MIN. Also there might be a better way to rewrite this but this should work.
car · 2020-01-07 09:11
>>16458
You don't want to use DBL_MIN actually, but -DBL_MAX, DBL_MIN is the smallest positive double, which isn't what you want.
Marly Krushkhova · 2020-01-07 10:03
This is more a fetch quest than a question but I read part of a paper a while back named something like "Programming is a Very Good Medium For Formulating Poorly Thought Out Ideas" but even with a name that hyper-specific, I can't find it to save my life. If anyone knows what I'm talking about I'd be indebted if you sent me in the right direction so I could finish reading it.
Random · 2020-01-08 03:18
>>16458
>The specific problem causing the crash here is that you never initialized the pointer (Result)
I see, I'm just too used to normal variables that can be left uninitialized, glad I learned but after fixing it the program keeps giving me the same error on the same line
From gdb:
Program received signal SIGSEGV, Segmentation fault.
x0000000100401261 in Largestf (Numbers=xffffcb80, Final=x0, Scroll=x1c)
at exr.c:63
63 * Final = Numbers[Step]; I suppose these addresses mean something but it's beyond me at the moment and I'd appreciate a resource on how to read stackdumps.
>I'd suggest making the "Result" just a plain old double, and then calling Largestf(Array, &Result, &List);
I want to use pointers since that's what I learned on this chapter of my book
> you only need to return the index here, not the number itself (according to the exercise),
I wanted to do more just for the shake of flexing, I thought it'll be easy. It's a school mindset of doing more than what the homework tells you
>For one, you seem to be using Step and Scroll in a way that doesn't seem to work, and that I can't even understand tbf.
my compiler won't let me use a pointer (Scroll) to index the array so I had to create Step just for that.
>Top decreasing every time, this isn't what you want to do
Interesting, I'm still not into optimizing my code and I designed the program not expecting people to input numbers larger than 9 but what you say makes sense if the program was meant for a general audience, it's just a matter of experience.
Sadly I don't want to change the function, too much work and I'm very tired of wrestling with this program and I just want it to work and be done with it. thanks anyway
>Note, you will need to include <float.h> to get the DBL_MIN.
Still haven't learn about that library.
Wirth · 2020-01-08 10:42
>after fixing it
>Final=x0
>Sadly I don't want to change the function, too much work and I'm very tired of wrestling with this program and I just want it to work and be done with it.
t · 2020-01-08 15:07
>>16462
What exactly did you change? Could you just show me what you passed as the Final in your call for Largestf, and maybe the code of your new version of Largestf, if you changed it in some way. Either way, you shouldn't need to read stackdumps to see this, since this is just a pointer problem.
>I want to use pointers since that's what I learned on this chapter of my book.
That's fine on it's own, but if you're using a pointer, you DO need a double, for it to point to, otherwise, you don't even know what you're pointing at, so you should probably just pass the address of a double as a pointer argument. If, for some reason you want there to be a specific variable, that serves as a pointer in your main function, then maybe you'll want to do something like double Result, *ResultPointer = &Result; where you make a pointer, but it's set to point to the variable you need, then you pass ResultPointer as an arg to Largestf, but this just complicates things, when you could have just used &Result once.
Now I think I get what you were trying with Scroll and Step tho, but the reason why you're running into problems here, and why you seem to even be trying this Step thing, is just because you forgot to dereference Scroll several times throughout the function, you wanted to increment the List variable in line 53 right? Then you should have done (*Scroll)++; remember, incrementing a pointer doesn't increment the value, it just makes it point to the next int size block in your memory, you need to increment the value it's pointing to, same with line 57, *Scroll = 0; would mean "make List equal 0", but your line Scroll = 0; , just makes Scroll point to NULL. So by the time you've gone through this loop a couple of times, Scroll could be pointing at the moon for all you know, but it's definitely not pointing at List in main (which is probably what you want).
Also, don't think of that algorithm for finding a max in a list as an "optimization", it's the default way people write a "find maximum" or "find minimum" function, not trying to soykaf on you or anything, but your way of doing it is pretty bad, not just because it's slow, and doesn't work for numbers above 10, but also because it sometimes won't work for numbers under 10 either, for example, try to input a list of numbers in your program, where the biggest two amongst them are, say 5.3 and 5.4, and if you put 5.3 BEFORE 5.4, it will give 5.3 as the maximum, because that'll be the first number it comes across that's bigger than the "Top". This is why you should find it the usual way, by going through a loop, with a value that represents the maximum of everything you've found so far, and setting that maximum to a new value, if you find something bigger than it.
>Still haven't learn about that library.
You don't necessarily have to do it with -MAX_DBL, you could set the final max, to be just the first value of the array, or any value of the array, or some minimum like -1000000, if you're sure the user won't write a number smaller than that. What matters in this algorithm is that the original beginning value of Final (the max), isn't bigger than what it's end value will eventually be, because then it's never gonna change to what you want. -MAX_DBL is just the "cleanest" way to do it.
Case · 2020-01-09 05:37
>>16465
>What exactly did you change?
Initialized Result = 0; and added a return 0; on Largestf but after reading this comment this is how the function looks now.
void * Largestf (double Numbers[], double * Final, int * Scroll)
{
double Top = 9.9;
while (Top >= Numbers[(* Scroll)])
{
(* Scroll)++;
if (* Scroll >= SIZE)
{
* Scroll = 0;
Top = Top - 1.0;
}
}
Final = Numbers + (* Scroll);
return 0;
} Which is still quiet bad and still gives me an error.
>but if you're using a pointer, you DO need a double, for it to point to, otherwise, you don't even know what you're pointing at
That is what initializing Result in the main function does, right?
>but this just complicates things, when you could have just used &Result once.
I'm not understanding all of this, where should I put an &?
>you forgot to dereference Scroll several times throughout the function
I remember now, this is how it's done but I forgot it, I'm trying to do that now in the function I have and I'm probably doing it wrong.
>don't think of that algorithm for finding a max in a list as an "optimization"
I understand and it all makes sense but I'm really frustrated and tired from working on this for almost a month, I just want an easy answer to move on, rewriting the function to be more sound sounds like too much work, I rather move to the next exercise but if I don't fix the bug here I'll repeat it on the next exercise and just continue this soykaf.
I been working on this since before xmas, I want to scream.
>You don't necessarily have to do it with -MAX_DBL
Interesting I hope my book teache sme more libraries soon
>>16463
I'm just making things worse, I should quit programming, it's obviously not for me.
XERXES · 2020-01-09 08:19
>>16472
If you're not using the value of Largestf in any way, in other words, if the line where you call it is just Largestf(arguments); , the value is discarded, so adding return 0; and changing it's type doesn't really do much. But Result = 0; doesn't do what you want, by setting it to 0, you have made it point to NULL, that's guaranteed to give an error when you try to dereference it (*Result, or *Final in the function), you need to make a double variable, in other words you need SOME variable that's just going to have the type double , not a pointer to double, but a double, the pointer to double should point to SOME double, so here are two ways you might want to go about it.
In your main function, you could either make a both a double variable, and a pointer variable, that points to the double variable, like so
double Array[SIZE] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0}, ResultValue /* this is of type (double) */, * Result=&ResultValue /* this is of type (double *) */; then you could call Largestf, like you originally do Largestf(Array, Result, &List); , now, your program shouldn't crash because the double pointer is actually pointing to a double, unless there's some other issue, but another, more simple way you could do this is to just get rid of the seperate double* variable, and just do this
double Array[SIZE] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,0.0}, Result /* this is now of type (double), NOT a (double *) */;
.
.
.
Largestf(Array, &Result /* this is of type (double *) */, &List); now you can change the original Result variable from inside the function, without even having a seperate (double *) variable.
From what I can tell, your use of *Scroll itself seems fine now, including that last expression Final = Numbers + (* Scroll); , if you were trying to get Numbers[*Scroll] here, congrats, you did it correctly (tho a bit weirdly).
Also, while I get that this is all really frustrating to do, you should really try to write correct code, you don't want to get in the habit of writing code you know is bad, hoping you'll correct it "eventually".
>I'm just making things worse, I should quit programming, it's obviously not for me.
That might be true, but maybe it's not, I'm just an outsider looking in, so I don't know how clever you are or not, or how you're learning this, but from what I can see, I think the problem with you is you're trying to run before you've even learned to stand up, you're dealing with all of this finnicky, pointery stuff, and you're struggling to get it right, when you haven't yet quite learned how to find the maximum of an array, or that a pointer should have a value of it's pointed type to point to, before you try to dereference it, otherwise, bad stuff usually happens. Consider changing the book, or tutorial you're learning from, maybe it's made for someone who already has experience in programming, maybe you'll even want to change the whole language, but I wouldn't recommend that, some would disagree with me, but I think C is a good language for a beginner, provided you're going with a tutorial or book that's actually trying to work on your level of knowledge or familiarity. Don't just give up, and try to keep your head up, as hard as that might get. If it makes you feel any better, when I first tried to write a C++ program, to sum integers 1 to 100, i literally wrote a = 1 + 2 + 3 + ... + 100; , hoping the compiler would magically recognise what ... means and just give me a sum, safe to say, I've learned a lot since then, and I'm sure you can too.
Ender · 2020-01-09 11:12
>>16472
>I'm just making things worse, I should quit programming, it's obviously not for me.
No. That post showed the problem and then called out your attitude rather than your skill level. You have reiterated that attitude in your current post. If you do not quit and make it as a programmer from your current skill level, you will acquire a determination to overcome obstacles that most others will lack, and this will serve you well in all your endeavors.
>Final = Numbers + (* Scroll);
'Final' is local.
>Largestf
1. Print out your incoming value of *Scroll.
2. Run it on paper for {9.1, 9.2}.
>>16474
>seems fine now, including that last expression
>From what I can tell
Switch · 2020-01-09 11:44
>>16472
>what_does_a_monad_mean.png
Leave the "monoid in the category of endofunctors" for later, that is an in-joke for people who already know what a monad is. Read up on the Kleisli construction, after which a monad is an endofunctor with precisely those properties that make the associated Kleisli construction a category. After that you can derive monadic bind from the Kleisli composition, and it will not seem so mysterious anymore.
https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/
if · 2020-01-09 11:51
This >>16479 was obviously meant for >>16474.
Incognito · 2020-01-09 16:59
>>16478
yeah, I'm 16474, you're right, I messed up, I was focusing on his correction of his usage of *Scroll, and I didn't even see he was making the same mistake with Final. eh, after writing two of these walls of text, you're bound to miss something obvious.
also yeah his alg for figuring out the maximum in an array is wrong, as I've said as well.
Chani · 2020-01-10 04:56
>>16474
I understand now, to use pointers between functions I need them to point to something in the other function so they can return something to that function, using pointers like if they were variables was my mistake and this is what I wanted somebody to me.
The program is returning something now but it's still buggy tho I'm going to blame that on scanf, so I'm going to call this done even if is not.
My book assumes the reader is familiar with basic, fortran or pascal which is odd for a relatively recent book. I don't think the book is much of an issue, I just need a teacher who knows my train of thought and can stop me before I do something stupid. Another issue is that I have a background in arts and fell for the "learn to code" meme so I'm not a technically minded person but I struggle more with the language the programming itself, I always have a clear idea of how should my program go but mostly struggle with the quirks of the language, all my problem are always with a missing operand or stuff like that. What is really taking me down is that I've learned to see the scope of software that is actually useful and if I'm struggling with stuff that is just less than 100 and two functions real software will be impossible for me.>>16478
> If you do not quit and make it as a programmer from your current skill level, you will acquire a determination to overcome obstacles that most others will lack, and this will serve you well in all your endeavors.
Inspirational, thanks
hash table · 2020-01-10 11:02
>>16495
>I'm going to blame that on scanf
>I struggle more with the language the programming itself
>I always have a clear idea of how should my program go
>all my problem are always with a missing operand or stuff like that
As the what_does_a_monad_mean Lain has repeatedly pointed out, your "clear idea" about how to find the maximum of an array is wrong, and it would continue to be wrong even if perfectly implemented. Writing out the code is the last and easiest step of making the first version of a program, as it is merely a translation of your conceptual plan into the programming language of your choice. It is the creation of the conceptual plan that is the essential part of programming. No amount of proficiency with a particular programming language will turn a faulty conceptual plan into a correct one.
Zero Cool · 2020-01-11 05:53
>>16417
I think I have brain damage. Can someone give me the name/ set of colors used in lainchan's code enviroments? It's very pretty and I'd like to copy it for several things.
Hououin Kyouma · 2020-01-11 06:26
>>16503
pretty sure it's gruvbox
Undisclosed · 2020-01-11 07:06
>>16504
Thank you friend
Anamika · 2020-01-11 11:28
>>16503
<link rel="stylesheet" type="text/css" id="code_stylesheet" href="/stylesheets/code/gruvbox-dark.css"> https://lainchan.org/stylesheets/code/gruvbox-dark.css
https://github.com/lainchan/lainchan/tree/master/stylesheets/code
https://github.com/morhetz/gruvbox
Elliot Alderson · 2020-01-11 18:43
Hello. I want to developed a Firefox add-on which will redirect to a specific HTML file or website if someone tries to search certain word or visits certain website. I am wondering if something like this is possible?
Or removing websites from search result if they contain certain words?
Junichi Fujiwara · 2020-01-11 19:19
>>16511
Absolutely. Follow this tutorial: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Your_first_WebExtension
Then, make your own extension. You'll need the webrequest API in order to redirect pages. You will need to scan pages from the content script and alter the dom in order to remove elements from search results.
Jens Hansen · 2020-01-11 21:12
>>16512
Thank you for you answer ^_^ i got an another question. Can i make this add-on non-removable?
akrasia · 2020-01-12 13:16
>>16460
Is the paper "Why programming is a good medium for expressing poorly understood and sloppily formulated ideas" by Minsky?
I enjoyed the article. Can I have some recommendations on papers to read on similar topics? I'd really appreciate it :)
FILLER · 2020-01-12 13:19
>>16516
As far as I know, that's not doable, any and all extensions you install can be removed through the about:extensions page, the only way you could make some extra feature to firefox become unremovable is if you made your own version of firefox with this word filter thingy, but that's not really a one person job, so your only real option are removable extensions
aliased · 2020-01-16 00:05
I want to create a site, what should I learn first?
I know that is a simple question that could be answered with a simple google search, but ever guide that I found say something different.
I know how to program in Java, Python, and a bit of C++
I just wanna know all things that I should learn first to create a site.
HELIOS · 2020-01-16 00:36
>>16571
>ever guide that i found say something different
That's because, like most things, you can do the same piece of software with different techologies and whatnot.
>all things that i should learn first to create a site
You could go the usual way and read books about HTML->CSS->PHP->JS, but honestly, if you just want to create a site go for html, then you'd say "soykaf this looks kind of ugly" so you learn css and add it to your site, then you say "soykaf this lacks some basic functionality" so you learn and code some php. That is just an advice.
> i know how to program in java, python, and a bit of C++
Python is also used to build websites, actually one of lains objective is to port the whole site to python.
> Some useful websites
https://developer.mozilla.org/en-US/docs/Learn
https://github.com/tuvtran/project-based-learning
Faye Valentine · 2020-01-16 01:15
>>16571
Python + Flask
minimal: https://flask.palletsprojects.com/en/1.1.x/quickstart/#quickstart
basic: https://flask.palletsprojects.com/en/1.1.x/tutorial/#tutorial
basic-src: https://github.com/pallets/flask/tree/master/examples/tutorial