1

Should I change something in written code? Compiler says that everything is right — no errors or warnings.

  • You are building a new home and you have calculated exactly how much cement you need for the foundation.
  • Ideally you'd like to purchase this exact amount of cement, but the store only sells cement in 120-pound bags.
  • Each of these bags costs 45 dollars.

Please write a C program that calculates the cost of the cement you will have to purchase to build your foundation.

  • Your program should first read a decimal number representing the amount of cement needed (in pounds) for the foundations of your new home.
  • Your program should then display the total cost of the cement bags you have to purchase to have enough cement to build your foundation.
  • To make your program simpler, you are guaranteed that the amount of cement needed will NEVER be a multiple of 120.

My code so far:

#include <stdio.h>
#include <math.h>
int main(void) {
    int price=45, totalPrice, OneBag=120;
    float needed;
    do
    {
        printf("Enter the amount of cement you need for your foundation that is not dividable by 120: ");
        scanf("%f", &needed);
    } while (fmodf(needed, OneBag)==0);
    
    totalPrice = ((int)needed/OneBag+1)*(price);
    printf("Total cost of cement you will need for your foundation is %d", totalPrice);
    

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Welcome to SO. *you are guaranteed that the amount of cement needed will NEVER be a multiple of 120.* That does not mean that **you** have to ensure this. Also you should always check return value of `scanf`. – Gerhardh Jan 06 '22 at 16:17
  • 1
    Have you tested it? Does it give the right answer for 119, 121, 239, 241, 240.01, 239.99 as the amount of cement you need? – Jonathan Leffler Jan 06 '22 at 16:20
  • 4
    Re “Compiler says that everything is right”: No compiler will ever say that. The most a compiler will tell you is that it did not find any problems. That does not mean there are not problems it did not find. – Eric Postpischil Jan 06 '22 at 16:22
  • 1
    What makes you think this is somehow wrong? – S3gfault Jan 06 '22 at 16:25
  • @S3gfault I will give you few examples that bother me 1. in fmodf(needed, OneBag) I can use an integer as stated above but when I try to do it like this: needed%120==0 I get this "invalid operands to binary % (have 'float' and 'int')" does fmodf automatically change integer to float? 2. If I first do this: totalPrice = ((int)needed/OneBag+1) and then printf("%d", totalPrice*price); it's correct. But this: totalPrice = (needed/OneBag+1)*(price); printf("%d", totalPrice); isn't. – Andrej Bijelić Jan 06 '22 at 17:12
  • 1. I does seem like when calling a function it automatically casts type, but I'm not sure on that one. Either way I think it is better to explicitly convert types. 2. in the second case you don't cast needed to an int. – S3gfault Jan 06 '22 at 20:54

3 Answers3

1

To be honest, I don't see any real mistakes in your code, but in my opinion, there is room for improvement:

Price per bag and size of a bag, are both constants, which you can actually make clear in your code, this is more readable and it allows the compiler to optimize your code better.

You also don't actually have to check if the input is a multiple of 120, because it is a given that it is not.

There is also something called the ceil function (or ceilf if working with floats), which actually takes any number and increases upward to the nearest integer. Which is pretty useful for this assignment.

One last thing just because the compiler says it's all right, doesn't mean it actually is.

code:

#include <stdio.h>
#include <math.h>

const float price_per_bag = 45;
const float bag_size = 120;

int main(void) {
    float needed;
    printf("Enter the amount of cement needed:\n");
    scanf("%f", &needed);

    float price = ceilf(needed / bag_size) * price_per_bag;
    printf("total price: %i\n", (int)price);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
S3gfault
  • 308
  • 2
  • 9
0
#include <stdio.h>

int main(void){
      
   float totalC;
   int count=0, totalPrice, i ;
      
   scanf("%f", &totalC);

   for (i=0; i<totalC; i+=120)
        {
           count = count+1;   
         }
     

         totalPrice = count *45;
         printf("%d", totalPrice);
         return 0;

      }
Yudino
  • 1
  • 2
  • Your answer is only a piece of code, provided without context. To help other programmers improve, it would be better to add some explanations on what your code snippet is supposed to address and how. – Laurent Jospin Jan 21 '22 at 02:12
-1
#include<stdio.h>

int main(void){
   double reqCement;
   int bags;
   int amount;
   printf("Type amount of cement:");
   scanf("%lf",&reqCement);
   bags = (reqCement/120)+1;
   amount = bags*45;
   printf("Amount = %d", amount);
   return 0;
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 06 '22 at 06:39