10-15-2013, 02:13 PM | #1 |
FFR Veteran
Join Date: Jan 2007
Age: 33
Posts: 2,407
|
Requesting help for programming/ CS majors?
This is probably easy for most, but I can't seem to figure it out. This is in C++ btw.
#include <iostream> using namespace std; int main() { int name[12]; int i; for(int i=0; i<12; i++) name[i] = i; for(i=0; i<12; i++) cout << "This is letter ["<< i <<"]: " << name[i] << "\n"; } int dummy; cin >> dummy; return 0; } I have to use dummy's because the PC won't display anything without it. This is what I have so far, I'm not really sure about what to do from here. I'm not even sure if this is correct, lol. Help>
__________________
Last edited by Superfreak04; 10-21-2013 at 08:37 PM.. |
10-15-2013, 02:45 PM | #2 | |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: Requesting help for programming/ CS majors?
I have nooo experience with C++ sorry, I'm sure someone else will come to the rescue soon. in the meantime
Quote:
for each element in the array, you're storing its number. is that intentional? sorting the array in alphabetical order manually would be the trickiest part of this assignment by far. do you know if you're allowed to use C++ library functions to do the work for you? for the frequency of letters, I would make another array with 26 elements. each element in the array should have an int initialized to 0. the 1st element represents 'a', the 2nd represents 'b', etc. as you read in each character from the input, you'll want to compute the 0-25 value based on the character's ASCII value, and then increment (+1) the int stored in that letter's element. ^ ew that's a yucky low-level way of doing it. again, you should find out if you're allowed to use all of the functions available to you. good luck! |
|
10-15-2013, 02:47 PM | #3 |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
|
Re: Requesting help for programming/ CS majors?
you're using the same control variable for nested for loops. EDIT: nvm confusing without curly braces, loops aren't nested.
Will edit with other thoughts gimme a min Code:
#include <iostream> using namespace std; int main() { int name[12]; int i; for(int i=0; i<12; i++) name[i] = i; for(i=0; i<12; i++) cout << "This is letter ["<< i <<"]: " << name[i] << "\n"; } int dummy; cin >> dummy; return 0; } to read in the inputs, you should use a scanf and assign those to name[i] instead of just assigning i to name[i]. Then you can loop through the array and perform the sorting algorithm. let me code up an example real quick..
__________________
Last edited by rushyrulz; 10-15-2013 at 02:56 PM.. |
10-15-2013, 02:48 PM | #4 |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: Requesting help for programming/ CS majors?
it doesn't look like those for loops are nested. the code just doesn't have them spaced out.
|
10-15-2013, 02:48 PM | #5 |
FFR Veteran
Join Date: Jan 2007
Age: 33
Posts: 2,407
|
Re: Requesting help for programming/ CS majors?
Thanks for the input!
Anyways, I believe we are allowed to use commands that do it for us. And I meant to put in characters, not integers. Afterall, we are input letters, not numbers haha. THis is what I have. It's still wrong, but yeah. #include <iostream> using namespace std; int main() { char name[12]; char i; for(i=0; i<12; i++) name[i] = i; cout << "Please enter your name one letter at a time: "; cin >> name; for(i=0; i<12; i++) cout << "This is letter ["<< i <<"]: " << name[i] << "\n"; int dummy; cin >> dummy; return 0; } Basically what I'm trying to get it to do. IS make it ask me to input one letter. So I would put in letter e. Then it would ask me to input another letter, in which I put "r". So on and so fourth. then once I'm done inputting all the letters, it will take every letter, put it into an array, and make them in alphabetical order.
__________________
Last edited by Superfreak04; 10-15-2013 at 02:52 PM.. |
10-15-2013, 02:54 PM | #6 | |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: Requesting help for programming/ CS majors?
Quote:
you're looking for something like: Code:
char name[12]; char c; for(int i=0; i<12; i++) name[i] = c; |
|
10-15-2013, 02:56 PM | #7 |
FFR Veteran
Join Date: Jan 2007
Age: 33
Posts: 2,407
|
Re: Requesting help for programming/ CS majors?
I thought characters were referred to as letters? 0_0 So I figured entering in char instead since I'm inputting letters. I guess that doesn't work haha.
__________________
|
10-15-2013, 03:00 PM | #8 |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: Requesting help for programming/ CS majors?
on a simple level, characters are basically your letters, yes. the problem is there in your actual definition of the for loop, not in the code inside the loop. once you've already hit
Code:
char i; Code:
i=0; |
10-15-2013, 03:02 PM | #9 | |
FFR Veteran
Join Date: Jan 2007
Age: 33
Posts: 2,407
|
Re: Requesting help for programming/ CS majors?
Quote:
__________________
|
|
10-15-2013, 03:03 PM | #10 |
aka Assertive
Join Date: Feb 2006
Location: Waterloo, ON
Age: 29
Posts: 2,567
|
Re: Requesting help for programming/ CS majors?
It seems like you're confusing the "for" loop as a sort of "for each" loop like the one in C#. In C++ you have to use a standard "for" loop, iterating over the loop i times.
|
10-15-2013, 03:09 PM | #11 |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
|
Re: Requesting help for programming/ CS majors?
I wrote this in java, shouldn't be too hard to translate into C++. But this takes the name characters into an array until the special character * is input.
Code:
import java.util.Scanner; //same as iostream, just java version public class namesort { public static void main(String args[]) { char[] name = new char[50]; //creating character array int count = 0; //loop control variable System.out.println("Enter the letters of your name one by one and type * when done."); Scanner input = new Scanner(System.in); //invoking iostream while(count < 50) //failsafe so it's not infinite. { name[count] = input.next().charAt(0); //assign first character of user input to name[count] if(name[count] == '*') //until * is entered break; //terminate loop count++; //iterate loop } } Then counting the letters should be as simple as looping an equality statement through your array and keeping counters.
__________________
Last edited by rushyrulz; 10-15-2013 at 03:16 PM.. |
10-15-2013, 03:17 PM | #12 |
Caelondia Represent
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
|
Re: Requesting help for programming/ CS majors?
If you need more direct help, I could help you through Skype possibly. It'll be easier since I can guide you through one step at a time instead of just giving you code.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник. |
10-15-2013, 03:20 PM | #13 | |
FFR Veteran
Join Date: Jan 2007
Age: 33
Posts: 2,407
|
Re: Requesting help for programming/ CS majors?
Quote:
If you post the code now though, it would help. Then I can skype with you once classes are over?
__________________
|
|
10-15-2013, 03:23 PM | #14 |
Caelondia Represent
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
|
Re: Requesting help for programming/ CS majors?
I guess. I'll make a section of code that'll work with the first part of your things you're supposed to do.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник. |
10-15-2013, 03:28 PM | #15 |
FFR Veteran
Join Date: Jan 2007
Age: 33
Posts: 2,407
|
Re: Requesting help for programming/ CS majors?
Alright, sounds good. Add me on Skype, and I'll accept it once I get home.
__________________
|
10-15-2013, 03:36 PM | #16 |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: Requesting help for programming/ CS majors?
see Rushy's post for a full explanation of how you can do these tasks manually. that's your fail-safe.
here's some info for a possible C++ specific shortcut for the alphabetizing task. I have no idea how the sort function works haha, but if you get it down, it could save you some time. http://stackoverflow.com/questions/1...th-char-arrayc and Superfreak, be sure to give yourself time for these assignments! having a "get it done now, learn later" mentality will hurt you in the long run edit: actually after staring at that StackOverflow page for a bit I think I get what the parameters for the function are. Code:
char array[] = "zabgqkzg"; std::sort(array, array+sizeof(array)); array is a pointer to the start of the array, and array+sizeof(array) is a pointer to the first memory address after the array I think? lol Last edited by dAnceguy117; 10-15-2013 at 03:48 PM.. |
10-15-2013, 03:38 PM | #17 |
FFR Veteran
Join Date: Jan 2007
Age: 33
Posts: 2,407
|
Re: Requesting help for programming/ CS majors?
Thanks haha. And yeah, right now I'm trying to convert what rushy said, into C++. Not going very well. :/
__________________
|
10-15-2013, 03:39 PM | #18 |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
|
Re: Requesting help for programming/ CS majors?
I went ahead and did steps 1 and 2 in java. you should be able to figure out how to count the letter frequency and make the histogram. Here's my java code:
Code:
import java.util.Scanner; public class namesort { public static void main(String args[]) { char[] name = new char[50]; int[] asciiname = new int[50]; int count = 0; System.out.println("Enter the letters of your name one by one and type * when done."); Scanner input = new Scanner(System.in); while(count < 50) { char dumbchar = input.next().charAt(0); if(dumbchar == '*') break; else { name[count] = dumbchar; count++; } } //cast char to int(ASCII) for(int i = 0; i < name.length; i++) { asciiname[i] = (int)name[i]; } //bubble sort for(int i = 0; i< asciiname.length-1; i++) { for(int j = 0; j<asciiname.length-1; j++) { if(asciiname[j+1] < asciiname[j]) { //swap elements int dummy = asciiname[j]; asciiname[j] = asciiname[j+1]; asciiname[j+1] = dummy; } } } //cast int(ASCII) to char(ANSI). for(int i = 0; i < asciiname.length; i++) { name[i] = (char)asciiname[i]; } for(int i=0; i<name.length; i++) { System.out.print(name[i]); } } }
__________________
|
10-15-2013, 03:44 PM | #19 |
Caelondia Represent
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
|
Re: Requesting help for programming/ CS majors?
Yeah, I didn't notice rushy's post. His will work perfectly fine if you just change the Java functions to C++ functions like he said.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник. |
10-15-2013, 03:45 PM | #20 |
FFR Veteran
Join Date: Jan 2007
Age: 33
Posts: 2,407
|
Re: Requesting help for programming/ CS majors?
Yeah I'm trying lol. Right now I'm mainly focused on step one.
__________________
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|