I'm trying to compile this code in C, 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>
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;
};
const int imax = 7;
const int jmax = 6;
const int kmax = 3;
struct reg A[imax][jmax][kmax];
void retardo() {
for(int i=0; i < 11000; i++) {
for(int j=0; j < 11000; j++) {
}
}
}
void processbar() {
system("pause");
system("cls");
int i, j;
for(i=0; i < 20 ; i++) {
printf("Cargando... \n");
printf("====================\n");
for(j=0; j < i ; j++) {
printf("X");
}
printf("\n====================\n");
retardo();
system("cls");
}
}
void etario(int j, char grupoed[10]) //ASIGNAMOS LOS RANGOS ETARIOS//
{
switch(j)
{
case(1):
grupoed ="12-15";
break;
case(2):
grupoed ="16-25";
break;
case(3):
grupoed ="26-35";
break;
case(4):
grupoed ="36-45";
break;
case(5):
grupoed ="46-55";
break;
case(6):
grupoed = "56-65";
default:
printf("\nHa ocurrido un error");
}
}
void grupoed(struct persona p, int j) {
if( p.edad < 12) {
printf("\nexiste un error en la edad");
}
if(p.edad < 16) {
j = 1;
}
if(p.edad < 26) {
j = 2;
}
if(p.edad < 36) {
j = 3;
}
if(p.edad < 46) {
j = 4;
}
if(p.edad < 56) {
j = 5;
}
if(p.edad < 66) {
j = 6;
}
else {
printf("\nexists un error en la edad");
}
}
void niveling(struct persona p, int i) {
if(p.notas < 2) {
i = 1;
}
if(p.notas < 4) {
i = 2;
}
if(p.notas < 6) {
i = 3;
}
if(p.notas < 8) {
i = 4;
}
if(p.notas < 10) {
i = 5;
}
}
void femasc(struct persona p, int k) {
if(p.sexo = 'M') {
k = 1;
}
else {
if(p.sexo = 'F') {
k = 2;
}
}
}
void matrizcero(struct reg A[imax][jmax][kmax], int i, int j, int k) {
for (i = 1; i < 7; i++)
{
for (j = 1; i < 6; j++)
{
for (k = 1; i < 3; k++)
{
A[i][j][k] = {0,0,0};
}
}
}
}
int main() {
struct reg A[imax][jmax][kmax];
void processbar();
void matrizcero(struct reg A[imax][jmax][kmax], int i, int j, int k);
struct persona p;
struct reg;
int i, j, k,rangomay, rangomen, rangointmay, resmay, resmen, jex, jmay, jmen, NIV, GRUPOET, sexo, cantidadM, califM, cantidadF, califF, grupoetario;
char grupoed[10];
float prom_m, prom_f;
FILE *entrada; //ABRO ARCHIVO
entrada=fopen("datospersonas.dat","rb");
if (entrada == NULL){
exit(1);
printf("\nError al abrir el archivo");
}
else {
printf("\nArchivo abierto exitosamente!");
}
while(!(feof(entrada))){
void niveling(struct persona p, int i);
void grupoedad(struct persona p, int j);
void femasc(struct persona p, int k);
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;
}
fread(&p,sizeof(p),1,entrada); //LEO ARCHIVO
}
for(i = 1; i < 6; i++) { //TOTALIZO HOMBRES Y MUJERES
for(j = 1; i < 7; i++) {
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 = 1; j < 7; j++) {
for(i = 1; i < 6; 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 = 1; j < 7; j++) {
for(i = 1; i < 6; 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 = 1; k < 3; k++) {
for(i = 1; i < 6; i++) {
for(j = 1; j < 7; 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 = 1; j < 7; 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 = 1; j < 7; j++) {
if(A[6][j][3].contex > resmay) {
resmay = A[6][j][3].contex;
jex = j;
}
}
for(k = 1; k < 3; k++) {
for(j = 1; j < 7; j++) {
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 = 1; j < 7; 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;
}
}
}
void etario(int jmay);
printf("\nEl rango etario con mayor calificacion en promedio fue el comprendido entre: %s", grupoed, " años");
void etario(int jmen);
printf("\nEl rango etario con menor calificacion en promedio fue el comprendido entre: %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);
void etario(int jex);
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
Just in case, here is the file that generates the .dat file...
`
#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.dat","w+");
if (entrada == NULL){
exit(1);
}
fclose(entrada);
}
int main()
{
int s;
int exi;
generacion();
persona p;
FILE* entrada;
entrada=fopen("datospersonas.dat","w+");
for (size_t i=0;i<30; i++)
{
p.edad =(rand() %53)+12;
p.notas =(rand() %9)+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 try writting the print and also process with less structs, I started compiling 500 structs, and now im trying to do it with less than 50...