Flash Flash Revolution

Flash Flash Revolution (http://www.flashflashrevolution.com/vbz/index.php)
-   Chit Chat (http://www.flashflashrevolution.com/vbz/forumdisplay.php?f=17)
-   -   Requesting help for programming/ CS majors? (http://www.flashflashrevolution.com/vbz/showthread.php?t=132425)

Superfreak04 10-15-2013 02:13 PM

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> :???:

dAnceguy117 10-15-2013 02:45 PM

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!

rushyrulz 10-15-2013 02:47 PM

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..

dAnceguy117 10-15-2013 02:48 PM

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.

Superfreak04 10-15-2013 02:48 PM

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.

dAnceguy117 10-15-2013 02:54 PM

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.

Superfreak04 10-15-2013 02:56 PM

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.

dAnceguy117 10-15-2013 03:00 PM

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.

Superfreak04 10-15-2013 03:02 PM

Re: Requesting help for programming/ CS majors?
 
Quote:

Originally Posted by dAnceguy117 (Post 3997128)
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!

megamon88 10-15-2013 03:03 PM

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.

rushyrulz 10-15-2013 03:09 PM

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.

Kibblre 10-15-2013 03:17 PM

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.

Superfreak04 10-15-2013 03:20 PM

Re: Requesting help for programming/ CS majors?
 
Quote:

Originally Posted by Kibblre (Post 3997139)
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?

Kibblre 10-15-2013 03:23 PM

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.

Superfreak04 10-15-2013 03:28 PM

Re: Requesting help for programming/ CS majors?
 
Alright, sounds good. Add me on Skype, and I'll accept it once I get home.

dAnceguy117 10-15-2013 03:36 PM

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

Superfreak04 10-15-2013 03:38 PM

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. :/

rushyrulz 10-15-2013 03:39 PM

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]);
        }
    }
}


Kibblre 10-15-2013 03:44 PM

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.

Superfreak04 10-15-2013 03:45 PM

Re: Requesting help for programming/ CS majors?
 
Quote:

Originally Posted by Kibblre (Post 3997164)
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.


All times are GMT -5. The time now is 10:45 PM.

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