Question

In: Computer Science

#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0;...

#include <stdio.h>
int main(void) {
float funds = 1.0, cost = 0.1;
int candies = 0;
while(cost <= funds) {
candies += 1;
funds -= cost;
cost += 0.1;
}
printf("%d candies with $%.2f left over.\n",candies,funds);
return 0;
}

When you compile and run this code you get 3 candies with $0.40 left over.

Without knowing how floating point numbers work in a computer, would this result be expected? Explain why or why not. Explain why this result, in fact, occurred.

Solutions

Expert Solution

In the computer system, the floating-point number is represented by using IEEE floating-point representation.

The float data type in Programming Language C is stored in the computer system by using IEEE single-precision floating-point representation as given below:

In single precision representation, 1 bit is used as a sign bit, 8 bits are used for the exponent, and 23 bits are used for the mantissa, and X-127 code is used.

The sign bit is stored in a single bit.

0 -> Positive

1 -> Negative

Yes the given output is expected by the given source code.

The statement by statement explanation of the given source code is given below:

#include <stdio.h>

int main(void)
{
//float type variable declaration and initialization
float funds = 1.0, cost = 0.1;
  
//integer type variable declaration and initialization
int candies = 0;
  
//while loop
while(cost <= funds)
{
//increment the candies by one
candies += 1;
  
//decrement the funds by cost value
funds -= cost;
  
//increment the cost by 0.1
cost += 0.1;
}
  
//display the value of candies and funds on the computer screen
printf("%d candies with $%.2f left over.\n",candies,funds);
return 0;
}

OUTPUT:

3 candies with $0.40 left over.


The while() loop will execute for three times.

The value of all three variable in each of the iteration will be:

Iteration 1:

candies = 1
funds = 0.90
cost = 0.20

Iteration 2:
candies = 2
funds = 0.70
cost = 0.30

Iteration 3:
candies = 3
funds = 0.40
cost = 0.40



Related Solutions

example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
#include <stdio.h> int main() { float sum_salary=0,sum_raise=0,sum_newsalary=0; while(1) { float salary,rate,raise,newsalary; printf("Enter Salary :"); scanf("%f",&salary); if(salary==-1)...
#include <stdio.h> int main() { float sum_salary=0,sum_raise=0,sum_newsalary=0; while(1) { float salary,rate,raise,newsalary; printf("Enter Salary :"); scanf("%f",&salary); if(salary==-1) { if(sum_salary==0) return 0; else { printf("Total Salary :%f Total raise :%f Total New Salary: %f",sum_salary,sum_raise,sum_newsalary); return 0; } } else { if(salary>=0 &&salary<30000) rate=7; else if(salary>=30000 &&salary<=40000) rate=5.5; else if(salary>40000) rate=4; raise=rate*salary/100; newsalary=salary+raise;    sum_salary=salary+sum_salary; sum_raise=raise+sum_raise; sum_newsalary=newsalary+sum_newsalary; printf("Salary :%f rate :%f Raise :%f New Salary: %f\n",salary,rate,raise,newsalary); } } } Hi can you please make sure that the following program will ask the user...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void)...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void) {      int count ; scanf("%d",&count);           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                            return 0; }
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT