I have a pascal's triangle with max rows of 5 . Lets suppose I want to find the integration of the fourth row . How do I access the fourth row in the pascal's triangle. More precisely I want to know how to access a row in the pascal's triangle by entering the number n of the row
Code
#include <iostream>
using namespace std;
int main(){
int a1, a2, a3, a4, a5, pascal, columns;
const int rows = 5;
int **array = new int *[rows]; //generating array
for(int i = 0; i <= rows; i++)
array[i] = new int [columns];
for (int i = 0; i <= rows; i++){ //loop for te pascal's triangle
for (int j = 0; j <= i; j++){
if(j == 0 || i == 0){
pascal = 1; //first element of pascal's triangle
}
else{
pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
}
cout << " " << pascal; // printing pascals triangle
}
cout << "\n";
}
cout << "enter which row to integrate: ";
// here I want to directly access a row rather that entering the elements of the row
cin >> a1;
cin >> a2;
cin >> a3;
cin >> a4;
cin >> a5;
}
1
1 1
1 2 1
1 3 3 1 ------> like of n = 4 i want to integrate the elements of this row
1 4 6 4 1
And the answer should be for 1,3,3,1 = 0, 1, 1.5, 1, 0.25
you should first fill the array with the elements then you can access them like so (EDIT: Make sure to initialize the columns variable, I set it to 5)
#include <iostream>
using namespace std;
int main() {
int row_nb, pascal, columns = 5; //Initialized columns with columns = 5
const int rows = 5;
int **array = new int *[rows]; //generating array
for (int i = 0; i <= rows; i++)
array[i] = new int[columns];
for (int i = 0; i <= rows; i++) { //loop for te pascal's triangle
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0) {
pascal = 1; //first element of pascal's triangle
}
else {
pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
}
array[i][j] = pascal; //fill the array
cout << " " << pascal; // printing pascals triangle
}
cout << "\n";
}
cout << "enter which row to intergrate: ";
// here I want to directly access a row rather that entering the elements of the row
cin >> row_nb; //input the row you want to access
for (int i = 0; i <= row_nb; i++) { //access the elements in this row in the array
cout << array[row_nb][i] << " ";
}
return 0; // add the return statement since the return type of the main function is int
}
As I said when you last asked this :
You could simply store each row into a std::vector<int> as you calculate it (in addition to, or instead of, printing it), then keep a list of rows in a std::vector<std::vector<int>>. Then, to access the nth row after calculating the triangle, you just grab the nth element of the second vector.
Related
I have been trying to debug this code for awhile, but I'm not sure how to actually access values in my IDE when they are located in dynamically allocated memory. This program is supposed to initialize two matrices based on the user's input and then multiply them together, if possible.
I have found a few mistakes and corrected them, but I'm not sure what is causing this issue.
#include <iostream>
using namespace std;
typedef int* IntArrayPtr;
int main() {
int r1, c1, r2, c2;
do {
//GET DIMENSIONS OF MATRICIES
cout << "Welcome! This program takes two matricies and multiplies them together.\n"
<< "Enter the number of rows and number of columns for Matrix 1: ";
cin >> r1 >> c1;
cout << "Enter the number of rows and number of columns for Matrix 2: ";
cin >> r2 >> c2;
//DETECT IF MULTIPLICATION CAN HAPPEN
if (r1 != c2) {
cout << "Error: matricies cannot be multiplied. Please enter a new set.\n";
}
} while (r1 != c2); //have the user enter again if the rows and columns don't match
cout << endl;
//INTIALIZE MATRICIES USING DYNAMIC ARRAYS
//intialize MATRIX 1
IntArrayPtr *a = new IntArrayPtr[r1];
cout << "For MATRIX 1: Enter the contained values. Press enter after each entry.\n";
for (int i = 0; i < r1; i++) {
a[i] = new int[c1]; //init columns for each row
cout << "ROW" << i + 1 << "\n";
for (int j = 0; j < c1; j++) {
cin >> a[i][j]; //fill columns of rows
}
cout << endl;
}
//intialize MATRIX 2
IntArrayPtr *b = new IntArrayPtr[r2]; //init rows
cout << "For MATRIX 2: Enter the contained values. Press enter after each entry.\n";
for (int i = 0; i < r2; i++) {
b[i] = new int[c2]; //intialize columns
cout << "ROW" << i + 1 << "\n";
for (int j = 0; j < c2; j++) {
cin >> b[i][j]; //fill columns of rows
}
cout << endl;
}
//INITIALIZE MATRIX TO STORE RESULT IN
//matrix will have the rows of the first and columns of the second, according to matrix multiplication
IntArrayPtr *c = new IntArrayPtr[r1]; //init rows
for (int i = 0; i < r1; i++) {
c[i] = new int[c2]; //init columns
}
//MULTIPLY MATRICIES
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
//PRINT RESULT
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
cout << c[i][j] << " ";
}
cout << endl;
}
delete[] a; delete[] b; delete[] c;
system("pause");
return 0;
}
The matrix should be the result of multiplication, but when I try to execute the program using small matricies (e.g. 3 x 2 times 2 x 3), the output spits out what seems to me to be garbage. I'm sure my mistake is silly, but help would be appreciated.
You didn't initialize the matrix c properly. Try this
//INITIALIZE MATRIX TO STORE RESULT IN
//matrix will have the rows of the first and columns of the second, according to matrix multiplication
IntArrayPtr *c = new IntArrayPtr[r1]; //init rows
for (int i = 0; i < r1; i++) {
c[i] = new int[c2]; //init columns
for (int j = 0; j < c2; j++) {
c[i][j] = 0;
}
}
I'm trying to write a test for a program that adds the product of matrices A and X to the matrix Y.
But I got the error:
"Identifier is required"
I couldn't solve or find a solution to this problem, so I ask for help here.
At first, I thought that the problem is that I compare it with the wrong array. Then I tried to pass other arguments. Dismembered my code into several functions. But still, nothing happened.
#include<iostream>
#include<cassert>
using namespace std;
void axpy(int n, int m, int k, int **A, int **X, int **Y)
{
int i, j, q;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
for (q = 0; q < k; q++)
{
Y[i][j] += A[i][q] * X[q][j];
}
}
}
cout << "Product of matrices\n";
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
cout << Y[i][j] << " ";
cout << "\n";
}
}
void TestAxpy()
{
int P[2][2] = { {13,11},{27,21} };
assert(axpy(2,2,2,[1,2][3,4],[4,3][2,1],[5,6][7,8]) == P);
}
int main()
{
int n, m, k, i, j, q;
cout << "Enter number of rows of matrix X and columns of matrix A: ";
cin >> k;
cout << "Enter number of rows of matrix A and Y: ";
cin >> n;
cout << "Enter number of columns of matrix X and Y: ";
cin >> m;
int **A = new int *[k];
for (i = 0; i < k; i++)
A[i] = new int[n];
int **X = new int *[m];
for (i = 0; i < m; i++)
X[i] = new int[k];
int **Y = new int *[m];
for (i = 0; i < m; i++)
Y[i] = new int[n];
cout << "Enter elements of matrix A: ";
for (i = 0; i < n; i++)
for (j = 0; j < k; j++)
cin >> A[i][j];
cout << "Enter elements of matrix X: ";
for (i = 0; i < k; i++)
for (j = 0; j < m; j++)
cin >> X[i][j];
cout << "Enter elements of matrix Y: ";
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
cin >> Y[i][j];
axpy(n, m, k, A, X, Y);
TestAxpy();
system("pause");
return 0;
}
I wanted to get a 2x2 matrix with the results of [13, 11] [27 21]. The input I used such as:
Enter number of rows of matrix X and columns of matrix A: 2
Enter number of rows of matrix A and Y: 2
Enter number of columns of matrix X and Y: 2
Enter elements of matrix A: 1 2 3 4
Enter elements of matrix X: 4 3 2 1
Enter elements of matrix Y: 5 6 7 8
This seems like a mix of C and C++. In C++ it is rare you need to use raw "C" arrays, almost always std::vector<> or std::array<> will be a better choice. There is also matrix in the boost library which will store exactly what you need.
In terms of your specific code there are two issues:
Pointers to pointers (**) are no the same thing as two dimension arrays. They are a two-layer indirection. The first are pointers to locations in memory that store the second layer in memory. See below for how it would need to work to be able to call axpy. Again would strongly recommend looking at std::vector or boost libraries.
The "==" operator won't work that way for C arrays. You need to specify how you want the comparison to happen. As written it will at best just compare memory address, but more likely will produce an error.
void TestAxpy()
{
int P[2][2] = { {13,11},{27,21} };
int A1[2] = {1,2};
int A2[2] = {3,4};
int* A[2] = { A1, A2 };
int X1[2] = {4,3};
int X2[2] = {2,1};
int *X[2] = { X1, X2 };
int Y1[2];
int Y2[2];
int *Y[2] = {Y1, Y2 };
axpy(2,2,2,A,X,Y);
//assert(Y == P); //C++ doesn't know how to do this.
}
I am trying to create a program that prints out an array based on user input. The array needs to start from 0 and scale to the number enter by user. So if user inputs 5 the the array values will be [0][1][2][3][4][5]. For some reason my code just prints out 0.
#include <iostream>
using namespace std;
int main() {
cout << "Enter the value of n: ";
int n;
cin >> n;
int *arr1 = new int[n];
for(int i = 0; i < n; i ++){
arr1[i] = 0;
}
cout << *arr1 << endl;
delete [] arr1;
return 0;
}
There are few bugs in your code.
You expect the output to be [0][1][2][3][4][5] when the n = 5. Therefore your output has (n + 1) elements. So your array should also have (n + 1) elements.
int *arr1 = new int[n + 1];
In your code you assign 0 to each element in your array. But you expect the array to contain 0, 1, 2, .., n
for(int i = 0; i < n + 1; i++){
arr1[i] = i;
}
In your code, you only print the first element. *arr1 is same as arr1[0]. So another for loop is required to print the each element in your array.
for(int i = 0; i < n + 1; i++){
cout << "[" << arr1[i] << "]" << endl;
}
Then you will get the output [0][1][2][3][4][5] when the n = 5
I am currently working on a task which says the following:
Input a two-dimensional array A (m,n) [m < 10, n < 20]. In the n + 1 column calculate the sum of the rows, and in the m + 1 row the product of the columns. Print out the resulting matrix.
According to my understanding of this task, at the end of each column must be the sum of according rows (so on the right hand side), and the product of the column (at the end/bottom?).
This task is so confusing I do not know where to start. I found some code that covers the idea but does not include the product and it does not display these values as the task asks me to:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3];
int i, j, s = 0, sum = 0;
cout << "Enter 9 elements of 3*3 Matrix \n";
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
cin >> a[i][j];
cout << "Matrix Entered By you is \n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
s = s + a[i][j];
cout << "sum of" << i + 1 << " Row is" << s;
s = 0;
cout << endl;
}
cout << endl;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
s = s + a[j][i];
cout << "sum of" << i + 1 << " Column is" << s;
s = 0;
cout << endl;
}
cout << endl;
for (i = 0; i < 3; i++)
sum = sum + a[i][i];
cout << "Sum of Diagnols Elements is \n" << sum;
getch();
}
We beginners should help each other.
Here you are
#include <iostream>
#include <iomanip>
int main()
{
const size_t M = 10;
const size_t N = 20;
int a[M][N] = {};
std::cout << "Enter number of rows: (less than " << M << "): ";
size_t m;
std::cin >> m;
if (!(m < M) || (m == 0)) m = M - 1;
std::cout << "Enter number of columns: (less than " << N << "): ";
size_t n;
std::cin >> n;
if (!(n < N) || (n == 0)) n = N - 1;
std::cout << std::endl;
for (size_t i = 0; i < m; i++)
{
std::cout << "Enter " << n
<< " numbers for the row " << i << ": ";
for (size_t j = 0; j < n; j++) std::cin >> a[i][j];
}
for (size_t j = 0; j < n; j++) a[m][j] = 1;
for (size_t i = 0; i < m; i++)
{
for (size_t j = 0; j < n; j++)
{
a[i][n] += a[i][j];
a[m][j] *= a[i][j];
}
}
std::cout << std::endl;
for (size_t i = 0; i < m + 1; i++)
{
for (size_t j = 0; j < n + 1; j++)
{
std::cout << std::setw(3) << a[i][j] << ' ';
}
std::cout << '\n';
}
std::cout << std::endl;
}
The program output might look like
Enter number of rows: (less than 10): 3
Enter number of columns: (less than 20): 3
Enter 3 numbers for the row 0: 1 2 3
Enter 3 numbers for the row 1: 4 5 6
Enter 3 numbers for the row 2: 7 8 9
1 2 3 6
4 5 6 15
7 8 9 24
28 80 162 0
So you have to declare an array with 10 rows and 20 columns. The user should enter the dimensions of the array that are correspondingly less than 10 and 20. One row and one column are reserved for sums and products.
It is desirable that the array would be initially initialized by zeroes.
int a[M][N] = {};
In this case you need not to set the last column with zeroes as you have to do with last row initializing it with ones.
That is all.:)
Start with the declaration: make sure that your program works with m×n matrix, not simply a 3×3 matrix. Since m and n have limits of 10 and 20, and because you must add an extra row and a column to the result, the declaration should be
int a[11][21];
You also need to declare m and n, have end-user enter them, and validate them to be within their acceptable ranges:
int m, n;
cin >> m >> n;
... // Do the validation
Now you can rewrite your loops in terms of m and n, rather than using 3 throughout your code.
With these declarations in place, you can total the numbers in place, i.e. for each row r you would write
for (int i = 0 ; i != n ; i++) {
a[r][n+1] += a[r][i];
}
Similarly, you would compute the product (don't forget to start it with the initial value of 1, not 0).
At the end you would print an (m+1)×(n+1) matrix to complete the task.
Solution : use this kind of functions for your array after making it global.
void Adder(int row, int colum)
{
for (int i = 0; i < row - 1; i++)
{
int temp = 0;
for (int j = 0; j < colum - 1; j++)
{
temp += a[i][j]; // added all other than last one
}
a[i][j] = temp; // assigned to last one in row
}
}
void Mul(int row, int colum)
{
for (int i = 0; i < colum- 1; i++)
{
int temp = 1;
for (int j = 0; j < row - 1; j++)
{
temp *= a[j][i]; // multiplied all element other than last one
}
a[j][i] = temp; // assigned to last one in column
}
}
so I've got integers m and n in my program, once you input the values it should create an array with values from m to n (for example m = 1 and n = 10, it creates array q with values from 1 to 10). Then it looks in the array if there are any numbers that are equal to any two number summ that are squared (for example, in the array, number 5 is equal to 1 squared + 2 squared). The problem is when I try to input the first value it crashes, pretty sure the problem is in the function but can't seem to figure it out. Thanks
#include <iostream>
using namespace std;
int squared (int a, int b, int q[]){
while (a<=0 || b<=0){
cout <<"You can't input an integer that is 0 or below: ";
cin >>a;
cin >>b;
if (a>0 || b>0) break;
}
for (int p=0; p<b; p++){
for (int i=a ; i<b; i++){
q[p] = a;
}
}
for (int z=0; z<b; z++){
for (int x=0; x<b; x++){
for (int c=0; c<b; c++){
if (q[z] == (q[x] * q[x]) + (q[c] * q[c])){
int result= (q[x] * q[x]) + (q[c] * q[c]);
return result;
}
}
}
}
}
int main () {
int m,n;
int M[100];
cout <<"Input integers m un n: ";
cin >>m,n;
cout <<squared(m,n,M);
return 0;
}
Most likely it crashes because of this: cin >>m,n;, it should be cin >> m >> n;, else you use n uninitialized on the next line, thus getting undefined behaviour, e.g. crash.
What compiler are you using and with what flags, since this would trigger some warnings/errors at compile normally.
cin >> m, n; is incorrect which inputs only m which can be interpreted as:
(cin >> m), n; which means: cin, n; to correct it:
cin >> m >> n;
if(a > 0 || b > 0) break; is redundant because you check for this condition twice: once in while condition second inside while loop thus checking for the same condition is redundant because while breaks automatically if the condition succeeds (a or b is equal or smaller than 0).
you passed an array without passing its size, you are lucky if you set the first element 1 the any second value is equal to the size of array eg:
m = 1; n = 10; then the size is ten which is correct.
what about:
m = 7; n = 10; // now is size 10? it's only 3
to correct it pass the size eg:
m = 4; n = 8;
int size = 8 - 4;
cout << Squared(m, n, M, size);
also:
for (int p = 0; p < b; p++)
{
for (int i = a ; i < b; i++)
{
q[p] = a;
}
}
you are assigning the same value a to all elements of array and iterating doing the same thing in the nested-loop!!! it's likely to write:
int x = 0; x = 0;
so the condition of result inside squared will never succeed because the same value is never equal to its square. 4 = 4 * 4 is never reached
here is what you search for:
#include <iostream>
using namespace std;
// I only make squared search for the result not inputing m and n lik e in yours
int squared (int m, int n, int* M)
{
int result;
for(int i(0); i < n; i++)
for(int j(0); j < n; j++)
for(int k(0); k < n; k++)
if( (M[i] == ( (M[j] * M[j]) + (M[k] * M[k]) )) && j != k) // here to avoid comparing with the same index
{
cout << M[i] << " = (" << M[j] << "*" << M[j] << ") + (" << M[k] << "*" << M[k] << ")" << endl;
result = ( (M[j] * M[j]) + (M[k] * M[k]) );
cout << result << endl;
return result; // if success we return result
}
return -1; // otherwise we return -1 as a sign of error (no square yields in negative value)
}
int main ()
{
int n, m, size;
do
{
cout <<"m: ";
cin >> m;
cout << "n: ";
cin >> n;
if(n <= 0 || m <= 0)
cout <<"You can't input an integer that is 0 or below: ";
// also it's very important to check whether n is greater than m or not because if m == n or m > n then size will be negative and as you know no array has a negative nor 0 size
if(m >= n)
cout << "n must be greater than m!" << endl;
}while (m <= 0 || n <= 0 || m >= n);
size = n - m; // getting size of array assuming m can be any value
int* M = new int[n - m]; // allocating dynamic array
// inputting array as you asked
for(int i(0), j = m; i < size; i++, j++)
M[i] = j;
// checking the values of array elements
for(int i = 0; i < size; i++)
cout << M[i] << ", " ;
cout << endl;
// getting the result
cout << squared(m, n, M) << endl;
// cleaning
delete[] M;
return 0;
}