Like Tree5Likes

New guy seeking C++ Teacher(s)

This is a discussion on New guy seeking C++ Teacher(s) within the C++ Programming forums, part of the General Programming Boards category; Hey, y'all. I'm Cheyne, new guy to the forums here. I think my thread title pretty much covers what I'm ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    New guy seeking C++ Teacher(s)

    Hey, y'all. I'm Cheyne, new guy to the forums here. I think my thread title pretty much covers what I'm asking about, but I will elaborate a little.

    I'm not completely new to programming, I've done a bit of java in the past, but recently I've started to really apply myself to C++. But before I get into using different libraries and such, or even designing my own simple programs, I want some guidance in really getting my fundamentals learned.

    Now so far I have a pretty good grasp of the first 3 sections in this site's c++ tutorials.Basics, pointers, arrays and strings, and file IO, command line arguments etc. My grasp of linked lists, binary trees and recursion is a little weaker, and once going beyond that I find myself a little lost... Ok, a lot lost...

    I've always been a very practical learner. And though I've managed to grasp a certain amount on my own thanks to my limited experience with Java, I'm starting to think I need help now. I need to start typing out some code, using the tutorials as a reference, but need a little direction. Get some more practical experience.

    So I humbly ask for the help of my more experienced programming peers. I would like some ideas for projects within my skill level. I don't want the code typed out for me, so much as be told what the program should do, and what programming principles I should use to get it done. Figuring out as much as I can on my own will really help me learn. And when the project is done, or I find myself totally stuck, I'll return here for guidance. To either learn what I'm missing, or learn any mistakes I made when writing my program.

    I also hope this thread will help other beginners to find some way to work on their fundamentals as well. I look forward to your replies.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    100
    You said your grasp of linked lists, binary trees, and recursion is a little weaker. You might try coming up with your own projects that center around creating different sorts of ADTs; in college students are generally given a course that involves those sorts of programming assignments. For your first project, do something that involves making a class of a dynamically-sized linked list of another class. Then do the same for a stack. Then a queue. Then a binary tree. Then other ADTs, if you like.

    This will really solidify in your mind a lot of the big differences between Java and C++. Java has these sorts of structures in libraries, as does C++, but making your own in C++ can be a very different ball game than it is in Java, due to how detailed C++ is.

    Also if you don't already know what the Towers of Hanoi is, look that up. Then make a program that will automatically solve it with a user-specified number of disks (don't go much more than 10). This will probably really help you with recursion, as well as help you with stacks (which this should have three of).

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Thanks for my first reply and lesson! ^_^ I'll get to work right away. Oh and thanks for the towers of hanoi idea, too. It's perfect!

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by jsrig88 View Post
    You said your grasp of linked lists, binary trees, and recursion is a little weaker. You might try coming up with your own projects that center around creating different sorts of ADTs; in college students are generally given a course that involves those sorts of programming assignments. For your first project, do something that involves making a class of a dynamically-sized linked list of another class. Then do the same for a stack. Then a queue. Then a binary tree. Then other ADTs, if you like.

    This will really solidify in your mind a lot of the big differences between Java and C++. Java has these sorts of structures in libraries, as does C++, but making your own in C++ can be a very different ball game than it is in Java, due to how detailed C++ is.

    Also if you don't already know what the Towers of Hanoi is, look that up. Then make a program that will automatically solve it with a user-specified number of disks (don't go much more than 10). This will probably really help you with recursion, as well as help you with stacks (which this should have three of).
    Ok first project done. A linked list that uses recursion to add nodes, print them and search the list for specific values. I learned a lot doing it and can feel a few of the concepts really solidifying in my mind. I even had fun doing it. Guess I'm one of those people who enjoy programming. Tell me where there's room for improvement, I can take and even like constructive criticism, so don't hold back. Well here it is... Damn, I hope I'm doing this code tag thing right...

    Code:
    //Basic linked list using loops, structures, pointers and recursion.
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    // my global variables
    struct listNode{
        int contents;
        int index;
        listNode *next;
    };
    
    void buildList(int i);
    void printList();
    int searchList(int i);
    
    listNode *root;
    listNode *cNode;
    int counter;
    
    
    
    int main()
    {
        //initailizing my random number generator to the system clock...  At least I think that's what it
        //does...  60 to 75 percent sure.
        srand((unsigned)time(0));
    
        //variables local to the main method.  The root node and its values, the current node, an int I used for my while
        //loop, and a counter for the number of elements in my list.
        root = new listNode;
        root->contents=rand();
        root->index=1;
        root->next=0;
        cNode = root;
        int i = 0;
        counter=2;
    
        //while loop that uses my buildList() method to fill my linked list with 100 random numbers.
        while (i<100)
        {
            buildList(rand());
            i++;
        }
    
        //resets the current node pointer to root, so my printList() method can use it to print out the full
        //list.
        cNode = root;
        printList();
    
        //again resets the current node pointer to root, this time for my searchList() method.  Then asks the
        //user to enter a number between 1 and 100, which is then passed into searchlist.  This will tell you what
        //random number is at what index number in the apropriate listNode object.
        cNode=root;
        cout<< "Please enter a number between 1 and 100.\n";
        cin>>i;
        cout<<"The number "<<searchList(i)<<" is found at index "<<i<<". \n";
    
        return 0;
    }
    
    //My list building method.  Uses recursion to find the next NULL node on the list and add the number passed into
    //it.  Also adds the counter number to index, and increments counter by 1.
    void buildList(int i)
    {
        if (cNode->next == 0)
        {
            cNode->next=new listNode;
            cNode=cNode->next;
            cNode->index = counter;
            cNode->contents = i;
            counter++;
        }
        else
        {
            cNode=cNode->next;
            buildList(i);
        }
    }
    
    //my Printlist method runs through the whole linked list using recursion, printing out the index number and the contents
    //separated by a full stop. Then it starts a new line for the next node and continues until the whole Linked list is printed.
    void printList()
    {
        if (cNode->next != 0)
        {
            cout<<cNode->index<<". "<<cNode->contents<<" \n";
            cNode=cNode->next;
            printList();
        }
        else
            return;
    
    }
    
    //this final method takes in a index number entered by the user, finds the listNode object with said number, and returns
    //its contents.
    int searchList(int i)
    {
        if(i != cNode->index)
        {
            cNode=cNode->next;
            searchList(i);
        }
            return cNode->contents;
    }
    I just noticed that when the print method reaches index number 75 it prints 0.0. Can't fiugure that one out, could you help please? =)

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    What a good idea for a thread

    I'd say from that code, you've" got it". Good stuff! I have some comments, but mostly style/good practice/niggles. Will post them in a min, let me just tell you what your bug is first

    You have a nasty bug in buildList: you check for (cNode->next == 0), but you never actually set it to 0:
    Code:
    void buildList(int i)
    {
        if (cNode->next == 0)
        {
            cNode->next=new listNode;
            cNode=cNode->next;
            cNode->index = counter;
            cNode->contents = i;
            counter++;
        }
        else
        {
            cNode=cNode->next;
            buildList(i);
        }
    }
    In C/C++ global variables are initialised to 0, but dynamically allocated stuff and local variables are uninitialised. So here on the first call, cNode->next is 0, all good. On the next call, cnode->next is not 0, it's whatever happened to have been in memory there, so it tries to follow the pointer. When I ran your code it crashed here -- but uninitialised variables can have any value, and I guess in your system it happened to point to valid memory. Pain to debug such things!

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Apart from the little bug it does what it's meant to do, so woot. And you had fun doing it....

    I have a few comments, not all criticisms, some just thoughts.

    • C++ isn't garbage collected like Java -- any memory you allocate on the heap with "new" you need to "delete". In reality, the OS gets the memory back when the program terminates, but it's a good habit.
      That means you need to loop through the linked list again and delete() all the next pointers.
    • srand() initialises the random number generator with a 'seed'. If you use the same seed you get the same sequence of random numbers. Useful for debugging, but usually not what you want! time() is a good way to get a different number each time through the program
    • You shouldn't have cNode as a global variable. Say I said I wanted you to have 2 linked lists. How would you do it?
      A better way to do it would be to have every function that uses cNode take it as a parameter. This has some implications: cNode itself wouldn't be modified by the functions, so you wouldn't need to reset it to root. That's the 'C' way to do it anyway -- the C++ way is the next chapter I think
    • If cNode wasn't modified by the functions and always pointed to the first node, it'd be quicker to add new elements to the beginning of the list than the end of it.


    So what next? It was definitely more of a C program than a C++ program. I'd say the next step is to learn a bit about classes in C++, and organise that program into a class or classes.

    Probably won't be quite as much fun but should be satisfying, using a struct (in C++ a struct can have functions, constructors, destructors) or class for this kind of thing is very natural.

    After that, you should be able to put the "abstract" back into "abstract data type".

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Wow this is just what I needed. Reading the tutorials allowed me to remember a lot of my programming theory, and learn a few new techniques too. But having some direction to my projects, really helped me solidify some of those techniques in my mind. Even a few I hadn't intended to work on. Then on top of that, having someone to comment on my code, which teaches me even more? Awesome! Thanks so much! I'm feeling much more confident that with enough time and effort I will get this. I'll get started on my linked list class, and try to implement everything you've taught me so far. Might even have a go at a binary tree while I'm at it.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    Quote Originally Posted by Cheyne Keresoma View Post
    I'll get started on my linked list class, and try to implement everything you've taught me so far.
    You've already done it
    Put your global variables as private members and the functions as member functions.
    (...and the initialization code going into the constructor)
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    One thing that you might want to look up is university course material.
    Stanford CS Ed Library
    Free Online Course Materials | Electrical Engineering and Computer Science | MIT OpenCourseWare

    I've used stuff like this without guilt but you might have to research what language is being used if you desperately need to see something in a certain tongue.
    manasij7479 likes this.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    Quote Originally Posted by whiteflags View Post
    One thing that you might want to look up is university course material.
    Stanford CS Ed Library
    Free Online Course Materials | Electrical Engineering and Computer Science | MIT OpenCourseWare

    I've used stuff like this without guilt but you might have to research what language is being used if you desperately need to see something in a certain tongue.
    I learned quite a bit from the lectures of this course after I got the basics of C++ by myself.
    ( Stanford School of Engineering - Stanford Engineering Everywhere )
    Unfortunately, the teacher speaks so fast that I spent a lot of time rewinding to understand what she meant.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Ok first roadblock I can't seem to overcome with a few google searches. Though I am guessing that its a pretty amateur question, just can't figure it out alone this time it seems. I try to avoid coming here to ask questions too often, as it would be a waste of your time, and a hundred other people have probably already asked it elsewhere, so hopefully posts like this from me will be few and far between. Besides, I find taking some time to figure stuff out on my own is great for me to really learn a concept, rather than just having it told to me.

    My current issue is with the switch case statement. I first tried using it in the program I was making, and when I couldn't figure out the error messages, even after reading dozen's of different posts, I started a new smaller project, focused only on switch case. I used some code that I'm pretty sure was identical to the code in this site's tutorial, but the same error message kept popping up. After finally finding a solution however a whole new error message pops up... Ah, the joy of learning C++...

    Anyway here's the code to my little Program.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int  d;
        int a=10;
        int b=20;
        int c=30;
    
        cout<<"please enter 10, 20 or 30.";
        cin<<d;
    
        switch(d){
        case a:
            cout<< "Value of the a variable a."<<endl;
            break;
        case b:
            cout<< "Value of the a variable b."<<endl;
            break;
        case c:
            cout<< "Value of the a variable c."<<endl;
            break;
        default:
            cout<<"Invalid entry."<<endl;
    
    }
    
        return 0;
    }
    This code causes the errors "a\b\c\d Cannot appear in a constant expression. I figured out that adding const to my variables fixed this, but the order was important ( int const a, not const int a). But then it causes a 'ambigious overload for operator >> in std::cin>>d.' The primary issue here now I think is being unable to alter a constant integer with cin, while being unable to pass a normal int into the switch case method. Is there a way around this? Where am I going wrong?

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    Switch's cases can only be compile time constants... no variables.
    Use if -> else-if ->else-if -> else .
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by manasij7479 View Post
    Switch's cases can only be compile time constants... no variables.
    Use if -> else-if ->else-if -> else .
    Ah thank you. Those little details are so important. Hopefully I can get this project done with not to many of these errors.

  14. #14
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Apparently the stream operators for the cin function is the wrong order (<< instead of >>)
    And if you use "const int a" etc your code works.

    cheers

  15. #15
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    I'd recommend programmingpraxis and project euler for some fun ways to practice problem solving and improving your coding speeds.
    Project euler seems to be down at the moment though. Hopefully it comes back.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a Teacher
    By baitman18 in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2008, 01:42 PM
  2. Dealing With Teacher
    By Nero_Martin in forum C++ Programming
    Replies: 20
    Last Post: 09-23-2007, 04:02 PM
  3. New Computer App teacher looking for help
    By kellijhae in forum C++ Programming
    Replies: 36
    Last Post: 09-20-2005, 10:43 PM
  4. C++ Teacher
    By Meth40oz in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 07:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21