Create a c file on linux

Asked

Viewed 158 times

0

I have a college job, which I am asked to implement a "C" program that produces files containing random integer numbers of the type long int, where the random numbers must be between 0 and RAND_MAX. You should make files with the following number of random integers for example : 50000.
This program must have an input parameter which is the number of integers to produce.
Since I don’t know where to start, I started making the following code :

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char * argv[])
{
FILE *fp;
fp = fopen("50000.c", "wb");
argv[50000];
fclose(fp);

}

Can someone give me some tips ? This code is to be done later on the linux terminal.

  • 6

    Can someone give me some tips ? Turn on your compiler’s warnings and pay attention to them.

  • The problem is that I don’t know which compiler to use on linux could give me the example of some ? But I’m in the right direction ?

  • In principle all Linux has installed gcc. Try gcc --help to verify.

  • ahahah I know this job :)

1 answer

-2

ola amigo to run on linux using gcc just use stdio library. h and nothing else.

I made a code where RAND_MAX can be changed in the define and ready, and apparently you already know how to use FILE.

I hope I’ve helped !!

 #include <stdio.h>
 #define RAND_MAX 100

    int main()
    {
        long numAleatorio;
        int i, vezes;

        numAleatorio = 0;
        i = 0;
        vezes = 0;

        printf("\nEntre quantos numeros aleatorios voce quer: ");
        scanf("%d", &vezes);

        while(i <= vezes)
        {
          numAleatorio = rand()%RAND_MAX;
          printf("\n%d", numAleatorio);
          i++;
        };

        printf("\n");

        return 0;
    }

To compile you do:

gcc teuCodigo.c -o nomeQualquerSemPontoC
./nomeQualquerSemPontoC

Example in my case:

gcc test.c -o test
./test

Voila

  • 1

    The redefinition of RAND_MAX is illegal! See Standard 6.10.3p2.

  • See the comments in Lacobus' reply. Your cycle while(i <= vezes) runs once more than necessary.

  • quiet only change the while(i <= times - 1) then

  • I used RAND_MAX as a constant not a reserved word. If you want to use the default in the Rand method or is its maximum non-price of the % in the Rand call. It would be numAleatorio = rand();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.