pilikikikirrere

Kolorwanie:
Status:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cstdio>

using std::cout;
using std::endl;

void write_data(const char* path, int k)
{
    FILE* file = fopen(path,"wb");
    unsigned int n;
    float temp;
    for(int i=0; i<k; i++)
    {
        n = rand()%10+1;
        cout<<n<<" ";
        fwrite(&n,sizeof(int),1,file);
        for(int j=0; j<n; j++)
        {
            temp = (float)rand()/RAND_MAX;
            cout<<temp<<" ";
            fwrite(&temp,sizeof(float),1,file);
        }
        cout<<endl;
    }
    fclose(file);
}

void process_data(const char* path)
{
    FILE* file = fopen(path,"rb");
    float total = 0;
    int counter = 0;
    unsigned int n;
    float* arr;
    float min = 1;
    float avg;
    fread(&n,sizeof(int),1,file);
    while(!feof(file))
    {
        cout<<n<<" ";
        arr = new float[n];
        fread(arr,sizeof(float),n,file);
        for(int i=0; i<n; i++)
        {
            if(arr[i]<min) min = arr[i];
            cout<<arr[i]<<" ";
        }
        total += min;
        counter++;
        cout<<endl;
        min = 1;
        delete[] arr;
        fread(&n,sizeof(int),1,file);
    }
    avg = (float)total/counter;
    cout<<avg;
    fclose(file);
}

int main(){
    srand(time(0));
    write_data("file",6);
    cout<<endl;
    process_data("file");
}

Copyrights 2014-2021 © Wklejaj.pl Wszelkie prawa zastrzeżone.