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;
}
Related
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
I wanted to do a coding where it reads a 4x4 matrices and sum them up. I dont know where I did wrong. My result is it keeps on asking to enter the elements. I just wanted a 4x4. Can anyone help me?
#include <iostream>
using namespace std;
const int SIZE = 4;
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex)
{
int sum = 0;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
sum = sum + m[i][j];
}
}
return sum;
}
int main()
{
double m[SIZE][SIZE], sum = 0;
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i<SIZE; i++)
for (int j = 0; j<SIZE; j++)
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
cout << sum << endl;
return 0;
}
A good practice is using curly braces even if the content of the "for" has one line, but in your case must be in the next way
for (int i = 0; i<SIZE; i++) {
for (int j = 0; j<SIZE; j++) {
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
}
}
Greetings
The complete code is:
#include <iostream>
using namespace std;
const int SIZE = 4;
double sumColumn(const double m[][SIZE], int rowSize, int columnIndex)
{
int sum = 0;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
sum = sum + m[i][j];
}
}
return sum;
}
int main()
{
double m[SIZE][SIZE], sum = 0;
cout << "Enter the elements of the matrix" << endl;
for (int i = 0; i<SIZE; i++)
{
for (int j = 0; j<SIZE; j++)
{
cin >> m[i][j];
sum = sumColumn(m, SIZE, SIZE);
}
}
cout << sum << endl;
return 0;
}
There is something wrong in the if condition but it's the 7th day I have no clue. It prints the taken integer like this
mike .. mike1 .. mike12 .. mike123
But actually I need it to be like this
mike .. mike1 .. mike2 .. mike3
Can anyone help? this is my code :
#include <bits/stdc++.h>
using namespace std;
string str(int o){
stringstream ss;
ss<<o;
return ss.str();}
int main(){
int n;
cin>>n;
int z[n];
for(int p=0;p<n;p++)
{
z[p]=-50;
}
string x[n];
int k;
for(int i=0;i<n;i++){
k=1;
cin>>x[i];
for(int j=0;j<i;j++){
if(x[j]== x[i]){
x[i] = x[j] + str(k) ;
k++;
z[i]=0;
}
}
}
for(int q=0;q<n;q++){
if(z[q]==0)
cout<<x[q]<<endl;
else
cout<<"OK"<<endl;
}
return 0;
}
Remove the second for loop and get k outside the loop like this:
k=1;
for(int i=0;i<n;i++){
cin>>x[i];
x[i] = x[i] + str(k) ;
k++;
z[i]=0;
}
You can simplify all your code with C++11
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k = 1;
cin >> n;
vector<bool> z(n, false);
vector<string> x(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
x[i] += to_string(k);
++k;
z[i] = true;
}
for (int i = 0; i < n; ++i)
cout << (z[i] ? x[i] : "OK") << endl;
return 0;
}
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.
I have this code. I am trying to pass a character array to a function and I get an error saying:
"Expected primary expression before the ']' token " at line 21
on which I call the function:
#include <iostream>
#include <cstring>
using namespace std;
char* mostFrequentWord(int, char [][10]);
int main()
{
int br = 0, n, br1 = 0;
char str[100][10];
cin >> n;
for(int i=0; i<n; ++i)
{
cout << " cin >> str"<< i << "= ";
cin>> str[i];
}
cout << mostFrequentWord(n, str[][10]) <<endl;
int m;
for(int i=0; i<n-1; ++i)
{
for(int j=i+1; j<n; ++j)
{
if(!strcmp(str[i],str[j]))
{
++br;
}
}
if(br>br1)
{
br1 = br;
m = i;
}
}
cout << str[m] <<endl;
return 0;
}
char* mostFrequentWord(int n, char str[][10])
{
int m, br = 0, br1 = 0;
for(int i=0; i<n-1; ++i)
{
for(int j=i+1; j<n; ++j)
{
if(!strcmp(str[i],str[j]))
{
++br;
}
}
if(br>br1)
{
br1 = br;
m = i;
}
}
return str[m];
}
This line:
cout << mostFrequentWord(n, str[][10]) <<endl;
needs to read
cout << mostFrequentWord(n, str) <<endl;
You answer is in your question:. Just pass the str.
cout << mostFrequentWord(n, str) <<endl;
Try calling the function this way
mostFrequentWord(n,str)
This should work. Rest of the code is fine.