Visual C# 8.15 (Command-Line Arguments)

This Solution is for the “Command-Line Arguments” of chapter 8 in the textbook; Visual c# How to program 6th edition by Deitel.

//Date: 5/24/2017

/*Description:
Fig 8.2 (in the textbook; Visual c# How to program 6th edition by Deitel) modified so that the array size is specified by the first
command line argument. If no argument is given default is 10.
*/
using System;

class program
{
static void Main(string[] args)
{
// headings
Console.WriteLine($”{“Index”}{“Value”,8}”);

// checks number of command-line arguments
if (args.Length != 1)
{
//creates space fo array and sets array size to 10
int[] array = new int[10];

//outputs each array elements values
for (int counter = 0; counter < array.Length; ++counter)
{
Console.WriteLine($”{counter,5}{array[counter],8}”);
}
}
else
{
// gets array size from first command-line argument
var arrayLength = int.Parse(args[0]);

// creates array
var array = new int[arrayLength];

//outputs each array elements values
for (int counter = 0; counter < array.Length; ++counter)
{
Console.WriteLine($”{counter,5}{array[counter],8}”);
}
}
}
}

 To use arguments when debugging in Visual C# 2015 

  1. Navigate to Debug.
  2. Click the properties at the bottom of the Debug menu.
    (It will have your program name followed by the word properties.)
  3. There you can enter the command line arguments needed.
    (In this example only one command line argument is needed, I chose 5.)
  4. Then run your program as normal ( <ctrl> F5 ).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Program Output: [ with command argument as 5 ]
Index       Value
0               0
1                0
2               0
3               0
4               0

Advertisements

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s