beginner c++ programming help please

Discussion in 'OT Technology' started by sushibug, Nov 17, 2007.

  1. sushibug

    sushibug :happysad:

    Joined:
    May 16, 2002
    Messages:
    3,096
    Likes Received:
    0
    Location:
    San Diego, CA
    i wrote this code where the main idea is dynamic memory allocation with matrices and c++. the problem is that it hangs after i input the matrix size (n). can anyone tell whats wrong with it? (i'm pretty good at java but i just started learning c/c++ and i suck at that). oh btw, the arraysum() function is probably incorrect but it still hung when i commented it out.

    #include<iostream.h>

    class mainprog {
    int n;
    int m;
    double **a;
    double sum;

    public:

    void input() {
    cout << "Input NxN size" << "\n";
    cin >> n;
    m = n;
    }

    void alloc();
    void arrayinput();
    void arraysum();
    void arrayoutput();
    ~mainprog();
    };


    // Allocation of memory for 2-dimensional array with n*n elements
    void mainprog::alloc() {
    a = new double *[n];
    if (a == NULL) {
    cout << "Memory has not been allocated \n";
    exit(0);
    }

    for (int i = 0; i < n; i++) {
    *(a+i) = new double [m]; // Allocates n elements for each row
    if (*(a+i) == NULL) {
    cout << "Memory has not been allocated \n";
    exit(0);
    }
    }
    }

    // Input of 2-dimensional array with n * m elements
    void mainprog::arrayinput() {
    cout << "Input " << n << " numbers for the array \n";
    for (int i=0; i<n; i++)
    for (int j=0; j<m; j++)
    cin >> *(*(a+i)+j);
    }

    void mainprog::arraysum() {
    sum = 0;
    for (int i=0; i<n; i++) {
    for (int j=0; j<m; j++) {
    double temp = *(*(a+i)+j);
    if (temp > 0 || temp < 5)
    sum += *(*(a+i)+j);
    }
    }
    cout << "sum =" << sum;
    }

    // Output of 2-dimensional array with n * n elements
    void mainprog::arrayoutput() {
    cout << "outputting array";
    for (int i=0; i<n; i++) {
    for (int j=0; j<n; j++)
    cout << *(*(a+i)+j) << " ";
    cout << "\n";
    }
    }


    // Destructor function
    mainprog::~mainprog() {
    delete a;
    }

    main() {
    mainprog * p;
    p= new mainprog; // Declaration of a pointer to class array2ddyn
    if (p==NULL) exit(0);
    p->input();
    p->alloc();//
    p->arrayinput();//
    p->arraysum();
    p->arrayoutput();//
    }
     
  2. TheDarkHorizon

    TheDarkHorizon \xC0\xFF\xEE

    Joined:
    Sep 26, 2002
    Messages:
    2,396
    Likes Received:
    0
    Location:
    San Francisco, CA
    Firstly, use the
    Code:
     VBB code so that your code retains the formatting.  It does look like the program is finishing:
    [code]
    Input NxN size
    2
    Input 2 numbers for the array 
    1
    2
    3
    4
    sum =10outputting array1 2 
    3 4 
    
    though your program is asking for n numbers, when you're really waiting for n*m numbers. Also, include iostream instead of iostream.h, as the latter has been deprecated for some time (and add 'using namespace std'). Just nitpicking, but main should always return an integer.
     
  3. CodeX

    CodeX Guest

    the program works, what you were trying to demonstrate works fine, you just are asking for the wrong inputs, and displaying the wrong outputs with shitty formatting
     

Share This Page