Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Algorithms and flowcharts (Yet another C++ assignment).


    If there's one thing I have a hard time with, it's creating algorithms. That isn't the assignment itself, obviously, but if I want to figure out how to write this program, I'll need to figure out the algorithm but the harder I look at the assignment, the more headache it's giving me.

    I'm trying to make a flowchart and this is as far as I got.

    This is the assignment, by the way.

    Excuse my crappy writing.


    I didn't get very far... I need to have the program read the file's inputs and decode what's inside. I'm sure I need while loops for this, and I've no idea when those structs come in. How do I continue?

  2. Nuclear testing facility Straight Male
    IGN: VerrKol
    Server: Zenith
    Level: 204
    Job: Bowmaster
    Guild: LegacyReborn
    Farm: Kolville
    usa

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    I've done something similar for a class, but we were effectively encoding/decoding based on null cyphers. Basically it would take an input file and add a random character at specified intervals and reverse the process given the interval used to encode it.

    What you first need to do is decide on a (de)coding method then start asking yourself how do I reverse it?
    For something like the example paragraph, you run a while loop to check characters from the input file against a list of known code phrases. If the input phrase matches a code phrase, replace the original with the translated.

    Let me know if you need more specific instructions on implementing the character checks.

  3. aka ClawofBeta Straight Male
    Corn's Avatar [Jr. Event Coordinator]

    IGN: ClawofBeta
    Server: LoL.NA
    Level: 30
    Job: Bot Lane
    Guild: N/A
    Alliance: N/A
    New_Jersey

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    String manipulation? hue hue hue

    Do you know how to decode it in the first place?
    /*
    aka when it says +1-2cat, this + is the line number and the - is the word number. So cat would be on line one and word two.

    They'll trick you by switching the order sometimes, like -1+2is is on line 2 and word one, which is a pain. */


    On the top of my head I'll split each word into different strings and chug them into an array. Then I'd using some String methods (C++ has those, right?) to find out which word and sentence each string goes to. That's basically the general idea.

  4. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    We can't use arrays. :U We have to use a switch, if I recall correctly.

    Also, yeah, I'm aware of how it works. - indicates word position, + indicates line position, and they can be in any order (like, +3-2 or -2+3). They don't even have to be entered in the same line (note in the example how the indicators skip lines), as long as the word comes after the indicators. So you can enter your code in multiple lines, as long as the sentinel (:0:0:) is entered to indicate a stop.

  5. Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    If the output file does not exist, it will be created once the program runs.
    I'm going to assume that no arrays also means no data types that can be based off arrays (queues/vectors); I'll wait for confirmation on this before I say anything more.

  6. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Yup, none of that.

  7. Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Structs should be declared like how functions are declared, and I assume they play a part into containing the output of those while loops.

    Speaking of while loops, you are going the right direction with your flowchart. The program should read the file word per word and send that data through a while loop (with the switch inside it I think).

  8. Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    The structs are used as a compact way of representing a paragraph/sentence. For example, the paragraph "Today is the 23rd of September. The current year is 2013" would be stored as:
    line1: Today is the 23rd of September.
    line2: The current year is 2013.
    And today is word1 of sentence1, and so on and so on.

    Since you're reading in a normal text file, the while loop should be while(!filename.eof()) so that you will continue reading inputs until there is nothing left in the file. Because sentences aren't stored as an array of words and paragraphs aren't stored as an array of sentences, you will have to use a switch statement to iterate through each individual element rather than have a while loop inside a while loop. Which then leads to how do you store the words and sentences. The only container data structure I can think of that works for this is a list, which will be incredibly tedious to iterate through and inserting elements in certain positions.

    How I would think about this problem is that first start at a word and think about how you will parse the string. After that, think about how will you interpret the information once it's parsed correctly. Once you've done that, draw a 10x8 grid and think about "How do I insert this word at this certain position"?

  9. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    All right, so this is what I have so far. It's late and I haven't added this to the actual flowchart yet. Note that the flowchart isn't part of the assignment, I'm just doing it so I know what the hell I'm doing. e_e Good practices and all that.

    1. Read names of input and output files.
    2. Open input file. Terminate program if file not opened.
    3. Open output file.
    4. Read indicator+number and move position accordingly (switch #1).
    5. Read indicator+number and move position accordingly (switch #2).
    6. Read word.
    7. Place word in position.
    8. Repeat steps 4-7 until the file ends.
    9. Print result on screen (with bars).
    10. Print same result on output file (without bars).

    Notes:

    - While loop for input file reading only, so only one is needed.
    - Inside the while loop, use switches for the indicators and numbers. These will be separate functions.
    - At least two switches: One for line number, one for word number.
    - Structs come in after it is determined where a word goes, in the form of name.Line#.word# = ""; (since these structs are nested). Use this in the switches??

    Any feedback is appreciated. I'm somewhat skeptical on it since I'm not sure if this accounts for the fact that the line number and word number isn't always going to be in that same order (+1-2 or -2+1 for example). Never used structs or switches before! :D

  10. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Steps 4 and 5 aren't switches, you are just reading numbers.
    Something like this
    (pseudocode because I'm too lazy to draw a flowchart and too mean to write your actual code for you :-P )
    Code:
      read two numbers into i and j;
      if (both are positive) or (both are negative)
        terminate with "bad input" error;
      if (i is positive)
        place i into lineNumber and j into wordNumber;
      else
        place j into lineNumber and i into wordNumber;
    The switch comes in when you have to put the word in the right place, so step 7, and it looks like this
    Code:
    switch (lineNumber)
    {
         case 1: place the word in text.line1; break;
         case 2: place the word in text.line2; break;
         case 3:....
         default: exit with "bad input" error;
    }
    (note that this is butt-ugly and I hope the purpose of the exercise is to show you how much unnecessary work you have to do when you choose the wrong data structure).

  11. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Ahhh that makes sense, yeah. That also made me remember that the program has to read whether or not it contains :0:0: to stop the process and I hadn't accounted for that. Whoops.

    More like it's just teaching us how to use data structures (structs) to begin with. :b

  12. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Siiigh. Okay so this thing is due tomorrow and I've made hardly any progress. I thought I would be okay after trying to figure out an algorithm (and I don't have that completely down), but once I went into the actual program and looked at the assignment instructions, I noticed that I still have no idea what I'm doing. :(

    First step, I don't know how on Earth I need to define this function.

    Code:
    void GetWord(istream & inF, int & linepos, int & wordpos, string & word);
    
    void GetWord(istream & inF, int & linepos, int & wordpos, string & word)
    {
    	while(
    }
    That's as far as I got. I know I need to get it to read the indicator, the position and the word. Also note that I can't change the functions' parameters, they're part of the assignment. The functions are the only defined things in there, though.

    I wasn't sure how to do that (though the pseudocode is helping) so I tried to see if I can print everything out first, both to the terminal and to the output file. But I can't even test it, because I'm getting errors in this section. All I want it to do is read the input file that gets entered...

    Code:
    string inFileName;				//Name of the string value that holds the input file name.
    string outFileName;				//Name of the string value that holds the output file name.
    ifstream inF;					//Declared input file.
    ofstream outF;					//Declared output file.
    
    //-| ----------------------------------------------------------------------
    //-| 1. Read names of input and output files.
    //-| ----------------------------------------------------------------------
    
    cout << "Enter Name of Input File: ";		//Prompts for input filename.
    cin >> inFileName.c_str();			//Enter input filename. I MAY HAVE NOT USED THIS PROPERLY.
    
    //-| ----------------------------------------------------------------------
    //-| 2. Open input file. If this fails, terminate the program.
    //-| ----------------------------------------------------------------------
       
    inF.open(inFileName);				//Opens input file.
    
    if(inF.fail())					//Checks if the file was opened successfully.
    {
    	return 1;				//Terminates program if file is not found.
    }
    @SaptaZapta; Do you recall the error I got when I tried to do this exact same thing (open an entered filename) in the last assignment? Apparently I need to use .c_str() to get it to work while keeping inFileName as a string value. But apparently I don't know how to use it... since it's not working when I use it here.

    And for the record, yes, I'm very picky about my programming format. GOTTA LOOK PRETTY.

    EDIT: Let's not pull another all-nighter for this. I'll come back to this thread in the morning. :(

  13. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Sorry for taking so long to get to this.

    As for the c_str() thing - you can't use it there because it is "const char *", meaning it's read-only. You can't use it if you're trying to update the value of the string.

    I just rechecked the reference and http://www.cplusplus.com/reference/s...perator%3E%3E/ says "cin >> inFileName" should work. I don't know why your compiler is giving you a hard time with it. Personally I'd just use the same workaround as before, changing the variables to char[256], because that really isn't the important part of this assignment and we shouldn't waste time on it.

    GetWord() is the important bit. What it needs to do is get the next word, including its position, from the input file provided.
    We discussed the overall algorithm before (read two numbers, decide which is lineNum and which is wordNum, and then read the word).
    Now we must finish the little details: the terminator (:0:0) and dealing with whitespace.

    Some more code (I don't have a compiler on this comp so there may be errors)

    Code:
    char NonWhiteChar(istream &inF)     // function to find the next meaningful input character
    {
        char ch;
        do
        {
           inF >> ch;
           if inF.eof() return '\0';
        } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
        return ch;
    }
    
    void GetWord(istream & inF, int & linepos, int & wordpos, string & word)
    {
        char ch;  
        int num;
        linepos = wordpos = -1;     // indicates they don't have values yet
    
    // Read two numbers, in unknown order and with possible whitespace between them
        do
        {
            ch = NonWhiteChar(inF);
            if (ch == '+' || ch == '-')
            {
                 inF >> num;
                 if (ch == '+')
                 {
                   if (linepos > 0) ExitWithError("two line positions provided for one word");
                   else linepos = num;
                 }
                 else
                 {
                   if (wordpos > 0) ExitWithError("two word positions provided for one word");
                   else wordpos = num;
                 }
             }
             else if (ch == ':')
             {
              // read the next 3 characters and if they are 0:0 then exit, otherwise exit with format error message - not coded yet
             }
             else ExitWithError("Format error: expecting +, -, or :");
         } while (linepos < 0 or wordpos < 0);
    
         inF >> word;      // this will likely give you the same trouble as cin >> inFileName... if someone doesn't post a fix for that soon you'll need to workaround with a char[] again :-(
    
         return;
    }
    The way you use this function is in your main loop. You call GetWord, and then do the switch for putting "word" in line#linepos of the struct. Do that repeatedly until input is exhausted.

  14. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Fair enough. My main issue with that is that the programs are submitted through UNIX, and it checks to see if we have completed specific conditions for the completed program. Last time, it checked if I'd used .c_str() and since I didn't, I'd lost points for it. It's not a huge loss right now, though; as long as the program works the way it's intended to work and I can submit this on time (it's due in five and a half hours OH LORD GOTTA HURRY).

    Now, a question... though maybe I should worry about making this work first and asking questions later, but what's the purpose of this exactly? I'm not quite getting it.

    Code:
    if (ch == '+')
    {
    	if (linepos > 0) ExitWithError("two line positions provided for one word");
    	else linepos = num;
    }
    else
    {
    	if (wordpos > 0) ExitWithError("two word positions provided for one word");
    	else wordpos = num;
    }
    The way I'm seeing it, based on the error, is that it's checking to see if there have been two positions entered using the same indicator? But I'm not seeing how if it's checking for the number used for word/linepos. Unless I'm misunderstanding it.

    EDIT: After some tweaking, I get a warning saying ch is uninitialized. I didn't know a char had to be initialized, though this is probably because the function is checking whether ch equals something.

    EDIT2: Nooooo... I'm trying to get it to count the words read, but it's not working. Is it too soon to do that, should I worry about the other stuff first? :( I tried doing so within the function before realizing that that would need another loop inside it and I don't want to complicate things even further.

    Code:
    while (inF >> contents)
    {
    	GetWord(inF, linepos, wordpos, word);
    	numWords = numWords + 1; //This has been initialized in the main.
    }
    
    cout << "PARAGRAPH CONTAINS " << numWords << " WORDS" << endl;
    It's telling me that linepos, wordpos and word are undeclared... but they were declared inside the function?? (I mean, word technically was not, but...)

  15. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Starting from the end: if you are passing something from the main program to the function, both of them need to know what it is. Therefore, it needs to be declared in both places. So add this to the main:
    Code:
      int linepos, wordpos; 
      string word;
    Next, the purpose of that bit of code. Yes, it's checking for something like +2+3. It does this by looking at the value of linepos/wordpos because when the function starts it sets them to -1. If they have a value other than that, it means they have already been assigned one, which means we've already seen that indicator. In the case of the +2+3 example, when we read the second '+', we check and see that linepos is already 2 (instead of -1, which indicates "not set yet"), and so we refuse to accept the 3.
    A different way to do this would have been by using boolean variables, thus:
    Code:
      boolean have_linepos = false;
      boolean have_wordpos = false;
    ...
      if (ch == '+')
      {
        if (have_linepos) ExitWithError("Saw a '+' when already have linepos");
        else 
        {
           have_linepos = true;
           linepos = num;
         }
      }
      else if (ch == '-')
      // same thing with wordpos
    Remember that this is inside a loop, so it can happen more than once. It looks a little odd because it looks like you're asking about linepos' value before you had a chance to set it, but no, this is checking whether a previous iteration of the loop already set it.

    If the program is submitted on UNIX you need to find a way to develop it on UNIX. Presumably "cin >> name" works there, and then you'd need c_str() for the "open" function since it wants a char * argument.
    Code:
      cin >> inFileName;
      inF.open(inFileName.c_str());
    Note that c_str() works here because open doesn't try to change the string that is passed to it.


    Finally, if it's due soon you need to find other helpers. It's 2:38 AM here and I've been asleep and will go back to sleep soon. Just got up to get a drink of water.

  16. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Oh, I thought I had that in the main, but that's lineNum and wordNum... so I just had to change the names. :P

  17. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Okay so, ch being uninitialized is making this not work, how should it be initialized?

    Also I should move on to the next function...

    Code:
    // Initialize SENTENCE record to contain empty strings. 
    void InitializeLine(SENTENCE & Line);
    I feel really dumb, I'm just staring at this and got no idea on what to do here.

    A record that contains empty strings, would that be something along the lines of...

    Code:
    SENTENCE Empty = {"", "", "", "", "", "", "", ""};
    Or do I just literally leave the space between curly braces blank?

    ... I'm probably misunderstanding this again because I'm not sure where Line comes in.

  18. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    It's ok for the main and function to call the variable by different names, e.g.

    Code:
    int square(int n)
    {
        return n*n;
    }
    
    void main()
    {
        int num;
    ...
        cout << "the square of " << num << " is " << square(num) << end;
    ...
    }
    Or in your case
    GetWord(inF, lineNum, wordNum, word);

  19. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Where is it complaining about ch not being initialized? Could you have missed copying a line that gives it a value? Or did you change my "do {} while ()" loop into a "while () {}" loop?

    Code:
    SENTENCE Empty = {"", "", "", "", "", "", "", ""};
    Line = Empty;

  20. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Algorithms and flowcharts (Yet another C++ assignment).


    Okay good, I was on the right track then!

    And as for the if explanation, that makes all kinds of sense now. And as for the GetWord function, all I did was remove the error functions because they were (ironically) giving me errors, I don't think the program recognized them at all. I'm going to assume that there won't be two of the same indicator entered.

    Code:
    void GetWord(istream & inF, int & linepos, int & wordpos, string & word)
    {
    	char ch;			//Variable to check for indicators.
    	int num;			//Position numbers before position check.
    	linepos = -1;		
    	wordpos = -1;		//These have not been given values yet and 0 is part of the sentinel: -1 is thus used.
    
    	//Read two numbers in unknown order and with possible whitespace between them.
    	do
    	{
    		if (ch == '+' || ch == '-') //Checks for position indicator before number.
    		{
    			inF >> num;				//Number comes after indicator, number is being read next.
    			if (ch == '+')
    			{
    				linepos = num;		//Gets line position if indicator is +.
    			}
    			else
    			{
    				wordpos = num;		//Gets word position if indicator is -.
    			}
    		}
    		else if (ch == ':')			//If no position indicator, checks for the start of the sentinel character, which is :.
    		{
    			// read the next 3 characters and if they are :0:0: then exit, otherwise exit with format error message - not coded yet
    		}
    	} while (linepos < 0 || wordpos < 0);
    
    	inF >> word;				//Read word after positions.
    
    	return;
    }

  21.  

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •