Related
I have a problem with the compiler and I must have some logical mistakes (C language)
I want to have one void function that gives me the maximum price, the minimum price and the average of a table with maximum 100 prices. And if the user gives price=-1 I want the program to end!
Here is my code:
#include <stdio.h>
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
int main()
{
int i=0,count=0;
float prc[100],mo;
for(i=0;i<=99;i++)
{
printf("Enter price:");
scanf("%f",&prc[i]);
if(prc[i]==-1)
{
break;
}
count++;
}
int min=prc[0];
int max=prc[0];
void function(float prc,int i,float *min,float *max,float *mo,int count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f",min,max,mo);
return 0;
}
void function(float pin[],int j,float *min,float *max,float *mo,int cnt)
{
float sum;
for(j=0;j<=cnt;j++)
{
if(pin[j]<*min)
{
min=pin[j];
}
if(pin[j]>*max)
{
*max=pin[j];
}
sum=+pin[j];
}
*mo=sum/j;
}
Looks like you have lots of little mistakes (missing dereference, using a variable out of scope, wrong operator, etc.):
#include <stdio.h>
void function(float pin[], float *min, float *max, float *mo, int cnt);
int main()
{
int i=0, count=0;
float prc[100], mo;
for (i=0; i<=99; i++)
{
printf("Enter price:");
scanf("%f", &prc[i]);
if (prc[i] == -1)
{
break;
}
count++;
}
float min=prc[0];
float max=prc[0];
function(&prc[0], &min, &max, &mo, count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f", min, max, mo);
return 0;
}
void function(float pin[], float *min, float *max, float *mo, int cnt)
{
float sum = 0.0f;
for (int j=0; j < cnt; j++)
{
if (pin[j] < *min)
{
*min = pin[j];
}
if (pin[j] > *max)
{
*max = pin[j];
}
sum += pin[j];
}
*mo = sum / cnt;
}
There are a few mistakes in the function :
I have modified the code and looks to be fine except the function argument mo , decide whether to declare it as a float pointer or a float
Look at the declaration inside main, u are calling a function but the syntax is for declaration.
used mo as argument but mo is defined as float variable whereas the function accepts a pointer as per the declaration and there are a few warning related to the types
Declared ints but printing them using format specifiers for float %f
include
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
int main()
{
int i=0,count=0;
float prc[100],mo;
for(i=0;i<=99;i++)
{
printf("Enter price:");
scanf("%f",&prc[i]);
if(prc[i]==-1)
{
break;
}
count++;
}
int min=prc[0];
int max=prc[0];
function(prc,i,min,max,mo,count);
printf("Minimum price is:%f Maximum price is:%f and Mo is:%f",min,max,mo);
return 0;
}
void function(float pin[],int j,float *min,float *max,float *mo,int cnt)
{
float sum;
for(j=0;j<=cnt;j++)
{
if(pin[j]<*min)
{
*min=pin[j];
}
if(pin[j]>*max)
{
*max=pin[j];
}
sum=+pin[j];
}
*mo=sum/j;
}
wrong use of pointer and type,use
float min=prc[0];
float max=prc[0];
function(prc,i,&min,&max,&mo,count);
You declare as:
void function(float pin[],int j,float *min,float *max,float *mo,int cnt);
So you must deliver a float-pointer but not a float number.
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
}
I'm trying to write a program to find the average of students' test scores. I'd also like to find the lowest score and drop it out when I calculate the average. I then want to display all 4 quiz scores for each student as well as the lowest score and the average.
This what I have wrote:
int findMin(int student, int scores[3][4]){
int i;
int min=scores[student][0];
for(i=0 ;i<4;i++){
if(scores[student][i]<=min)
min= scores[student][i];
}
return min;
}
float getAverage(int student, int min, int scores[3][4])
{
float tot=0.0;
int i;
float average;
for(i=0 ;i<4;i++)
{
tot=tot+scores[student][i];
}
tot=tot-min;
average=tot/3.0;
return average;
}
void printTable(int scores[3][4], int min[3], float avg[3])
{
int i,j;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
}
}
}
int main(){
int scores[3][4];
printf("Enter score for first sdudent:\n");
int j=0;
for(j=0;j<4;j++){
scanf("%d",& scores[0][j]);
}
printf("Enter score for second sdudent:\n");
j=0;
for(j=0;j<4;j++){
scanf("%d",& scores[1][j]);
}
printf("Enter score for third sdudent:\n");
j=0;
for(j=0;j<4;j++){
scanf("%d",& scores[2][j]);
}
int min[3];
float avg[3];
int i=0;
for(i=0;i<3;i++) {
min[i]=findMin(i, scores);
avg[i]=getAverage(i, min[i],scores);
}
printTable(scores, min, avg);
return 0;
}
It does not output the average and the main. What am I doing wrong?
I thik you missed the printf in the printTable function
#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);
}