Nothing happens when I run program - c

The guys just helped me with a problem, got solved 100%, but now I have another one.
I compile my files and everything without problems, but when i want to run the program nothing happens.
enter.c : http://pastebin.com/GGzVeAhw
simple_interest.c: http://pastebin.com/XdESrxSk
This is how my header file looks like:
int enter(void);
double Calc_Interest(double principal, double rate, int term);
double Calc_Amount(double principal, double interest);
double Calc_Payments(double total, int term);
void Display_Results(double principal, double interest, double total, double mPay);
PS This forum is really helpful, especially for a beginner like me.

You have a bunch of prototypes in main(), but no function call.
It should be:
int main(void)
{
enter();
return 0;
}
You seem really confused about how to declare and use functions, perhaps you need some tutorial?

simple_interest.h
int enter(void);
double Calc_Interest(double principal, double rate, int term);
double Calc_Amount(double principal, double interest);
double Calc_Payments(double total, int term);
void Display_Results(double principal, double interest, double total, double mPay);
simple_interest.c
//Implement all functions in this file
#include <stdio.h>
double Calc_Interest(double principal, double rate, int term)
{
double interest;
interest = principal * (rate/100) *term;
return interest;
}
double Calc_Amount(double principal, double interest)
{
double total;
total = principal + interest;
return total;
}
double Calc_Payments(double total, int term)
{
double mPay;
mPay= total / (12 * term);
return mPay;
}
void Display_Results(double principal, double interest, double total, double mPay)
{
printf("\n");
printf("Personal Loan Statement\n");
printf("\n");
printf("Amount Borrowed: %.2f\n" , principal);
printf("Interest on Loan: %.2f\n" , interest);
printf("--------------------------------------------------------------------------------\n");
printf("Amount of Loan: %.2f\n" , total);
printf("Monthly Payment: %.2f\n" , mPay);
}
sample.c
#include <stdio.h>
#include "simple_interest.h"
int enter(void)
{
double principal, rate, interest, total, mPay;
int term;
printf("--------------------------------------------------------------------------------\n");
printf("This program calculates the monthly payments due from a personal loan\n");
printf("--------------------------------------------------------------------------------\n");
printf("Enter amount of the loan: ");
scanf("%lf", &principal);
printf("Enter current annual interest rate in \n");
scanf("%lf", &rate);
printf("Enter the number of years of the loan: ");
scanf("%d", &term);
interest = Calc_Interest(principal, rate, term);
total = Calc_Amount(principal, interest);
mPay = Calc_Payments(total, term);
Display_Results(principal, interest, total, mPay);
return 0;
}
int main(void)
{
enter();
return 0;
}
compile like this way
gcc sample.c simple_interest.c
./a.out

Related

Need help solving a issue involving 'function definition is not allowed here' in c language

Tried running this in C but it keeps saying "function definition is not allowed here" when referring to '{'. This is the full code that im trying to run, its called a "greedy algorithm" so im still learning how to make it. The program is supposed to give you your exact change back then let you know how many coins of which type will be needed.
#include <cs50.h>
#include <math.h>
int main()
{
int len = 4;
double cost;
double pay;
{
//promt user for cost
printf("Enter cost: ");
scanf("%lf", &cost);
//prompt user for pay amount
printf("Enter pay: ");
scanf("%lf", &pay);
double change = pay - cost;
if(pay > cost)
printf("Trasncation complete\nHeres your change: %f\n", change);
else if(pay == cost)
printf("Transaction complete.\n");
else if(pay < cost)
printf("insufficient funds\nStill need: %f", cost - pay);
//greddy algorithm
}
void greedy(double change)
{
int i=o;
double number;
double coin[len] = [0.01, 0.05, 0.10, 0.25];
while(i>len)
{
if(coin[i] <= change)
{
number = change / coin[i];
printf("%f of %f is needed", number, change);
change = change * coin[i];
}
i++
}
}
}
You have to define your greedy() function outside of the main function like so:
#include <cs50.h>
#include <math.h>
int main()
{
int len = 4;
double cost;
double pay;
{
//promt user for cost
printf("Enter cost: ");
scanf("%lf", &cost);
//prompt user for pay amount
printf("Enter pay: ");
scanf("%lf", &pay);
double change = pay - cost;
if(pay > cost)
printf("Trasncation complete\nHeres your change: %f\n", change);
else if(pay == cost)
printf("Transaction complete.\n");
else if(pay < cost)
printf("insufficient funds\nStill need: %f", cost - pay);
}
}
// greddy algorithm
void greedy(double change)
{
int i=o;
double number;
double coin[len] = [0.01, 0.05, 0.10, 0.25];
while(i>len)
{
if(coin[i] <= change)
{
number = change / coin[i];
printf("%f of %f is needed", number, change);
change = change * coin[i];
}
i++
}
}

C program: Calculating Interest

Intent : This program asks a user for The amount that is currently in their bank account, the APR and the number of years. The output is the starting and ending of the amount that shows an accumulative interest of the years specified by the user.
Question: I'm trying to find a way to correctly add the interest, as of right now for a specified amount of years all i'm doing is multiplying the (current amount in bank account * interest rate * years) I know this is wrong and I think I need a loop for the accumulating interest rate from one year to the next and that's where I need help.Thanks and here is my current code below...
The Code
#include <stdio.h>
#include <stdlib.h>
float getPV()
{
float d;
float start;
printf("Start: ");
scanf("%f", &start);
d = start;
return d;
}
void getIR(float *a)
{
printf("Rate: ");
scanf("%f",a);
}
void getNP(int *years)
{
printf("NumPeriods: ");
scanf("%d", years);
}
float interest(float a,float b,float c)
{
float x;
x = a*b*c;
return x;
}
int main()
{
float pv,ir,fv,Total;
int np;
pv = getPV(); //Amount in account
getIR( &ir ); // APR
getNP( &np ); // Number of years
fv = interest(pv,ir,np);
Total=fv + pv;
printf("Starting: %.2f\n", pv);
printf(" Ending: %.2f\n", Total);
return 0;
}
Maybe something like this?
float interest(float a,float b,float c)
{
return a * pow(1.0f + b, c);
}
Assuming b is a number like 0.05 to mean 5% a year, and c is a number of years (which doesn't have to be an integer).

deposit() function of this checking account C program

I am writing a program in C to simulate a checking account. There are codes for transactions, I = Initial Balance, D = Deposit, and C = Check(you write a check to someone, like a withdrawal). There is a monthly fee of $3.00 to maintain the account, $0.06 fee for every check cashed, %0.03 for every deposit made, $5.00 overdraft fee whenever a check cashed brings the balance below $0.00.
I am having trouble completing the functions. If you don't think helping a bit with all of them is ok then please just help with the deposit() function. I am only a couple months into C and we just got into functions. Here is my unfinished code. Thanks for any help.
#include <stdio.h>
void outputHeaders (void);
void initialBalance (double iBalance);
void deposit(double amount, double balance, double service, int numDeposit,double amtDeposit);
void check(char code, double amtCheck, double balance);
void outputSummary ();
int main (void)
{
char code;
double amount, service, balance;
double amtCheck, amtDeposit, openBalance, closeBalance;
int numCheck, numDeposit;
amount = 0.0;
service = 0.0;
balance = 0.0;
amtCheck = 0.0;
amtDeposit = 0.0;
openBalance = 0.0;
closeBalance = 0.0;
numCheck = 0;
numDeposit = 0;
outputHeaders();
printf("Enter the code of transaction and the amount: ");
scanf("%c %lf\n", &code, &amount);
if (code == 'I')
{
initialBalance(amount, &balance, &service, &numDeposit, &amtDeposit);
}
else if (code == 'D')
{
deposit (amount, &balance, &service, &numDeposit);
}
else
{
check(amount, &balance, &service, &numCheck, &amtCheck);
}
getchar(); getchar();
return 0;
}
void outputHeaders (void)
{
printf("Transaction Deposit Check Balance\n"
"-------------- -------- ------ -------");
}
void initialBalance (double amount, double *balance, double *service, int *numDeposit, double *amtDeposit)
{
}
void deposit (double amount, double *balance, double *service, int *numDeposit, double *amtDeposit)
{
*balance = *balance + *amtDeposit;
*numDeposit++; //need to keep track of amount of deposits
*service = *service - 0.03; //service charge
printf("Deposit %lf %lf\n", *amtDeposit, *balance);
}
void check (double amount, double *balance, double *service, int *numCheck, double *amtCheck)
{
}
void outputSummary (int *numDeposit, double *amtDeposit, int *numCheck, int *amtCheck, double *openBalance, double *service, double *closeBalance)
{
}
I have seen only deposit(); function when you declared/defined In your function you are using five arguments but calling time used only four argument.
so if you are calling like deposit (amount, &balance, &service, &numDeposit);
Then change your definition/declaration like this
void deposit (double amount, double *balance, double *service, int *numDeposit)
{
*balance = *balance + amount;
*numDeposit++; //need to keep track of amount of deposits
*service = *service - 0.03; //service charge
//I think service change need to reduce from main balance so
*balance = *balance - 0.03;
printf("Deposit %lf balance %lf\n", amount, *balance);
}

Output shows all 0's. The gross and OT is not calculating

Output shows all 0's. The gross and OT is not calculating..
#include <stdio.h>
#define STD_HOURS 40.0
#define OT_RATE 1.5
#define SIZE 5
void read_hours(float worked_hours[], long int clockNumber[]);
void calculate_gross(float wage[], float worked_hours[], float gross);
void calculate_gross_ot(float wage[], float worked_hours[]);
void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[], float gross);
int i;
int main()
{
long int clockNumber [SIZE] = {98401, 526488, 765349, 34645, 127615};/* employee ID */
float gross = 0.0; /* gross */
float OT [SIZE] = {}; /* overtime */
float wage [SIZE] = {10.6, 9.75, 10.5, 12.25, 8.35}; /* hourly wage */
float worked_hours [SIZE] = {}; // Hours worked
read_hours(worked_hours, clockNumber);
if (worked_hours[i]>STD_HOURS)
{
calculate_gross_ot(wage, worked_hours);
}
else
{
calculate_gross(wage,worked_hours, gross);
}
printFunction(clockNumber, wage, worked_hours, OT, gross);
return(0);
}
void read_hours(float worked_hours[], long int clockNumber[])
{
for (i=0; i<SIZE; i++)
{
printf("\n Enter Hours for Emlployee ID: %ld\n", clockNumber[i]);
scanf ("%f", &worked_hours[i]);
}
}
void calculate_gross(float wage[], float worked_hours[], float gross)
{
for(i=0; i<SIZE; i++)
gross=(wage[i]*worked_hours[i]);
}
void calculate_gross_ot(float wage[], float worked_hours[])
{
float gross;
float OT[SIZE];
for (i=0; i<SIZE; i++)
{
/* calculating overtime hours */
OT[i]=worked_hours[i]-STD_HOURS;
/* calculating gross pay with overtime */
gross = (STD_HOURS*wage[i]) + (OT[i]*OT_RATE*wage[i]);
//}
}
}
void printFunction(long int clockNumber[], float wage[], float worked_hours[], float OT[], float gross)
{
/* creating a table for the output */
printf("------------------------------------------------\n");
printf("%7s","Clock#");
printf("%7s","Wages");
printf("%7s","Hours");
printf("%7s","OT");
printf("%7s\n","Gross");
printf("------------------------------------------------\n");
for (i=0; i<SIZE; i++)
{
/* printing the results */
printf("%6ld", clockNumber[i]);
printf("%10.2f",wage[i]);
printf("%6.1f", worked_hours[i]);
printf("%6.1f", OT[i]);
printf("%10.2f",gross);
printf("\n");
}
}
The program is used to calculate gross with and without OT hours. The output is showing all 0's for the gross and OT. Please help to figure out where is the mistake.
In the first case, you are passing gross by value.
In the second case, you are not passing it at all (the function has a local function called gross).
In both cases, whenever the respective function changes gross, this change does not propagate to the caller.
You need to either pass gross by pointer, or return the value from the function using the return statement (and changing the return type appropriately).
Pass OT to your calculate_gross_ot() function from main().
...
calculate_gross_ot(wage, worked_hours, OT);
...
void calculate_gross_ot(float wage[], float worked_hours[], float OT[])

Calculator program issue in C

I'm writing a statistical calculator, with 3 different calculation options. The problem is whenever I choose the 2nd option, it wants to print both the answer from the 1st option and the 2nd option. When I choose the 3 option, it just prints the answer the the 3rd option(the wrong answer, but thats probably a mishap in the formula). Here is the results:
Please Enter a number of inputs
3
Please enter number 1
1
Please enter number 2
2
Please enter number 3
3
Statistical Calculator Menu
(1) Mean
(2) Standard Deviation
(3) Range
(4) Restart/Exit
2
Here is the Mean 2.0Standard Devition is 0.8
Now I thought it might be an issue with how I'm calling each function, but the best I can tell thats not the case. Then I thought it might be a value that I didn't initialize, but it seems as though thats not it either. I just need another pair of eyes to see where I went wrong here.
#include <stdio.h>
#include <conio.h>
#include <math.h>
const int MAX_DATA=8;
void menu(float numbers[], int amount);
float mean(float numbers[],int amount);
float standard_dev(float numbers[], int amount);
float range( float numbers[], int amount);
int main()
{
int i=0, amount=0;
float numbers[MAX_DATA];
printf("Please Enter a number of inputs \n");
scanf("%d", &amount);
if (amount>MAX_DATA)
{
printf("You entered too many numbers");
}
else
{
for (i=0; i<amount; i++)
{
printf("Please enter number %d\n", i+1);
scanf("%f",&numbers[i]);
}
menu(numbers,amount);
}
getch();
return 0;
}
void menu(float numbers[],int amount)
{
int input2=0;
printf("Statistical Calculator Menu");
printf("\n(1) Mean\n(2) Standard Deviation\n(3) Range\n(4) Restart/Exit\n");
scanf("%d",&input2);
if(input2==1)
{
mean(numbers,amount);
}
if (input2==2)
{
standard_dev(numbers,amount);
}
if (input2==3)
{
range(numbers,amount);
}
}
float mean(float numbers[],int amount)
{
int i;
float sum=0;
float average=0;
for (i=0; i<amount; i++)
{
sum=sum+numbers[i];
}
average=sum/amount;
printf("Here is the Mean %.1f", average);
return average;
}
float standard_dev(float numbers[], int amount)
{
float sdev=0,dev=0,sumsqr=0,variance=0;
int i;
float mean2=0;
mean2=mean(numbers,amount);
for (i=0; i<amount; i++)
{
dev=numbers[i]-mean2;
sumsqr+=dev*dev;
}
variance=sumsqr/(float)amount;
sdev=sqrt(variance);
printf("Standard Devition is %.1f", sdev);
return sdev;
}
float range(float numbers[],int amount)
{
int i;
float diff=0;
for (i=0; i<=amount; i++)
{
diff=numbers[amount]-numbers[1];
}
printf("%f\n",diff);
return diff;
}
float standard_dev(float numbers[], int amount)
{
float sdev=0,dev=0,sumsqr=0,variance=0;
int i;
float mean2=0;
mean2=mean(numbers,amount); // Here it is.
It calls the mean function, which actually prints something :) You can add boolean flag shouldPrint to functions and pass it as true when you want to print it.
Also, this problem is easily solvable with simple debugging your code...if actually looking at it doesn't seem to help...

Resources