In a forest n animals has homes. Each home has m racks, each rack for each of the m products which the animals can buy from the shop. From the shop, only the king can buy and only in the first day of the month. The king needs to know at the end of the month how much food he must buy. So he goes to all the animals to take the command. Then, he goes to the shop to buy. Each products are being selled at piece.

To calculate:
1. How much food the animals have after the king returned from the shop?
2. How much an animal should pay, if the prieces for each m food are known?
3. How many days an animal should work for paying? The animals are not paying for anything else and an animal, for each day of work, it receives g dollars.

From a file animals.in I have to read the necessary data.
On the first row is an integer which represents how many dollars an animal receives for a day of work.
On the next row there are two numbers separated by a space. A number,n , the number of the animals and the number of racks, m, from each house.
On the next n rows there is the ammount of the food that each animal has, given by separated numbers. M numbers on each row.
On the next n rows there are the commands for m type of foods that each animal made. M numbers on each row.
On the last line the are the prieces for each type of food. M numbers.

The results should be displayed in animals.out.

I want JUST to read the data and to assign the numbers to some variables in my C program and print them to monitor. I will manage from there.

I wrote the file like this:
20
5 3
2 3 4
5 7 8
10 24 3
8 5 1
2 4 8
5 8 3
1 4 6
8 9 5
3 4 1
5 7 9
150 200 350

Here is what I tried:

//functions.c

#include<stdio.h>
#include"header.h"
#include<stdlib.h>
#include<string.h>
void read(double a[][200])
{
    char line[250];
    int v[200];
    int i;
    //int g;

    FILE *in;
    in = fopen("animals.in", "r");
    if(in==0)
    {
        printf("Eroare.\n");
        exit(EXIT_FAILURE);
    }
    //fscanf(in, "%d", &g);

    while(fgets(line,sizeof(line),in)!=NULL)
    {
        v[i]=atoi(line);
        i++;
            //v[i]=atoi(ptr);
            //i++;
            //ptr=strtok(NULL," ");
    }
    int qwer;
    qwer=i;
    for(i=0;i<qwer;i++)
    {
        printf("\nThe number %d of %d is: %d", i+1,qwer,v[i]);
    }

    printf("\n\n");
    //printf("%d", g);

    /*
    fscanf(in, "n=%d, m=%d", &n, &m);

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
    fscanf(in, "%lf", &a[i][j]);

    printf("%d %d %d", g,n,m);

    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
            printf("%lf", a[i][j]);
    */
}

I am calling the function read from a file main.c

I do not know what to do!!!

PLEASE HELP ME!!!

Thanks in advance!!!

Recommended Answers

All 7 Replies

It might be easier to use fscanf() rather than fgets()

float dollars = 0.0F;
int nAnimals, nRacks;
fscanf(in,"%f", &dollars);
fscanf(in,"%d%d", &nAnimals,nRacks);
// etc etc

As for the rest of the problem, I'd first sit down with and figure it all out by hand with pencil & paper so that you will find out how to proceed programming it. The instructions seem a little confusing to me, maybe because they were translated into English from some other language??? Example: what is a "rack"?

The problem in translated, you are right!

rack=shelf

I tried to do it by hand, but I do not know how to read the data from file, as the problem wants. That is all I do not know how to do.

rack=shelf , translate problem

i need to know just how to read t text file that contain numbers line by line

Try this.

FILE *fp = fopen(filename,"r");

while( fscanf ( fp,"%s", text ) > 0 )
{
    printf("%s", text);
}

i need to know just how to read t text file that contain numbers line by line

I posted an example. You don't really need to know line by line, just read the numbers in a loop with fscanf(). I showed you how to read the first two lines of the file. From there on it should be fairly simple to create loops for nAnimals and nRacks.

Thanks! I have managed! Thanks for all help! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.