sum of all positive numbers of lower diagonal in 2d array - c++

So there is my code:
#include<iostream>
using namespace std;
int main() {
double M[50][50];
int eilst;
int sum=0;
cout <<"row and colum number:";
cin >> eilst;
cout << "matrix elemkents:";
for (int i=0; i<eilst ; i++)
for (int j=0 ; j<eilst ; j++)
cin>> M[i][j];
cout<<endl;
cout<< "sum: \n";
for (int i =0 ; i<eilst ; i++)
{
for (int j=0 ; j<eilst ; j++)
if ( i > j )
{
if (M[i][j]>0){
sum= sum + M[i][j];
cout<< sum<<"";
}
}
cout<<endl;
}
return 0;
}
Its is printing all my code lower diagnol matrix, but now I added sum method, because I want to add all my positive elements in that array. Maybe you could tell me what I am doing wrong?

Try this:
#include<iostream>
using namespace std;
int main() {
double M[50][50];
int eilst;
int sum=0;
cout <<"row and colum number:";
cin >> eilst;
cout << "matrix elemkents:";
for (int i=0; i<eilst ; i++)
{
for (int j=0 ; j<eilst ; j++)
{
cin>> M[i][j];
cout<<endl;
}
}
cout<< "sum: \n";
for (int i =0 ; i<eilst ; i++)
{
for (int j=0 ; j<eilst ; j++)
{
if ( i > j )
{
if (M[i][j]>0)
sum= sum + M[i][j];
}
}
}
cout<< sum<<"";
cout<<endl;
return 0;
}
I think this will work

Related

C++: 2D Dyanamic Arrays, outputting all values in one line [duplicate]

This question already has answers here:
adding a newline to file in C++
(4 answers)
Most efficient way to output a newline
(7 answers)
Closed 5 months ago.
so I'm starting to write a program that multiplies two square matrices using dynamic 2D arrays. I'm just learning how dynamic arrays work, so I'm testing to make sure everything is storing properly.
When I run my code, it outputs the two matrices on a single line each, rather than like a matrix with rows and columns. How do I fix this?
#include <iomanip>
#include <iostream>
#include <array>
using namespace std;
int main()
{
int **C, n, m; //pointer, rows, columns for matrix 1;
int **D, p, q; //pointer, rows, columns for matrix 2;
cout << "Enter the dimensions of your matrices: ";
cin >> n >> m;
p = n;
q = m;
cout << endl;
C = new int *[n];
D = new int *[p];
for (int x=0 ; x < n; x++)
{
C[x] = new int [m];
}
for (int x=0 ; x < p; x++)
{
D[x] = new int [q];
}
cout << "Enter the values of your first matrix: ";
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cin >> C[I][K];
}
cout << "Enter the values of your second matrix: ";
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cin >> D[L][Z];
}
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cout << setw(4)<< C[I][K];
}
cout << endl;
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cout << setw(4)<< D[L][Z];
}
cout << endl;
}
Just add one more statement
cout << endl;
in your for loops like
for (int I=0 ; I < n; I++ )
{
for (int K=0 ; K < m; K++)
cout << setw(4)<< C[I][K];
cout << endl;
}
cout << endl;
for (int L=0 ; L < p; L++ )
{
for (int Z=0 ; Z < q; Z++)
cout << setw(4)<< D[L][Z];
cout << endl;
}
cout << endl;

I can't properly set one matrix variable to another inside of the for loop

With :
ifstream cin ("cowsignal.in") ;
ofstream cout ("cowsignal.out") ;
int n, m, k ;
cin >> n >> m >> k ;
char paper[n][m] ;
for (int i = 0; i<n; i++) {
for (int j=0; j<m; j++) {
cin >> paper[i][j] ;
}
}
char ans[k*n][k*m] ;
for (int i=0; i<k*n; i++) {
for (int j=0; j<k*m; j++) {
ans[i][j] = paper[j/k][i/k] ;
}
}
for (int i=0; i<k*n; i++) {
for (int j=0; j<k*m; j++) {
cout << ans[i][j] ;
}
cout << endl ;
}
return 0 ;
and an input of :
XXX.
X..X
XXX.
X..X
XXX.
I was expecting the result to be :
XXXXXX..
XXXXXX..
XX....XX
XX....XX
XXXXXX..
XXXXXX..
XX....XX
XX....XX
XXXXXX..
XXXXXX..
I got a different solution. Trying to find that problem, I added :
cout << ans[0][7] << paper[0][7/2] ;
And found that it hasn't been set correctly in the second for loop. I cannot figure out my problem.

Gauss Elimination C++ segmentation fault

I'm really stuck with my code for Gauss Elimination in C++, I need to return upper triangular matrix but still only thing I get is Segmentation fault. I know there must be some sort of going of allocated memory but I can't find where.
Code:
#include <iostream>
using namespace std;
double ** allocateDynamicArray(int order){
double ** dynArray = new double *[order];
int cols = order+1;
double *pool = new double [order * cols];
for(int i = 0;i < order; i++, pool += cols){
dynArray[i] = pool;
}
return dynArray;
}
void deallocateDynamicArray(double **dynArray){
delete [] dynArray[0];
delete [] dynArray;
}
void addAndPrintArray(double **dynArray, int order){
cout << "Zadejte prvky pole radu " << order << endl;
for(int i=0; i< order; i++){
for(int j=0; j< order+1; j++){
cout << "Pole[" << i << "][" << j << "]: ";
cin >> dynArray[i][j];
}
}
for(int i=0; i< order; i++){
for(int j=0; j< order+1; j++){
if(dynArray[i][j] < 10 && dynArray[i][j] >= 0){
cout << " ";
}
cout << dynArray[i][j] << " ";
}
cout << endl;
}
}
double ** gaussElimination(double** dynArray, int order){
for(int j=1; j<=order; j++) /*Horni trojuhelnikova matice*/
{
for(int i=1; i<=order; i++)
{
if(i>j)
{
double c=dynArray[i][j]/dynArray[j][j];
for(int k=1; k<=order+1; k++)
{
dynArray[i][k] = dynArray[i][k] - c * dynArray[j][k];
}
}
}
}
return dynArray;
}
int main()
{
cout << "Zadejte rad matice: ";
int order;
cin >> order;
double **arr = allocateDynamicArray(order);
addAndPrintArray(arr, order);
gaussElimination(arr, order);
deallocateDynamicArray(arr);
return 0;
}
Can anyone tell me what's wrong?
The problem is that in C/C++ the first element of an array should have index 0, so your
for(int i=1; i<=order; i++)
should be
for(int i=0; i<order; i++)
in the gaussElimination function.

Detrminant of a Matrix n x n

Who can tell me my mistake?
The determinant isnĀ“t correct!
I did a test in a piece of paper and the answer is correct!
I believe that maybe my mistake is in the while, in the line "Matriz[i+1][j] = (Matriz[i+1][j] - (Matriz[fila][j]*(Matriz[i+1][0]/Matriz[fila][fila]))); " but my test is correct.
#include <iostream>
#include <stdio.h>
using namespace std;
class Matriznxm{
private:
int n,m;
float **Matriz;
public:
Matriznxm(int f, int c){
n = f;
m = c;
Matriz = new float *[n];
for (int i=0; i<n; i++){
Matriz[i]=new float [m];
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++ ){
Matriz[i][j]=0.0;
}
}
}
void llenarMatriz(){
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cout << "\nMatriz ["<<i+1<<"]["<<j+1<<"]: ";
cin >> Matriz[i][j];
}
}
}
void mostrarMatriz(){
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cout << Matriz[i][j]<< " ";
}
cout << "\n";
}
}
int determinante(){
float det = 1.0;
int fila = 0;
while(fila < n-1){
for(int i=fila; i<n-1; i++){
for(int j=fila; j<m; j++){
Matriz[i+1][j] = (Matriz[i+1][j] - (Matriz[fila][j]*(Matriz[i+1][0]/Matriz[fila][fila])));
}
}
fila++;
}
for (int i=0; i<n; i++){
det= det * Matriz[i][i];
}
return det;
}
};
int main(){
int n,m;
cout<<"\nNumero de Filas y Columnas: ";
cin >> n;
cout << "\n\n\n";
m=n;
Matriznxm m1(n,m);
m1.llenarMatriz();
m1.mostrarMatriz();
cout << "\nEl determinante es: "<< m1.determinante() <<"\n\n";
m1.mostrarMatriz();
return 1;
}
#include <iostream>
using namespace std;
int main()
{
int siz;
cout<< "enter the size of you matrix AxA\nA = ";
cin >>siz;
int mat[siz][siz],rez=0,rezA=1,rezB=1;
for(int i=0;i<siz;i++)
for(int j=0;j<siz;j++)
cin >> mat[i][j];
for(int t=0;t<siz;t++){
for(int i=0;i<siz;i++)
{
rezA = rezA *mat[i][i+t>siz-1?(i+t-siz):i+t];
rezB = rezB *mat[i][siz-t-1-i<0?(2*siz-t-1-i):(siz-t-1-i)];
}
rez = rez + (rezA - rezB);
rezA =rezB = 1;
}
cout <<endl<<"The Determinat is : "<<rez;
return 0;
}

Nested "for" loops c++

On the way to understand working of nested for loop i wrote a program that takes a input and display a pyramid upto that input value like this:
1
22
333
4444
It's displaying just the height of pyramid but its not displaying written part in the second for loop.
Here is the code(after modification but required result not yet)
#include <iostream>
using namespace std;
int main(void)
{
int num;
cout << "Enter the number of pyramid" << endl ;
cin >> num ;
for (int i = 0; i < num ; i++)
{
int max;
for (int j = 0 ; j <= max ; j++)
{
cout << j ;
}
cout << endl ;
max++ ;
}
system("PAUSE");
return 0;
}
#include <iostream>
using namespace std;
int main(void)
{
int num ;
cout << "Enter the number of pyramid" << endl ;
cin >> num ;
for (int i = 0; i < num ; i++)
{
int max = i +1; //change 1
for (int j = 0 ; j < max ; j++)
{
cout << max; //change 2
}
cout << endl ;
//max++ ; //change 3
}
system("PAUSE") ;
return 0;
}
You should initialize max to 0.
int max = 0;
Additionally there are two more bugs.
int max ;
should be declared before the for loop for i. (Otherwise max is defined to be 0 always)
In the inner loop print i, not j.
First of all, please try to have a proper structure in your code:
#include <iostream>
using namespace std;
int main(void)
{
int num;
cout << "Enter the number of pyramid" << endl;
cin >> num;
for(int i = 0; i < num; i++)
{
int max;
for(int j = 0; j <= max; j++)
{
cout << j;
}
cout << endl;
max++;
}
system("PAUSE");
return 0;
}
And your mistake:
Change int max; to int max = 0;
You cannot add 1 to a non existing value.
As has been stated in other answers, your max counter isn't initialized. Additionally, you don't really need it, as you already have i doing the same task:
for (int i = 1; i <= num; i++)
{
for (int j = 0; j < i; j++)
{
cout << i;
}
cout << endl;
}
Unless you actually want to print something like 0 01 012 0123, this is the code you're looking for:
for (int i = 1; i <= num; i++)
{
for (int j = 0; j < i; j++)
cout << i;
cout << endl;
}
max is not set to an initial value.
Its declared insides 1st loop and then used in the 2nd loop.