settingsAccountsettings
Menusettings

Q: Problem solving in C# Task - Cheat Sheet

+4 votes

Goshko is a great singer, but he sucks at math - multiplication table is the thing he hates the most. Help him by generating a cheat sheet with the multiplication table for him. Goshko should be able to enter the following things:

  • The numbers of rows and columns of the output table
  • The start number vertically
  • The start number horizontally

For example, if he enters 9 rows, 9 columns, vertical and horizontal start numbers 1, the generated cheat sheet should look like this:

cheat sheet example 1

If he enters 3 rows, 5 columns, vertical start number 4, horizontal start numbers 8, the generated cheat sheet should look like this:

cheat sheet example 2

Input

The input data should be read from the console.

  • The first line will contain the number of rows R. The second line will contain the number of c columns C. The third line will contain the vertical start number V. The fourth line will contain the horizontal start number H.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output

The output data should be printed on the console.

The output should contain exactly R lines with exactly C numbers per line – representing each line of the cheat sheet. Numbers should be separated by exactly one whitespace (" "), and there shouldn't be any whitespaces after the last number on a line.

Constraints

  • 0 ≤ R ≤ 100.
  • 0 ≤ C ≤ 100.
  • Any number N in the cheat sheet will be in the range [-9223372036854775808…9223372036854775807].
  • Allowed working time for your program: 0.2 seconds. Allowed memory: 16 MB.

Examples

cheat sheet output example

asked in C# category by user andrew
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Before sharing my solution, I would like to explain how I did this task:

  1. First of all it is clear that we are going to use for loop and there will be another one inside it - to make somewhat of a table;
  2. Second - this line: long currentValue = (verticalStart + i) * (horizontalStart + j); means that to each start number we will add the i and j identifiers of each for loop - because they increase by one (i++ and j++);

Do not forget to use long as a numerical data type for variables verticalStart and horizontalStart because of the constraints in the task...

Here is my solution:

using System;

class CheatSheet
{
    static void Main()
    {
        int rows = int.Parse(Console.ReadLine());
        int columns = int.Parse(Console.ReadLine());
        long verticalStart = long.Parse(Console.ReadLine());
        long horizontalStart = long.Parse(Console.ReadLine());

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                long currentValue = (verticalStart + i) * (horizontalStart + j);
                Console.Write(currentValue + " ");
            }
            Console.WriteLine();
        }
    }
}

 

answered by user matthew44
selected by user golearnweb
...