|
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. |
On February 18 2013 13:46 obesechicken13 wrote: Does anyone else have trouble learning programming when they're near a computer? Like I keep getting distracted. What helps?
You can always try to learn this way + Show Spoiler +
I actually don't have the issue because I usually get into it when I'm learning that the only time I really do something else is because I need a break. You can try to block the sites you go on often at least when you're learning.
|
On February 18 2013 11:24 JKholodnov wrote:Hey guys, I'm working on a program for my c++ uni class, and am trying to figure out how to initialize n sets of arrays. For example if the input n = k, I need to initialize "k" arrays. What I've got right now is this: + Show Spoiler + while (i<n) { int arrayName+i; // I want this to return arrayName1 on the first run, arrayName2 on the second, etc. i++; }
Where i is initialized to 1 and n is inputted earlier in the program. Any ideas to make it work? Much appreciated. That thing you want with variable variable names, is not possible in C++. What you're looking for is something like this:
int **bar = new int*[n]; for (int i = 0; i < n; ++i) { bar[i] = new int[k]; }
This allocates n arrays of k elements each.
|
On February 18 2013 13:46 obesechicken13 wrote: Does anyone else have trouble learning programming when they're near a computer? Like I keep getting distracted. What helps?
1. Create a custom hosts file called "_hosts" that binds all the sites that distract you to 255.0.0.1 2. Write a batch file that swaps the files "_hosts" and "hosts", then runs ipconfig /flushdns 3. Whenever you want to work on something, run your batch file. Run it again when you're ready to stop.
That's a variation of what I do. I find that just having the extra step of switching from work mode to play mode keeps me from getting distracted too much.
|
On February 18 2013 16:06 Shenghi wrote:Show nested quote +On February 18 2013 11:24 JKholodnov wrote:Hey guys, I'm working on a program for my c++ uni class, and am trying to figure out how to initialize n sets of arrays. For example if the input n = k, I need to initialize "k" arrays. What I've got right now is this: + Show Spoiler + while (i<n) { int arrayName+i; // I want this to return arrayName1 on the first run, arrayName2 on the second, etc. i++; }
Where i is initialized to 1 and n is inputted earlier in the program. Any ideas to make it work? Much appreciated. That thing you want with variable variable names, is not possible in C++. What you're looking for is something like this: int **bar = new int*[n]; for (int i = 0; i < n; ++i) bar[i] = new int[k];
This allocates n arrays of k elements each. c++ has stl-containers for a reason:
int arraySize; int numberOfArrays; std::vector<std::vector<int>> array(numberOfArrays, std::vector<int>(arraySize));
or this if the size of the (inner) arrays is constant
const int arraySize(10); int numberOfArrays; std::vector<std::array<int, arraySize>> array(numberOfArrays);
edit: didn't read the previous page, I'm sorry
|
On February 18 2013 18:18 AmericanUmlaut wrote:Show nested quote +On February 18 2013 13:46 obesechicken13 wrote: Does anyone else have trouble learning programming when they're near a computer? Like I keep getting distracted. What helps?
1. Create a custom hosts file called "_hosts" that binds all the sites that distract you to 255.0.0.1 2. Write a batch file that swaps the files "_hosts" and "hosts", then runs ipconfig /flushdns 3. Whenever you want to work on something, run your batch file. Run it again when you're ready to stop. That's a variation of what I do. I find that just having the extra step of switching from work mode to play mode keeps me from getting distracted too much. I think this works well when I am at work and just need that little push. I just use notepad though and the search and replace feature. Thanks.
|
On February 18 2013 13:46 obesechicken13 wrote: Does anyone else have trouble learning programming when they're near a computer? Like I keep getting distracted. What helps?
I have similar problem, except I don't have trouble learning programming itself. Like openning e-book, or reading online article or even reading some source code. But the moment I need to actually *write* some code then I immediatly begin procrastinating ) It's like some kind of writers block. When I finally push myself to write, then I won't stop untill I hit some really hard design/implementation problem that requires lots of thinking. And the moment I start thinking how to solve it, procrastination hits again...
Aside from hosts file, you can also install browser add-ons. For firefox leechblock is a great anti-procrastinatioin tool. To block reddit I had to block it in both hosts and leechblock (and if there's yet another way to block it, I'd love to hear it). I also had tl.net blocked, but I've snapped and undid the block lol.
|
On February 19 2013 00:33 LukeNukeEm wrote:Show nested quote +On February 18 2013 16:06 Shenghi wrote:On February 18 2013 11:24 JKholodnov wrote:Hey guys, I'm working on a program for my c++ uni class, and am trying to figure out how to initialize n sets of arrays. For example if the input n = k, I need to initialize "k" arrays. What I've got right now is this: + Show Spoiler + while (i<n) { int arrayName+i; // I want this to return arrayName1 on the first run, arrayName2 on the second, etc. i++; }
Where i is initialized to 1 and n is inputted earlier in the program. Any ideas to make it work? Much appreciated. That thing you want with variable variable names, is not possible in C++. What you're looking for is something like this: int **bar = new int*[n]; for (int i = 0; i < n; ++i) bar[i] = new int[k];
This allocates n arrays of k elements each. c++ has stl-containers for a reason: int arraySize; int numberOfArrays; std::vector<std::vector<int>> array(numberOfArrays, std::vector<int>(arraySize));
or this if the size of the (inner) arrays is constant const int arraySize(10); int numberOfArrays; std::vector<std::array<int, arraySize>> array(numberOfArrays);
edit: didn't read the previous page, I'm sorry We're guessing that the person asking the question is in a very intro class and won't be amenable to more proper solutions.
|
On February 18 2013 16:06 Shenghi wrote:Show nested quote +On February 18 2013 11:24 JKholodnov wrote:Hey guys, I'm working on a program for my c++ uni class, and am trying to figure out how to initialize n sets of arrays. For example if the input n = k, I need to initialize "k" arrays. What I've got right now is this: + Show Spoiler + while (i<n) { int arrayName+i; // I want this to return arrayName1 on the first run, arrayName2 on the second, etc. i++; }
Where i is initialized to 1 and n is inputted earlier in the program. Any ideas to make it work? Much appreciated. That thing you want with variable variable names, is not possible in C++. What you're looking for is something like this: int **bar = new int*[n]; for (int i = 0; i < n; ++i) bar[i] = new int[k];
This allocates n arrays of k elements each.
Protip: Get in the habit of surrounding if/for loops with brackets even if you only intend to evaluate one line. This will save you many hours of headaches at some point in your career for not noticing things.
|
On February 19 2013 05:43 Phunkapotamus wrote:Show nested quote +On February 18 2013 16:06 Shenghi wrote:On February 18 2013 11:24 JKholodnov wrote:Hey guys, I'm working on a program for my c++ uni class, and am trying to figure out how to initialize n sets of arrays. For example if the input n = k, I need to initialize "k" arrays. What I've got right now is this: + Show Spoiler + while (i<n) { int arrayName+i; // I want this to return arrayName1 on the first run, arrayName2 on the second, etc. i++; }
Where i is initialized to 1 and n is inputted earlier in the program. Any ideas to make it work? Much appreciated. That thing you want with variable variable names, is not possible in C++. What you're looking for is something like this: int **bar = new int*[n]; for (int i = 0; i < n; ++i) bar[i] = new int[k];
This allocates n arrays of k elements each. Protip: Get in the habit of surrounding if/for loops with brackets even if you only intend to evaluate one line. This will save you many hours of headaches at some point in your career for not noticing things. I have set that as a general rule for myself whenever I write any code... Except when I do so before my morning coffee with only five minutes to spare. :D
Went back and edited in order to set a better example.
|
I just want to express my appreciation of how much easier Java is than C. I also think I'm going to ask for help in the near future, haha. At least now I know why my C lecturer asks us to implement only math stuff. His major was math before switching to Computer Science. -.-
Edit: Ok, here's a simple problem.
Input two positive integers a and b (separated by one space). a and b can be very large (up to 99 digits). Output a+b. You are not allowed to use floating-point data types, including float, double, and long double.
Could you give a hint what to use or what to look for? Except for GMP (which may not be accepted by the Online Judge) and floating-point data types.
|
On February 19 2013 08:28 darkness wrote:I just want to express my appreciation of how much easier Java is than C. I also think I'm going to ask for help in the near future, haha. At least now I know why my C lecturer asks us to implement only math stuff. His major was math before switching to Computer Science. -.- Edit: Ok, here's a simple problem. Show nested quote +Input two positive integers a and b (separated by one space). a and b can be very large (up to 99 digits). Output a+b. You are not allowed to use floating-point data types, including float, double, and long double. Could you give a hint what to use or what to look for? Except for GMP (which may not be accepted by the Online Judge) and floating-point data types.
Keep in mind that your computer can only store 64-bit or 32-bit numbers. Your best bit is to store the numbers in an array and write the addition operation yourself. It's quite easy. Actually, I find this really interesting, so if you like I could write it for you.
|
On February 19 2013 08:36 Abductedonut wrote:Show nested quote +On February 19 2013 08:28 darkness wrote:I just want to express my appreciation of how much easier Java is than C. I also think I'm going to ask for help in the near future, haha. At least now I know why my C lecturer asks us to implement only math stuff. His major was math before switching to Computer Science. -.- Edit: Ok, here's a simple problem. Input two positive integers a and b (separated by one space). a and b can be very large (up to 99 digits). Output a+b. You are not allowed to use floating-point data types, including float, double, and long double. Could you give a hint what to use or what to look for? Except for GMP (which may not be accepted by the Online Judge) and floating-point data types. Keep in mind that your computer can only store 64-bit or 32-bit numbers. Your best bit is to store the numbers in an array and write the addition operation yourself. It's quite easy. Actually, I find this really interesting, so if you like I could write it for you.
Yeah, go ahead if you don't mind. I find C very challenging, sometimes even really hard because Java provides so much more support in my opinion. I try to learn it anyway.
|
On February 19 2013 08:37 darkness wrote:Show nested quote +On February 19 2013 08:36 Abductedonut wrote:On February 19 2013 08:28 darkness wrote:I just want to express my appreciation of how much easier Java is than C. I also think I'm going to ask for help in the near future, haha. At least now I know why my C lecturer asks us to implement only math stuff. His major was math before switching to Computer Science. -.- Edit: Ok, here's a simple problem. Input two positive integers a and b (separated by one space). a and b can be very large (up to 99 digits). Output a+b. You are not allowed to use floating-point data types, including float, double, and long double. Could you give a hint what to use or what to look for? Except for GMP (which may not be accepted by the Online Judge) and floating-point data types. Keep in mind that your computer can only store 64-bit or 32-bit numbers. Your best bit is to store the numbers in an array and write the addition operation yourself. It's quite easy. Actually, I find this really interesting, so if you like I could write it for you. Yeah, go ahead if you don't mind. I find C very challenging, sometimes even really hard because Java provides so much more support in my opinion. I try to learn it anyway.  If you're studying computer science, I'd vouch for C though. I hear too often about guys that graduate with Java degrees around here and can't pass an interview at the local studios and companies here in Redmond WA.
|
Solve the it in java then, using list or array, and then do it again in C. The problem should be the same, the syntax of the language isn't going to be that complicated for a problem of that scope.
And then just be glad that you're not being asked to do this problem using transistors or logic gates, because that's what you have to do in an EE course :p
What will really prevent you from getting a job in the future is not whether you learn java or C, it's if you don't actually learn how to crank out code solutions to problems yourself. The specific language is pretty irrelevant.
|
On February 19 2013 08:37 darkness wrote:Show nested quote +On February 19 2013 08:36 Abductedonut wrote:On February 19 2013 08:28 darkness wrote:I just want to express my appreciation of how much easier Java is than C. I also think I'm going to ask for help in the near future, haha. At least now I know why my C lecturer asks us to implement only math stuff. His major was math before switching to Computer Science. -.- Edit: Ok, here's a simple problem. Input two positive integers a and b (separated by one space). a and b can be very large (up to 99 digits). Output a+b. You are not allowed to use floating-point data types, including float, double, and long double. Could you give a hint what to use or what to look for? Except for GMP (which may not be accepted by the Online Judge) and floating-point data types. Keep in mind that your computer can only store 64-bit or 32-bit numbers. Your best bit is to store the numbers in an array and write the addition operation yourself. It's quite easy. Actually, I find this really interesting, so if you like I could write it for you. Yeah, go ahead if you don't mind. I find C very challenging, sometimes even really hard because Java provides so much more support in my opinion. I try to learn it anyway. 
This is by no means a pretty solution (I didn't include comments in the for loop that does the actual adding on purpose, variables are named stupidly, etc etc). Also, by no means is the code robust or error free. I didn't really test all the cases. I just wrote the program and verified that it kinda worked. It's up to you from here. Here you go.
Also, this code might go a little above your head. Sorry if it does!
+ Show Spoiler + #include <stdio.h> #include <stdlib.h> #include <string.h>
int main(int argc, char *argv[]) { char a[100], b[100], temp[100], sum[101]; memset(sum,0,101); scanf("%s",a); scanf("%s",b); //Swap the strings if ( strlen(b) > strlen(a) ) { strcpy(temp,b); strcpy(b,a); strcpy(a,temp); } // We could reuse temp but that's bad practice char temp2[100]; strcpy(temp2,b); //Fill b with zeros until strings are aligned if( strlen(a) != strlen(b) ) { int difference = strlen(a) - strlen(b); memset(b,'0',difference); strcpy(b+difference,temp2); } printf("A is: %s \n",a); printf("B is: %s \n \n",b); char carry = '0'; short int a_int = 0, b_int = 0, carry_int = 0; char temp3[3]; temp3[0] = '\0'; temp3[1] = '\0'; temp3[2] = '\0'; for ( int i = 0; i < strlen(b); i++ ) { a_int = a[strlen(a)-i-1] - '0'; //A small hack to convert a character to an int b_int = b[strlen(b)-i-1] - '0'; carry_int = carry - '0';
sprintf(temp3,"%s%d",temp3,(a_int+b_int+carry_int)); sum[ strlen(a)-i-1 ] = (temp3[1] == NULL) ? temp3[0] : temp3[1]; carry = (temp3[1] != NULL) ? temp3[0] : '0'; temp3[0] = '\0'; temp3[1] = '\0'; } printf("Sum is: %s \n",sum); system("PAUSE"); return 0; }
|
functions are your friend
|
On February 19 2013 09:34 phar wrote: functions are your friend
To be fair, I wrote that code while "reading" The Bell Jar for one of my GE classes (I don't like reading books like that).
It's procrastination code. Didn't put much though into it. Just wanted to get him an idea of how to solve it.
|
On February 17 2013 10:59 darkness wrote:Okay, this is JavaScript code for long division which I found here - http://weblog.bocoup.com/long-division-in-javascript/+ Show Spoiler + function longDivision(n,d){ var num = n + "", numLength = num.length, remainder = 0, answer = '', i = 0;
while( i < numLength + 3){ var digit = i < numLength ? parseInt(num[i] : 0;
if (i == numLength){ answer = answer + "."; }
answer = answer + Math.floor((digit + (remainder * 10))/d); remainder = (digit + (remainder * 10))%d; i++; } return parseFloat(answer); }
longDivision(356,8);
I've tried to make it work in Java, and this is what I have so far: + Show Spoiler + public class test { public static double longDivision(int d, int n) { String[] num = new String[1000]; num[0] = "" + n; int numLength = num.length; int remainder = 0; String answer = ""; int i = 0; while (i < numLength + 3) { int digit = i < numLength ? Integer.parseInt(num[i] : 0; if (i == numLength){ answer = answer + "."; } answer = answer + Math.floor((digit + (remainder * 10))/d); remainder = (digit + (remainder * 10))%d; i++; } return Double.parseDouble(answer); }
public static void main (String[] args) { System.out.printf("%f\n", longDivision(356,8)); } }
I have 2 problems though: 1. 'var' in JavaScript is so ambigious, I'm left to guess what data type to use in Java. 2. I'm still not sure what to do with the num array which probably causes the whole program not to work as it should. Edit: Nevermind, ternary isn't touched here, but the num array looks rather messy. And yeah, I get this error: + Show Spoiler +
Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at test.longDivision(test.java:11) at test.main(test.java:26)
NumberFormatException: null at the line of declaring 'digit'.
Hi,
I'm not sure if you've figured this out, but here are a couple of things I noticed and hopefully this should give you a bit of a nudge into the right direction.
+ Show Spoiler +JS //this returns the length of the input. e.g "100" will return 3 var num = n + "", numLength = num.length,
JAVA //this is returning the length of the array i.e. it will always return 1000 String[] num = new String[1000]; num[0] = "" + n; int numLength = num.length;
Change your numLength declaration to: int numLength = String.valueOf(n).length();
also change int digit = i < numLength ? Integer.parseInt(num[i]) : 0; to
int digit = i < numLength ? numLength : 0;
This should get rid of your errors
Also I think you may also have your function parameters swapped around?
|
From someone with very little to no programming experience, can someone mentor me?
|
|
|
|
|
|
|
|