In: Computer Science
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();
}
}
}
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.