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;
}
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.
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);
When I run my code with Ctrl + F5 in Visual Studio I get this error:
Debug Error !
Program: name of my program bla bla
Run-Time Check Failure#2-S
My code
// fig07_26.cpp : definisce il punto di ingresso dell'applicazione console.
// Programma di ordinamente multifunzione che fa uso dei puntatori a funzioni
#include <stdio.h>
#define SIZE 10
//Prototipi di funzione
void bubble(int work[], const int size, int(*compare) (int a, int b));
int ascending(int a, int b);
int descending(int a, int b);
int main()
{
int order; // 1 per ordinamento ascendente 2 per quello discendente
int counter; // contatore
// inizializza il vettore a
int a[SIZE] = { 2,5,3,6,1,5,8,0,6,1 };
printf("Enter 1 to sort ascending order\n" "Enter 2 to sort descending order: \n");
scanf_s("%d", &order);
printf("\nData items printed in original order");
for (counter = 1; counter < SIZE; counter++) {
printf("%5d", a[counter]);
}
// Ordina il vettore in ordine ascendente; passa la funzione ascending
// come argomento per specificare l'ordinamento ascendente
if (order == 1) {
bubble(a, SIZE, ascending);
printf("\nData items in ascending order");
}
else
{
bubble(a, SIZE, descending);
printf("\nData items in descending order");
}
// visualizza il vettore ordinato
for (counter = 0; counter < SIZE; counter++) {
printf("%5d", a[counter]);
}
printf("\n");
return 0;
}
/** Bubble sort multifunzione: Il parametro compare è un puntatore a una funzione di comparazione che determina il senso
dell'ordinamento **/
void bubble(int work[], const int size, int (*compare) (int a, int b)) {
int pass; //contatore dei passaggi
int count; //contatore dei confronti
void swap(int *element1Ptr, int *element2Ptr); // prototipo
// ciclo per controllare i passaggi
for (pass = 1; pass < size; pass++) {
// ciclo per controllare i confronti
for (count = 0; count < size; count++)
{
// Scambia gli elementi adicenti se non sono in ordine
if ( (*compare) (work[count], work[count + 1]) ) {
swap(&work[count], &work[count + 1]);
}
}
}
}
// Scambia i valori nelle locazioni di memoria puntate da element1Ptr e element2Ptr
void swap(int *element1Ptr, int *element2Ptr) {
int hold; // variabile temporanea
hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}
// Determina se gli elementi non sono in ordine rispetto a un ordinamento ascendente
int ascending(int a, int b) {
return b < a; // effettua uno scambio se b è minore di a
}
// Determina se gli elementi sono in ordine rispetto a un ordinamento discendente
int descending(int a, int b) {
return b > a;
}
This program has some parts of it in spanish, so I'll explain: It's a dynamic 2D array, a message is printed to the user requesting the input of the number of rows and columns. Then the arrays get their space allocated using the function aloc, where calloc is used for the allocation, The second pointer receives the number of rows and then a for() cycle is used to allocate the space for the columns.
The problem arises in function imp(), which is supposed to print the resulting matrix, I believe it is due to the pointer arithmetic, but I'm unsure. The program crashes but there's no problem in the compilation process, and as far as I understand, all operations are valid.
I tried using the classic 2D array form, but it still crashes with a[i][j], insted of ((a+i)+j). I tried running the program without thhe printf, and it would crash upon reaching suma(), which is supposed to add both matrices, I believe it crashed when reaching the pointer arithmetic as well. When these were ommited, the program ran smoothly.
#include<stdio.h>
#include<stdlib.h>
#define p printf
#define s scanf
float** aloc(float **m,int r,int c);
void asig(float **a,int r,int c);
void imp(float **a,int r,int c);
float** suma(float **a,float **b,float **c,int r,int x);
int main(int argc,char*argv[])
{
float **ma=NULL,**mb=NULL,**mc=NULL;
int r=0,c=0,i=0,j=0;
p("\n\tEste programa suma 2 matrices cuyas dimensiones son dadas por el usuario\n\n\t%cCu%cntos renglones tienen las matrices?",168,160);
s("%d",&r);
p("\n\t%cCu%cntas columnas tienen las matrices?",168,160);
s("%d",&c);
ma=aloc(ma,r,c);
mb=aloc(mb,r,c);
mc=aloc(mc,r,c);
p("\n\n\t\tMATRIZ 1");
asig(ma,r,c);
imp(ma,r,c);
p("--------------------------\n\n\t\tMATRIZ 2");
asig(mb,r,c);
imp(mb,r,c);
p("--------------------------");
mc=suma(ma,mb,mc,r,c);
p("\n\tLa matriz resultante es:\n");
imp(mc,r,c);
fflush(stdin);
getchar();
return 0;
}
float** aloc(float **m,int r,int c)
{
int i=0;
if((m=(float**)calloc(r,sizeof(float*)))==NULL)
{
p("Error al asignar espacio");
exit(0);
}
for(i=0;i<r;i++)
if((m[i]=(float*)calloc(c,sizeof(float)))==NULL)
{
p("Error al asignar espacio");
exit(0);
}
return m;
}
void asig(float **a,int r,int c)
{
int i=0,j=0;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
p("\n\t%cCu%cl es el elemento [%d][%d] de la matriz?",168,160,i+1,j+1);
s("%f",((a+i)+j));
}
}
void imp(float **a,int r,int c)
{
int i=0,j=0;
p("\n\tLa matriz queda:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
p("%f ",*(*(a+i)+j));
}
p("\n");
}
}
float** suma(float **a,float **b,float **c,int r,int x)
{
int i=0,j=0;
for(i=0;i<r;i++)
for(j=0;j<x;j++)
*(*(c+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);
return c;
}de here
Here is the problem
s("%f", ((a + i) + j));
this
scanf("%f", &a[i][j]);
always use the proper function names, and this (*(a + i) + j) is also ok, but is confusing as you can see.
Use the array indexing notation to avoid problems.
If you want to know how I found it in the mess that you posted, I used compiler warnings. They are your friends.
BTW: scanf() has a return value, check it, if you accidentaly give the program some invalid input, then unexpected problems will happen, also for each malloc() you need a free() and don't use calloc() unless you intend to initialize the values to zero later, if you are going to initialize each value use malloc().
And here a corrected version of your program
#include <stdio.h>
#include <stdlib.h>
float** aloc(float **m,int r,int c);
void asig(float **a,int r,int c);
void imp(float **a,int r,int c);
float** suma(float **a,float **b,float **c,int r,int x);
int main()
{
float **ma = NULL,**mb = NULL,**mc = NULL;
int r = 0, c = 0;
printf("\n\tEste programa suma 2 matrices cuyas dimensiones son dadas por el usuario\n\n\t%cCu%cntos renglones tienen las matrices?",168,160);
if (scanf("%d", &r) != 1)
return -1;
printf("\n\t%cCu%cntas columnas tienen las matrices?", 168, 160);
if (scanf("%d", &c) != 1)
return -1;
ma = aloc(ma, r, c);
if (ma == NULL)
goto failed;
mb = aloc(mb, r, c);
if (mb == NULL)
goto failed;
mc = aloc(mc, r, c);
if (mc == NULL)
goto failed;
printf("\n\n\t\tMATRIZ 1");
asig(ma, r, c);
imp(ma, r, c);
printf("--------------------------\n\n\t\tMATRIZ 2");
asig(mb, r, c);
imp(mb, r, c);
printf("--------------------------");
mc = suma(ma, mb, mc, r, c);
printf("\n\tLa matriz resultante es:\n");
imp(mc, r, c);
getchar();
free(ma);
free(mb);
free(mc);
return 0;
failed:
free(ma);
free(mb);
free(mc);
return -1;
}
float** aloc(float **m,int r,int c)
{
int i = 0;
if ((m = calloc(r, sizeof(float*))) == NULL)
{
printf("Error al asignar espacio");
return NULL;
}
for (i = 0 ; i < r ; i++)
{
if ((m[i] = calloc(c, sizeof(float))) == NULL)
{
int j;
for (j = i ; j >= 0 ; j--)
free(m[j]);
free(m);
return NULL;
}
}
return m;
}
void asig(float **a,int r,int c)
{
int i = 0, j = 0;
for(i = 0 ; i < r ; i++)
{
for(j = 0 ; j < c ; j++)
{
printf("\n\t%cCu%cl es el elemento [%d][%d] de la matriz?", 168, 160, i + 1, j + 1);
scanf("%f", &a[i][j]);
}
}
}
void imp(float **a,int r,int c)
{
int i = 0,j = 0;
printf("\n\tLa matriz queda:\n");
for(i = 0 ; i < r ; i++)
{
for(j = 0 ; j < c ; j++)
{
printf("%10f", a[i][j]);
}
printf("\n");
}
}
float** suma(float **a,float **b,float **c,int r,int x)
{
int i = 0,j = 0;
for(i = 0 ; i < r ; i++)
{
for(j = 0 ; j < x ; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}