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

C ONLY------- PART 1 Instruction: Write a SINGLE if/else statement that has 3 ru

ID: 3881292 • Letter: C

Question

C ONLY-------

PART 1

Instruction: Write a SINGLE if/else statement that has 3 rules and a default (else) case, in a function named "newNumber". The function will accept a single integer and will return another number.

The list below are the four cases (rules), the higher on the list the higher the priority. i.e. -7 returns 7, not 0.

Return 1 when the number is 0.

Return negative numbers as a positive.

Return 0 when numbers can be evenly be divided by 7.

for all other cases, return the same number (default case).

Hint: For finding the remainder of a number, use the modulus operation.

part 2:

Instruction: Write a switch statement accepting three integer parameters (int userInput, int numOne, int numTwo). The user input will dictate which operation will happen between the two integers.

1 = add

2 = subtract (numOne - numTwo)

3 = multiply

4 = division ( numOne/replaceZero(numTwo) )

If the userInput is not one of the above, return 0.

part 3:

This loop will find the overflow of a division operation.

Instruction: Create a function named overflow to calculate the how far the closest multiple of the divisor is relative to the dividend. If the divisor is greater than the dividend, the dividend is returned. Do not worry about having a divisor of 0, although any negative dividends should be converted to a positive

i.e.

the overflow of 25/6 is 30 - 25 = 5

the overflow of -14/9 is 18 - 14 = 4 (This is treated as overflow 14/9)

the overflow of 3/28 is 3

There is a skeleton of the control flow to get you started, you just need to fill in the missing sections. There is also an absolute value function required for the overflow calculation that must be completed.

Reminder: Dividend / Divisor

Explanation / Answer

Part1 Answer:

int newNumber(int num){
/*
We can write single line if/else statement using ternary operator or we can go with first option
as I wrote in comment section below
*/
/*if(num < 0)
return -(num);
else if(num == 0)
return 1;
else if(num % 7 == 0)
return 0;
else
return num;
*/
return num < 0 ?-(num) : num == 0 ? 1 : num % 7 == 0 ? 0 : num ;
}

Part 2 Answer:

int replaceZero(int numTwo){

/*This function returns 1 if numTwo is 0 otherwise it reurns numTwo*/
return num == 0 ? 1 : num ;
}
int newSwitch(int userInput, int numOne, int numTwo)
{

/*switch cannot take 3 arguments so I ahve written function for switch statement which will take arguments

and act as switch internally.

*/
switch(userInput){
case 1:
return numOne + numTwo;
break;
case 2:
return numOne - numTwo;
break;
case 3:
return numOne * numTwo;
break;
case 4:
return numOne/replaceZero(numTwo);
break;
default:
return 0;
  
}
}

Part3 Answer:

int overFlow(int dividend,int divisor){

int result,relative_value,overflow;

if( dividend < divisor){

return dividend;

}

else if(dividend == divisor)

return 0;

else{

result = ((((int)(abs(dividend)/divisor))+1) * divisor) - abs(dividend);

return overflow;

}

}