Thread: Program Help C++ Arrays/Pointers/Streams involved

  1. #1
    Registered User
    Join Date
    Oct 2007

    Program Help C++ Arrays/Pointers/Streams involved

    So I created a data file with the following integers in it:
    4 2
    -3 1
    10 7
    3 5

    the first number is the dimension of my matrices and the rest represent two
    2x2 matrices
    |4 2 | |10 7 |
    |-3 1| | 3 5 |
    now I ask the user for the name of this input file that I created which is very simple. Then comes the troublesome stuff for me.

    I want to read this data from the input file onto a multidimensional dynamic array of integers by using pointers and new.

    Lastly I want to multiply the two matrices and print the result on the screen (which I have already figured out a loop for doing multiplying them w/out using pointers just simple arrays but I can't figure it out with pointers/streams).

    Here is what I have gotten so far I need some suggestions/insight on where to go from here b/c I am stuck b/c i get so confused with how I should read the integers from my input file and incorporate that into a loop that multiplies the two matrices.

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    typedef int* IntArrayPtr;
    int main()
            string infile;
            cout<<"Enter the input file: ";
            ifstream in_stream;
            if (
              cout<<"Input file "<< infile <<" opening failed.\n";
            IntArrayPtr *m = new IntArrayPtr[2];
            in_stream >> number;
            while (in_stream >> number)
    Thanks for reading.

  2. #2
    Registered User
    Join Date
    Jan 2005
    >> IntArrayPtr *m = new IntArrayPtr[2];
    That code allocates space for two int pointers. You still have to allocate space for two ints for each pointer (hint: use a loop).

    Also, your problem states that the first number read in from the file is the dimension of the matrix, so why are you using 2 there and not the first number you read from the file?

    Finally, note that pointers and new are poor ways of creating dynamic arrays in C++. If your class/instructor allows it, a vector is much more appropriate.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    So the first line is the number of matrices right?

    Lets begin with this
    int (*mats)[2][2];
    which will be our pointer to a number of [2][2] matrices.

    Then we do this with the first line
    in_stream >> numMats;
    mats = new int[numMats][2][2];
    Then we read all the data
    for ( m = 0 ; m < numMats ; m++ ) {
      for ( r = 0 ; r < 2 ; r++ ) {
        for ( c = 0 ; c < 2 ; c++ ) {
          if_stream >> mats[m][r][c];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM