Computer Science Canada

Stuck on a problem, Quick fix

Author:  Panphobia [ Sat Dec 08, 2012 8:50 pm ]
Post subject:  Stuck on a problem, Quick fix

I have been learning c++ and I have been looking over the old dwite questions and solving them in c++ for practise and such, I have a current question im finishing from the new round 2 dwite http://dwite.org/questions/magic_trick.html , i know how to solve it, just a little compiling error, if someone could point me in the right direction that would be nice
code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <set>
using namespace std;
int main()
{
     ifstream fin ("/home/daniel/DATA4.txt");
     for(int x = 0; x < 5; ++x)
     {
      int n;
      fin >> n;
          set<int> cards;
           int cardsLeft[n];
        for(int i = 0; i < n;++i)
        {
                int temp;
                fin >> temp;
                cardsLeft[i] = temp;
                cards.insert(i+1);
        }
        string phrase = "";
        for(int i = 0; i < n;++i)
        {
            if(cards.size() - 1 - cardsLeft[i] < 0)
            {
                        cout << "IMPOSSIBLE" << endl;
                }else{
                        phrase += cards(cards.size() - 1 - cardsLeft[i])+" ";
                        cards.erase(cards.size() - 1 - cardsLeft[i]);
                }
        }
        cout << phrase << endl;
}
    return 0;
 }
the error is
code:
cppTest.cpp: In function ?int main()?:
cppTest.cpp:30:51: error: no match for call to ?(std::set<int>) (std::set<int>::size_type)?

Author:  Insectoid [ Sat Dec 08, 2012 10:03 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

Read the error. What does it say? "no match for call to ?(std::set<int>)" on line 30. Does 'set' exist?

Author:  Panphobia [ Sat Dec 08, 2012 10:37 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

how does it not exist, and how do you get a certain value in a set, im checking the c++ api and it doesnt work?

Author:  Insectoid [ Sat Dec 08, 2012 10:54 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

Ah. Well, after perusing the documentation, I've discovered that you're using it wrong. You do not retrieve elements from a set with parentheses; you need to use the find() function.

Author:  Panphobia [ Sat Dec 08, 2012 11:03 pm ]
Post subject:  Re: Stuck on a problem, Quick fix

I actually did but it doesnt work, right after i tried lol. I am thinking about changing it to a vector and when i did this is what happened, can you see anything wrong?
code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <set>
using namespace std;
int main()
{
     ifstream fin ("/home/daniel/DATA4.txt");
     for(int x = 0; x < 5; ++x)
     {
      int n;
      fin >> n;
          vector <int> cards;
           int cardsLeft[n];
        for(int i = 0; i < n;++i)
        {
                int temp;
                fin >> temp;
                cardsLeft[i] = temp;
                cards.push_back(i+1);
        }
        string phrase = "";
        for(int i = 0; i < n;++i)
        {
            if(cards.size() - 1 - cardsLeft[i] < 0)
            {
                        cout << "IMPOSSIBLE" << endl;
                }else{
                        phrase += cards[cards.size() - 1 - cardsLeft[i]]+" ";
                        cards.erase(cards.size() - 1 - cardsLeft[i]);
                }
        }
        cout << phrase << endl;
}
    return 0;
 }
and this is the error it maybe I am using them wrong too :S
code:
cppTest.cpp: In function ?int main()?:
cppTest.cpp:31:47: error: invalid types ?<unresolved overloaded function type>[std::vector<int>::size_type {aka long unsigned int}]? for array subscript

Author:  Panphobia [ Sun Dec 09, 2012 2:33 am ]
Post subject:  Re: Stuck on a problem, Quick fix

still havent solved the question up there ^, if anyone could help that would be great but i am also having some trouble converting my recursive knowledge from java to c++, i think everyone starts off with the factorial recursive problem but here is my code for factorial in recursion
code:
int factorial(int n)
{
   if(n == 1)
   {
      return 1;
   }
   else
   {
      return n * factorial(n - 1);
   }
}
here is the error, and if i change the n ==1 to n <=1 it just outputs 1,
code:
Segmentation fault (core dumped)

Author:  Insectoid [ Sun Dec 09, 2012 8:38 am ]
Post subject:  RE:Stuck on a problem, Quick fix

The erase() function does not take an int as a parameter. It takes an iterator.

I recommend you learn about pointers. They're everywhere in C++, and you won't get far without them.

As for your recursion function, it works just fine on my computer. Depending on your system, the segfault may just be due to calculating far too large of a number. Mine returns 0 when it gets too big.

Author:  Panphobia [ Sun Dec 09, 2012 12:17 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

I'm calculating 5!....

Author:  Insectoid [ Sun Dec 09, 2012 12:31 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

Then the issue is likely somewhere else in your code. A segmentation fault means you're accessing RAM that you aren't allowed to or that does not exist.

Author:  Panphobia [ Sun Dec 09, 2012 12:40 pm ]
Post subject:  Re: Stuck on a problem, Quick fix

I made a little test of input output etc, here is the rest of my code...
code:
#include <iostream>
using namespace std;
int factorial(int n)
{
   if(n == 1)
   {
      return 1;
   }
   else
   {
      return n * factorial(n - 1);
   }
}
int main()
{
        int c;
        cout << "Enter a number: ";
        cin >> c;
        while(c>0)
        {
        cout << c << "\n";
        --c;   
        }
        cout << factorial(c) << "\n";
        return 0;
}

Author:  Insectoid [ Sun Dec 09, 2012 12:47 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

What is the value of c at line 24? What happens if you pass that value to factorial()?

Author:  Panphobia [ Sun Dec 09, 2012 1:03 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

oh shit >.< you just made me look at the entire thing LOOOOOOOOOL

Author:  Panphobia [ Sun Dec 09, 2012 3:21 pm ]
Post subject:  Re: Stuck on a problem, Quick fix

Am I doing something wrong, because I kind of now copied someone elses answer on the dwite and they got 5/5 but it will not run and print out the answers, here is the code, and it just stops when i run it, it does not even go
code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <limits>
#include <iomanip>

#define all(c) (c).begin(),(c).end()

using namespace std;

typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main()
{
     ifstream fin ("/home/daniel/DATA4.txt");
   
    for (int t=0; t<5; t++) {
        int N;
        fin >> N;

        vi in(N, 0);
        vi val(N, 0);
        vector<bool> used(N+1, false);

        for (int i=0; i<N; i++) {
            fin >> in[i];
        }

        bool impossible=false;
        for (int i=0; i<N; i++) {
            val[i] = N-in[i];
            for (int j=N; j>=val[i] && j > 0; j--) {
                if (used[j])
                    val[i]--;
            }

            if (val[i] <= 0) {
                impossible = true;
                break;
            }
            used[val[i]] = true;
        }

        if (impossible)
            cout << "IMPOSSIBLE" << endl;
        else {
            for (int i=0; i<N-1; i++) {
                cout << val[i] << " ";
            }
            cout << val[N-1] << endl;
        }
    }
fin.close();
    return 0;
 }

Author:  Insectoid [ Sun Dec 09, 2012 6:21 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

Runs fine for me.

Author:  Panphobia [ Sun Dec 09, 2012 6:28 pm ]
Post subject:  RE:Stuck on a problem, Quick fix

Am I compiling wrong???????? Because I am compiling through the command line like, g++ cppTest.cpp -o test and then ./test and it isnt printing ANYTHING


: