Question

In: Computer Science

Output and Debugging Questions (10 marks each) [20 Marks] Note: Provide a copy of the code...

Output and Debugging Questions (10 marks each) [20 Marks]
Note: Provide a copy of the code and screen shot for the output in the solutions’
1. Trace the following program and write the exact output for the following inputs.
Explain each output. [10 Marks]
a. Input of an array { 20, 80 , 63, 89 }
b. Input of an array { 1, 2 ,3, 4}
c. Input of an array { 100, 200 ,300, 400}
d. Input of an array { -8, -9, -10, -11} ( Negative numbers)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int[] M = new int[4] ;
for (int i = 0; i <4; i++)
{
M[i] = Int32.Parse(Console.ReadLine());
}
for (int i = 0; i < M.Length; i++)
{
M[i] = M[i] + 10;
Console.Write((M[i] / 3) + " ");

}
Console.ReadKey();
}
}
}

Solutions

Expert Solution

Hi,

Please see the output and explanation of each output:

Input: { 20, 80 , 63, 89 }

For loop in the code will execute/show the output for each input array element.

For the first element - 20

M[i] = M[i] + 10; here First element will become 30 and then M[i] / 3 it will become 30/3 = 10

For the second element - 80

M[i] = M[i] + 10; here First element will become 90 and then M[i] / 3 it will become 90/3 = 30

For the third element - 63

M[i] = M[i] + 10; here First element will become 73 and then M[i] / 3 it will become 73/3 = 24. As this is Integer array so the output 24.33 will be show as 24

For the fourth element - 89

M[i] = M[i] + 10; here First element will become 99 and then M[i] / 3 it will become 99/3 = 33

===========================================================================

Input: { 1, 2 ,3, 4}

For loop in the code will execute/show the output for each input array element.

For the first element - 1

M[i] = M[i] + 10; here First element will become 11 and then M[i] / 3 it will become 11/3 = 3.66. As this is Integer array so the output 3.66 will be show as 3

For the second element - 2

M[i] = M[i] + 10; here First element will become 12 and then M[i] / 3 it will become 12/3 = 4

For the third element - 3

M[i] = M[i] + 10; here First element will become 13 and then M[i] / 3 it will become 13/3 = 4.33. As this is Integer array so the output 4.33 will be show as 4

For the fourth element - 4

M[i] = M[i] + 10; here First element will become 14 and then M[i] / 3 it will become 14/3 = 4.66. As this is Integer array so the output 4.66 will be show as 4

===========================================================================

Input: { 100, 200 ,300, 400}

For loop in the code will execute/show the output for each input array element.

For the first element - 1

M[i] = M[i] + 10; here First element will become 110 and then M[i] / 3 it will become 110/3 = 36.66. As this is Integer array so the output 36.66 will be show as 36

For the second element - 2

M[i] = M[i] + 10; here First element will become 210 and then M[i] / 3 it will become 210/3 = 70

For the third element - 300

M[i] = M[i] + 10; here First element will become 310 and then M[i] / 3 it will become 310/3 = 103.33. As this is Integer array so the output 103.33 will be show as 103

For the fourth element - 400

M[i] = M[i] + 10; here First element will become 410 and then M[i] / 3 it will become 410/3 = 136.66. As this is Integer array so the output 136.66 will be show as 136

===========================================================================

Input: { -8, -9, -10, -11}

For the first element - -8

M[i] = M[i] + 10; here First element will become -8+10 = 2 and then M[i] / 3 it will become 2/3 = 0

For the second element - -9

M[i] = M[i] + 10; here First element will become -9+10 = 1 and then M[i] / 3 it will become 1/3 = 0

For the third element - -10

M[i] = M[i] + 10; here First element will become -10+10 = 0 and then M[i] / 3 it will become 0/3 = 0

For the fourth element - -11

M[i] = M[i] + 10; here First element will become -11+10 = -1 and then M[i] / 3 it will become -1/3 = -0.33 and it will become as 0

============================================================================

Please find the code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] M = new int[4];//declare arary of int of size 4
            for (int i = 0; i < 4; i++)
            {
                M[i] = Int32.Parse(Console.ReadLine()); //Read the array from user
            }
            for (int i = 0; i < M.Length; i++)
            {
                M[i] = M[i] + 10; //Add the value to the array element by 10
                Console.Write((M[i] / 3) + " "); //divide the number and print the result

            }
            Console.ReadKey();
        }
    }
}

Thanks.


Related Solutions

Question 5 (10 marks) Python Language What is the output of the following code ? (2...
Question 5 Python Language What is the output of the following code ? (2 points) a, b = 0, 1 while b < 10: print b a, b = b, a+b B. Explain List Comprehension (2 points) Given v = [1 3 5] w = [ [2*x, x**2] for x in v] What is the content of w? c. What is tuple ?   What is the difference between tuple and list ? (2 points) D. What is a module ?  ...
INSTRUCTIONS: 1. The Assignment contains 3 questions (20 MARKS Each) with a total of 60 MARKS...
INSTRUCTIONS: 1. The Assignment contains 3 questions (20 MARKS Each) with a total of 60 MARKS 2. All questions must be answered electronically (No hand-written submissions) 3. Draw all the required diagrams using a drawing software application/tool e.g. Microsoft Visio. (No snapshots/screengrabs will be accepted). You can use one of the CASE tools you use in OOSAD if you wish. 4. Submit a Portable Document Format (PDF) as your final submission. No other file QUESTION ONE 1. Briefly describe the...
Q. 6 (10 marks) Each of the questions below is INDEPENDENT of the other. (Timelines are...
Q. 6 Each of the questions below is INDEPENDENT of the other. (Timelines are not required.) (a) Your sister has a $5,000 debt balance on her credit card that charges 18.5% interest compounded semi-annually. The monthly payment is 3% of the starting debt balance. Required: If your sister stops using the credit card for purchases, how many months (round up) will it take her to pay off the credit card balance? (Timeline not required.) (b) Your high school guidance counsellor...
Questions 1. [10 marks] For each of the following statements, identify whether the statement is true...
Questions 1. [10 marks] For each of the following statements, identify whether the statement is true or false, and explain why. Please limit each response to no more than 2 sentences. a) The probability that a continuous random variable takes a specific value is 0. b) Statistical inference is the process of drawing conclusions about unknown statistics by using known parameters. c) The terms “histogram” and “bar graph” are synonyms. d) If two events A and B are independent, then...
Question 2 Probability Questions: Each question is worth 2 marks: Total (10 marks)Business makes a profitBusiness...
Question 2 Probability Questions: Each question is worth 2 marks: Total Business makes a profitBusiness makes a lossInternational Manager.15.30Local Manager.10.45A. P (Local Manager) = B. P (Business makes a loss) = C. P (International Manager OR Business makes a profit) = D. P (Local Manager AND International Manager) = E. P (Business makes a loss | Manager is International) Probability:P(B)B)P(AB)|P(A)()()(ypxpyxp)()()(ypxpyxp)|()()|()()(yxpypxypxpyxp)()()()(yxpypxpyxp
D. Now, provide short answers to the following questions. (10 marks) i. At first patents might...
D. Now, provide short answers to the following questions. i. At first patents might seem like a deterrent to growth because in effect they restrict the use of new technology. Yet many economists believe that patents generate growth. Explain why. ii. Some economists argue that it is possible to raise the standard of living by reducing population growth. As an economist interested in incentives rather than coercion, what kind of policy would you recommend to slow population growth? C. Now,...
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
(Please provide references for answers) Q6. Write a short notes on (2*10=20 Marks) a) E- commerce...
(Please provide references for answers) Q6. Write a short notes on (2*10=20 Marks) a) E- commerce and its effects on the logistics activities b) Distribution Requirement Planning (Please provide references for answers)
Use Minitab to answer the questions. Make sure to copy all output from the Minitab: The...
Use Minitab to answer the questions. Make sure to copy all output from the Minitab: The U.S. Bureau of Labor Statistics publishes a variety of unemployment statistics, including the number of individuals who are unemployed and the mean length of time the individuals have been unemployed. For November 1998, the Bureau of Labor Statistics reported that the national mean length of time of unemployment was 14.5 weeks. The mayor of Chicago has requested the study on the status of unemployment...
• Based on the following code snippet and plan attributes, provide the output for Plan A,...
• Based on the following code snippet and plan attributes, provide the output for Plan A, Plan B and Plan C. Code Snippet • {{IF:[Copay]=Not Covered && [Coinsurance]=Not Covered, show ‘Not covered’}} • {{IF:[Copay]!=$0.00 && [Copay]!=Blank, show value from {{Copay}} copayment}} • {{IIF:[Copay]!=$0.00 && [CopayFrequency]!=Blank, show {{Copay}} copayment/{{CopayFrequency}}}} • {{IF:[CopayMax]=$0.00 OR [Copay]=$0.00, show No Charge}} • {{IF:[[CopayMax]!=$0.00 && [CopayMax] contains ‘$’ && [Coinsurance] contains ‘%’ && [CopayMaxFrequency]=Blank, show {{CopayMax}} copayment then {{Coinsurance}} coinsurance]}} Plan Copay CopayFrequency CopayMax CopayMaxFrequency Coinsurance Plan...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT