Hi guys,
I study mathematics and wrote a simple program calculating binomial
distribution (probability) - I'm just the beginner with C, but I managed to write it
so that it can be run from the command line (DOS). I am sure my code is far from
perfect and everything may be done easier, more elegant, etc. The point is it works (= gives the right answers, hehe). My problem is how can I make it run as a normal windows application (not from command line). When I run DevC there is a template for windows application, however, I tried and I still have no idea where to insert my code.
Here is my program, and after it I pasted the code of a windows application template, I'd be extremely grateful if anyone could just merge the two codes for me. My knowledge of is very low, and I do not understand 95% of the things from the windows template
thank you
My program:
Code:
#include<stdio.h>
int data_input(); /* function which lets you input the data */
int num_trials;
int count=1;
int n_result=1;
int k;
int k_result;
int num_successes;
int factorial (int factorial_arg);
int knerror ();
int n_kresult;
int nk_final;
float power(float base, int exponent);
float pow_result;
float prob;
int prob_error();
float prob_power_return;
float subtr_power_return;
float final_pdf;
int main(void)
{
data_input();
n_result=factorial(num_trials); /* calculates the factorial of nr of trials */
k_result=factorial(num_successes);
/* calculates the factorial of nr of successes */
n_kresult=factorial(num_trials-num_successes);
printf("\nn %d! = %d\n", num_trials,n_result);
printf("k %d! = %d\n", num_successes,k_result);
printf("(n - k)! %d! = %d\n", num_trials-num_successes, n_kresult);
nk_final=n_result / (k_result * n_kresult);
printf("\nThe binomial coefficient (n/k) equals %d\n", nk_final);
prob_power_return=power(prob, num_successes);
subtr_power_return=power(1-prob, num_trials-num_successes);
final_pdf=nk_final * (prob_power_return * subtr_power_return);
printf("\nBinomial distribution for %d trial(s) and %d success(es) is % f\n", num_trials, num_successes, final_pdf);
return 0;
}
/* Function calculating the factorial of the passed argument */
int factorial(int factorial_arg)
{
int result_factorial = 1;
count=1;
for(count; count <= factorial_arg; count++)
{
result_factorial = result_factorial * count;
}
return result_factorial;
}
/* knerror function is executed when the number of successes
* is higher than the number of trials */
int knerror()
{
printf("\nThe number of successes cannot be higher than the number of trials!!!\n");
data_input();
return 0;
}
/* data_input() - Here you enter the input values */
int data_input()
{
printf("\nEnter the number of trials: ");
scanf("%d", &num_trials);
printf("Enter the number of successes: ");
scanf("%d", &num_successes);
if (num_successes>num_trials) /* if the number of successes is higher than trials - do it again */
{
printf("\nThe number of successes cannot be higher than the number of trials");
data_input();
}
else
printf("\nEnter the probability of a single independent outcome: ");
scanf("%f", &prob);
if (0 > prob > 1 )
{
prob_error();
}
else
/* everything seems ok - go to the main function )*/
return 0;
}
/* Works out the power of two values - x to the power of y */
float power (float base, int exponent)
{
int x_count = 1;
pow_result = 1;
for(x_count;x_count <= exponent; x_count++)
{
pow_result= pow_result * base;
}
return pow_result;
}
int prob_error()
{
printf("\nThe probability cannot be higher than 1 or lower than 0!!!\n");
data_input;
}
*************************
windows application template
Code:
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof(WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use light-gray as the background of the window */
wincl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
/* Register the window class, if fail quit the program */
if(!RegisterClassEx(&wincl)) return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx(
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow(hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage( ) returns 0 */
while(GetMessage(&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage( ) gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage( ) */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
Thank you