Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 11th, 2005, 9:50 AM   #1
Irhaha
Newbie
 
Join Date: Jan 2005
Posts: 3
Rep Power: 0 Irhaha is on a distinguished road
To Dizzutch

Dizzutch,
Hi! Thanks for replying me in "Read and print a square matrix". But I have tried your suggestion and errors came out.
I inputted
#include <iostream.h>
#include <stdlib.h>


void main()
{
int i,j,N;
cout<<"Enter N:";
cin>>N;

for(i=0;i<N;i++)
for(j=0;j<N;j++)
int array[i][j]=(int)malloc(sizeof(int));
}
Irhaha is offline   Reply With Quote
Old Jan 11th, 2005, 10:20 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
was the error during compile time or during run time? and what is the error? I'm not that good with C++, but the syntax looks correct for ANSII C.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Jan 14th, 2005, 9:27 PM   #3
Tama
Programmer
 
Join Date: Dec 2004
Posts: 35
Rep Power: 0 Tama is on a distinguished road
>void main()
int main(). A void return value has never been correct C or C++.

>int array[i][j]=(int)malloc(sizeof(int));
This will fail to work for quite a few reasons. First, arrays must have a constant size in C++, variables won't work. Second, malloc returns a pointer yet you cast it to int. Third, array isn't a valid lvalue and thus cannot be assigned the return value of malloc, even if you do cast it to int.

Here is the correct C++ code to do what you want (using malloc):
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
  int N;
  int **array;

  cout<<"Enter N: ";
  cin>> N;

  // Create
  array = (int **)malloc(N * sizeof *array);
  for (int i = 0; i < N; i++)
    array[i] = (int *)malloc(N * sizeof *array[i]);

  // Initialize
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      array[i][j] = i * j;
  }

  // Display
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      cout<< array[i][j] <<'\t';
    cout<<endl;
  }

  // Destroy
  for (int i = 0; i < N; i++)
    free(array[i]);
  free(array);
}
However, with C++ you should be using new and delete instead of malloc and free because they understand C++ better:
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
  int N;
  int **array;

  cout<<"Enter N: ";
  cin>> N;

  // Create
  array = new int*[N];
  for (int i = 0; i < N; i++)
    array[i] = new int[N];

  // Initialize
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      array[i][j] = i * j;
  }

  // Display
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      cout<< array[i][j] <<'\t';
    cout<<endl;
  }

  // Destroy
  for (int i = 0; i < N; i++)
    delete [] array[i];
  delete [] array;
}
Or, even better, use the vector class that comes with the C++ standard library:
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

int main()
{
  int N;

  cout<<"Enter N: ";
  cin>> N;

  vector< vector<int> > array(N, vector<int>(N));

  // Initialize
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      array[i][j] = i * j;
  }

  // Display
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      cout<< array[i][j] <<'\t';
    cout<<endl;
  }
}
Tama is offline   Reply With Quote
Old Jan 14th, 2005, 9:31 PM   #4
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
sorry, i wrote that on a limb, didn't really test it.
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC