I am working with makefiles, when compiling the following code the random option works perfectly, but in the option to enter the matrix using the keyboard this Segmentation fault (core dumped)
is displayed, could you tell me why this is displayed or what is wrong with the code, please.
The error originates after entering the array ( user(b, N, P);
). I think the error is in the function printm(b, N, P);
.
function.h
#ifndef function_H
#define function_H
int user(int **m, int N, int P);
void printm(int **m, int N, int P);
int generator(int **m, int N, int P);
void lmax(int **m, int N, int P);
#endif
function.cc
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "function.h"
using namespace std;
int user(int **m, int N, int P)
{
for (int i=0; i<N; i++)
{
for (int j=0; j<P; j++)
{
cin >> m[i][j];
}
}
return 0;
}
void printm(int **m, int N, int P)
{
int i;
for (i=0; i<N; i++);
{
for (int j=0; j<P; j++)
{
cout << " " << m[i][j] << " ";
}
cout << endl;
}
}
int generator(int **m, int N, int P)
{
srand(time(NULL));
for (int i=0; i<N; i++)
{
for (int j=0; j<P; j++)
{
m[i][j] = rand()%50-0;
cout << " " << m[i][j] << "\t";
}
cout << endl;
}
cout << endl;
return 0;
}
void lmax(int **m, int N, int P)
{
int j_max;
for (int i=0; i<N; i++)
{
j_max = 0;
for (int j=1; j<P; j++)
if (m[i][j_max] < m[i][j])
{
j_max = j;
}
m[i][j_max] = 0;
}
for (int i=0; i<N; i++) {
for (int j=0; j<P; j++) {
cout << " " << m[i][j] << "\t"; }
cout << endl; }
cout << endl;
bool kl = true;
for (int i=1; i<N; i++)
{
for (int j=0; j<i; j++)
{
if (m[i][j] != m[j][i])
kl = false;
}
}
if (kl)
{
cout << " Symetric matrix." << endl;
}
else
{
cout << " Not symetric matrix." << endl;
}
cout << endl;
}
test.cc
#include <stdio.h>
#include <iostream>
#include "function.h"
using namespace std;
int main()
{
int N, P, **a, **b;
char ch;
cout << "\n 1 - Initialize matrix by using a random number generator." << endl;
cout << " 2 - Initialize matrix via user input." << endl;
cout << " 0 - Exit." << endl;
cout << "\n Your choice: ";
do {
ch = getchar();
switch(ch)
{
case '1':
cout << "\n Rows: ";
cin >> N;
cout << " Columns: ";
cin >> P; cout << endl;
a = new int *[N];
for (int i=0; i<N; i++)
{
a[i] = new int [P];
}
generator(a, N, P);
lmax(a, N, P);
return 0;
case '2':
cout << "\n Rows: ";
cin >> N;
cout << " Columns: ";
cin >> P; cout << endl;
b = new int *[N];
for (int i=0; i<N; i++)
{
b[i] = new int [P];
}
user(b, N, P);
printm(b, N, P);
lmax(b, N, P);
return 0;
}
}
while(ch != '0');
return 0;
}
In this answer I am going to assume that a practical requirement is to use dynamic memory. If not, you should consider rewriting the program using arrays or encapsulating dynamic memory usage in a class (if you don't break any rules of thumb).
Duplicate code is usually a good friend of errors because when you need to make a change, either make exactly the same change in all the places where the duplicates are or the program will start to cause problems.
Let's do some refactoring to see if we get more readable code.
Your program handles arrays. Unless it is an explicit requirement of practice, the ideal would be for all the information concerning the matrix to always travel together:
For now we are not going to complicate the example with the use of constructors.
The thing is that in your exercise you are creating arrays in two different places. The idea of having a mechanism that allows creating a matrix is therefore interesting. A function perfectly suits our needs
Of course we cannot consider making memory reservations in a program without cleaning it later... we have to create another function to free the reserved memory:
Another element that you have duplicated is the function to fill the array. In your specific case you could do this with a single function, since the only difference lies in the data source (standard input or random generator).
It is in this part of your code where your error lies. Compare the two functions carefully:
Despite doing exactly the same thing, they look quite different... above all, it is striking to find a
return
in the functionuser
inside the first loop (your mistake).How can this be simplified? There is a whole range of options. Here are some ideas:
One option, since looping through the array is often a recurring thing in your exercise, you can encapsulate that loop in a function and execute "something" on each iteration:
They can also be used
lambdas
to make code more readable:And well, now it would only be necessary to print the matrix:
Another option, with simpler and more modest but also more rigid code, could be to have a single function
RellenarMatriz
that receives abool
(or even aenum
if you want to be more flexible). Depending on the value of that variable, it will take one data input or another:In a similar way, you could act before the entry of the values
fila
andcolumna
, I leave that to you to practice if you see yourself with energy.Del segmentation fault que mencionas no veo ni rastro, pero también es cierto que falta la implementación de
lmax
...Un saludo.
Estas son las funciones que tenía error. Ya están corregidas.
Tenías un mal return en la función
user
y un;
en el primer ciclo de la funciónprintm