1

I'm new this site and to coding in general, please bear with me (I don't really know the proper way to post or ask a question here)! I'm having trouble understanding a certain part of my assignment. I'm supposed to:

Create the structure, which will contain two data members. The year will be stored in the structure as an integer values, and the sales for that year will be stored as a double. The structure will also contain a constructor (which receives a year and sales value as parameters) and a ToString override.

When a button is pressed, an array of ten structures will be created. The structures will contain year values from 2000 to 2009 inclusive, and each year will be assigned a random sales value ranging from $1000.00 to $50,000.00.

I've italicized the part I'm having trouble wrapping my head around. Basically, I'm asking how do I go about this. How do I start? How do I create an array of ten structures? etc.

Any tips, examples, or help in general would be greatly appreciated!

  • 1
    start by searching for "array of structures C#" .. http://stackoverflow.com/questions/7079206/how-to-initialize-array-of-struct – Slai May 02 '17 at 02:30

2 Answers2

3

From your question, it looks like you have already successfully created the structure, its members and the constructor.

The part you have italicized requires these steps:

  1. Creating an array of your structure type.
  2. Adding elements to this array.

Say your structure is named `Sales'. Then the first step will be done like this:

Sales[] MySales = new Sales[10];

Second step will be done like this:

MySales[0] = new Sales(2001, 34000);

You add 9 more lines like that.

If you want to randomize the sales value instead of specifying them manually, use Random class like this:

Random r = new Random(); //Note: One instance of Random is sufficient

Sales[0] = new Sales(2001, r.Next(1000, 50000));
Sales[1] = new Sales(2002, r.Next(1000, 50000));
...

You could fill your array in a loop too, but I'm not sure if you have studied that stuff yet.

dotNET
  • 33,414
  • 24
  • 162
  • 251
-3

Use Dictionary bro:

Dictionary<int, double> dcData = new Dictionary<int, double>();
dcData.Add(2000, 1930000);
dcData.Add(2001, 1930000);

And this to generate random number:

Random rnd = new Random();
int randomValue = rnd.Next(1000, 50000);

Good luck

julanove
  • 405
  • 5
  • 19