?
Solved

Problem with Matrix

Posted on 2003-03-18
5
Medium Priority
?
229 Views
Last Modified: 2010-04-01
Hello Friends,

I have a text file with the following contents
3
1 3 46
1 2 67
2 3 12

the first line tells me that i have a 3 x 3 matrix
now my matrix should look like this
  1  2  3
1 0 67 46
2 -  0 12
3 -  -  0

this is my code
#include <iostream.h>

#include <fstream.h>

int main()
{
     int a,b,c,d,m,i,j;
     ifstream inClientFile("clients.txt");

     if(!inClientFile)
     {
          cerr << "File could not be found:\n";
     }
     inClientFile >> a ;
     cout << a<<"\n";
         int *m = new int[a][a];

     inClientFile.ignore(1024,'\n');

     for (i=0;i<=a;i++)
          for (j=0;j<=a;j++)
          {
               if (i=j)
               {
                    inClientFile >> b >> c >> d;
                    cout << b <<" "<< c <<" "<<d<<"i=j"<<"\n";
                    //m[i][j] = 0;<-- says error C2109: subscript requires array or pointer type .
               //     i = b;
               //     j = c;
               //     m[i][j] = d;
               }
               else
                        exit();
     //cout << m[i][j];
          }
     return 0;
}

How can i insert the third value accordingly.
Please help me create my Matrix array dynamically.
Thank you.
0
Comment
Question by:anarki5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Expert Comment

by:efn
ID: 8164911
You can't create a two-dimensional array with the new operator, and you can't put two subscripts on an integer pointer, because the compiler doesn't know how big your rows are.

This FAQ answer will tell you how to create and use a dynamically allocated two-dimensional array:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 100 total points
ID: 8164920
here is code modified for you..
#include <iostream>

#include <fstream>

int
main ()
{
  int a, b, c, d, i, j;
  ifstream inClientFile ("clients.txt");

  if (!inClientFile)
    {
      cerr << "File could not be found:\n";
    }
  inClientFile >> a;
  cout << a << "\n";
  int **m = new (int *)[a];
  for (i = 0; i < a; i++)
    m[i] = new int[a];

  inClientFile.ignore (1024, '\n');

  for (i = 0; i < a; i++)
    for (j = 0; j < a; j++)
      inClientFile >> m[i][j];

  for (i = 0; i < a; i++)
    {
      for (j = 0; j < a; j++)
     cout << m[i][j] << " ";
      cout << endl;
    };
  return 0;
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8165009
>> You can't create a two-dimensional array with the new operator

Why not? Its done exactly the way Akshay has done. First, allocate memory for an array of pointers, then allocate memory in a loop and assign the base addresses to those pointers.

Mayank.
0
 
LVL 15

Expert Comment

by:efn
ID: 8165123
Perhaps I was a bit too terse.  I meant you can't do it the way anarki5 tried to do it:

int *m = new int[a][a];

Certainly it's possible to allocate some memory with the new operator and use the memory as a two-dimensional array.  The FAQ I cited describes the approach Akshay used and others.
0
 

Author Comment

by:anarki5
ID: 8168651
Thank you very much akshayxx.
Mayank and efn Thank you for the support guys. All's fair in the game when you want to learn.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question