Getting an error using a second BCast with MPI in C - c

Im having troubles with the following code
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
//1 mostrar lista
//2 borrar elementos
//3 ordenar lista
//4 añadir elemento
int main(int argc,char**argv){
int rank;
int *list; //Lista
int *liste; //Lista encargados
int *perm; //Permutaciones
int *capa; //Capacidad
int size;
int i;
int aux;
const int root = 0;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank==root){
perm = (int*)malloc(size*sizeof(int));
capa = (int*)malloc(size*sizeof(int));
for(i=0;i<size;i++){
scanf("%d",&aux);
capa[i]=aux;
}
for(i=0;i<size;i++){
scanf("%d",&aux);
perm[i]=aux;
}
}
//printf("\n[%d]: Antes de Bcast: %d %d %d %d\n",rank,capa[0],capa[1],capa[2],capa[4]);
MPI_Bcast(&capa[0],size,MPI_INT,root,MPI_COMM_WORLD);
MPI_Bcast(&perm[0],size,MPI_INT,root,MPI_COMM_WORLD);
//MPI_Bcast(&aux,1,MPI_INT,root,MPI_COMM_WORLD);
printf("\n[%d]: Despues de Bcast capa: %d %d %d %d\n",rank,capa[0],capa[1],capa[2],capa[3]);
printf("\n[%d]: Despues de Bcast perm: %d %d %d %d\n",rank,perm[0],perm[1],perm[2],perm[3]);
MPI_Finalize();
return 0;
}
Well there is nothing special, Im just trying to Bcast two arrays of ints (for now Im trying with 4-lenght strings of ints, that why the last printfs are statics).
Anyway, I tried first just with the "capa" string and it worked, but when I tried it with both strings, Im getting a segmentation fault error but I dont know why, both string are the same size, the only differences are their values.
EDIT:
Just changed the code to this and worked:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
//1 mostrar lista
//2 borrar elementos
//3 ordenar lista
//4 añadir elemento
int main(int argc,char**argv){
int rank;
int *list; //Lista
int *liste; //Lista encargados
int *perm; //Permutaciones
int *capa; //Capacidad
int size;
int i;
int aux;
const int root = 0;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
perm = (int*)malloc(size*sizeof(int));
capa = (int*)malloc(size*sizeof(int));
if(rank==root){
for(i=0;i<size;i++){
scanf("%d",&aux);
capa[i]=aux;
}
for(i=0;i<size;i++){
scanf("%d",&aux);
perm[i]=aux;
}
}
//printf("\n[%d]: Antes de Bcast: %d %d %d %d\n",rank,capa[0],capa[1],capa[2],capa[4]);
MPI_Bcast(&capa[0],size,MPI_INT,root,MPI_COMM_WORLD);
MPI_Bcast(&perm[0],size,MPI_INT,root,MPI_COMM_WORLD);
//MPI_Bcast(&aux,1,MPI_INT,root,MPI_COMM_WORLD);
printf("\n[%d]: Despues de Bcast capa: %d %d %d %d\n",rank,capa[0],capa[1],capa[2],capa[3]);
printf("\n[%d]: Despues de Bcast perm: %d %d %d %d\n",rank,perm[0],perm[1],perm[2],perm[3]);
MPI_Finalize();
return 0;
}

Related

Linker command failed _main not found

I'm a college student beginner in C language. I was trying to run a program in which a matrix initializes with all values = 0, then the user enters values and finally the program searches a user input number inside a matrix, telling the row and column of said value.
But this error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
won't stop coming up (I use visual studio code for mac) and I cannot get rid of it.
I've been searching all over internet but I can't find a solution and my teacher said "never heard of this error." I would appreciate your help :)
The function in which I need help is CercadorNumero.
I have two libraries, main library and functions library.
main:
#include <stdio.h>
#include <stdlib.h>
#define FILA 3
#define COLUMNA 3
void printMatriu (int entraMatriu[FILA][COLUMNA]);
int sumaFila (int matriu[][COLUMNA], int numFila);
int CercadorNumero (int buscaMatriu[FILA][COLUMNA], int valor, int *p, int *f); //p i f indicaran les cordenades
int main(int argc, char *argv[])
{
int matriu [FILA][COLUMNA];
for (int i=0; i<FILA; i++){
for (int j=0; j<COLUMNA; j++)
{
matriu[i][j]=0;
}
}
for (int i=0; i<FILA; i++)
{
for (int j=0; j<COLUMNA; j++)
{
do{
printf("Entrant el valor de la columna %d, fila %d: ", i+1, j+1);
scanf("%2d", &matriu[i][j]);
}while(matriu[i][j]<0 || matriu[i][j]>15);
}
printf("\n");
}
printMatriu(matriu);
int suma, numFila;
for(int i = 0; i < FILA; i++)
{
suma = sumaFila(matriu, i);
printf("La suma de la fila %d es: %d\n", i+1, suma);
}
int resultat, numCercar, columnaTrobada, filaTrobada;
do{
printf("Diga'm el numero que vols buscar: \n");
scanf("%d", &numCercar);
resultat = CercadorNumero (matriu, numCercar, &columnaTrobada, &filaTrobada);
if (resultat == 1)
{
printf("%d Trobat a les coordenades: %d %d", numCercar, columnaTrobada, filaTrobada);
} else
{
printf("valor no trobat");
}
} while(numCercar<0 || numCercar>100);
return 0;
}
functions:
#include <stdio.h>
#define FILA 3
#define COLUMNA 3
void printMatriu (int entraMatriu[FILA][COLUMNA]){
for (int i=0; i<FILA; i++){
for (int j=0; j<COLUMNA; j++){
printf("%2d ", entraMatriu[i][j]);
}
printf("\n");
}
}
int sumaFila (int matriu[][COLUMNA], int numFila)
{
int suma=0;
for (int j = 0; j < COLUMNA; j++)
{
suma = suma + matriu[numFila][j];
}
return suma;
}
int CercadorNumero (int buscaMatriu[FILA][COLUMNA], int valor, int *p, int *f)
{
int valorResultat = 0;
*p = 0;
*f = 0;
for (int i = 0; i < FILA; i++)
{
for (int j = 0; j < COLUMNA;)
{
if (buscaMatriu[i][j] == valor)
{
valorResultat = 1;
*p = i; //posició de memoria p guarda el valor de i en el moment que es igual al valor (fila)
*f = j; //posició de memoria f guarda el valor de j en el moment que es igual al valor (columna)
return valorResultat;
} else j++;
}
}
return valorResultat;
}

zsh: segmentation fault in C after compile

I have an program in C for showing appropiate number prime (max or min). After input a value, i have error "zsh:segmentation fault". Why?
Thanks.
#include <stdio.h>
#include <stdlib.h>
int max(int n){
for (int i=2;i<=n;i++)
if(n%i == 0)
return max(n+1);
return n;
}
int min(int n){
for(int i=2;i<=n;i++)
if(n%i==0)
return min(n-1);
return n;
}
int main(){
int n;
printf("Numarul =");scanf("%d",&n);
printf("Numarul anterior prim numarului dat este %d",min(n));
printf("Numarul urmator prim numarului dat este %d",max(n));
}

Find saddle point - C

I need to find the saddle point of a matrix loading a file with a sequence of numbers. In this case my matrix is 4x3. The sequence of numbers (content of the file) is: 3 -4 8 5 2 7 12 7 9 0 1 2. The functions and some variables are in spanish, I will try to comment everything so you guys can understand it. The thing is that my output is always the same: "the saddle point is in row 0 and column 2" and that´s not true. I'm doing something wrong and I can't spot the problem. Thanks guys!
#include <stdio.h>
#include <stdlib.h>
#define M 4
#define N 3
int escribeMat(int matriz[M][N]);
int Punto_de_silla_Mat(int matriz[M][N]);
int main()
{
int numeros, i, j;
int matriz[M][N];
FILE *fichero;
fichero = fopen("C:\\Users\\diego\\Documents\\Practicas_TP\\Ficheros\\numeros.txt", "r");
if (fichero == NULL){
printf("Error, el archivo no se ha abierto correctamente");
return 0;
}
else{
for(i=0;i<M;i++){
for(j=0;j<N;j++){
fscanf(fichero, "%d", &matriz[i][j]);
}
}
escribeMat(matriz);
Punto_de_silla_Mat(matriz);
return 0;
}
}
int escribeMat(int matriz[M][N]){ //function that prints the matrix
int i, j;
for(i=0;i<M;i++){
for(j=0;j<N;j++){
printf("%3d", matriz[i][j]);
}
printf("\n");
}
}
int Punto_de_silla_Mat(int matriz[M][N]){
int i=0,j=0,k=0;
int P_fila=0,P_columna=0, encontrado=0;
for(i=0;i<N;i++){
P_fila=i; //row
P_columna=j; //column
encontrado=0; //flag
for(j=0;j<M;j++){
if(matriz[i][j] < matriz[P_fila][P_columna]){
P_fila = i;
P_columna = j;}
for(k=0;k<N;k++){
if(matriz[k][P_columna] > matriz[P_fila][P_columna]){
encontrado = 1;
break;
}
}
}
}
if(encontrado == 1){
printf("El punto de silla la fila %d y en la columna %d", matriz[P_fila][P_columna], P_fila, P_columna); //found it + position
}else
printf("No hay punto de silla"); //there is no saddle point.
}
Edit 1: Added the missing argument in printf. But still the output should be 7, row 2 and column 1 and I'm getting number 0, row 2 and column 3.
printf("El punto de silla es %d y está en la fila %d y en la columna %d", matriz[P_fila][P_columna], P_fila, P_columna); //saddle point number + position
Edit 2: Removed int numeros. Changed the funtion escribeMat from int to void. Added return to Punto_De_silla function.
#include <stdio.h>
#include <stdlib.h>
#define M 4
#define N 3
void escribeMat(int matriz[M][N]);
int Punto_de_silla_Mat(int matriz[M][N]);
int main()
{
int i, j;
int matriz[M][N];
FILE *fichero;
fichero = fopen("C:\\Users\\diego\\Documents\\Practicas_TP\\Ficheros\\numeros.txt", "r");
if (fichero == NULL){
printf("Error, el archivo no se ha abierto correctamente");
return 0;
}
else{
for(i=0;i<M;i++){
for(j=0;j<N;j++){
fscanf(fichero, "%d", &matriz[i][j]);
}
}
escribeMat(matriz);
Punto_de_silla_Mat(matriz);
/* if(Punto_de_silla_Mat(matriz) == 1){
printf("El punto de silla es %d y esta en la fila %d y en la columna %d", matriz[P_fila][P_columna], P_fila, P_columna);
}
else
printf("No hay punto de silla"); */
return 0;
}
}
void escribeMat(int matriz[M][N]){ //function that prints the matrix on screen
int i, j;
for(i=0;i<M;i++){
for(j=0;j<N;j++){
printf("%3d", matriz[i][j]);
}
printf("\n");
}
}
int Punto_de_silla_Mat(int matriz[M][N]){
int i=0,j=0,k=0;
int P_fila=0,P_columna=0, encontrado=0;
for(i=0;i<N;i++){
P_fila=i; //row
P_columna=j; //column
encontrado=0; //flag
for(j=0;j<M;j++){
if(matriz[i][j] < matriz[P_fila][P_columna]){
P_fila = i;
P_columna = j;}
for(k=0;k<N;k++){
if(matriz[k][P_columna] > matriz[P_fila][P_columna]){
encontrado = 1;
break;
}
}
}
}
if(encontrado == 1){
printf("El punto de silla es %d y esta en la fila %d y en la columna %d", matriz[P_fila][P_columna], P_fila, P_columna); //saddle point number + position
}else
printf("No hay punto de silla"); //there is no saddle point.
return 0;
}
Still the program is not finding the saddle point.
Edit 3: Swapped M and N at the for loops. Still not getting expected results.
int Punto_de_silla_Mat(int matriz[M][N]){
int i=0,j=0,k=0;
int P_fila=0,P_columna=0, encontrado=0;
for(i=0;i<M;i++){
P_fila=i; //row
P_columna=j; //column
encontrado=0; //flag
for(j=0;j<N;j++){
if(matriz[i][j] < matriz[P_fila][P_columna]){
P_fila = i;
P_columna = j;}
for(k=0;k<M;k++){
if(matriz[k][P_columna] > matriz[P_fila][P_columna]){
encontrado = 1;
break;
}
}
}
}
if(encontrado == 1){
printf("El punto de silla es %d y esta en la fila %d y en la columna %d", matriz[P_fila][P_columna], P_fila, P_columna); //saddle point number + position
}else
printf("No hay punto de silla"); //there is no saddle point.
return 0;
Edit 4:
Changed the position of a } in the second for loop. The where two for loops executing at the same time. Nevertheless I'm getting the same wrong result.

Why only codeblocks give me return error(0xc00000FD)?

When I compile the program with Codeblocks (GNU gcc compiler) it gives me this error: Process returned -1073741571 (0xC00000FD)
I tried it without the binary search and it works, but I can't understand why when I insert the binary search it gives me this error.
But if I compile it with another compiler (I tried on mine university online compiler) it works very well.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
int v[n];
int i;
int aux;
int j;
printf("Quanti numeri vuoi inserire? \n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("%d: ",i);
scanf("%d",&v[i]);
}
for(i=0;i<n;i++) {
printf("%d,",v[i]);
}
printf("\n");
for(j=0;j<n-1;j++) {
for(i=0;i<n-1;i++) {
if(v[i]>v[i+1]) {
aux = v[i];
v[i] = v[i+1];
v[i+1] = aux;
}
}
}
for(i=0;i<n;i++)
printf("%d ", v[i]);
//Binary search
int alto, basso, pos, ele, k;
alto = 0;
basso = n-1;
pos = -1;
printf("Mi dia un elemento da cercare:\n");
scanf("%d",&ele);
while(alto<=basso && pos==-1){
k=(alto+basso)/2;
if(v[k]==ele)
pos=k;
else
if(v[k]<ele)
alto=k+1;
else
basso=k-1;
}
if(pos==-1)
printf("Il numero inserito non e' presente");
else
printf("L'elemento e' presente in posizione %d del vettore", pos);
return 0;
}
int n;
int v[n];
you have undefined behaviour(UB) here.
you declare a VLA with the size set by not initialized variable n.
So answering the question
Why only codeblocks give me return error(0xc00000FD)
the answer is : because it does. It is not predictable.

I can´t get out my array from a function to the main in c

In escribirVect(suma[MAX]), the compiler tells me that suma is undeclared but I declared it in my function sumarV, how can I use my variable 'suma' in main?
#include <stdio.h>
#define MAX 10
void leerVect(int vect[MAX]);
void escribirVect (int v[MAX]);
void sumarV (int vector1[MAX], int vector2[MAX]);
int main ()
{
int vector1[MAX], vector2[MAX];
printf("Introduzca los valores del primer vector: \n");
leerVect(vector1);
printf("Introduzca los valores del segundo vector: \n");
leerVect(vector2);
sumarV(vector1, vector2);
escribirVect(suma[MAX]); // here is the problem
return 0;
}
void leerVect(int v[MAX])
{
int i;
for (i=0; i<MAX; i++)
{
printf("Introduzca el valor de v[%d]: ", i);
scanf("%d", &v[i]);
}
}
void escribirVect (int v[MAX])
{
int i;
for (i=0; i<MAX; i++)
{
printf("El valor de la suma de el elemento v[%d] es: %d \n", i, v[i]);
}
}
void sumarV (int vector1[MAX], int vector2[MAX])
{
int suma[MAX], i; //here is the problem
for (i=0; i<MAX; i++)
{
suma[i]=vector1[i]+vector2[i]; //here is the problem
}
}
The problem disappears when I comment 'here is the problem' inside the code.
Declare suma in main and pass it to sumaV()
int main ()
{
int vector1[MAX], vector2[MAX], suma[MAX];
...
sumarV(vector1, vector2, suma);
Then, in the function
void sumarV (int vector1[MAX], int vector2[MAX], int suma[MAX])
{
int i;
for (i=0; i<MAX; i++)
{
suma[i]=vector1[i]+vector2[i];
}
}
Finally, don't pass the number of elements
escribirVect(suma[MAX]); // here is the problem
just pass the array, which decays into a pointer to the first element:
escribirVect(suma);

Resources