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

The four part problem is below. I cannot think of any inputs that would result i

ID: 3626861 • Letter: T

Question

The four part problem is below. I cannot think of any inputs that would result in an outcome other than what is suggested in the problem. I tried using 5 billion and other werid "boundry" type numbers and I still get the same answers. I find it unlikely the answer is yes to all four parts...help?

This is in C.

/* Problem 1: Answer the questions below. For each answer, explain whether
the expression shown evaluates to true (non-zero) or false (zero). For example,
you'd say that

(n1 < n2) == (-n1 > -n2)

always evaluates to true or, for some values, might evaluate to false. The
easiest way to show that it might evaluate to false is to provide the values
and then explain. In general, of course, the expressions are more interesting
if they somethings evaluate to false.
*/

#include <stdio.h>
#include <limits.h>

int main() {

char string[10];
int n1 = /*input your choice*/;
int n2 = /*input your choice*/ ;

unsigned int u1 = (unsigned int) n1;
unsigned int u2 = (unsigned int) n2;

/* Is result always 1? Explain. */
int result = (n1 < n2) == (-n1 > -n2);
printf("(%d < %d) == (-%d > -%d) evaluates to %d ",
n1, n2, n1, n2, result);


/* Is result always 0? Explain. */
result = ~n1 + ~n2 == ~(n1 + n2);
printf("(~%d + ~%d) == ~(%d + %d) evaluates to %d ",
n1, n2, n1, n2, result);

/* Is result always 1? Explain. */
result = ((n1 >> 1) << 1) <= n1;
printf("((%d >> 1) << 1) <= %d evaluates to %d ",
n1, n1, result);

/* Is result always 1? Explain. */
/** On left hand side, the expression (int) (u1 - u2) means that
the subtraction of u2 from u1 is done and then this result is
cast to an int, that is, a signed int.
**/
result = ((int) (u1 - u2)) == -(n2 - n1);
printf("((int) (%u - %u)) == -(%d - %d) evaluates to %d ",
u1, u2, n2, n1, result);

gets(string);
return 0;
}

Explanation / Answer

#include <limits.h>

char string[10];

int n1;

/*input your choice*/;

scanf("%d",&n1);

int n2; /*input your choice*/ ;

scanf("%d",&n2);

unsigned int u1 = (unsigned int) n1;

unsigned int u2 = (unsigned int) n2;

cout<<(n1 >> 1)<<endl;

/*

For any input values the given expression

if the first condition is true then second condition also true

similarly if first condition is false then second con also

false thus these two expressions results same value hence == results in 1 always*/

int result = (n1 < n2) == (-n1 > -n2);

printf("(%d < %d) == (-%d > -%d) evaluates to %d ",

n1, n2, n1, n2, result);

/*

For any input values

The result of first (~n1+~n2) is true then second expression is false similarly vise versa therefore the result of expression is always 0*/

result = ~n1 + ~n2 == ~(n1 + n2);

printf("(~%d + ~%d) == ~(%d + %d) evaluates to %d ",n1, n2, n1, n2, result);

/*

For any input values

The result of first expression is true then second expression is true similarly vise versa therefore the result of expression is always 1*/

result = ((n1 >> 1) << 1) <= n1;

printf("((%d >> 1) << 1) <= %d evaluates to %d ",

n1, n1, result);

/*For any input values

The result of first expression is true then second expression is true similarly vise versa therefore the result of expression is always 1*/

/** On left hand side, the expression (int) (u1 - u2) means that

the subtraction of u2 from u1 is done and then this result is

cast to an int, that is, a signed int.

**/

result = ((int) (u1 - u2)) == -(n2 - n1);

printf("((int) (%u - %u)) == -(%d - %d) evaluates to %d ",

u1, u2, n2, n1, result);

system("pause");

gets(string);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote