|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
Honestly one of my favourite things in C++ is initialization lists. Not a big deal usually, but for prototyping it's really nice to be able to do std::set<int> combos = { 1, 2, 3, }; and have it compile. Or just call funcThatTakesInAList({ 1, 2, 3, });.
Otherwise I really like C++11 onwards, nullptr_t, range fors, strongly typed enums, auto return type, const exprs.
edit: apparently you can add ' to numbers to help readability, so 1'000'000 is the same as 1000000 :o
edit: binary literals too whoa 0b010 compiles :o
|
On April 29 2016 00:23 tofucake wrote: no, netbeans is stupid
At first I thought it was me being awful but: NetBeans keeps marking me things a wrong, saying identifiers are missing etc, yet the program compiles and runs fine! I made changes, made a cleanbuild etc. everything runs and compiles fine, even though it keeps marking things as wrong. Of course I am awful at coding, but what the hell if wrong with this IDE…
|
Honestly either it's that you need to more aggressively clean the build (because netbeans isn't recompiling files you've changed), or you should really just not use an ide if you can avoid it. Or at least use CLion or VS.
Dicking around with CMake is really enjoyable IMO. Learning stuff about build process.
Plus the output is pretty 😍
https://github.com/Blisse/CS452-Kernel/blob/master/CMakeLists.txt
|
I am forced to use netbeans for a number of reasons. I am just baffled how "incredible" it is, given the fact that we are forced to work with it in some lectures. Right now I am just coding with gedit and giggle like a retard when I copy my code into netbeans and it just compiles even though it marks almost everything as wrong. It is better then the other way around
|
GRAND OLD AMERICA16375 Posts
On May 01 2016 02:21 Artesimo wrote:I am forced to use netbeans for a number of reasons. I am just baffled how "incredible" it is, given the fact that we are forced to work with it in some lectures. Right now I am just coding with gedit and giggle like a retard when I copy my code into netbeans and it just compiles even though it marks almost everything as wrong. It is better then the other way around 
netbeans is fine but you have to spend time to set the IDE up right.
|
Since I'm trying to brush up on my C a bit (been years since I last did anything in it) I'm here to ask you guys if this code is acceptable and please point out any errors or give suggestions if you have them.
I've decided to start with something relatively simple - generating an array of numbers in range (eg. irange(1, 3) results in {1, 2, 3}, irange(3, 1) results in {3, 2, 1} etc.). Generating forward and reverse arrays of letters comes next:
#include <stdio.h> #include <stdlib.h>
typedef struct integer_range integer_range;
integer_range *irange(int start, int end);
struct integer_range { int length; int nums[]; };
integer_range *irange(int start, int end) { int reverse = (end < start); int size = (reverse ? start - end : end - start) + 1; size_t mem = sizeof(integer_range) + size * sizeof(int); integer_range *range = malloc(mem);
range->length = size;
for (int i = 0; i < size; i += 1) { range->nums[i] = reverse ? start - i : start + i; }
return range; }
int main(void) { integer_range *range = irange(7, -1);
for (int i = 0; i < range->length; i += 1) { printf("%d\n", range->nums[i]); }
free(range);
return 0; }
Please don't mind the main function for now. It's just there for testing purposes...
|
When you're defining types you typically include an _t at the end of the names.
typedef struct integer_range_s { int length; int nums[]; } integer_range_t;
I'd also question your use of malloc in this scenario. The stack is large enough to have a small array printed, unless you're intending very large ranges to be printed or wanted to do some memory management.
|
On May 01 2016 03:52 Blitzkrieg0 wrote:When you're defining types you typically include an _t at the end of the names. struct integer_range_s { int length; int nums[]; } integer_range_t; I'd also question your use of malloc in this scenario. The stack is large enough to have a small array printed, unless you're intending very large ranges to be printed or wanted to do some memory management.
I'm trying to remember how to properly use malloc and such  Also, I was a bit afraid of getting segfaults while trying to populate an unitialized variable length array inside of struct (trying to malloc it with too small size sure did that).
Edit: Are there any current "best practices" or something like that for C this days?
|
On May 01 2016 04:02 Manit0u wrote:Show nested quote +On May 01 2016 03:52 Blitzkrieg0 wrote:When you're defining types you typically include an _t at the end of the names. struct integer_range_s { int length; int nums[]; } integer_range_t; I'd also question your use of malloc in this scenario. The stack is large enough to have a small array printed, unless you're intending very large ranges to be printed or wanted to do some memory management. I'm trying to remember how to properly use malloc and such  Also, I was a bit afraid of getting segfaults while trying to populate an unitialized variable length array inside of struct (trying to malloc it with too small size sure did that). Edit: Are there any current "best practices" or something like that for C this days?
Yes, the best practice is to use C++ over C. No, really, why bother with all the primitive stuff C forces you to do when you can get all of that and safety from C++?
Edit: Why do you increment loop like i += 1 ? The common way is ++i instead.
Edit #2:
int reverse = (end < start); int size = (reverse ? start - end : end - start) + 1;
Please use booleans. They exist even in C.
|
On May 01 2016 08:47 Shield wrote:Show nested quote +On May 01 2016 04:02 Manit0u wrote:On May 01 2016 03:52 Blitzkrieg0 wrote:When you're defining types you typically include an _t at the end of the names. struct integer_range_s { int length; int nums[]; } integer_range_t; I'd also question your use of malloc in this scenario. The stack is large enough to have a small array printed, unless you're intending very large ranges to be printed or wanted to do some memory management. I'm trying to remember how to properly use malloc and such  Also, I was a bit afraid of getting segfaults while trying to populate an unitialized variable length array inside of struct (trying to malloc it with too small size sure did that). Edit: Are there any current "best practices" or something like that for C this days? Yes, the best practice is to use C++ over C.  No, really, why bother with all the primitive stuff C forces you to do when you can get all of that and safety from C++? Edit: Why do you increment loop like i += 1 ? The common way is ++i instead. Edit #2: int reverse = (end < start); int size = (reverse ? start - end : end - start) + 1;
Please use booleans. They exist even in C.
I don't like C++.
i += 1 is a habit from writing python scripts, I don't mind it 
Using bools is a good idea, I'll be sure to do that.
|
Has anyone here tried HackerRank challenges in JavaScript?
I'm trying this one right here and I don't know how to deal with their inputs. The input is served as an array like so:
console.log(_input[0],_input[1],_input[2],_input[3],_input[4],_input[5],_input[6]); [2, "", 1, "", 0, "", 1, "", 0]
I've solved 2 challenges using this method before but I'm aware of how idiotic it is. Anyone know if I'm doing it wrong or if I should just do everything in Java instead?
|
On May 01 2016 13:29 Thaniri wrote:Has anyone here tried HackerRank challenges in JavaScript? I'm trying this one right here and I don't know how to deal with their inputs. The input is served as an array like so: console.log(_input[0],_input[1],_input[2],_input[3],_input[4],_input[5],_input[6]); [2, "", 1, "", 0, "", 1, "", 0] I've solved 2 challenges using this method before but I'm aware of how idiotic it is. Anyone know if I'm doing it wrong or if I should just do everything in Java instead?
I got a different input, essentially just a newline separated string "2\n10\n100".
function processData(input) { var count = null; input.split("\n").forEach(function(item) { // First item is number of testcases, we don't need that // Just storing it for completeness sake if (count === null) { count = parseInt(item); return; } var sum = 0; for (var i=1; i < parseInt(item); ++i) { if (!(i % 3) || !(i % 5)) sum += i; } sum && process.stdout.write(sum+"\n"); }); } PS: That is ugly code. Don't write production code like that.
|
Thanks man, didn't know an array could be manipulated like that. I'm an amateur programmer anyway I can't even tell what good code looks like. It's like asking an american what good cheese is if they've been eating it out of a jar or cheddar for their whole life.
|
On May 01 2016 03:36 Manit0u wrote:Since I'm trying to brush up on my C a bit (been years since I last did anything in it) I'm here to ask you guys if this code is acceptable and please point out any errors or give suggestions if you have them. I've decided to start with something relatively simple - generating an array of numbers in range (eg. irange(1, 3) results in {1, 2, 3}, irange(3, 1) results in {3, 2, 1} etc.). Generating forward and reverse arrays of letters comes next: + Show Spoiler + #include <stdio.h> #include <stdlib.h> // Include stdbool.h to get booleans
// Merge the typedef with the declaration of the struct typedef struct integer_range integer_range;
integer_range *irange(int start, int end);
struct integer_range { int length; int nums[]; };
integer_range *irange(int start, int end) { // Use booleans for the next two declarations int reverse = (end < start); int size = (reverse ? start - end : end - start) + 1; size_t mem = sizeof(integer_range) + size * sizeof(int); integer_range *range = malloc(mem);
// Check return value of malloc.
range->length = size;
for (int i = 0; i < size; i += 1) { range->nums[i] = reverse ? start - i : start + i; }
return range; }
int main(void) { integer_range *range = irange(7, -1);
for (int i = 0; i < range->length; i += 1) { printf("%d\n", range->nums[i] ; }
free(range);
return 0; }
Please don't mind the main function for now. It's just there for testing purposes...
Threw in a few comments in the spoiler for your code.
|
On May 01 2016 19:39 bangsholt wrote:Show nested quote +On May 01 2016 03:36 Manit0u wrote:Since I'm trying to brush up on my C a bit (been years since I last did anything in it) I'm here to ask you guys if this code is acceptable and please point out any errors or give suggestions if you have them. I've decided to start with something relatively simple - generating an array of numbers in range (eg. irange(1, 3) results in {1, 2, 3}, irange(3, 1) results in {3, 2, 1} etc.). Generating forward and reverse arrays of letters comes next: + Show Spoiler + #include <stdio.h> #include <stdlib.h> // Include stdbool.h to get booleans
// Merge the typedef with the declaration of the struct typedef struct integer_range integer_range;
integer_range *irange(int start, int end);
struct integer_range { int length; int nums[]; };
integer_range *irange(int start, int end) { // Use booleans for the next two declarations int reverse = (end < start); int size = (reverse ? start - end : end - start) + 1; size_t mem = sizeof(integer_range) + size * sizeof(int); integer_range *range = malloc(mem);
// Check return value of malloc.
range->length = size;
for (int i = 0; i < size; i += 1) { range->nums[i] = reverse ? start - i : start + i; }
return range; }
int main(void) { integer_range *range = irange(7, -1);
for (int i = 0; i < range->length; i += 1) { printf("%d\n", range->nums[i] ; }
free(range);
return 0; }
Please don't mind the main function for now. It's just there for testing purposes... Threw in a few comments in the spoiler for your code.
Thanks. By checking return value of malloc do you mean something like that?
if (range == NULL) { return -1; // something went wrong }
|
On May 01 2016 21:00 Manit0u wrote:Show nested quote +On May 01 2016 19:39 bangsholt wrote:On May 01 2016 03:36 Manit0u wrote:Since I'm trying to brush up on my C a bit (been years since I last did anything in it) I'm here to ask you guys if this code is acceptable and please point out any errors or give suggestions if you have them. I've decided to start with something relatively simple - generating an array of numbers in range (eg. irange(1, 3) results in {1, 2, 3}, irange(3, 1) results in {3, 2, 1} etc.). Generating forward and reverse arrays of letters comes next: + Show Spoiler + #include <stdio.h> #include <stdlib.h> // Include stdbool.h to get booleans
// Merge the typedef with the declaration of the struct typedef struct integer_range integer_range;
integer_range *irange(int start, int end);
struct integer_range { int length; int nums[]; };
integer_range *irange(int start, int end) { // Use booleans for the next two declarations int reverse = (end < start); int size = (reverse ? start - end : end - start) + 1; size_t mem = sizeof(integer_range) + size * sizeof(int); integer_range *range = malloc(mem);
// Check return value of malloc.
range->length = size;
for (int i = 0; i < size; i += 1) { range->nums[i] = reverse ? start - i : start + i; }
return range; }
int main(void) { integer_range *range = irange(7, -1);
for (int i = 0; i < range->length; i += 1) { printf("%d\n", range->nums[i] ; }
free(range);
return 0; }
Please don't mind the main function for now. It's just there for testing purposes... Threw in a few comments in the spoiler for your code. Thanks. By checking return value of malloc do you mean something like that? if (range == NULL) { return -1; // something went wrong }
-1 for a * isn't so cool. Just return NULL.
|
On April 29 2016 17:08 Djagulingu wrote:Show nested quote +On April 27 2016 23:42 WarSame wrote:On April 27 2016 18:41 Wrath wrote:On April 27 2016 10:43 WarSame wrote:Folks, looking for a bit of help with mean.io. I dled and installed node, mongodb and git. I then init a directory and run npm install inside of it. I then run gulp to start the server. This throws errors that I'm missing various modules such as 'mongoose', 'async', etc. but these should be installed from the npm install, correct? The exact error is + Show Spoiler +E:\mean_workspace\monoprobability\node_modules\q\q.js:155 throw e; ^
Error: Cannot find module 'mongoose' at Function.Module._resolveFilename (module.js:325:15) at Function.Module._load (module.js:276:25) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object.<anonymous> (E:\mean_workspace\monoprobability\node_modules\meanio-users\server\models\user.js:6:17) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at requireModel (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\index.js:116:19) at E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\util.js:21:7 at Array.forEach (native) at walk (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\util.js:16:25) at E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\util.js:23:7 at Array.forEach (native) at Object.walk (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\util.js:16:25) at MeanUserKlass.Module (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\index.js:134:14) at new MeanUserKlass (E:\mean_workspace\monoprobability\node_modules\meanio-users\app.js:10:10) at Object.<anonymous> (E:\mean_workspace\monoprobability\node_modules\meanio-users\app.js:21:16) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Module.activate (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\dependablelist.js:52:15)
When I tried to start using mean.io on another computer it worked fine so it is almost certainly a problem with my current environment. When I look inside of my root's node_modules folder it is missing both 'async' and 'mongoose'. Are they supposed to be in that folder? Can't you copy them from the other computer and see if that solves the issue for you? The other computer is a work computer which disables copying files :/ On April 27 2016 19:43 Acrofales wrote:On April 27 2016 10:43 WarSame wrote:Folks, looking for a bit of help with mean.io. I dled and installed node, mongodb and git. I then init a directory and run npm install inside of it. I then run gulp to start the server. This throws errors that I'm missing various modules such as 'mongoose', 'async', etc. but these should be installed from the npm install, correct? The exact error is + Show Spoiler +E:\mean_workspace\monoprobability\node_modules\q\q.js:155 throw e; ^
Error: Cannot find module 'mongoose' at Function.Module._resolveFilename (module.js:325:15) at Function.Module._load (module.js:276:25) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object.<anonymous> (E:\mean_workspace\monoprobability\node_modules\meanio-users\server\models\user.js:6:17) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at requireModel (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\index.js:116:19) at E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\util.js:21:7 at Array.forEach (native) at walk (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\util.js:16:25) at E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\util.js:23:7 at Array.forEach (native) at Object.walk (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\util.js:16:25) at MeanUserKlass.Module (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\index.js:134:14) at new MeanUserKlass (E:\mean_workspace\monoprobability\node_modules\meanio-users\app.js:10:10) at Object.<anonymous> (E:\mean_workspace\monoprobability\node_modules\meanio-users\app.js:21:16) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Module.activate (E:\mean_workspace\monoprobability\node_modules\meanio\lib\core_modules\module\dependablelist.js:52:15)
When I tried to start using mean.io on another computer it worked fine so it is almost certainly a problem with my current environment. When I look inside of my root's node_modules folder it is missing both 'async' and 'mongoose'. Are they supposed to be in that folder? Is your internet stable, and no timeout problems? I've had issues with npm skipping stuff if it cannot get it sufficiently quickly, and then claiming success all the same. rerunning npm install usually works, but sometimes it's really confused and you need to throw away your node_modules and start again. either that, or your package.json is wrong. Yup, my internet is pretty stable here. I've run the command like 5 times in a row and I've deleted and reinited the app ~5 times too, so that shouldn't be it. My package.json is simply the default mean.io package, so it should be fine. I know it's a dumb question but... Does your package.json have async and mongoose in dependencies section? The only other thing I can think about is async and mongoose getting deleted from central npm repo. I don't think internet connection creates major issues because I might be having one of the shittiest internet connections in the entire thread and still didn't have any issues with npm claiming success but not downloading dependencies. Regardless, you can run: npm install --save async mongoose and solve your problem that way. It now starts the server, looks like it's going fine and then refuses to serve the pages. :/ Not sure why it's doing that... This is the same thing that happened last time I tried to manually install the packages.
|
On May 01 2016 21:00 Manit0u wrote:Show nested quote +On May 01 2016 19:39 bangsholt wrote:On May 01 2016 03:36 Manit0u wrote:Since I'm trying to brush up on my C a bit (been years since I last did anything in it) I'm here to ask you guys if this code is acceptable and please point out any errors or give suggestions if you have them. I've decided to start with something relatively simple - generating an array of numbers in range (eg. irange(1, 3) results in {1, 2, 3}, irange(3, 1) results in {3, 2, 1} etc.). Generating forward and reverse arrays of letters comes next: + Show Spoiler + #include <stdio.h> #include <stdlib.h> // Include stdbool.h to get booleans
// Merge the typedef with the declaration of the struct typedef struct integer_range integer_range;
integer_range *irange(int start, int end);
struct integer_range { int length; int nums[]; };
integer_range *irange(int start, int end) { // Use booleans for the next two declarations int reverse = (end < start); int size = (reverse ? start - end : end - start) + 1; size_t mem = sizeof(integer_range) + size * sizeof(int); integer_range *range = malloc(mem);
// Check return value of malloc.
range->length = size;
for (int i = 0; i < size; i += 1) { range->nums[i] = reverse ? start - i : start + i; }
return range; }
int main(void) { integer_range *range = irange(7, -1);
for (int i = 0; i < range->length; i += 1) { printf("%d\n", range->nums[i] ; }
free(range);
return 0; }
Please don't mind the main function for now. It's just there for testing purposes... Threw in a few comments in the spoiler for your code. Thanks. By checking return value of malloc do you mean something like that? if (range == NULL) { return -1; // something went wrong }
I still find it hilarious how you force yourself to do manual memory management and reinvent a basic version of std::vector. Here is your code with more safety (leak-free) that tries to resemble your C variant. That's not how I'd have written it anyway.
+ Show Spoiler + #include <vector> #include <iostream>
typedef struct integer_range integer_range;
integer_range irange(int start, int end);
struct integer_range { std::vector<int> nums; };
integer_range irange(int start, int end) { bool reverse = end < start; int size = (reverse ? start - end : end - start) + 1; integer_range range;
for (int i = 0; i < size; ++i) { range.nums.push_back(reverse ? start - i : start + i); }
return range; }
int main(void) { integer_range range = irange(7, -1);
for (const int& num : range.nums) { std::cout << num << std::endl; }
return 0; }
Are you trying to develop operating systems or embedded systems? Why C? "I don't like C++" is not a good reason.
|
GRAND OLD AMERICA16375 Posts
On May 02 2016 09:45 Shield wrote:Show nested quote +On May 01 2016 21:00 Manit0u wrote:On May 01 2016 19:39 bangsholt wrote:On May 01 2016 03:36 Manit0u wrote:Since I'm trying to brush up on my C a bit (been years since I last did anything in it) I'm here to ask you guys if this code is acceptable and please point out any errors or give suggestions if you have them. I've decided to start with something relatively simple - generating an array of numbers in range (eg. irange(1, 3) results in {1, 2, 3}, irange(3, 1) results in {3, 2, 1} etc.). Generating forward and reverse arrays of letters comes next: + Show Spoiler + #include <stdio.h> #include <stdlib.h> // Include stdbool.h to get booleans
// Merge the typedef with the declaration of the struct typedef struct integer_range integer_range;
integer_range *irange(int start, int end);
struct integer_range { int length; int nums[]; };
integer_range *irange(int start, int end) { // Use booleans for the next two declarations int reverse = (end < start); int size = (reverse ? start - end : end - start) + 1; size_t mem = sizeof(integer_range) + size * sizeof(int); integer_range *range = malloc(mem);
// Check return value of malloc.
range->length = size;
for (int i = 0; i < size; i += 1) { range->nums[i] = reverse ? start - i : start + i; }
return range; }
int main(void) { integer_range *range = irange(7, -1);
for (int i = 0; i < range->length; i += 1) { printf("%d\n", range->nums[i] ; }
free(range);
return 0; }
Please don't mind the main function for now. It's just there for testing purposes... Threw in a few comments in the spoiler for your code. Thanks. By checking return value of malloc do you mean something like that? if (range == NULL) { return -1; // something went wrong }
I still find it hilarious how you force yourself to do manual memory management and reinvent a basic version of std::vector. Here is your code with more safety (leak-free) that tries to resemble your C variant. That's not how I'd have written it anyway. + Show Spoiler + #include <vector> #include <iostream>
typedef struct integer_range integer_range;
integer_range irange(int start, int end);
struct integer_range { std::vector<int> nums; };
integer_range irange(int start, int end) { bool reverse = end < start; int size = (reverse ? start - end : end - start) + 1; integer_range range;
for (int i = 0; i < size; ++i) { range.nums.push_back(reverse ? start - i : start + i); }
return range; }
int main(void) { integer_range range = irange(7, -1);
for (const int& num : range.nums) { std::cout << num << std::endl; }
return 0; }
Are you trying to develop operating systems or embedded systems? Why C? "I don't like C++" is not a good reason.
maybe he just wants to use C?
|
Suddenly I have to write JavaScript at work, and I'm a hardware design/verification guy...! What's the best practice for saving info done on a webpage? Not global variables I assume? I am thinking that when the user chooses settings in boxes I have made, the settings get saved in a string that is shown in a box, so that he can copy/paste it and give it to coworkers. When the coworker pastes and presses "go" in this box, the settings will be applied so he can get the same settings.
|
|
|
|