Problem: Given two non-negative integers as input, square the smaller of the two
ID: 3649241 • Letter: P
Question
Problem: Given two non-negative integers as input, square the smaller of the two values and multiply thelarger by 2. Make no changes when the values are the same.
NOTE: THE USE OF SELECTION IS PROHIBITED ON THIS ASSIGNMENT. (Do not use If Else, while loops, for loops, <, >, =, ||)
Needs to output
Example Execution #1:
Enter the x and y values: 4 5
Resulting values: [16, 10]
Example Execution #2:
Enter the x and y values: 5 4
Resulting values: [10, 16]
Example Execution #3:
Enter the x and y values: 4 4
Resulting values: [4, 4]
Example Execution #4:
Enter the x and y values: 0 0
Resulting values: [0, 0]
I can get example 1 and 2 to output correctly however output 3 and 4 I can't think of the logic for it let alone the code so any help will be much appreciated. When Example 3 and 4 are inputed it returns Resulting values: [24, 24] and arithmatic error
Explanation / Answer
Exponentiating by squaring is a general method for fast computation of large integer powers of a number. Some variants are commonly referred to as square-and-multiply algorithms or binary exponentiation. In additive notation the appropriate term is double-and-add. These can be of quite general use: for example in modular arithmetic or powering of matrices. Not being able to use an 'if' statement is harsh, nearly ridiculous really, but at least if forces you into some creative thinking. My example below should give you an idea of what you need to do. I know that 'while' is also on your list of banned statements. I only used it to make my code easier to test. Without an 'if' statement you'll find it hard, maybe impossible given the max score of 555, to do any input validation and ensure you don't index outside the bounds of the array. A max value of 511 for A, for example, would give you a chance to do something. As it is you're going to be at the mercy of the user, hoping they input valid values. /* * Determine grade based on score: * * A = 555-470 * B = 469-415 * C = 414-360 * D = 359-305 * */ #include #include #define MAX_GRADE 555 #define MIN_A 470 #define MIN_B 415 #define MIN_C 360 #define MIN_D 305 char grade[MAX_GRADE+1]; int main(int argc, char *argv[]) { float score; /* initialize */ memset(grade,'F',MIN_D); memset( &grade[MIN_D], 'D', MIN_C - MIN_D ); memset( &grade[MIN_C], 'C', MIN_B - MIN_C ); memset( &grade[MIN_B], 'B', MIN_A - MIN_B ); memset( &grade[MIN_A], 'A', MAX_GRADE - MIN_A + 1 ); /* get scores and look up grades */ while (1) { printf("> "); scanf("%f",&score); printf("%c ",grade[(int)(score + 0.5)]); } return 0; } #if 0 Sample run: > 452.5 B > 359.8 C > 555 A > 469 B #endifRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.