Thread: Jumping Into C++ Chapter 14 Practice Problem 1

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    36

    Jumping Into C++ Chapter 14 Practice Problem 1

    I could use some help with this one. I don't really know where to begin. This is my first programing book, and I've only read up to this chapter, and I didn't do very well with the last chapter (CH 13) either, but I completed most of the practice problems in chapter 13. I need help with the two dimensional arrays. I don't really know how to put them into a function, either.

    The code I've got seems like it should work, but it freezes when I try to run it. I'm working in Xcode. Here is my code so far.

    Code:
    #include <iostream>
    
    usingnamespacestd;
    
    
    void multiplicationTable (int numbers) {
        cout << numbers;
    }
    
    
    int main() {
        int **p_p_numbers;
        p_p_numbers = new int*[3];//(myarray);
        for (int i = 0; i < 3; i++) {
            p_p_numbers[i] = new int[3];
        }
    
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                p_p_numbers[i][j] = 0;
            }
        }
    
    //print out the array
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                multiplicationTable(p_p_numbers[i][j]);
            }
        }
    
        for (int i = 0; i < 3; i++) {
            delete [] p_p_numbers[i];
        }
        delete [] p_p_numbers;
    
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Hmmm... Maybe I'm missing something too because your code looks fine to me. I mean, you forgot to make your main loop return something but I'm assuming most compilers are smart enough now to just default to returning 0.

    Have you tried running your code through debuggers or memory trackers?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code properly and post it without extra markup within [code][/code] bbcode tags, e.g.,
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void multiplicationTable(int numbers) {
        cout << numbers;
    }
    
    
    int main() {
        int **p_p_numbers;
        p_p_numbers = new int*[3];//(myarray);
        for (int i = 0; i < 3; i++) {
            p_p_numbers[i] = new int[3];
        }
    
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                p_p_numbers[i][j] = 0;
            }
        }
    
        //print out the array
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                multiplicationTable(p_p_numbers[i][j]);
            }
        }
    
        for (int i = 0; i < 3; i++) {
            delete [] p_p_numbers[i];
        }
        delete [] p_p_numbers;
    
    }
    Other than the lack of exception safety, use of magic numbers, and somewhat unusual names, I do not see a problem with your code, at least not one for which we might expect "freezes when I try to run it" as a symptom.

    Quote Originally Posted by MutantJohn
    you forgot to make your main loop return something but I'm assuming most compilers are smart enough now to just default to returning 0.
    If they are not smart enough to do that, then they have a bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    I mean, you forgot to make your main loop return something but I'm assuming most compilers are smart enough now to just default to returning 0.
    The standard mandates that if there is no return statement in main, it shall return 0 implicitly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    36
    Ok, so I ran it again after restarting my computer, and it seems my laptop was just being slow. But, I still have no idea how to go about using this pointer to make a function that turns them into a multiplication table. Any help on that? How do you pass a two diminutional pointer array into a function?

    Other than the lack of exception safety, use of magic numbers, and somewhat unusual names, I do not see a problem with your code
    What is exception safety? And what are magic numbers?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArtemisFowl2nd
    But, I still have no idea how to go about using this pointer to make a function that turns them into a multiplication table. Any help on that? How do you pass a two diminutional pointer array into a function?
    In this case you have a pointer to a pointer, not a 2D array, though you are using it as if it were a 2D array. Therefore, to pass it to a function, you just have the corresponding parameter of the function be a pointer to a pointer. You should also pass the number of elements (probably as 2 separate variables: one for the number of arrays, the other for the number of elements in each of those arrays).

    Quote Originally Posted by ArtemisFowl2nd
    What is exception safety?
    The topic requires more thorough treatment than I would like to give in a forum post, but consider: when you call new int [3], an exception could be thrown, e.g., std::bad_alloc. If this does happen after you have allocated memory on a previous iteration, it is your responsibility to call delete[] on the already allocated elements, or otherwise proceed and call delete[] later. This means using a try/catch construct. Better yet, you can make use of say, a standard container that will do cleanup by itself.

    Quote Originally Posted by ArtemisFowl2nd
    And what are magic numbers?
    A literal number (in your case: 3) that has a meaning that should be clarified. This can be done by a comment, but it usually is better to do so by using a name, i.e., a named constant, elaborating with a comment if necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. jumping into c++ chapter 5 problem 7
    By etricity in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2014, 11:23 PM
  2. Replies: 2
    Last Post: 02-24-2014, 05:51 PM
  3. "Jumping into C++" Chapter 5 Practice Problem
    By Grae in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2013, 01:46 PM
  4. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  5. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM

Tags for this Thread