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;
}
Basically, we made a neural network that tests if a digit from a digital clock is odd or even.
The C code works on my windows machine, however, it gives me a lot of errors on my mac.
Like: function definition is not allowed here.
I think it's just a compilation issue. Any help would be much appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define ligne 10
#define colonne 8
int main (){
//--------------------------creation A, W, O, d, X, epsilon--------------------------
int i,j;
float epsilon = 0.2;
float cpt_A;
int O[ligne];
float A[ligne];
float W[colonne];
srand(time(NULL));
for( i = 0 ; i < colonne ; i++ ) {
W[i]=rand() % 6;
}
int d[ligne] = {0,1,0,1,0,1,0,1,0,1};
int X[ligne][colonne]= {{1,1,1,1,1,1,1,0},{1,0,1,1,0,0,0,0},{1,1,1,0,1,1,0,1},{1,1,1,1,1,0,0,1},
{1,0,1,1,0,0,1,1},{1,1,0,1,1,0,1,1},{1,1,0,1,1,1,1,1},{1,1,1,1,0,0,0,0},
{1,1,1,1,1,1,1,1},{1,1,1,1,1,0,1,1}};
//--------------------------Check for O--------------------------
int check(float a){
if(a>=0) return 1;
else return 0;
}
//--------------------------Remplissage de A[]--------------------------
float* Remplissage_A(float A[],float nW[],int index ){
for(i=index ; i<ligne ; i++){
for(j=0 ; j<colonne ; j++){
cpt_A += nW[j]*X[i][j];
}
A[i]=cpt_A;
}
return A;
}
//--------------------------Fonction check pour remplir O--------------------------
int* Remplissage_O(float A[]){
for ( i = 0; i < ligne; i++){
O[i]=check(A[i]);
}
return O;
}
//--------------------------Affichage X--------------------------
for(i=0; i<colonne; i++){
printf("X%d\t",i);
}
printf("Valeur\n");
for(i=0; i<ligne; i++){
for(j=0; j<colonne; j++){
printf("%d",X[i][j]);
printf(j<colonne-1?"\t":" => %d ",i);
}
printf("\n");
//printf("%f\n",A[i]);
}
//--------------------------Print A et O et d--------------------------
printf("\nA\t");printf("O\t");printf("D\n");
float *Ax= Remplissage_A(A,W,0);
int *Ox= Remplissage_O(A);
for ( i = 0; i < ligne; i++){
printf("%.1f\t",*(Ax + i));printf("%d\t",*(Ox + i));printf("%d\n",d[i]);
}
//--------------------------Fonction Correction W --------------------------
void Correction_W(int index){
for(j=0;j<colonne;j++){
W[j]=W[j]+(epsilon*((d[index]-O[index])*X[index][j]));
}
}
//--------------------------Fonction Correction--------------------------
printf("-----------------------------------------------\n");
void Correction(float *A,float *W,int *O){
int stop=0;
int allCorrect= false;
int i,index;
A=Remplissage_A(A,W,0);
O=Remplissage_O(A);
while (!allCorrect){
for ( i = 0; i < ligne; i++){
if(d[i]!=O[i]){
index=i;
Correction_W(index);
A = Remplissage_A(A,W,index);
O = Remplissage_O(A);
stop++;
}
}
if(stop==0){
allCorrect=true;
}
stop=0;
}
}
//--------------------------Appel Fonction--------------------------
Correction(A,W,O);
//--------------------------Apres correction--------------------------
printf("Apres correction : \n");
printf("-----------------------------------------------\n");
printf("\nA\t");printf("O\t");printf("D\n");
for ( i = 0; i < ligne; i++){
printf("%.1f\t",*(A + i));
printf("%d\t",*(O + i));
printf("%d\n",d[i]);
}
}
(main issue) I changed your nested functions to, well, non-nested functions. You may not like the order of arguments that I chose.
Remplissage_A(): I initialized cpt_A = 0. Please check that this is what you want.
main(): Fixed the conditional print to silence warning about one of the strings not having a matching format for variable being passed in.
check(): refactored then inlined.
Minimized scope of variables (i, j, size, cpt_A, etc).
Combined consecutive printf() calls.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ligne 10
#define colonne 8
//--------------------------Remplissage de A[]--------------------------
float* Remplissage_A(float A[],float nW[], int X[ligne][colonne], int index) {
float cpt_A = 0;
for(int i=index ; i<ligne ; i++){
for(int j=0 ; j<colonne ; j++){
cpt_A += nW[j]*X[i][j];
}
A[i]=cpt_A;
}
return A;
}
//--------------------------Fonction check pour remplir O--------------------------
int* Remplissage_O(float A[], int *O) {
for (int i = 0; i < ligne; i++){
O[i]=A[i] >= 0;
}
return O;
}
//--------------------------Fonction Correction W --------------------------
void Correction_W(int *O, float *W, int X[ligne][colonne], int d[ligne], float epsilon, int index){
for(int j=0;j<colonne;j++){
W[j]=W[j]+(epsilon*((d[index]-O[index])*X[index][j]));
}
}
//--------------------------Fonction Correction--------------------------
void Correction(float *A,float *W, int X[ligne][colonne], int *O, int d[ligne], float epsilon){
int stop=0;
int allCorrect= false;
A=Remplissage_A(A,W,X,0);
O=Remplissage_O(A,O);
while (!allCorrect){
for (int i = 0; i < ligne; i++){
if(d[i]!=O[i]){
Correction_W(O,W,X,d,epsilon, i);
A = Remplissage_A(A,W,X,i);
O = Remplissage_O(A,O);
stop++;
}
}
if(stop==0){
allCorrect=true;
}
stop=0;
}
}
int main (){
//--------------------------creation A, W, O, d, X, epsilon--------------------------
float epsilon = 0.2;
int O[ligne];
float A[ligne];
float W[colonne];
srand(time(NULL));
for(int i = 0 ; i < colonne ; i++ ) {
W[i]=rand() % 6;
}
int d[ligne] = {0,1,0,1,0,1,0,1,0,1};
int X[ligne][colonne]= {{1,1,1,1,1,1,1,0},{1,0,1,1,0,0,0,0},{1,1,1,0,1,1,0,1},{1,1,1,1,1,0,0,1},
{1,0,1,1,0,0,1,1},{1,1,0,1,1,0,1,1},{1,1,0,1,1,1,1,1},{1,1,1,1,0,0,0,0},
{1,1,1,1,1,1,1,1},{1,1,1,1,1,0,1,1}};
//--------------------------Affichage X--------------------------
for(int i=0; i<colonne; i++){
printf("X%d\t",i);
}
printf("Valeur\n");
for(int i=0; i<ligne; i++){
for(int j=0; j<colonne; j++){
printf("%d",X[i][j]);
if(j<colonne-1)
printf("\t");
else
printf(" => %d ",i);
}
printf("\n");
}
//--------------------------Print A et O et d--------------------------
printf("\nA\t");printf("O\t");printf("D\n");
float *Ax= Remplissage_A(A,W,X,0);
int *Ox= Remplissage_O(A,O);
for (int i = 0; i < ligne; i++){
printf(
"%.1f\t"
"%d\t"
"%d\n",
*(Ax + i),
*(Ox + i),
d[i]
);
}
printf("-----------------------------------------------\n");
//--------------------------Appel Fonction--------------------------
Correction(A,W,X,O,d,epsilon);
//--------------------------Apres correction--------------------------
printf(
"Apres correction : \n"
"-----------------------------------------------\n"
"\nA\t"
"O\t"
"D\n"
);
for (int i = 0; i < ligne; i++){
printf(
"%.1f\t"
"%d\t"
"%d\n",
*(A + i),
*(O + i),
d[i]
);
}
}
Just as the title says, here I ask for two inputs in order to generate a matrix based on such inputs from the user, which then will be filled with random numbers up to the number 20.The problem is I can't wrap my head around the logic needed to generate an exact copy of this matrix without generating a second matrix with randomly generated numbers(again) so I can make useof the data stored in the copy.
(Edit)
I've added an image as reference of what I'm trying to achieve.
I tried to store the random values in a one dimensional array, so I can use them later, but I'm not sure if it is the right approach. I'm relatively new to coding and still struggling with the basics, so any help will be greatly appreciated.
Here´s the code that generates the matrix and stores random int values in it.
#include <stdio.h>
#include<time.h>
int matrizAleatoria(int m, int n, int r);
int srand();
int rand();
int main()
{
int m, n;
printf("Introduce el numero de filas:\n");
scanf("%d", &m);
printf("Introduce el numero de columnas:\n");
scanf("%d", &n);
printf("\n");
srand(time(0));
int r = (rand()% 20);
matrizAleatoria(m, n, r);
printf("\n");
return 0;
}
int matrizAleatoria(int m, int n, int r)
{
int i, j, p = m * n;
int matriz1[m][n];
int serie[p];
for(i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
{
matriz1[i][j] = r = (rand()% 20);
serie[i] = r;
printf("[ %-2d ] " , matriz1[i][j]);
}
printf("\n");
}
return serie[i];
}
You cannot return a variable-sized array from a function to outside scope. You have to implement 2-dimensional subscripts yourself and only work with pointers. Like this:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define SUBSCRIPT_2DIM_TO_1DIM(subscript1, subscript2, size2) \
((subscript1) * (size2) + (subscript2))
int* matrizAleatoria(int m, int n);
int main()
{
int m, n;
int* matriz1;
printf("Introduce el numero de filas:\n");
scanf("%d", &m);
printf("Introduce el numero de columnas:\n");
scanf("%d", &n);
printf("\n");
srand(time(0));
matriz1 = matrizAleatoria(m, n);
printf("\n");
// use matriz1 with SUBSCRIPT_2DIM_TO_1DIM
free(matriz1);
return 0;
}
int* matrizAleatoria(int m, int n)
{
int i, j, p = m * n;
// return matrix value, must free() in the caller
int *matriz1 = malloc(sizeof(int) * p);
for(i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
{
matriz1[SUBSCRIPT_2DIM_TO_1DIM(i, j, n)] = rand()% 20;
printf("[ %-2d ] " , matriz1[SUBSCRIPT_2DIM_TO_1DIM(i, j, n)]);
}
printf("\n");
}
return(matriz1);
}
I wrote some code and it showed me this error: Exception thrown at 0x00007FF93F57B016 (ucrtbased.dll) in Ficha 5.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
I can't find the reason behind it.
#include <stdio.h>
#define num 10
void ler_matriz(int **matriz1, int n, int m);
void mostrar_matriz(int matriz1[num][num], int n, int m);
//int num_min_matriz(int matriz1[][], int n, int m);
//void teste_simetria(int matriz1[][], int n, int m);
//void transposta_matriz(int matriz1[][], int n, int m);
//void soma_matriz(int matriz1[][], int matriz2[][], int matriz3[][], int n, int m);
int main()
{
int x[num][num], y[num][num], z[num][num], numL, numC;
printf("Introduza o número de linhas e colunas para a matriz:\n");
scanf(" %d%d", &numL, &numC);
printf("\n\nIntroduza os valores para a matriz 1: ");
ler_matriz(x, numL, numC);
mostrar_matriz(x, numL, numC);
return 0;
}
void ler_matriz(int **matriz1, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("\nx[%d][%d]: ", i + 1, j + 1);
scanf(" %d", &matriz1[i][j]); // the exception error
}
}
}
void mostrar_matriz(int matriz1[num][num], int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; i < m; j++)
{
printf("%d ", matriz1[i][j]);
}
putchar('\n');
}
}
You have:
for (int j = 0; i < m; j++)
This should be
for (int j = 0; j < m; j++)
Otherwise, i < m is going to be true forever (because you're not changing i in that loop) and you'll eventually be accessing 0xFFFFFFFFFFFFFFFF (the very edge of memory).
For memory errors like this a good idea is to use a memory checking tool (you can try valgrind memcheck). Now let's see what's wrong with your code :)
The array
You have to take into account that int[m][n] isn't the same as int**. Using the gcc compiler you'll get a warning about it. (Of course, you can alter your code to use int**)
The For Loop
Just as VoteyDisciple said you should be using
for (int j=0;j<m;j++)
instead of
for(int j=0;i<m;j++)
Uninitialized values
Creating an array and not initializing it can lead to memory errors later on (assuming we're talking about C - some languages initialize arrays with 0's). Here you create the x,y,z matrixes but you end up using a portion of them which you assign values to. The rest remain uninitialized and you can end up running into errors if you try accessing them later on.
The scanf exception
Really the exception you get is due to the above, as you're getting errors from trying to access the memory address at &matriz1[i][j]
Fixing it all
Here's how I'd write your code so that it works:
#include <stdio.h>
#include <stdlib.h>
#define num 10
void ler_matriz(int **matriz1, int n, int m);
void mostrar_matriz(int** matriz1, int n, int m);
//int num_min_matriz(int matriz1[][], int n, int m);
//void teste_simetria(int matriz1[][], int n, int m);
//void transposta_matriz(int matriz1[][], int n, int m);
//void soma_matriz(int matriz1[][], int matriz2[][], int matriz3[][], int n, int m);
int main()
{
//int x[num][num], y[num][num], z[num][num], numL, numC;
int i,j,**x,**y,**z,numL,numC; //Proper declarations
x=malloc(num*sizeof(int*));
y=malloc(num*sizeof(int*));
z=malloc(num*sizeof(int*));
for(i=0;i<num;i++) {
x[i]=malloc(num*sizeof(int));
y[i]=malloc(num*sizeof(int));
z[i]=malloc(num*sizeof(int));
}
//Initialization
for(i=0;i<num;i++) {
for(j=0;j<num;j++) {
x[i][j]=y[i][j]=z[i][j]=0;
}
}
printf("Introduza o número de linhas e colunas para a matriz:\n");
scanf(" %d%d", &numL, &numC);
printf("\n\nIntroduza os valores para a matriz 1: ");
ler_matriz(x, numL, numC);
mostrar_matriz(x, numL, numC);
for(i=0;i<num;i++) {
free(x[i]);
free(y[i]);
free(z[i]);
}
free(x);
free(y);
free(z);
return 0;
}
void ler_matriz(int **matriz1, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("\nx[%d][%d]: ", i + 1, j + 1);
scanf(" %d", &matriz1[i][j]);
}
}
}
void mostrar_matriz(int **matriz1, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", matriz1[i][j]);
}
putchar('\n');
}
}
I'm new here and this is my first question, i couldn't find anything like this in the search engine so my problem is basically a vector of vectors on C, here is what i have done until now but i keep getting a deadly warning, so i know that I'm not using well the structure with the vector and I'd really appreciate some help.
Thanks
PD: Sorry for my english.
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int n;
int *vector;
}Vector_T;
int inicializar_original(int *n,int dim)
{
int i,r,s,j,*k;
Vector_T *t;
Vector_T l;
srand(time(NULL));
r=rand()%10;
scanf("%d",&s);
t->vector=k;
l.n=s;
k=(int*)malloc(s*sizeof(int));
for(j=0;j<s;j++)
{
k[j]=r;
}
for(i=0;i<dim;i++)
{
n[i]=k;
}
}
int main()
{
int *v,dim;
scanf("%d",&dim);
v=(int*)malloc(dim*sizeof(int));
inicializar_original(v,dim);
}
Asumo que hablas español, así que aquí va: El problema más grande es que no estás inicializando la variable "k". Mi sugerencia es que intentes lo siguente:
int i,r,s,j;
int* k;
If English only: Your problem is probably that you're initializing K in the wrong way. Try to do:
int i,r,s,j;
int* k;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int n;
int *vector;
}Vector_T;
int inicializar_original(Vector_T *v, int dim){
int i, j, s, r = rand()%10;
scanf("%d", &s);
for(i=0; i<dim; i++){
int *k = malloc( s * sizeof(int));
for(j=0; j<s; j++){
k[j]=r;
}
v[i].n = s;
v[i].vector = k;
}
}
int main(void){
Vector_T *v;
int dim;
srand(time(NULL));
scanf("%d", &dim);
v = malloc(dim * sizeof(*v));
inicializar_original(v, dim);
{ //check print & release
int i, j;
for(i = 0; i < dim; ++i){
for(j = 0; j < v[i].n ; ++j){
printf("%d ", v[i].vector[j]);
}
puts("");
free(v[i].vector);
}
free(v);
}
return 0;
}