1

I started learning how to program last month (and have been making really slow progress ever since), so I really don't know much about stuff.

I code in a Linux (Ubuntu 20.04) and I use VSCode, Sublime, and Code::Blocks (trying to find the perfect IDE for me). I want to learn how to create an executable Windows file so I can share the program I made with my friends (they're all Windows users). Even though it's a really silly objective, learning how to do that will certainly be really useful in the future. I'll leave the code below so you can see it if you like.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Cute little program that allows the user to choose between calculating factorials, finding the real roots
// of quadratic equations and counting the quantity of even numbers inside the sequence they can tipe.
// I'm a native (Brazilian) Portuguese speaker and therefore the program will communicate with the user through (Brazilian) Portuguese language.

// this function finds real roots of quadratic equations
void findRoots(){
    float a, b, c;
    printf("Calculadora de raízes de equações do segundo grau. \n Os parâmetros devem ser observados da fórmula: ax² + bx + c\n");
    printf("\nInsira o parâmetro 'a':\n");
    scanf("%f", &a);
    printf("Insira o parâmetro 'b':\n");
    scanf("%f", &b);
    printf("Insira o parâmetro 'c':\n");
    scanf("%f", &c);
    float discriminante = pow(b,2) - (4 * a * c);
    float sqrtds = sqrt(discriminante);
    float x1 = -b - sqrtds;
    float x2 = -b + sqrtds;
    float den = 2 * a;
  if(a == 0){
        printf("Essa não é uma equação do segundo grau!\n");
    }else {
        if (discriminante < 0){
            printf("A equação não possui raízes reais\n");
        }else if (discriminante == 0) {
            float raiz = -b / den;
            printf("A única raíz da equação é %f\n", raiz);
        } else { 
            float raiz1 = x1 / den;
            float raiz2 = x2 / den;
            printf("O discriminante da equação é %f\n", discriminante);
            printf("Logo, a equação possui 2 raízes reais. \n Uma delas é %f \n A outra é %f\n", raiz1, raiz2);
        }     
    }   

}

//this function calculates factorials
void fatorial(){
    int n;
    int x = 0;
    printf("Calculadora de fatoriais.\n Insira um número até 12: \n");
    scanf("%d", &n);
    int y = n;
    if(n == 0){
        printf("0! é 1\n");
    }else if ( n < 13 && n > 0){
        while ( y  > (x + 1) ){
            x = x + 1;
            n = n * (y - x);
        }
        printf("%d! é %d\n", y, n);
    } else{
        printf("O programa não é capaz de calcular esse fatorial.");

    }
    
    
}

//this function asks the user to enter a sequence and then tells how many even numbers there are in it (it also shows the quantity of odd numbers)
int paridade(){
    printf("Digite o tamanho da sequência:\n");
    int n;
    scanf("%d", &n);
    int par = 0;
    int impar = 0;
    int x = 0;
    int y;
    while(x < n){
        x = x + 1;
        printf("Digite o %dº numero inteiro: \n", x);
        scanf("%d", &y);
        if((y%2) == 0 ){
            par = par + 1;
        }else {
            impar = impar + 1;
        }
    }
    printf("Na sequencia existem %d números pares e %d números ímpares.\n", par, impar);
    return 0;
}

// the main functions allows the user to choose between the funcions mentioned above. It also allows the user to finish the program.
int main(){
    printf("Bem vindo(a).\n");
    int x = 1;
    while(x!= 0){
        
        int opcao;
        printf("\nEscolha entre:\n -Calcular o fatorial de um número (Insira 1)\n -Calcular raízes de uma equação do segundo grau (Insira 2)\n -Contar a quantidade de números pares ou ímpares em uma sequência (Insira 3)\n -Terminar o programa (Insira 4)\n");
        scanf("%d", &opcao);
        switch (opcao)
        {
        case 1:
            fatorial(); //calculates factorial
            break;
        case 2:
            findRoots(); //self-explicative
            break;
        case 3:
            paridade();
            break;
        case 4:
            x = 0; /// closes program
            break;
        default:
            printf("Você selecionou uma opção inválida."); // error message (Invalid option)
            break;
        }
    }
    printf("Obrigado por usar o programa."); // "thnks for using the program"
    return 0;
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Rodrigo M
  • 13
  • 3
  • [Here's one possible solution](https://arrayfire.com/cross-compile-to-windows-from-linux/). I haven't done it myself so I can't vouch for it, but it may be worth a try. – John Bode Sep 24 '20 at 21:23

1 Answers1

0

I code in a Linux (Ubuntu 20.04) and I use VSCode, Sublime, and Code::Blocks (trying to find the perfect IDE for me).

The IDE or editor, in general, is orthogonal to the compiler.

I want to learn how to create an executable Windows file so I can share the program I made with my friends (they're all Windows users).

That means you are cross-compiling. That is, targeting a different platform than the one you are running your compiler on. How to do that depends on your compiler, but it is doable in both GCC and Clang.

Having said that, if you have just started, then you may find it a little be complicated. It may require to install extra packages, tweak things or build the compiler. It is usually way easier to setup a VM with Windows and compile (and test!) the program on it.

The same applies in the opposite case: compiling a Linux program under Windows.

Even though it's a really silly objective, learning how to do that will certainly be really useful in the future.

Certainly, but it is not a silly objective at all. Many important projects are cross-compiled every day.

Acorn
  • 24,970
  • 5
  • 40
  • 69