Go Back   Flash Flash Revolution > General Discussion > Chit Chat
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
Old 10-15-2013, 02:13 PM   #1
Superfreak04
FFR Veteran
FFR Veteran
 
Superfreak04's Avatar
 
Join Date: Jan 2007
Age: 33
Posts: 2,407
Default 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..
Superfreak04 is offline   Reply With Quote
Old 10-15-2013, 02:45 PM   #2
dAnceguy117
new hand moves = dab
FFR Simfile AuthorFFR Veteran
 
dAnceguy117's Avatar
 
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
Default 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:
int i;
for(int i=0; i<12; i++) name[i] = i;
you can get rid of the second "int" there, you already declared this variable.

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!
dAnceguy117 is offline   Reply With Quote
Old 10-15-2013, 02:47 PM   #3
rushyrulz
Digital Dancing!
Retired StaffFFR Simfile AuthorFFR Music ProducerD7 Elite KeysmasherFFR Veteran
 
rushyrulz's Avatar
 
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
Default 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;
}
I'm not so much as experienced in C++ as I am in C, but I assume the concepts are the same.

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..
rushyrulz is offline   Reply With Quote
Old 10-15-2013, 02:48 PM   #4
dAnceguy117
new hand moves = dab
FFR Simfile AuthorFFR Veteran
 
dAnceguy117's Avatar
 
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
Default 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.
dAnceguy117 is offline   Reply With Quote
Old 10-15-2013, 02:48 PM   #5
Superfreak04
FFR Veteran
FFR Veteran
 
Superfreak04's Avatar
 
Join Date: Jan 2007
Age: 33
Posts: 2,407
Default 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..
Superfreak04 is offline   Reply With Quote
Old 10-15-2013, 02:54 PM   #6
dAnceguy117
new hand moves = dab
FFR Simfile AuthorFFR Veteran
 
dAnceguy117's Avatar
 
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
Default Re: Requesting help for programming/ CS majors?

Quote:
char i;

for(i=0; i<12; i++) name[i] = i;
that's no good haha. you've declared your variable as a char type, but you're assigning int values to it.

you're looking for something like:

Code:
char name[12];
char c;

for(int i=0; i<12; i++) name[i] = c;
that's still not everything you need, though, because no value is ever assigned to c.
dAnceguy117 is offline   Reply With Quote
Old 10-15-2013, 02:56 PM   #7
Superfreak04
FFR Veteran
FFR Veteran
 
Superfreak04's Avatar
 
Join Date: Jan 2007
Age: 33
Posts: 2,407
Default 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.
__________________
Superfreak04 is offline   Reply With Quote
Old 10-15-2013, 03:00 PM   #8
dAnceguy117
new hand moves = dab
FFR Simfile AuthorFFR Veteran
 
dAnceguy117's Avatar
 
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
Default 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;
it's invalid to then have

Code:
i=0;
because 0 is an int value, not a char value.
dAnceguy117 is offline   Reply With Quote
Old 10-15-2013, 03:02 PM   #9
Superfreak04
FFR Veteran
FFR Veteran
 
Superfreak04's Avatar
 
Join Date: Jan 2007
Age: 33
Posts: 2,407
Default Re: Requesting help for programming/ CS majors?

Quote:
Originally Posted by dAnceguy117 View Post
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;
it's invalid to then have

Code:
i=0;
because 0 is an int value, not a char value.
Ah, that makes a lot more sense now!
__________________
Superfreak04 is offline   Reply With Quote
Old 10-15-2013, 03:03 PM   #10
megamon88
aka Assertive
Retired StaffFFR Simfile AuthorFFR Music ProducerFFR Veteran
 
megamon88's Avatar
 
Join Date: Feb 2006
Location: Waterloo, ON
Age: 29
Posts: 2,567
Default 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.
__________________
megamon88 is offline   Reply With Quote
Old 10-15-2013, 03:09 PM   #11
rushyrulz
Digital Dancing!
Retired StaffFFR Simfile AuthorFFR Music ProducerD7 Elite KeysmasherFFR Veteran
 
rushyrulz's Avatar
 
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
Default 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
        }

}
To sort into alphabetical order, you'll need to write an algorithm that interprets the ascii values of each letter and you could probably just bubble sort it.

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..
rushyrulz is offline   Reply With Quote
Old 10-15-2013, 03:17 PM   #12
Kibblre
Caelondia Represent
FFR Veteran
 
Kibblre's Avatar
 
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
Default 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.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.
Kibblre is offline   Reply With Quote
Old 10-15-2013, 03:20 PM   #13
Superfreak04
FFR Veteran
FFR Veteran
 
Superfreak04's Avatar
 
Join Date: Jan 2007
Age: 33
Posts: 2,407
Default Re: Requesting help for programming/ CS majors?

Quote:
Originally Posted by Kibblre View Post
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.
Unfortunately I'm in class atm lol. I won't be able to go onto skype until after 10 PM central time. :/

If you post the code now though, it would help. Then I can skype with you once classes are over?
__________________
Superfreak04 is offline   Reply With Quote
Old 10-15-2013, 03:23 PM   #14
Kibblre
Caelondia Represent
FFR Veteran
 
Kibblre's Avatar
 
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
Default 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.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.
Kibblre is offline   Reply With Quote
Old 10-15-2013, 03:28 PM   #15
Superfreak04
FFR Veteran
FFR Veteran
 
Superfreak04's Avatar
 
Join Date: Jan 2007
Age: 33
Posts: 2,407
Default Re: Requesting help for programming/ CS majors?

Alright, sounds good. Add me on Skype, and I'll accept it once I get home.
__________________
Superfreak04 is offline   Reply With Quote
Old 10-15-2013, 03:36 PM   #16
dAnceguy117
new hand moves = dab
FFR Simfile AuthorFFR Veteran
 
dAnceguy117's Avatar
 
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
Default 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));
once you've got a char array, you can use that sort() function. your arguments inside those parentheses are actually pointers.
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..
dAnceguy117 is offline   Reply With Quote
Old 10-15-2013, 03:38 PM   #17
Superfreak04
FFR Veteran
FFR Veteran
 
Superfreak04's Avatar
 
Join Date: Jan 2007
Age: 33
Posts: 2,407
Default 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. :/
__________________
Superfreak04 is offline   Reply With Quote
Old 10-15-2013, 03:39 PM   #18
rushyrulz
Digital Dancing!
Retired StaffFFR Simfile AuthorFFR Music ProducerD7 Elite KeysmasherFFR Veteran
 
rushyrulz's Avatar
 
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,980
Default 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]);
        }
    }
}
__________________


rushyrulz is offline   Reply With Quote
Old 10-15-2013, 03:44 PM   #19
Kibblre
Caelondia Represent
FFR Veteran
 
Kibblre's Avatar
 
Join Date: Jul 2004
Location: A place of hearts and ghosts
Age: 31
Posts: 1,984
Default 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.
__________________
Какой идиот придумал Бутерброд с дикобраза? Он хулиган и бездельник.
Kibblre is offline   Reply With Quote
Old 10-15-2013, 03:45 PM   #20
Superfreak04
FFR Veteran
FFR Veteran
 
Superfreak04's Avatar
 
Join Date: Jan 2007
Age: 33
Posts: 2,407
Default Re: Requesting help for programming/ CS majors?

Quote:
Originally Posted by Kibblre View Post
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.
Yeah I'm trying lol. Right now I'm mainly focused on step one.
__________________
Superfreak04 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 07:42 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution