473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

generation of NXN matrix

Hello all,
I have a program in writing a program to generate a N by N matrix.
The program will ask for the dimension of the matrix to be generated and if the user inputs 4,the program should automatically generate
2 4 7 8
3 9 1 5
3 6 5 2
7 4 6 9

and if the user inputs 40 as the value of N then the program should generate a 40 by 40 matrix .
i would apprecaite it if someone in this room can urgently help out with this b4 monday.
As i need it by then,,,I have but i didnt get it.

Thanks all.
Feb 3 '07 #1
3 5812
Ganon11
3,652 Expert 2GB
Will the array be populated with random numbers, or are the numbers predetermined in some way?

In order to make a 2D array with an undetermined (variable) amount of rows/columns, you will have to use a double pointer. You will declare it as:

Expand|Select|Wrap|Line Numbers
  1. int **arr;
  2. arr = new int*[rows];
  3. for (int i = 0; i < rows; i++) {
  4.    arr[i] = new int[cols];
  5. }
Feb 3 '07 #2
Will the array be populated with random numbers, or are the numbers predetermined in some way?

In order to make a 2D array with an undetermined (variable) amount of rows/columns, you will have to use a double pointer. You will declare it as:

Expand|Select|Wrap|Line Numbers
  1. int **arr;
  2. arr = new int*[rows];
  3. for (int i = 0; i < rows; i++) {
  4.    arr[i] = new int[cols];
  5. }

Thanks for the reply,i got the code you sent me but like i said am just learning c++ and i really dont understand it.If you dont mind,can u help in writing the full code.The program will have to use rand() to populate the matrix when the user must have inputted the dimension.
thanks
Feb 4 '07 #3
Ganon11
3,652 Expert 2GB
Take it step by step. First, get the number from the user. Then, initialize your array as I showed you. Now you need to fill the array with random values. You can do this with a separate set of nested for... loops, or you can start a second loop inside the one used to initialize the variable. Each element in the array will be something like rand() % HIGH_NUM, if you want numbers between 0 and HIGH_NUM.
Feb 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
18
by: Hypnotik | last post by:
Hello everyone. I'm writing a program which uses a class called matrix. I have written all of the different functions, constructor, etc. When I run the program I receive "Constructor", which I...
2
by: nvinhphu | last post by:
Dear all, I would like to generate images like the ones in the right column at this link http://www.micthemodel.org/pages/examples.html (Sorry but I can not post attachment). These images...
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.