I'm struggling with this code, its purpose is to accumulate data from a .dat file with structs that have information about marks, age, gender. When I compile it, an error shows that say "segmentation fault", I wrote a printf to show me the value of i, j and k; because I think them are the problem, here is the code and a photo of the failure: `
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int i = 0, j = 0, k = 0;
struct persona {
int edad; //EDAD DE 12-65//
int notas; //NOTAS DE 0-10//
char sexo; //SEXO 1=MASCULINO Y 2=FEMENINO//
char exi; //EXI 1=SI 2=NO
};
struct reg {
int notas;
int contex;
int cont;
};
struct reg A[7][6][3];
int grupoedad(struct persona p) {
if( p.edad < 12) {
printf("\nexiste un error en la edad");
} else if(p.edad < 16) {
return 1;
} else if(p.edad < 26) {
return 2;
} else if(p.edad < 36) {
return 3;
} else if(p.edad < 46) {
return 4;
} else if(p.edad < 56) {
return 5;
} else if(p.edad < 66) {
return 6;
}
}
int niveling(struct persona p) {
if(p.notas < 2) {
return 1;
} else if(p.notas < 4) {
return 2;
} else if(p.notas < 6) {
return 3;
} else if(p.notas < 8) {
return 4;
} else if(p.notas <=10) {
return 5;
}
}
int femasc(struct persona p) {
if(p.sexo == 'M'){
return 1;
} else if(p.sexo == 'F') {
return 2;
}
}
void etario(int j, char grupoed[10]) //ASIGNAMOS LOS RANGOS ETARIOS//
{
switch(j)
{
case(1):
strcpy(grupoed, "12-15");
break;
case(2):
strcpy(grupoed, "16-25");
break;
case(3):
strcpy(grupoed, "26-35");
break;
case(4):
strcpy(grupoed, "36-45");
break;
case(5):
strcpy(grupoed, "46-55");
break;
case(6):
strcpy(grupoed, "56-65");
default:
printf("\nHa ocurrido un error");
}
}
//void matrizcero(struct reg A[7][6][3], int i, int j, int k) {
// for (i = 1; i < 7; i++)
// {
//for (j = 1; j < 6; j++)
// {
// for (k = 1; k < 3; k++)
// {
// A[i][j][k].contex = 0;
// A[i][j][k].cont = 0;
//A[i][j][k].notas = 0;
// }
// }
// }
// }
int main() {
struct reg {
int notas;
int contex;
int cont;
};
struct reg A[7][6][3];
struct persona p;
int rangomay, rangomen, rangointmay, resmay, resmen, jex, jmay, jmen, NIV, GRUPOET, sexo, cantidadM, califM, cantidadF, califF, grupoetario;
// matrizcero(A[7][6][3], i, j, k);
char grupoed[10];
float prom_m, prom_f;
FILE *entrada; //ABRO ARCHIVO
entrada=fopen("datospersonas.txt","ab");
if (entrada == NULL){
exit(1);
printf("\nError al abrir el archivo");
}
else {
printf("\nArchivo abierto exitosamente!");
}
while(!(feof(entrada))){
fread(&p,sizeof(p),1,entrada);
// void niveling(struct persona p, int i);
// grupoedad(struct persona p, int j);
// femasc(struct persona p, int k);
i = niveling(p);
j = grupoedad(p);
k = femasc(p);
printf("\n%d",i);
printf("\n%d",j);
printf("\n%d",k);
A[i][j][k].notas = A[i][j][k].notas + p.notas;
A[i][j][k].cont = A[i][j][k].cont;
if(p.exi = 'S') {
A[i][j][k].contex = A[i][j][k].contex + 1;
}
}
for(i = 0; i < 5; i++) { //TOTALIZO HOMBRES Y MUJERES
for(j = 0; j < 6; j++) {
A[i][j][3].notas = A[i][j][3].notas + A[i][j][2].notas;
A[i][j][3].cont = A[i][j][3].cont + A[i][j][2].cont;
A[i][j][3].contex = A[i][j][3].contex + A[i][j][2].contex;
}
}
for(j = 0; j < 6; j++) {
for(i = 0; i < 5; i++) {
cantidadM = cantidadM + A[i][j][1].cont;
califM = califM + A[i][j][1].notas;
}
}
prom_m = califM / cantidadM; //PROMEDIO DE CALIFICACIONES DE LOS HOMBRES
for(j = 0; j < 6; j++) {
for(i = 0; i < 5; i++) {
cantidadF = cantidadF + A[i][j][2].cont;
califF = califF + A[i][j][2].notas;
}
}
prom_f = califF / cantidadM; //PROMEDIO DE CALIFICACIONES MUJERES
for(k = 0; k < 2; k++) {
for(i = 0; i < 5; i++) {
for(j = 0; j < 6; j++) {
A[6][j][k].cont = A[6][j][k].cont + A[i][j][k].cont;
A[6][j][k].contex = A[6][j][k].contex + A[i][j][k].cont;
A[6][j][k].notas = A[6][j][k].notas + A[i][j][k].notas;
}
}
}
for(j = 0; j < 6; j++) {
A[6][j][3].cont = A[6][j][1].cont + A[6][j][2].cont;
A[6][j][3].cont = A[6][j][1].contex + A[6][j][2].contex;
A[6][j][3].notas = A[6][j][1].notas + A[6][j][2].notas;
}
resmay = A[6][1][3].contex;
for(j = 0; j < 6; j++) {
if(A[6][j][3].contex > resmay) {
resmay = A[6][j][3].contex;
jex = j;
}
}
for(j = 0; j < 6; j++) {
for(k = 0; k < 3; k++) {
A[7][j][k].notas = A[6][j][k].notas / A[6][j][k].cont;
}
}
resmen = A[7][1][3].notas;
resmay = A[7][1][3].notas;
for(j = 0; j < 6; j++) {
if(A[7][j][3].notas > resmay) {
resmay = A[7][j][3].notas;
jmay = j;
}
else {
if(A[7][j][3].notas < resmen) {
resmen = A[7][j][3].notas;
jmen = j;
}
}
}
etario(jmay, grupoed);
printf("\nEl rango etario con mayor calificacion en promedio fue el comprendido entre: %s%s", grupoed, " años");
etario(jmen, grupoed);
printf("\nEl rango etario con menor calificacion en promedio fue el comprendido entre: %s%s", grupoed, " años");
printf("\nEl promedio de calificaciones en femeninos fue: %f", prom_f);
printf("\nEl promedio de calificaciones en masculinos fue: %f", prom_m);
etario(jex, grupoed);
printf("\nEl rango etario con mayor interes en rendir examenes internacionales es el comprendido entre: %s", grupoed, "años");
fclose(entrada);
system("pause");
return 0;
}
` enter image description here there we can see that the values of i, j and k are out of the array defined range, so obviusly it breaks the code...
Just in case i will explain how the file was made, it was generated by another program that use rand function to generate random data about people in a certain range, also it has a print just to have control of the values that the structs are taking: `
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
struct persona {
int edad; //EDAD DE 12-65//
int notas; //NOTAS DE 0-10//
char sexo; //SEXO 1=MASCULINO Y 2=FEMENINO//
char exi; //EXI 1=SI 2=NO
};
void generacion()
{
FILE *entrada;
entrada=fopen("datospersonas.txt","ab");
if (entrada == NULL){
exit(1);
}
fclose(entrada);
}
int main()
{
int s;
int exi;
generacion();
persona p;
FILE* entrada;
entrada=fopen("datospersonas.txt","ab");
srand(time(NULL));
for (size_t i=0;i<650; i++)
{
p.edad =(rand() %54)+12;
p.notas =(rand() %10)+1;
s=(rand() %2)+1;
if(s == 1){
p.sexo='M';
}
else{
p.sexo='F';
}
exi=(rand() %2)+1;
if(exi == 1){
p.exi='S';
}
else{
p.exi='N';
}
fwrite(&p, sizeof(struct persona),1,entrada);
printf("edad: %d\n",p.edad);
printf("nota: %d\n",p.notas);
printf("sexo: %c\n",p.sexo);
printf("esta interesado en rendir examenes internacionales: %c\n",p.exi);
}
fclose(entrada);
return 0;
}
`
I have tried doing a file manually, like charging the data by myself to see if the file its not the problem.