Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Annanda

Como gravar uma matriz float aleatoria em arquivo txt !?

Recommended Posts

O programa abaixo aleatoriza uma matriz de dimensões inseridas pelo usuário e através da mesma calcula a média de cada linha formando uma nova coluna, ao lado da matriz.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <time.h>
#include <string.h>

/*run this program using the console pauser or add your own getch, system("pause") or input loop*/

float matriz(void){

int i,j;

int n,o;

float soma[100];

float media[100];

printf("\n\tEntre com o numero de provas (Ex.: 4): ");

scanf("%d",&o);

printf("\n\tEntre com o numero de alunos (Ex.: 30): ");

scanf("%d",&n);

float m[100][50];

for(i=0;i<n;i++){

    for(j=0;j<o;j++){

        m[i][j] = (rand() %105 + 1)/10.5;

        soma[i] = soma[i] + m[i][j];

        media[i] = (soma[i])/o;

        }

    }

for(i=0;i<n;i++){

    printf("\n\t");

    for(j=0;j<o;j++){

        printf("\t%.2f",m[i][j]);

    }

    printf("\t\t%.2f\t",media[i]);

    }

}

int main(){
	printf("\n\t\t\tNotas das provas e media de cada aluno\n\t\n");
	printf("\n\tObs.: Cada linha representa um aluno e as colunas representam as provas, sendo que a ultima representa a media de cada aluno.\n\n");
	matriz();
	return 0;
} 

A saída ficaria:


                        Notas das provas e media de cada aluno


        Obs.: Cada linha representa um aluno e as colunas representam as provas,
 sendo que a ultima representa a media de cada aluno.


        Entre com o numero de provas (Ex.: 4): 4

        Entre com o numero de alunos (Ex.: 30): 30

                4.00    8.86    3.33    3.90            5.02
                5.71    7.62    3.24    6.10            5.67
                7.90    10.00   3.43    0.57            5.48
                7.33    2.67    8.76    6.86            6.40
                5.33    7.43    9.81    7.81            7.60
                4.95    0.95    1.71    4.67            3.07
                7.90    9.33    9.24    2.57            7.26
                8.00    4.86    8.86    9.24            7.74
                6.86    8.95    8.10    6.48            7.60
                4.57    4.76    2.48    2.38            3.55
                3.71    7.81    3.14    8.95            5.90
                3.24    4.29    2.10    4.48            3.52
                0.86    4.19    3.14    2.86            2.76
                0.76    9.81    8.38    4.76            5.93
                0.86    7.81    1.90    4.19            3.69
                3.05    9.14    3.43    5.52            5.29
                7.52    7.33    1.05    1.71            4.40
                4.76    7.05    4.00    7.24            5.76
                3.43    0.95    1.05    2.00            1.86
                9.24    2.10    3.24    8.00            5.64
                9.52    2.29    3.81    0.48            4.02
                6.38    7.71    3.05    2.57            4.93
                6.86    5.62    3.81    9.52            6.45
                5.43    8.48    7.43    1.33            5.67
                5.14    8.38    3.81    5.43            5.69
                0.38    3.43    1.90    4.67            2.60
                2.38    5.81    1.24    9.71            4.79
                6.57    2.10    8.29    7.71            6.17
                4.29    6.48    7.24    5.24            5.81
                0.29    3.24    5.52    2.67            2.93
--------------------------------
Process exited with return value 0
Press any key to continue . . . 

Como gravar essa matriz e essa coluna da forma como está em um arquivo txt e ler através dele, já tentei de tudo.

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

Veja se te ajuda

int gravadados(float arr[][50], int qtdProvas, int qtdAlunos)
{
    int i, j;
    FILE *file;
    if (!(file = fopen("C:\\ttt.txt", "w")))
    {
        printf("Erro ao gravar arquivo");
        return 1;
    }
    fwrite(&qtdAlunos, sizeof(int), sizeof(int), file);
    fwrite(&qtdProvas, sizeof(int), sizeof(int), file);
    for (i = 0; i < qtdAlunos; i++)
        for (j = 0; j < qtdProvas; j++)
            fwrite(&arr[i][j], sizeof(float), sizeof(float), file);

    fclose(file);
    free(file);
    return 0;
}

int ledados(float arr[][50], int &qtdProvas, int &qtdAlunos)
{
    int i, j;
    FILE *file;
    if (!(file = fopen("C:\\ttt.txt", "r")))
    {
        printf("Erro ao ler arquivo");
        return 1;
    }
    fread(&qtdAlunos, sizeof(int), sizeof(int), file);
    fread(&qtdProvas, sizeof(int), sizeof(int), file);
    for (i = 0; i < qtdAlunos; i++)
        for (j = 0; j < qtdProvas; j++)
            fread(&arr[i][j], sizeof(float), sizeof(float), file);

    fclose(file);
    free(file);
    return 0;
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.