Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CISC 260 Machine org C Chegg Study Guided Sc Sakai@UD 17S-CISC26 X /content/atta

ID: 3827519 • Letter: C

Question

CISC 260 Machine org C Chegg Study Guided Sc Sakai@UD 17S-CISC26 X /content/attachment/876b1554-7dd4-4931-809-8822ed9e19d5/Assign a0ea-d162210 ffab6/cis260s17 hw5.pdf Q O C Secure aka Apps Facebook N Netflix YouTube a Amazon.com: Online e SakaieUD My work D CISC220 Data Struct Library Genesis ome I chegg.com CISC 260 Machine organization and Assembly Language Problem 4. At a candy shop, the 1st candy costs 10cents. Each sequent candy costs 10 cents more than the previous one. For one dollar, how many candies can you purchase at most? Here is a program written in C to answer that question 1.0; float un Left float cost int numCandies 0 for (cost 0.1; cost fundLeft; cost 0.1) num Candies++; fund Left X fundLeft cost; printf Sd candies; Sf left over n", numCandies, fundLeft); Run the program and report the result. If the result is not what is expected, explain why? Modify the program so that the correct answer can be obtained. Present your modified program. 2:12 PM Type here to search 4/27/2017

Explanation / Answer

#include<stdio.h>
#include <math.h>
int main()
{
//Declare all required variables

double fundLeft = 1.0;
double cost;
int numCandies = 0;

//Changes in if condition
for(cost=0.1;(fundLeft-cost)>=-0.1;cost=cost+0.1)
{

  numCandies++;
  fundLeft = fundLeft - cost;
}

//Changes in print statement
printf("%d candies; %f left over ",numCandies,fabs(fundLeft));

return 0;
}

-----------------
Explanation:
-----------------

The problem with the given code is comparison issue between two floating points.
Basically when fundLeft reaches 0.400000 and cost reaches 0.400000.

Performing substraction will results in -0.00000 this is due to compiler consideration.
Inorder to overcome this check we must change the if condition.

Ideally with the given problem description the number of candies would be 4. With the earlier code it will give 3
and with the modified code it will give 4.