Jump to content


Photo

Programmer shizzies


261 replies to this topic

#16 auriplane

auriplane

    Shizz Captain

  • Members
  • PipPipPipPipPipPipPip
  • 1,322 posts

Posted 28 April 2012 - 11:33 PM

I tried calculating 5000^5000 on a few online calculators just now for kicks. they report "infinity" as the result :lol:


http://www.wolframal...ut/?i=5000^5000

:-D
  • 3

#17 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,441 posts

Posted 29 April 2012 - 06:58 AM

12497500 comparisons? Welcome to O(N^2) algorithms :)

You've discovered exactly why bubble sort is so fucking shitty, hence the reason for this exercise.
  • 2

#18 Rize

Rize

    Shizz Gawd

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 11,242 posts
  • Location:Baton Rouge

Posted 29 April 2012 - 09:46 AM

I'm going to pick up my old algorithms book and read it cover to cover. I know way less about algorithms than I should considering...
  • 1

#19 XMark

XMark

    Sleeveless

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 12,201 posts
  • Location:Vancouver, BC

Posted 29 April 2012 - 10:12 AM

Bubble sort is the best sorting algorithm for an array that's already sorted :)
  • 1

#20 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,441 posts

Posted 29 April 2012 - 10:37 AM

could be a path issue? Do you have to enter a fully qualified file name? Are there spaces in the file path?
  • 1

#21 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,441 posts

Posted 29 April 2012 - 10:59 AM

Well first off, notice that, due to the nature of a do-while, you're always assuming the first attempt to fail. Secondly, "!inFile" may not be doing what you think it's doing. You might want to try inFile.good() or inFile.is_open(). If the variable inFile is anything except NULL (which it will never be, I think, since it's not a pointer), then !inFile will always return FALSE.
  • 1

#22 Rize

Rize

    Shizz Gawd

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 11,242 posts
  • Location:Baton Rouge

Posted 29 April 2012 - 11:55 AM

Well first off, notice that, due to the nature of a do-while, you're always assuming the first attempt to fail. Secondly, "!inFile" may not be doing what you think it's doing. You might want to try inFile.good() or inFile.is_open()

Hmm, you're probably right about that. I was basing it off of the example in my book, but I'll give that a go and see how it pans out. Also, I see what you mean about that loop. Since it failed every single time with me I didn't even notice that.

edit: okay, yeah, inFile.is_open() is working just right, but now I've hit a new problem. If I enter the file name directly into the code it works a-ok, but when I try to use a variable -- inFile.open(fileName) instead of inFile.open("HW6Names.txt") -- I get hit with this error:
No matching function for call to 'std::basic_ifstream<char, std:: char_traits<char>::open(std::string&)'
Am I missing a heading? Shouldn't it be able to accept a string variable as the file name?


It's been a while since I've used C++, but it looks like the function requires a string& not a string as input. Try to give it

inFile.open(fileName*) or less likely
inFile.open(fileName&) instead of just plain
inFile.open(fileName)

EDIT or is that

inFile.open(*fileName)

... ?

lol, good luck with my terrible advice.
  • 1

#23 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,441 posts

Posted 29 April 2012 - 12:06 PM

You're trying to call the open function with a parameter that is not supported. List the declaration of the filename variable for me.
  • 1

#24 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,441 posts

Posted 29 April 2012 - 01:17 PM

If you look at the documentation for the open function you'll see that it requires a c-style string, not a std::string like you are using. Getting around this is easy. Just pass in fileName.c_ctr(), which will return the c-style equivalent of the std::string you are using.



Edit: whoops forgot to include doc link http://www.cplusplus.../ifstream/open/
  • 1

#25 Rize

Rize

    Shizz Gawd

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 11,242 posts
  • Location:Baton Rouge

Posted 29 April 2012 - 02:04 PM

Ah c++ vs. c strings. Then I think you would have to use:

inFile.open(fileName.c_str());

You said you tried that and it didn't work though?




To print a string vector backward you have two possibilities. You can reverse the vector then print, or you can manually loop backward through the vector printing one character at a time. The first solution is probably better. You could do that by looping over half the string swapping the first and last, then second and second to last and so on. If it's odd length, then just try to arrange for the loop to end and do nothing with the middle character (or you could swap it with itself which would work).
  • 1

#26 juef

juef

    Shizz Captain

  • Members
  • PipPipPipPipPipPipPip
  • 1,048 posts
  • Location:Quebec, Canada

Posted 29 April 2012 - 02:23 PM

Damn, I thought the last reply on page 2 was the very last reply. Pretty much all that had to be said has been. :P

Still, if you're interested, here's code that seem to be working the way you'd like it to. I haven't read all the previous replies so you might want to compare the whole thing to your current code.

#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    string fileName;
    ifstream inFile;
    int errorCount = 0;

    cout << "Enter the file name: ";
    cin >> fileName;

    inFile.open(fileName.c_str(), ifstream::in);

    do
    {
    cout << endl;
    cout << "Invalid file name, try again." << endl;
    cout << "Enter the file name: ";
    cin >> fileName;
    inFile.open(fileName.c_str());
    errorCount++;
    }
    while (!inFile && errorCount < 4);

    if (errorCount > 3)
    {
    cout << "You have exceeded the number of tries." << endl;
    cout << "Program terminating" << endl;
    return 1;
    }

    cout << "File opened successfully." << endl;

    system("PAUSE");

    return 0;
}

  • 1

#27 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,441 posts

Posted 29 April 2012 - 02:30 PM

You can print a vector of strings backwards character by character, you just need a for loop nested within a for loop. I'd tell you to use iterators, ut then your teache4 would know you're asking us for help ;)
  • 1

#28 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,441 posts

Posted 29 April 2012 - 03:20 PM

Vectors have a bracket operator, so you can access a string's Nth element via myString[N]

You know the size of the vector via the vector's .size() function, so just cycle backwards over the vector. For loops can count down, too!
  • 1

#29 radne

radne

    Pimpin

  • Members
  • PipPipPipPipPip
  • 555 posts
  • Location:in my room

Posted 29 April 2012 - 03:21 PM

You can print a vector of strings backwards character by character, you just need a for loop nested within a for loop. I'd tell you to use iterators, ut then your teache4 would know you're asking us for help ;)

Hmm, how would I print a single character from the string, then?


the string class overloads [] so you can access characters in a string by their index. So something like this would print out the 2nd letter in a string.
string s = "hello";
cout << s[1];
would print e.

Alternatively you could use the substr(int pos, int length) function included in the string class to print out a substring of length length starting at position pos.
string s = "hello";
cout << s.substr(1,1);

If you want to print all your strings backwards you use the outer loop to retrieve each string from your vector and the inner loop to print that string backwards using one of the above methods for character retrieval. (though you could also use a reverse_iterator to print the string backwards if you know what that is)

http://www.cplusplus.../string/string/
  • 1
"AIGHT , a bbig penis for MoonightGThang aand myoer MIG PICS, l lovooove me some chidlsbreading hips, m m mgood" -- Sherv

Motown Philly's back again

#30 Rize

Rize

    Shizz Gawd

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 11,242 posts
  • Location:Baton Rouge

Posted 29 April 2012 - 06:15 PM

I've never even used iterators... at a glance, it looks like you're iterating directly on memory locations instead of working with an index. That would be waaaay beyond Dameon's course work :)


I'm curious how you all got your programming experience. Any schooling? Complete any degrees? Just screw around with coding in spare time? Have job doing it?
  • 1



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users