Program Help C++ Arrays/Pointers/Streams involved

This is a discussion on Program Help C++ Arrays/Pointers/Streams involved within the C++ Programming forums, part of the General Programming Boards category; So I created a data file with the following integers in it: 2 4 2 -3 1 10 7 3 ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    Program Help C++ Arrays/Pointers/Streams involved

    So I created a data file with the following integers in it:
    2
    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.

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

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> 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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    So the first line is the number of matrices right?

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

    Then we do this with the first line
    Code:
    in_stream >> numMats;
    mats = new int[numMats][2][2];
    Then we read all the data
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 12: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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09: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

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