I tried running the program but it's not running correctly. The problem is at the functions, but I don't know exactly where. I declared the functions first, then I tried calling them in main. However, I'm not sure that's the case. I think the problem is at the function definition ? But I have no clue what else to do. If anyone could look at it and point it out to me that would be great.
#include <stdio.h>
#include <math.h>
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
float computeMonthlyInterest(float p, float YearRate);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
void printTable(float MonthlyPayment, float p, float YearRate,float YearTerm);
int main()
{
float n, p, r, MonthlyPayment, YearRate, YearTerm;
printf("Enter the loan amount: ");
scanf("%f", &p);
if (p <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (p <= 0.0)
{
printf("\nEnter the loan amount: ");
scanf("%f", &p);
}
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
if (YearRate <= 0.0 || YearRate > 30.0)
printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n");
while (YearRate <= 0.0 || YearRate > 30.0)
{
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
}
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
if (YearTerm <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (YearTerm <= 0.0)
{
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
}
float computeMonthlyInterest(float p, float YearRate);
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
return 0;
}
float computeMonthlyPayment(float p, float YearRate, float YearTerm)
{
float r = YearRate/12;
float n = YearTerm*12;
float MonthlyPayment = 0;
MonthlyPayment = (r*p)/1-((1+r)/n);
return MonthlyPayment;
}
float computeMonthlyInterest(float p, float YearRate)
{
float r = 0;
r = ((YearRate)/12)/12;
return r;
}
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n)
{
printf("LOAN INFORMATION\n");
printf("-----------------------\n");
printf("Initial loan amount: %.2f\n", p);
printf("Annual interest rate: %.3f\n", YearRate);
printf("Monthly interest rate: %.3f\n", r);
printf("Term of loan (years): %f\n", YearTerm);
printf("Term of loan (months): %f\n", n);
printf("Monthly payment amount: %.2f\n", MonthlyPayment);
}
Take a look at lines 48..50, in main().
int main()
{
// ...
float computeMonthlyInterest(float p, float YearRate);
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
// ...
}
Those are the functions' prototypes, not how you actually call them. You're supposed to call them the same way you call printf() or scanf(), by passing them the appropriate parameters. As it stands now, you're just redeclaring the functions in main(), and not actually calling them. Try something like this:
int main()
{
// ...
r = computeMonthlyInterest(p, YearRate); // r should be labeled better.
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);
// ...
}
This still isn't 100% fixed, though, because n isn't being initialised. While printLoanInfo() says what it's for (this is also how I identified r), you forgot to set it in main(). So..
int main()
{
// ...
r = computeMonthlyInterest(p, YearRate);
n = YearTerm * 12; // You did this in computeMonthlyPayment(), but not here.
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);
// ...
}
So, with those changes in mind, your main() should probably look like this:
int main()
{
float n, p, r, MonthlyPayment, YearRate, YearTerm;
printf("Enter the loan amount: ");
scanf("%f", &p);
if (p <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (p <= 0.0)
{
printf("\nEnter the loan amount: ");
scanf("%f", &p);
}
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
if (YearRate <= 0.0 || YearRate > 30.0)
printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n");
while (YearRate <= 0.0 || YearRate > 30.0)
{
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
}
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
if (YearTerm <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (YearTerm <= 0.0)
{
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
}
// Remove these lines:
// float computeMonthlyInterest(float p, float YearRate);
// float computeMonthlyPayment(float p, float YearRate, float YearTerm);
// void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
// Add these lines:
r = computeMonthlyInterest(p, YearRate);
n = YearTerm * 12;
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);
return 0;
}
The problem is not the function definitions. It is the fact that, in main(), you have copy and pasted these lines.
float computeMonthlyInterest(float p, float YearRate);
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
These are function declarations which inform the compiler on HOW to call the functions, but do not actually call them.
If you intend to actually call them, you will need to do something like
variable1 = computeMonthlyInterest(p, YearRate);
variable2 = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);
Essentially this removes the type specification from the function arguments and passes actual values. variable1 and variable2 are variables that you will need to define (possibly with more meaningful names) before these statements to hold returned values. Presumably you will also need to write more code in main() to use those variables.
Keeping in mind what the other answers have said concerning calling your functions correctly. The code below should work. I had to rework your computeMonthlyPayment, since it was calculating incorrectly.
#include <stdio.h>
#include <math.h>
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
float computeMonthlyInterest(float p, float YearRate);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
void printTable(float MonthlyPayment, float p, float YearRate,float YearTerm);//why are you defining this if it doesn't exist?
int main()
{
float n=0, p, r=0, MonthlyPayment=0, YearRate, YearTerm;
printf("Enter the loan amount: ");
scanf("%f", &p);
if (p <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (p <= 0.0)
{
printf("\nEnter the loan amount: ");
scanf("%f", &p);
}
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
if (YearRate <= 0.0 || YearRate > 30.0)
printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n");
while (YearRate <= 0.0 || YearRate > 30.0)
{
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
}
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
if (YearTerm <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (YearTerm <= 0.0)
{
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
}
printLoanInfo(p,MonthlyPayment,YearRate,r,YearTerm,n);
}
float computeMonthlyPayment(float p, float YearRate, float YearTerm)
{
float r = YearRate/(12*100);
float n = YearTerm*12;
return (p*r*pow(1 + r, n)) / (pow(1 + r, n) - 1); // just return the calculation
}
float computeMonthlyInterest(float p, float YearRate)
{
float r = 0;
return (r = ((YearRate)/12)/12);
}
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n)
{
printf("LOAN INFORMATION\n");
printf("-----------------------\n");
printf("Initial loan amount: %.2f\n", p);
printf("Annual interest rate: %.3f\n", YearRate);
printf("Monthly interest rate: %.3f\n", computeMonthlyInterest(p,YearRate)); //call function computeMonthlyInterest
printf("Term of loan (years): %.0f\n", YearTerm);
printf("Term of loan (months): %.0f\n", (YearTerm * 12));//simple calc for number of months
printf("Monthly payment amount: %.2f\n", computeMonthlyPayment(p,YearRate,YearTerm)); // call function computeMonthlyPayment
}
Related
I am new to c and I am trying to make a calculator that asks if you want to calculate the standard deviation of a set or the average of a set. However when I run the code I get the error seen in the picture. Error screen. I don't know how to correct it. I believe I have also made some mistakes throught the code so if you see anything else a miss would you mind pointing it out. Thank you.
#include <stdio.h>
#include <math.h>
float StandardDev(float data[]);
float Average(float data[]);
float Average(float data[]) {
int n;
float num[100], sum = 0.0, avg;
avg = sum / n;
return 0;
}
float StandardDev(float data[]){
float sum = 0.0, mean, SD = 0.0;
int i, n;
for (i = 0; i < n; ++i) {
sum += data[i];
}
mean = sum / n;
for (i = 0; i < n; ++i) {
SD += pow(data[i] - mean, 2);
}
return sqrt(SD / 10);
}
int main()
{
int n, i;
float num[100], sum = 0.0, c;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n < 1) {
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
printf("%d. Enter number: ", i + 1);
scanf("%lf", &num[i]);
sum += num[i];
}
printf("Do you want to calculate the Standard Deviation(1) of a set or find the Average(2) of a set? ");
scanf("%u", &c);
if(c==1){
printf("The Standard Deviation of your set is %f", StandardDev);
}
else{
printf("The average of your set is %f", Average);
}
return(0);
}
You declared an array with the element type float
float num[100], sum = 0.0, c;
So you need to use the conversion specifier %f instead of %lf. The last is designed for objects of the type double
scanf("%f", &num[i]);
In this call of scanf
scanf("%u", &c);
you are using the conversion specifier %u designated for objects of the type unsigned int with an object of the type float
Declare the variable c like
unsigned int c;
In these calls of printf
printf("The Standard Deviation of your set is %f", StandardDev);
printf("The average of your set is %f", Average);
You are trying to output function pointers instead of results of the function calls.
It seems you mean something like the following
printf("The Standard Deviation of your set is %f", StandardDev( num ));
printf("The average of your set is %f", Average( num ) );
Pay attention to that the program will have undefined behavior at least because you are using uninitialized variables in the functions like for example
int n;
float num[100], sum = 0.0, avg;
avg = sum / n;
//...
In this code I thought I would get the result of calculation x / y and x - y but the program shows 0 for i and j. What is wrong?
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It is undefined behavior, when you call the calculate() function within the same printf and i as well as j are calculated within that printf (same function). By the way, it is not a good idea to use global variables ( i, j ) ... For test purpose only, you could calculate() before the next printf of i and j.
You can test that behavior with:
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f", calculate(a, b));
printf(" %f %f\n", i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It might be related to the parsing, reference, and execution order of the arguments in printf function. The printf function uses the arguments right-to-left direction. You can easily check the order through below codes.
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
//printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
printf("\nThe results are: %f %f %f\n", i, j, calculate(a, b));
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
void power(float P,float R,float n,float A);
void main()
{
float A,P,R,n;
clrscr();
printf("Enter principal amount:");
scanf("%f",&A);
printf("Enter rate of interest:");
scanf("%f",&R);
printf("Enter number of years:");
scanf("%f",&n);
power(A,R,P,n);
getch();
}
void power(float A,float R,float P,float n)
{
int i;
for(i=1;i<=n;i++)
{
A=1+(R*0.01);
A=A*i;
A=A*P;
printf("For year %d, C.I =%f\n",i,A);
}
}
I put
Principal = 2000
Rate of interest=3
Years=3
I am getting the value of compound interest in negative. The error is in A=A*P; The compound interest must be calculated for each year without using the power function
#include<stdio.h>
void power(float P,float R,float n,float A);
void main()
{
float A,P,R,n;
// clrscr();
printf("Enter principal amount:");
scanf("%f",&A);
printf("Enter rate of interest:");
scanf("%f",&R);
printf("Enter number of years:");
scanf("%f",&n);
power(A,R,P,n);
// getch();
}
void power(float A,float R,float P,float n)
{
int i;
float intr=0;
for(i=1;i<=n;i++)
{
intr=0;
intr=A*(R/100);
A=A+intr;
// A=A*P;
printf("For year %d, C.I =%f\n",i,intr);
}
}
#include<stdio.h>
float length() {
float length;
printf("Input Length: ");
scanf("%f", &length);
return length;
}
float width(){
float width;
printf("Input Width: ");
scanf("%f", &width);
return width;
}
float area(){
float area=length * width;
return area;
}
float perimeter(){
float perimeter=(length*2) + (width*2);
return perimeter;
}
int main(){
printf("Area = %f\n", area());
printf("Perimeter = %f\n", perimeter());
}
Invalid Operand under Area and Perimeter
skafhasfhsafsakhfsakfhasfkhsafksahfakshfaskfhasfkshfaskfhsfashfkashfasfhasfhasfksahfasfasfasfasfsfsfsfas
length and width in area and perimeter are computed as function pointers, for which multiplication is not defined.
Here is quick fix:
#include <stdio.h>
float width(){
float width;
printf("Input Width: ");
scanf("%f", &width);
return width;
}
float length(){
float length;
printf("Input Length: ");
scanf("%f", &length);
return length;
}
float area() {
return length() * width();
}
float perimeter(){
float perimeter=(length()*2) + (width()*2);
return perimeter;
}
int main(){
printf("Area = %f\n", area());
printf("Perimeter = %f\n", perimeter());
}
but here is how would I do it:
#include <stdio.h>
float width(){
float width;
printf("Input Width: ");
scanf("%f", &width);
return width;
}
float length(){
float length;
printf("Input Length: ");
scanf("%f", &length);
return length;
}
float area(float length, float width) {
return length * width;
}
float perimeter(float length, float width){
return length*2 + width*2;
}
int main(){
float l = length();
float w = width();
printf("Area = %f\n", area(l, w));
printf("Perimeter = %f\n", perimeter(l, w));
}
I am getting no output for the grosspay and taxespaid functions for this programming assignment I am writing to keep track of employee payroll. I am just wondering if I am properly passing parameters and passing by reference, which is the method we are supposed to use. My class doesn't meet often and the book sucks so I would appreciate any advice.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
void userinput(char name[][20], int hoursworked[], float rate[]);
void calculatepay(float rate[], int hoursworked[]);
float tax(float grosspay[]);
float totalpay(float grosspay[], float taxespaid[]);
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]);
int main(){
int hoursworked[]={};
float rate[]={};
char name[][20]={};
float grosspay[]={};
float taxespaid[]={};
float sum;
userinput(name, hoursworked, rate);
calculatepay(rate, hoursworked);
sum = tax(grosspay);
totalpay(grosspay, taxespaid);
print(name, rate, hoursworked, grosspay, sum, taxespaid);
return (0);
}
void userinput(char name[][20], int hoursworked[], float rate[]){
for(int i=0; i<SIZE; i++){
printf("Enter name: ");
scanf("%s", &name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Enter hours worked: ");
scanf("%d", &hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Enter hourly rate: ");
scanf("%f", &rate[i]);
if(rate[i] == -1){
break;
}
puts("\n");
}
}
void calculatepay(float rate[], int hoursworked[]){
float grosspay[]={};
for(int i=0; i<SIZE; ++i){
if(hoursworked[i]<=40){
grosspay[i] = rate[i] * hoursworked[i];
}
else{
grosspay[i] = (40*rate[i])+((hoursworked[i] - 40)*1.5*rate[i]);
}
}
}
float tax(float grosspay[]){
float taxespaid[]={};
for(int i=0; i<SIZE; ++i){
taxespaid[i] = grosspay[i]*0.2;
return taxespaid[i];
}
return 0;
}
float totalpay(float grosspay[], float taxespaid[]){
float sum = 0.0;
for (int i=0; i<SIZE; i++)
{
sum=sum+(grosspay[i] - taxespaid[i]);
}
return sum;
}
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]){
for(int i=0; i<SIZE; i++){
printf("Pay to: %s\n", name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Hours worked: %d\n", hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Hourly rate: %.2f\n", rate[i]);
if(rate[i] == -1){
break;
}
if(hoursworked[i]<=40){
printf("Gross Pay: %.2f\n", grosspay[i]);
}
else
{
printf("Gross Pay: %.2f\n", grosspay[i]);
printf("Base Pay: %.2f\n", rate[i]*40);
printf("Overtime Pay: %.2f\n", grosspay[i] - (rate[i]*40));
}
printf("Taxes paid: %.2f\n", taxespaid[i]);
printf("Net pay: %.2f\n\n", grosspay[i] - taxespaid[i]);
}
printf("Total paid to all employees: %.2f\n", sum);
}
I hate to be the bearer of bad news, but there is no pass-by-reference in C. The only method of parameter passing that C defines is pass-by-value, in 6.5.2.2p4:
In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument.
Perhaps you intended to ask about C++, in which case... No, you're not using pass-by-reference.
Otherwise, if you intended to ask how C mimics pass-by-reference, it is by using pointers (that is, the parameters have pointer types and pointer values).
In void userinput(char name[][20], int hoursworked[], float rate[]);, for example, name, hoursworked and rate are all pointer types. The values that are passed will be pointer values. The pointer values point into the objects that are to be modified, so the same outcome is possible (that is, the changes that are made to the array are still visible once the function returns).
In case you're curious as to why this doesn't technically qualify as pass-by-reference, it's because the unary *pointer or array[subscript] (or pointer[subscript] operators are required to change the value pointed to, where-as when pass-by-reference is used you should merely be able to assign a value without those like: array = value;. It makes more sense if you look at it as a test in writing a swap function:
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
If you try calling this function, you will notice that it hasn't successfully swapped its arguments. If you were to use pass-by-value, those three lines would have swapped its arguments. For example, in C++:
void swap(int& x, int& y) {
int temp = x;
x = y;
y = temp;
}
This will swap the arguments. All I had to change was the function signature; I didn't have to change the way the function is used or the code in the function itself... That is pass-by-reference.
When I declared the variables before main() and initialised the arrays with SIZE then it worked fine :)
#define SIZE 5
void userinput(char name[][20], int hoursworked[], float rate[]);
void calculatepay(float rate[], int hoursworked[]);
float tax(float grosspay[]);
float totalpay(float grosspay[], float taxespaid[]);
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]);
int hoursworked[SIZE];
float rate[SIZE];
char name[SIZE][20];
float grosspay[SIZE];
float taxespaid[SIZE];
float sum;
int main(){
userinput(name, hoursworked, rate);
calculatepay(rate, hoursworked);
sum = tax(grosspay);
totalpay(grosspay, taxespaid);
print(name, rate, hoursworked, grosspay, sum, taxespaid);
return (0);
}
void userinput(char name[][20], int hoursworked[], float rate[]){
int i;
for(i=0; i<SIZE; i++){
printf("Enter name: ");
scanf("%s", &name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Enter hours worked: ");
scanf("%d", &hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Enter hourly rate: ");
scanf("%f", &rate[i]);
if(rate[i] == -1){
break;
}
puts("\n");
}
}
void calculatepay(float rate[], int hoursworked[]){
float grosspay[SIZE];
int i;
for(i=0; i<SIZE; ++i){
if(hoursworked[i]<=40){
grosspay[i] = rate[i] * hoursworked[i];
}
else{
grosspay[i] = (40*rate[i])+((hoursworked[i] - 40)*1.5*rate[i]);
}
}
}
float tax(float grosspay[]){
int i;
float taxespaid[SIZE];
for(i=0; i<SIZE; ++i){
taxespaid[i] = grosspay[i]*0.2;
return taxespaid[i];
}
return 0;
}
float totalpay(float grosspay[], float taxespaid[]){
float sum = 0.0;
int i;
for (i=0; i<SIZE; i++)
{
sum=sum+(grosspay[i] - taxespaid[i]);
}
return sum;
}
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]){
int i;
for(i=0; i<SIZE; i++){
printf("Pay to: %s\n", name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Hours worked: %d\n", hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Hourly rate: %.2f\n", rate[i]);
if(rate[i] == -1){
break;
}
if(hoursworked[i]<=40){
printf("Gross Pay: %.2f\n", grosspay[i]);
}
else
{
printf("Gross Pay: %.2f\n", grosspay[i]);
printf("Base Pay: %.2f\n", rate[i]*40);
printf("Overtime Pay: %.2f\n", grosspay[i] - (rate[i]*40));
}
printf("Taxes paid: %.2f\n", taxespaid[i]);
printf("Net pay: %.2f\n\n", grosspay[i] - taxespaid[i]);
}
printf("Total paid to all employees: %.2f\n", sum);
}