by now I have this code
int main() {
int cols;
cout << "cols";
cin >> cols;
int rows;
cout << "rows";
cin >> rows;
char** charArray = new char*[rows];
for (int i = 0; i < rows; ++i) {
charArray[i] = new char[cols];
}
// Fill the array
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
charArray[i][j] = char('1' + i + j );
}
}
// Output the array
for (int i = 0; i < rows; ++i) {
for (int j = i; j < cols ; ++j) {
cout << charArray[i][j];
}
cout << endl;
}
// Deallocate memory by deleting
for (int i = 0; i < rows; ++i) {
delete[] charArray[i];
}
and the output is like
1 2 3
2 3 4
How can I make it to be
1 2 3
4 5 6
I tried a lot, im a newbie in programming so can you please explain me what's the matter with this problem! Thanks a lot!!
Just change this:
charArray[i][j] = char('1' + i + j );
by:
charArray[i][j] = char('1' + i*cols + j);
BTW:
You have a typo in the output array loop:
for (int j = i; j < cols ; ++j) {
Should be:
for (int j = 0; j < cols ; ++j) {
Array filling loop should be
int count = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
charArray[i][j] = char('1' +count );
count++;
}
}
try with above code
You are building a matrix, here right. So it's 2-dimensional.
What the output means is that your matrix looks like:
1 2 3
2 3 4
Which is only natural, since, while filling it, you wrote for each element at coordinate i,j to be value 1+i+j. That means if i and j both equal 1 (second line, second column, since index start at 0), you put value the 1+1+1=3.
If you want to get the matrix:
1 2 3
4 5 6
You'll have to fill it with
charArray[i][j] = char('1' + i*ncols + j );
This way when you fill the first line, i = 0, so it's the same.
But then on the second line, i=1, so you get 1 + 3 + 0 = 4. And then 1+3+1=5, etc.
Edit: people are fast here. By the time I write my answer, already 2 others posted theirs :D
Related
I want make any matrix[n][n+1] become an upper triangular matrix[n][n+1].
I did this code but that causes a segmentation fault.
void diagonalizarMatriz(float** Matriz, int n){
for(int i = 0; i < n-1; i++)
for(int k = 0; k < n; k ++)
for(int j = n; j >= i; j++)
Matriz[k][j] = Matriz[k][j] - ((Matriz[k][i] * Matriz[i][j]) / Matriz[i][i]);
}
int main(){
float** Matriz = new float* [3];
for(int i = 0; i < 3 ; i++)
Matriz[i] = new float [4];
//test matrix
Matriz[0][0] = 1;
Matriz[0][1] = 4;
Matriz[0][2] = 52;
Matriz[0][3] = 57;
Matriz[1][0] = -27;
Matriz[1][1] = -110;
Matriz[1][2] = -3;
Matriz[1][3] = -134;
Matriz[2][0] = 22;
Matriz[2][1] = 2;
Matriz[2][2] = 14;
Matriz[2][3] = 38;
diagonalizarMatriz(Matriz, 3);
Here
for(int j = n; j >= i; j++)
you start with n at the upper border of the dimensions of your array and the count up,
very soon you therefor access beyond your array, which gets you a segfault if you are lucky.
At a guess you want
for(int j = n; j >= i; j--)
to count down.
UPDATE
Assuming your question is about diagonalizing a matrix (as Eugene claims in the comment section):
In addition to what Yunnosch pointed out, diagonalizing a matrix means that the matrix must be a square n x n. However, in main you're initializing it to be a 3 x 4 matrix.
Original code
float** Matriz = new float* [3];
for(int i = 0; i < 3 ; i++)
Matriz[i] = new float [4];
To get rid of the segfault, change the following part in main (set matrix to 3 x 3):
float** Matriz = new float* [3];
for(int i = 0; i < 3 ; i++)
Matriz[i] = new float [3];
//test matrix
Matriz[0][0] = 1;
Matriz[0][1] = 4;
Matriz[0][2] = 52;
Matriz[1][0] = -27;
Matriz[1][1] = -110;
Matriz[1][2] = -3;
Matriz[2][0] = 22;
Matriz[2][1] = 2;
Matriz[2][2] = 14;
Finally, to get the following matrix (lower-triangular):
1 0 0
4 5 0
7 8 9
Remove the equal sign from your third nested loop:
for(int j = n; j > i; j--)
I assume from here you can work your way through this, to make it an upper-triangular matrix.
OLD ANSWER
Try this:
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int row = 3;
int col = 3;
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (i > j) {
cout << "0" << " ";
} else
cout << matrix[i][j] << " ";
}
cout << endl;
}
Will give you this matrix
1 2 3
0 5 6
0 0 9
I'm trying to turn an ordered pair of numbers into a 3x3 matrix and am having troubles writing the conversion.
I've tried multiple variations of nested for loops to solve this issue, but I'm not getting the desired results.
This is my current attempt:
for(i = 0; i < 6; i++) {
row = matrixAin[i][0];
col = matrixAin[i][1];
for(j = 1; j <= row; j++) {
for(int k = 1; k <= col; k++) {
matrixA[j][k] = 1;
}
}
}
This is all the code I have:
#include <iostream>
using namespace std;
int main() {
int matrixAin[6][2]; // ordered pair of Matrix A
int matrixA[3][3]; // 3x3 matrix of Matrix A
int i, j, row, col; // for the for loops
// Sets Matrix A & B values to 0
for (i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
matrixA[i][j] = 0;
matrixB[i][j] = 0;
}
}
// input of Matrix A
cout << "Please input the ordered pairs for matrix A: ";
for (i = 0; i < 6; i++) {
cin >> matrixAin[i][0] >> matrixAin[i][1]; // row , col
}
// sets row / col to 1 for Matrix 3x3
// this is the code with the issue
for(i = 0; i < 6; i++) {
row = matrixAin[i][0];
col = matrixAin[i][1];
for(j = 1; j <= row; j++) {
for(int k = 1; k <= col; k++) {
matrixA[j-1][k] = 1;
}
}
}
// Displays matrix A
cout << "A= ";
for(int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrixA[i][j] << ' ';
}
cout << endl;
}
}
This is what the input for this matrix should look like
Please input the ordered pairs for matrix A:
1 2
1 3
2 1
2 2
3 2
3 3
This is the expected results:
A =
0 1 1
1 1 0
0 1 1
Your current code takes in each pair and then for each subrectangle of size row x col, sets the area of the rectangle to 1. It's really close. You just need to set once for each ordered pair:
for(i = 0; i < 6; i++) {
row = matrixAin[i][0];
col = matrixAin[i][1];
matrixA[row - 1][col - 1] = 1;
}
Replace the inner for-loop with matrixA[row - 1][col - 1] = 1;. Do not forget to check if col and row are between 1 and 3.
User inputs desired amount of rows and columns, then if any row is found with even numbers the code should add a duplicate row below it. But instead I get no anwswer. Help needed. I only need to add rows with this manual method. I know their are much stable ways with vectors. But right now this is only way for me.
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int rows, columns;
std::cout << "Enter the number of rows: ";
std::cin >> rows;
std::cout << "Enter the number of columns: ";
std::cin >> columns;
//-----------------------Generating 2-D array---------------------------------------------------------
int **array = new int*[2 * rows]; // allocating memory for the array
for (int i = 0; i < rows; i++)
array[i] = new int[columns];
//-----------------------Input Array Elements---------------------------------------------------------
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < columns; i++)
for (int j = 0; j < rows; j++)
std::cin >> array[i][j];
//--------------------Loop for the array output--------------------------------------------------------
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
std::cout << array[i][j] << " ";
}
std::cout << "\n";
}
//-------------------Loop for finding rows with even numbers And adding the clone rows----------------------------------------
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
if (array[i][j] % 2 == 0) {
for (int k = rows; k > i; k--) {
for (int j = 0; j < rows; j++) {
array[k][j] = array[k - 1][j];
;
}
}
}
for (int j = 0; j < rows; j++)
array[i + 1][j] = 0;
i++;
columns++;
}
}
std::cout << "\n";
//--------------------Loop for the answer OUTPUT--------------------------
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
std::cout << array[i][j] << " ";
}
std::cout << "\n";
}
return 0;
}
For example if we enter an array
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
The answer should be
1 1 1 1 1
2 2 2 2 2 -------> even
2 2 2 2 2 --------> Duplicate
3 3 3 3 3
4 4 4 4 4 ---------> Even
4 4 4 4 4 ----------> clone
5 5 5 5 5
I am trying to fill an array of 52 with the numbers 0 - 12. Once it hits 12, it needs to go back to 0 - 12 again. You might have already guessed it's a deck of cards. My code is below and doesn't work. It prints 0 - 12 one time, but then prints the address of the array I believe for the remainder of the iterations left.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int myArray[52];
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
myArray[i] = i;
}
}
for (int k = 0; k < 52; k++)
{
cout << myArray[k] << endl;
}
//system("pause");
return 0;
}
Can someone please help me with this brain fart?
int myints[52];
for (int idx = 0; idx < 52; idx++)
{
myints[idx] = idx % 13;
}
Modulus of 13 will range from 0 to 12.
You're indexing the same first 12 elements of the array in the inner loop for every iteration of the outer loop.
Try changing it to something like this
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
myArray[i + 13 * j] = i;
}
}
I have tried without error, [2x2] * [2x2] and [2x3] * [3x2].
The "odd one" is the output from the [3x2] * [2x3]. its output looks something like this: row 1: [9 10 11], row 2: [39 44 49], row 3: [69 78 87 0 0 0297 0]. Rows 3's output is adding elements unlike it did with the previous trials of 2x2 and the (2x3 * 3x2).
Here's a snippet of my code where I believe the problem may lie.
int sum;
outputVec.resize(vec1.size());
for(int i = 0; i < vec1.size(); i++) {
for(int k = 0; k < vec2[i].size(); k++) {
sum = 0;
for(int j = 0; j < vec1[i].size(); j++) {
sum += (vec1[i][j] * vec2[j][k]);
}
outputVec[i].push_back(sum);
}
}
for(int i = 0; i < outputVec.size(); i++) {
for(int j = 0; j < outputVec[i].size(); j++) {
printf("%3d",outputVec[i][j]);
}
cout << endl;
}
for(int i = 0; i < vec1.size(); i++)
{
for(int k = 0; k < vec2[i].size(); k++)
...
vec2 doesn't necessarily have as many rows as vec1 (as in the (3x2) * (2x3) case). You're reading past the end of vec2 and getting undefined behavior.