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

1. Explain the output of the following program. Does interchanging the arguments

ID: 3557942 • Letter: 1

Question

1. Explain the output of the following program. Does interchanging the arguments in the last printf () statement make any difference?

#include<stdio.h>

int main (void)

{

char a=127, b=128;

printf ("%5d %5d %5d ", a, a+1, b);

printf (%5d %5d %5d %5d ", a, a += 1, b, a);

return 0;

}

output:

127 128   -128

127 -128    -128   -128

2. Write a function, timeUpdate ( ), which is passed three int parameters, hour, min, sec, representing the current time and returns the time updated by one second. The function should work with a 24-hour clock where 4:30 a.m. is represented by 4:30 and 4:30 p.m. is represented by 16:30.

3. Answer the following questions regarding an array called num.

a) Name the fourth element from the beginning of the array.

b) Display the value of the fith element of the array.

c) Input a value into second element of array num.

d) Initialize each of the 10 elements of the array to 8

e) Total elements of array num into summation variable sum.

f) Copy the contents of array num into array f, also of size SIZE.

4. As a practical joke, a friend gives you an int array for your birthday. As if that were not bad enough, your friend tell you that the array contains almost all 0's except for a small string of consecutive 1's contained somewhere in the middle. Overwhelmed by the novelty of this you decide to write a function that will print out the location of the first 1 in the array, the location of the last one in the array, and the total number of 1's in the list of consecutive 1 values. Given below is the function prototype:

void joke (int num [ ], int max);

where num is the array of int values and max contains the number of elements in the array.

5. Write a preprocessor directive to accomplish each of the following:

a) Define macro SUM that will sum two numbers.

b) Define macro MIN2 that will determine the smallest of two numeric values.

Explanation / Answer

1)
printf ("%5d %5d %5d ", a, a+1, b);
in this printf %5d is printing integer with 5 width space.
a+1 is 128 so it is printing 128
but an range of char( 8 bit ) is -128 to 127 so char b is printed as -128 folding.

printf (%5d %5d %5d %5d ", a, a += 1, b, a);
in this a+=1 changes the value of char a to a+1 so it is printing -128 .
the difference in the output of two statements lies in a+1 and a=a+1

** interchanging arguments will not make any difference

2)
void timeUpdate(int hour,int min,int sec)
{
if(sec==59)
{
sec=0;
min=min+1;
}
else
sec+=1;
if(min==60)
{
min=0;
hour=hour+1;
}

if(hour==24)

{

hour=0;

}

}

3)

a)num[3];

b) printf("%d",num[4]);

c) scanf("%d",&num[1]);


d) int i;
for(i=0; i<10; i++)
num[i] = 8;

e)int sum = 0;
for(i=0; i<10; i++)
sum = sum + num[i];

f)for(i=0; i<10; i++)
f[i] = num[i];

4)
void joke (int num [ ], int max)
{
int start = -1;
int end = -1;
int count = 0;
int i;
for(i=0; i<max; i++)
{
if(num[i] == 1)
{
if(start== -1) start = i;
count = count + num[i];
}
if(num[i]==1 && num[i+1] == 0)
{
if(end== -1) end= i;
break;
}
}
printf(" Starting index is %d",start);
printf(" Ending index is %d",end);
printf(" Stum of Total values is %d",count);
}

5)
a)#deinfe SUM(a,b) (a+b)
b)#deinfe MIN2(a,b) (a<b?a:b)