I'm in the early stages of my program and right now I'm simply trying to initialize a 2D array to hold all dashes but I keep getting a ECX_BAD_ACCESS error. My code seems to work with a square array (ex: 5x5 or 6x6) but if I do 10 by 5 I get the error.
void readMatrix(char **twoDarray, int &rows, int &cols)
{
std::cout << "Enter number of rows for board";
std::cin >> rows;
std::cout << "Enter number of columns for board";
std::cin >> cols;
//dynamic 2D array initialization
twoDarray = new char*[rows];
for(int i = 0; i < cols; ++i)
twoDarray[i] = new char[rows];
//set elements of array to dashes
for(int i = 0; i < rows; ++i)
for(int j = 0; j < cols; ++j){
twoDarray[i][j] = '-';
}
//printing the array
for(int i = 0; i < rows; ++i){
std::cout << " " << std::endl;
for(int j = 0; j < cols; ++j)
std::cout << twoDarray[i][j] << " ";
}
}
Your first for-loop should go from 0 to #rows, not #cols. Also, in the body of that same loop allocate cols, not rows.
Related
the problem is
arange the columns of the array X (n, m) in ascending order.
I wrote such code in c ++ myself, but in the end it does not sort all the columns correctly. Please help if anyone knows.
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "array size: n = ";
cin >> n;
cout << "array size: m = ";
cin >> m;
int array[n][m];
int arrayS[n];
for (int z = 0; z < n; z++) {
for (int a = 0; a < m; a++) {
cin >> array[z][a];
}
}
for (int i = 0; i < n; i++) {
arrayS[i] = array[i][0];
}
for (int y = 0; y < n; y++) {
for (int i = 0; i < m; i++) {
cout << array[y][i] << " ";
}
cout << endl; //endline
}
cout << "************************************\n";
cout << "massivin sutunlari\n";
int enk = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
if (arrayS[j] > arrayS[j + 1])
{
enk = arrayS[j];
arrayS[j] = arrayS[j + 1];
arrayS[j + 1] = enk;
}
}
for (int i = 0; i < n; i++)
{
cout << arrayS[i] << " ";
}
}
First of all, we need to make your code compilable. You are using so called VLA (variable length arrays), like in int array[n][m];. Rhese VLAs are not part of the C++ language and will not compile. I replaced them with a std::vector which will work in the same way. Then I corrected the code identations (I edited also your question) and then it is possible to compile.
It will then lool like the below (but of course still not working)
#include <iostream>
#include <vector>
int main() {
int n, m;
std::cout << "array size: n = ";
std::cin >> n;
std::cout << "array size: m = ";
std::cin >> m;
std::vector<std::vector<int>> array(n, std::vector<int>(m, 0));
std::vector<int> arrayS(n);
for (int z = 0; z < n; z++) {
for (int a = 0; a < m; a++) {
std::cin >> array[z][a];
}
}
for (int i = 0; i < n; i++) {
arrayS[i] = array[i][0];
}
for (int y = 0; y < n; y++) {
for (int i = 0; i < m; i++) {
std::cout << array[y][i] << " ";
}
std::cout << std::endl; //endline
}
std::cout << "************************************\n";
std::cout << "massivin sutunlari\n";
int enk = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
if (arrayS[j] > arrayS[j + 1])
{
enk = arrayS[j];
arrayS[j] = arrayS[j + 1];
arrayS[j + 1] = enk;
}
}
for (int i = 0; i < n; i++)
{
std::cout << arrayS[i] << " ";
}
}
Some basic hints. Please use "speaking" variable names and write many many comments, best, one than more comment per line of code. Then you would already see the problems by yourself.
You have some hard bugs in your code that will most likely crash you program. Where you thy to sort, you use the magic number 9 (for whatever reasons) and so go out of bounds of the define arrays.
Additionally, your implementation of bubble sort is wrong. You may check many many examples in the net.
Then, you do only sort one column. You need to implement a bubble sort for each column of your matrix.
As a next step I will use better variable names and write comments. The code will still not work. But see and understand the difference.
#include <iostream>
#include <vector>
int main() {
// Here we will define the dimensions for our matrix
int numberOfRows{}, numberOfColumns{};
// Inform user what to do. Enter number of rows of the matrix
std::cout << "\nPlease enter the number of rows: ";
// Read the number of rows from the user via std::cin
std::cin >> numberOfRows;
// Inform user what to do. Enter number of columns of the matrix
std::cout << "\nPlease enter the number of columns: ";
// Read the number of rows from the user via std::cin
std::cin >> numberOfColumns;
// Define the array for the complete 2d matrix
std::vector<std::vector<int>> array(numberOfRows, std::vector<int>(numberOfColumns, 0));
// We can store one column here
std::vector<int> arrayS(numberOfRows);
// Inform user what to do. Enter all data for the matrix
std::cout << "\nPlease enter all data for the matrix";
// Now read all data, for all rows and columns from the user, via std::cin
for (int row = 0; row < numberOfRows; ++row) {
for (int col = 0; col < numberOfColumns; ++col) {
std::cin >> array[row][col];
}
}
// Copy the first column in a separate array
for (int i = 0; i < numberOfRows; i++) {
arrayS[i] = array[i][0];
}
// Show current data in our matrix
std::cout << "\n\nThe matrix:\n\n";
for (int row = 0; row < numberOfRows; ++row) {
for (int col = 0; col < numberOfColumns; ++col) {
std::cout << array[row][col] << " ";
}
std::cout << '\n'; //endline
}
// SHow some status message
std::cout << "\n\n\n************************************\n";
std::cout << "Sorted first column\n";
// This is a temporary variable needed to swap 2 values
int tempValue = 0;
// Try to do bubble sort
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
if (arrayS[j] > arrayS[j + 1])
{
tempValue = arrayS[j];
arrayS[j] = arrayS[j + 1];
arrayS[j + 1] = tempValue;
}
}
// Show the result of the sorting
for (int i = 0; i < numberOfRows; i++)
{
std::cout << arrayS[i] << " ";
}
}
Next, we fix the sorting for one column.
Only minor changes needed:
#include <iostream>
#include <vector>
int main() {
// Here we will define the dimensions for our matrix
int numberOfRows{}, numberOfColumns{};
// Inform user what to do. Enter number of rows of the matrix
std::cout << "\nPlease enter the number of rows: ";
// Read the number of rows from the user via std::cin
std::cin >> numberOfRows;
// Inform user what to do. Enter number of columns of the matrix
std::cout << "\nPlease enter the number of columns: ";
// Read the number of rows from the user via std::cin
std::cin >> numberOfColumns;
// Define the array for the complete 2d matrix
std::vector<std::vector<int>> array(numberOfRows, std::vector<int>(numberOfColumns, 0));
// We can store one column here
std::vector<int> arrayS(numberOfRows);
// Inform user what to do. Enter all data for the matrix
std::cout << "\nPlease enter all data for the matrix\n";
// Now read all data, for all rows and columns from the user, via std::cin
for (int row = 0; row < numberOfRows; ++row) {
for (int col = 0; col < numberOfColumns; ++col) {
std::cin >> array[row][col];
}
}
// Copy the first column in a separate array
for (int i = 0; i < numberOfRows; i++) {
arrayS[i] = array[i][0];
}
// Show current data in our matrix
std::cout << "\n\nThe matrix:\n\n";
for (int row = 0; row < numberOfRows; ++row) {
for (int col = 0; col < numberOfColumns; ++col) {
std::cout << array[row][col] << " ";
}
std::cout << '\n'; //endline
}
// SHow some status message
std::cout << "\n\n\n************************************\n";
std::cout << "Sorted first column\n";
// This is a temporary variable needed to swap 2 values
int tempValue = 0;
// Try to do bubble sort
// Go over all entries-1 in the column array.
// And compare the current value with the next value
for (int i = 0; i < numberOfRows-1; i++)
for (int j = 0; j < numberOfRows - 1; j++)
{
// If this value is bigger than the following value, then swap them
if (arrayS[j] > arrayS[j + 1])
{
// Swap
tempValue = arrayS[j];
arrayS[j] = arrayS[j + 1];
arrayS[j + 1] = tempValue;
}
}
// Show the result of the sorting
for (int i = 0; i < numberOfRows; i++)
{
std::cout << arrayS[i] << " ";
}
}
This works already. But, my gues is that all columns shall be sorted.
Then, finally, it would look like that:
#include <iostream>
#include <vector>
int main() {
// Here we will define the dimensions for our matrix
int numberOfRows{}, numberOfColumns{};
// Inform user what to do. Enter number of rows of the matrix
std::cout << "\nPlease enter the number of rows: ";
// Read the number of rows from the user via std::cin
std::cin >> numberOfRows;
// Inform user what to do. Enter number of columns of the matrix
std::cout << "\nPlease enter the number of columns: ";
// Read the number of rows from the user via std::cin
std::cin >> numberOfColumns;
// Define the array for the complete 2d matrix
std::vector<std::vector<int>> array(numberOfRows, std::vector<int>(numberOfColumns, 0));
// Inform user what to do. Enter all data for the matrix
std::cout << "\nPlease enter all data for the matrix\n";
// Now read all data, for all rows and columns from the user, via std::cin
for (int row = 0; row < numberOfRows; ++row) {
for (int col = 0; col < numberOfColumns; ++col) {
std::cin >> array[row][col];
}
}
// Show current data in our matrix
std::cout << "\n\nThe matrix:\n\n";
for (int row = 0; row < numberOfRows; ++row) {
for (int col = 0; col < numberOfColumns; ++col) {
std::cout << array[row][col] << " ";
}
std::cout << '\n'; //endline
}
// SHow some status message
std::cout << "\n\n\n************************************\n";
std::cout << "Matrix with sorted column\n";
// This is a temporary variable needed to swap 2 values
int tempValue = 0;
// Try to do bubble sort
// Go over all entries-1 in the column array.
// And compare the current value with the next value
// Do for all columns
for (int col = 0; col < numberOfColumns; ++col)
for (int i = 0; i < numberOfRows-1; ++i)
for (int row = 0; row < numberOfRows - 1; ++row)
{
// If this value is bigger than the following value, then swap them
if (array[row][col] > array[row + 1][col])
{
// Swap
tempValue = array[row][col];
array[row][col] = array[row + 1][col];
array[row + 1][col] = tempValue;
}
}
// Show the result of the sorting
for (int row = 0; row < numberOfRows; ++row) {
for (int col = 0; col < numberOfColumns; ++col) {
std::cout << array[row][col] << " ";
}
std::cout << '\n'; //endline
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So, I dynamically allocated a 2d array in this way:
int rs, co;
cin >> ro;
cin >> co;
int **array = new int* [ro];
for(int i = 0; i < ro; i++){
array[i] = new int [co];
}
I filled it:
for(int i = 0; i < ro; i++){
for(int j = 0; j < co; j++){
cout << "Row: " << i << " Column: " << j << " : ";
cin >> *(*(array + i) + j);
}
}
My question is:
how can I deallocate an X row or column where X is given by the user?
I know I should use "delete" command but I can not understand how
You cannot delete a column, but you can delete a row.
If this doesn't make sense to you directly, try reading my 2D dynamic array (C). Focus on the figure:
I know that my link is in C, but what you try to do reminds me of C. In C++, you could use an std::vector<int> instead.
I assume that you know that you can write:
array[i][j]
instead of:
*(*(array + i) + j)
Example on deleting first row on a matrix with two rows.
delete [] array[0];
int **tmp = new int*[1];
tmp[0] = array[1];
delete [] array;
array = tmp;
Generalized example:
#include <iostream>
using namespace std;
int main(void) {
int ro = 3, co = 2;
int **array = new int* [ro];
for(int i = 0; i < ro; i++){
array[i] = new int [co];
}
// fill and print the matrix
for(int i = 0; i < ro; i++){
for(int j = 0; j < co; j++) {
array[i][j] = i;
cout << array[i][j] << " ";
}
cout << endl;
}
int rowToDel = 1;
delete [] array[rowToDel];
int **tmp = new int*[ro - 1];
int tmpI = 0;
for(int i = 0; i < ro; ++i)
if(i != rowToDel)
tmp[tmpI++] = array[i];
delete [] array;
array = tmp;
ro = ro - 1;
cout << "Array after deleting " << rowToDel << "-th row\n";
for(int i = 0; i < ro; i++){
for(int j = 0; j < co; j++) {
cout << array[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output:
0 0
1 1
2 2
Array after deleting 1-th row
0 0
2 2
You can delete a row by simply calling
delete [] array[i];
You cannot delete columns, since columns are not lined up like rows are.
For example, lets say we have a 2D array like this:
row1: abcd
row2: efgh
row3: ijkl
In the computer, the data is actually lined up like this:
abcd .... other data.... efgh .... other data..... ijkl
Deleting a row is easy, since the computer has them lined up next to eachother. But, because the rows are created dynamically, they do NOT have to reside next to eachother. They are allocated wherever there is space is available.
So, the columns are not lined up at all, and it doesn't make sense to "deallocate".
With a 2D array of vector is easy.
Just use erase function for rows.
For columns, iterate on rows and delete each element:
int ro = 4, co = 3;
vector< vector<int> > vec(ro);
for (int i = 0; i < ro; i++)
vec[i].resize(co);
// fill and print the matrix
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec[i].size(); j++) {
vec[i][j] = i;
cout <<vec[i][j] << " ";
}
cout << endl;
}
int rowErasePosition = 1;
vec.erase(vec.begin() + rowErasePosition);
// reprinting
cout << "second row deleted" << endl;
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec[i].size(); j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
//deleting column
int columnErasePosition = 1;
for (int i = 0; i < vec.size(); i++) {
vec[i].erase(vec[i].begin() + columnErasePosition);
}
// reprinting
cout << "second column deleted" << endl;
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec[i].size(); j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
first off sorry for the slightly messy code as I was fiddling with different things to try to get it to work. As of now, my code can multiply Square Matrices just fine; however, it has difficulties computing Non Square Matrices. My best guess after debugging is how i re-size my vectors, and that there is an out of bounds error which causes the program to crash. Any help would be appreciated, my code should be able to Multiply any Vectors sizes withing Matrix Multiplication Rules.
I also would like to note this is a HW assignment so I am limited to how I can build my code, basically ONLY using vectors, cannot write your own class etc....
#include <iostream>
#include <vector>
using namespace std;
void multiply_matrices(vector <vector<int> > matrix1,vector <vector<int> > matrix2, int cols, int rows2);
void setMatrix(vector <vector<int> > &matrix, int rows, int cols);
int main()
{
int rows, cols, rows2, cols2;
vector< vector<int> > matrix, matrix2;
cout<<"Please enter the number of Rows and Columns for your first Matrix."<<endl;
cout<<"Rows: ";
cin>>rows;
cout<<"Columns: ";
cin>>cols;
matrix.resize(cols, vector<int>(rows,0)); //Saw this online so not sure how it works but it works, if i take out one i cant do row<column and vice versa
matrix.resize(rows, vector<int>(cols,0));
cout<<"Size has been declared, please enter data for your matrix"<<endl;
setMatrix(matrix,rows,cols);
cout<<"Second Matrix Automatically Set by Matrix Multiplication Rule"<<endl; //Just automatically sets second matrix as per Matrix Multiplication Rule
rows2=cols;
cols2=rows;
cout<<"Second Matrix Size is: " << rows2 << " by " << cols2 << endl;
matrix2.resize(cols2, vector<int>(rows2,0));
matrix2.resize(rows2, vector<int>(cols2,0));
setMatrix(matrix2,rows2,cols2);
cout<<"Multiplied Matrix is:"<<endl;
multiply_matrices(matrix,matrix2,cols,rows2);
system("PAUSE");
return 0;
}
void setMatrix(vector <vector<int> > &matrix, int rows,int cols){
int num;
for(int i = 0; i < rows; i ++)
{
for (int j = 0; j < cols; j++)
{
cout << "Enter Value for Row " << (i+1) << " Column " << (j+1) << ": ";
cin>>num;
matrix[i][j]=num;
}
cout << endl;
}
/*for(int i = 0; i < rows; i ++)
{
for (int j = 0; j < cols; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
*/
}
void multiply_matrices(vector <vector<int> > matrix1,vector <vector<int> > matrix2, int cols, int rows2){
vector< vector<int> > tempMatrix;
int newrows=rows2;
int newcols=cols;
int sum;
tempMatrix.resize(newcols, vector<int>(newrows,0)); //Resizing new matrix to proper size, so if it was (2x3)(3x2), new matrix is (3x3)
for (int i = 0; i < newrows; i++) //This Works Fine for Square Matrixes but not for others, i have no clue how to fix it?
{
for (int j = 0; j < newcols; j++){
//sum=0;
for (int u = 0; u < newcols; u++)
{
//sum+=matrix1[i][u] * matrix2[u][j];
//tempMatrix[i][j]=sum;
tempMatrix[i][j] += matrix1[i][u] * matrix2[u][j];
}
}
}
for(int i = 0; i < newrows; i ++)
{
for (int j = 0; j < newcols; j++)
{
cout << tempMatrix[i][j] << " ";
}
cout << endl;
}
}
Initialize your first matrix like this:
matrix.resize(rows, vector<int>(cols,0));
and your second like this:
matrix2.resize(rows2, vector<int>(cols2,0));
where rows2 = cols. Note that there is no "multiplication rule" that implies cols2 == rows.
The problem is in your multiply_matrices function where the loops should be
for (int i = 0; i < rows; i++) // or matrix1.size()
for (int j = 0; j < cols2; j++) // or tempMatrix[i].size()
for (int u = 0; u < cols; u++) // or rows2 or matrix1[i].size()
but as already stated in the comments, it would be better to use vector::size() instead of passing the sizes as additional parameters.
Also, if you multiply (2x3)(3x2), the result is (2x2):
tempMatrix.resize(rows, vector<int>(cols2,0));
There is nothing wrong with the resize() function. What may be wrong is that you ignore the maximum size and rely solely on variables that are passed to your functions.
For example, your setMatrix function is passed rows and cols, but this is not necessary.
The function should be rewritten using only the matrix to provide the sizes to the loops:
void setMatrix(vector<vector<int> > &matrix)
{
int num;
for(int i = 0; i < matrix.size(); ++i)
{
for (int j = 0; j < matrix[i].size(); ++j)
{
cout << "Enter Value for Row " << (i+1) << " Column " << (j+1) << ": ";
cin>>num;
matrix[i][j] = num;
}
cout << endl;
}
}
You have the same issue with multiply_matrix. What you should be doing is ensure that you're loops use the return value of vector::size(), which you do not do. The problem is here:
for (int i = 0; i < newrows; i++)
{
for (int j = 0; j < newcols; j++)
{
for (int u = 0; u < newcols; u++)
{
tempMatrix[i][j] += matrix1[i][u] * matrix2[u][j];
You sized the tempMatrix to newrows rows and newcols columns. But how do you know that matrix1 and matrix2 have at least newrows rows and newcols columns? You don't know, but you just assume they do.
So you either need to ensure that the size of matrix1 and matrix2 can accommodate the number of rows/columns, or you throttle those loops to use the minimum rows/columns.
Overall, the issue is that nowhere do you use vector::size() in your code that I see. So start to use size() to your advantage -- don't create superfluous (and possibly erroneously set) variables that supposedly denote the sizes of the rows and columns.
I made Matrix Multiplication program in c++ using dynamic Multidimensional arrays.
problem is when i enter test values
matrix A = row1{ 1 } , row2 { 2 } matrix B = row1 {1 ,2 , 3} , it stops working at the loop where user
inputs values of first array , i found it using debugging. but program works fine when i enter
matrix A = row1{ 1 , 2 } , row2 { 3 , 4 } matrix B = row1 {5 , 6 } , row2 {7 , 8}
i want this program to be a general program that can multiply all matrixs
#include <iostream>
using namespace std;
class Lab_02
{
public:
void Product(){
int a1Rows, a1Columns;
int a2Rows, a2Columns;
cout << "Plz Enter the no. of rows for Array 1 :";
cin >> a1Rows;
cout << "Plz Enter the no. of columns for Array 1 :";
cin >> a1Columns;
cout << "Plz Enter the no. of rows for Array 2 :";
cin >> a2Rows;
cout << "Plz Enter the no. of columns for Array 2 :";
cin >> a2Columns;
int **dynamicArray = 0;
int **dynamicArray2 = 0;
int **dynamicArray3 = 0;
cout << endl;
for (int i = 0; i < a1Rows; i++)
{
dynamicArray3 = new int *[a1Rows];
}
for (int i = 0; i < a2Columns; i++)
{
dynamicArray3[i] = new int[a2Columns];
}
// memory allocated for elements of rows.
for (int i = 0; i < a1Rows; i++)
{
dynamicArray = new int *[a1Rows];
}
// memory allocated for elements of each column.
for (int i = 0; i < a1Columns; i++)
{
dynamicArray[i] = new int[a1Columns];
}
// memory allocated for elements of rows.
for (int i = 0; i < a2Rows; i++)
{
dynamicArray2 = new int *[a2Rows];
}
// memory allocated for elements of each column.
for (int i = 0; i < a2Columns; i++)
{
dynamicArray2[i] = new int[a2Columns];
}
cout << "enter the values or array 1 \n";
for (int i = 0; i < a1Rows; i++)
{
for (int j = 0; j < a1Columns; j++)
{
cout << "array[" << i << "][" << j << "]\t";
cin >> dynamicArray[i][j];
}
}
cout << "enter the values or array 2 :\n";
for (int i = 0; i < a2Rows; i++)
{
for (int j = 0; j < a2Columns; j++)
{
cout << "array[" << i << "][" << j << "]\t";
cin >> dynamicArray2[i][j];
}
}
int sum;
for (int i = 0; i < a1Rows; i++)
{
for (int j = 0; j < a1Columns ; j++)
{
sum = 0;
for (int k = 0; k < a2Columns ; k++)
{
sum = sum + (dynamicArray[i][k] * dynamicArray2[k][j]);
}
dynamicArray3[i][j] = sum;
}
}
cout <<"Result" << endl << endl;
for (int i = 0; i < a1Rows; i++)
{
for (int j = 0; j < a2Columns; j++)
{
cout << dynamicArray3[i][j] << "\t";
}
cout << endl;
}
}
};
void main(void)
{
Lab_02 object;
object.Product();
}
Your memory allocations for the matricies are the issue.
Change them to something like the following
// memory allocated for elements of rows.
dynamicArray = new int *[a1Rows];
// memory allocated for elements of each column.
for (int i = 0; i < a1Rows; i++)
{
dynamicArray[i] = new int[a1Columns];
}
You need to allocate one array of array for the rows and then you need to loop over the rows and allocate the columns.
The issue with the code is that you are not supposed to allocate the "rows" in a loop. All you need is one allocation for the rows, and then loop to allocate the data for each row.
So for example, instead of this:
for (int i = 0; i < a1Rows; i++)
{
dynamicArray = new int *[a1Rows];
}
// memory allocated for elements of each column.
for (int i = 0; i < a1Columns; i++)
{
dynamicArray[i] = new int[a1Columns];
}
The correct way would be this:
dynamicArray = new int *[a1Rows];
for (int i = 0; i < a1Columns; i++)
{
dynamicArray[i] = new int[a1Columns];
}
You made the same mistake for each of the loops.
Also, some points:
You failed to deallocate the memory that was allocated.
If you used std::vector, then things become much easier.
You need to check if the matrices are multiplyable before performing the sum loop. By multiplyable meaning that the number of columns and rows of matrix A and B satisfy the requirements for multiplication of A and B.
for (int i = 0; i < a1Rows; i++)
{
for (int j = 0; j < a1Columns; j++)
{
sum = 0;
for (int k = 0; k < a2Columns; k++)
sum = sum + (dynamicArray[i][k] * dynamicArray2[k][j]);
dynamicArray3[i][j] = sum;
}
}
This loop will go haywire if the dynamicArray1 and dynamicArray2 do not have the requisite number of columns and rows before multiplying.
First, the following test should be done before multiplying:
if (a1Columns != a2Rows)
return;
Second, your k loop is wrong. It should be this:
for (int k = 0; k < a2Rows; k++)
sum = sum + (dynamicArray[i][k] * dynamicArray2[k][j]);
dynamicArray3[i][j] = sum;
I am learning C++ by myself,by solving different problems.
I am trying to solve problem which was originally designed for Pascal in C++.
It should ask user to input 3 integers M,N and q.
Then it should make 2d array of integers with size MxN, where all the elements of (I=I,...M) line will be the members of geometrical progression with first element equal to number of line (I) and denominator q.
I wanted to create a dynamic massive, but I realized that it won't really work with two undefined integers. So, I tried vectors. I guess that I created them in a right way, but I've got no idea how to make a geometrical progression.
Here is my code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int m, n, q;
cout << "Enter the number for M \n";
cin >> m;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "This is not a number! " << endl;
system("pause");
return 0;
}
cout << "Enter the number for N \n";
cin >> n;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "This is not a number! " << endl;
system("pause");
return 0;
}
cout << "Enter the number for Q \n";
cin >> q;
if (cin.fail())
{
cin.clear();
cin.ignore();
cout << "This is not a number! " << endl;
system("pause");
return 0;
}
int** matrix;
matrix = new int*[m];
for (int i = 0; i < m; i++)
matrix[i] = new int[n];
for (int i = 0; i < m; i++)
{
matrix[i][0] = i + 1;
}
for (int i = 0; i < m; i++)
{
for (int j = 1; j < n; j++)
{
matrix[i][j] = (i + 1)*pow(i, j);
cout << matrix[i][j];
}
}
system("pause");
return 0;
}
Note: You can create a two dimensional array of a variable size, although it involves memory allocation and is slightly ugly.
int** matrix;
matrix = new int*[M];
for (int i = 0; i < M; i++)
matrix[i] = new int[N];
That's the code to create an array of size MxN.
Don't forget to deallocate your memory like so:
for (int i = 0; i < M; i++)
delete matrix[i];
delete matrix;
As far as your question about the geometric progression, I am unsure of what you are asking. When you say geometric progression do you refer to something along the lines of 2 10 50 250 etc.? I am not sure what you mean by "lines" as you don't refer to any such variable in your code.
EDIT
So once the MxN matrix is created, iterate through the rows and initialize the rows like so:
for (int i = 0; i < M; i++)
{
matrix[i][0] = i+1;
}
This should set the first column of each row to the correct number.
Then something along the lines of this should fill out the rest of the geometric progression:
for (int i = 0; i < M; i++)
{
for (int j = 1; j < N; j++)
{
matrix[i][j] = (i+1)*pow(r,j);
//note that you'll probably have to do some typecasting
//p.s. I'm not 100% sure that this is the correct formula
}
}
I think this is what you are looking for. Let me know if it works because I haven't tested it myself.
Print the matrix like this:
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
std::cout << matrix[i][j] << " ";
}
std::cout << "\n";
}