Consider the following C program include void divideint x in

Consider the following C program: #include void divide(int x, int div) {int tmp = 0; if (div == 0){printf(\"Input parameter: div is zero. \ \"); printf(\"Please check the parameters and call again. \ \");} else tmp = x/div; printf (\" The result is: %d\ \", tmp); > int main(){divide(4, 2); divide(4, 0); return 0;} When this program is compiled and run, what will be printed out? Justify your answer. Illustrate with examples two common programming errors in C that are not caught by the compiler. Write C code (using a for loop) to create and print an array GS that stores the first 10 terms of the geometric sequence that halves each time: {1/2, 1/4, 1/8, 1/16, ... 1/1024}

Solution

a)

The result is: 2

Input parameter: div is zero.

please check the parameters and call again.

The result is: 0

first time as div is not zero it executes else condition and then prints the result.

second time as div is zero it executes if conditions and prints to printf statements and prints the printf statement after else condition

b)

Overflow Numeric calculations which produce a result too large to represent.

Divide by Zero Dividing a numeric value by zero.

these were some of the errors which are not caught during compile time but are caught during run time.

c)

#include<stdio.h>

int main(){

float a[10];
int i=0;

for(i=0;i<10;i++)
{
if(i==0)
a[i]=(float)1/2;
else
a[i]=a[i-1]/2;
}
for(i=0;i<10;i++)
{
printf(\"a[%d] is %f \ \",i,a[i]);
}
return 0;
}

 Consider the following C program: #include void divide(int x, int div) {int tmp = 0; if (div == 0){printf(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site