In my code:
int nAlunos, nNotas;
/*size input*/
scanf("%d", &nAlunos);
scanf("%d", &nNotas);
/*arrays*/
int ordenador [nAlunos];
char listaNomes [nAlunos][16];
float listaNotas [nAlunos][nNotas+1];
/*non-arrays*/
int i, k;
/*input*/
for(i = 0; i < nAlunos; i++){
scanf("%s", listaNomes[i]);
for(k = 1; k <= nNotas; k++){
scanf("%f", &listaNotas[i][k]);
}
}
/*sum of grades / number os grades for each person*/
for(i = 0; i < nAlunos; i++){
listaNotas[i][0] = 0;
for(k = 1; k <= nNotas; k++)
listaNotas[i][0] += listaNotas[i][k];
listaNotas[i][0] /= nNotas;
}
/*process order on vector*/
for(i = 0; i < nAlunos; i++){
menor = 1111;
for(k = i; k < nAlunos; k++)
if (listaNotas[k][0] < menor)
menor = listaNotas[k][0];
for(k = i; listaNotas[k][0] != menor; k++){}
ordenador[k] = i;
ordenador[i] = k;
}
for(i = 0; i < nAlunos; i++)
printf("%d", ordenador[i]);
}
I am trying to fill a table of data from the user, I may not use pointers for this class.
it needs to receive data as presented:
String1
float1 float2...
String2
.
.
.
When running it, it works well until I finish with the last input. After that it just breaks the execution and returns me segmentation fault: 11
I tried to find where could it be accessing improper memory, but it just doesn't make sense to me because I cannot find it.
Here's a simple adaptation of your code, with error checking on the scanf()
calls:
#include <stdio.h>
#include <stdlib.h>
static void err_exit(const char *msg);
int main(void)
{
int nAlunos, nNotas;
if (scanf("%d", &nAlunos) != 1 || scanf("%d", &nNotas) != 1)
err_exit("failed to read dimensions");
char listaNomes[nAlunos][16];
float listaNotas[nAlunos][nNotas+1];
int i, k;
for (i = 0; i < nAlunos; i++)
{
if (scanf("%s", listaNomes[i]) != 1)
err_exit("failed to read name");
for (k = 1; k <= nNotas; k++)
{
if (scanf("%f", &listaNotas[i][k]) != 1)
err_exit("failed to read grade");
}
}
for (i = 0; i < nAlunos; i++)
{
printf("%s:", listaNomes[i]);
for (k = 1; k <= nNotas; k++)
printf(" %f", listaNotas[i][k]);
putchar('\n');
}
return 0;
}
static void err_exit(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
Given the input file:
3
4
name-the-first
12 13 14 15
name-the-second
21 22 23 24
name-the-third
31 310 3109 31098
The output from the program is:
name-the-first: 12.000000 13.000000 14.000000 15.000000
name-the-second: 21.000000 22.000000 23.000000 24.000000
name-the-third: 31.000000 310.000000 3109.000000 31098.000000
This is what should be expected.
The only oddity in the code is the use of for (k = 1; k <= nNotas; k++)
because it leaves listaNotas[i][0]
uninitialized and unused. You did, however, allocate enough space so that isn't a cause of trouble.
The problem is not self-evidently in the code you showed. Please show the actual code that crashes!
for(k = i; listaNotas[k][0] != menor; k++){}
The above statement is causing the out of bound access of the array! Hence the segmentation fault
. Handle it according to your functionality.
The condition listaNotas[k][0] != menor
is fatal.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.