8 Replies - 492 Views - Last Post: 15 June 2013 - 07:33 PM Rate Topic: -----

#1 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Using an operator inside an array variable

Posted 14 June 2013 - 04:51 AM

The title may be a bit misleading as I don't know exactly what to call this. Here is my code. It's another exercise from a book I am reading and it has something I haven't seen used before.

            string[] jobTitles = new string[5];
            string username;

            Console.Write("What is your first name: ");
            username = Console.ReadLine();

            Console.WriteLine("Please list your tob 5 job titles. \n");

            for (int jb = 0; jb < 5; jb++)
            {
                Console.Write("\t\t#{0}: ", jb + 1);
                jobTitles[jb] = Console.ReadLine();
            }
            Console.Clear();

            Console.WriteLine("Which job do you like best: \n");

            for (int jb = 0; jb < 5; jb++)
            {
                Console.WriteLine("\t\t#{0} {1} {2}", jb + 1, jobTitles[jb], username);
            }
            Console.Write("\n\nYour choice: ");
            int yourChoice = int.Parse(Console.ReadLine());

            Console.Clear();

            Console.WriteLine("\n\n\t\tIntroducing {0} {1}", jobTitles[yourChoice - 1], username);
            Console.ReadLine();


If you take a look at this line by itself:

            Console.WriteLine("\n\n\t\tIntroducing {0} {1}", jobTitles[yourChoice - 1], username);


You'll notice that there is an operator inside the array variable. I wanted to ask what exactly that was doing there. If I delete the operator it gives me an exception and states that it's outside the bounds of the array. It kind of makes sense since it's a string array and not an int array. I'm assuming it's subtracting something, or perhaps in this case removing something.

logic tells me that it's removing the integer from the variable so that it can display only the string value. Is this correct?

I've never seen this done before and was just curious what it meant.

EDIT: I believe I just figured it out. I think it's subtracting 1 because the array starts at 0. So if I pick number 3, it would actually print out the fourth variable in the array instead of the third (if I didn't subtract 1). Oops. Sorry guys.

This post has been edited by Semus: 14 June 2013 - 04:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using an operator inside an array variable

#2 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: Using an operator inside an array variable

Posted 14 June 2013 - 04:58 AM

Not at all. Logic must be supported by knowledge.

Computers count collection elements starting with 0. (by convention)
Humans count collection elements starting with 1. (also by convention)

If a human wants to say "the 6th element" to a computer, he must say "the 5th".

So, if you ask for input and the user types 3, to show the 3th element of the collection ( as you see it ) you ask for the second element in the collection ( as the computer sees it ).


PS: You can think of it this way if it helps: humans think of which element in the collection they want , computers "think" about the offset of that element from the origin. So the first element has an offset of 0, the second one an offset of 1, etc.

This post has been edited by coder3788: 14 June 2013 - 05:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 Linesofcode  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 62
  • Joined: 23-May 13

Re: Using an operator inside an array variable

Posted 14 June 2013 - 05:06 AM

What you always should remember is that 0 is a number. And all the controls have an index..and all those index, the first element is always 0.

In Portugal, we usually say that, if you're going to the troop, you always gonna start your push-ups at number 0, not a number 1. :D
Was This Post Helpful? 0
  • +
  • -

#4 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Using an operator inside an array variable

Posted 14 June 2013 - 05:07 AM

If you take a look at my EDIT at the bottom of the post I realized that I had a brain fart and made the edit. I knew about the "computers counting from 0" tidbit for quite awhile, but that logic really seemed to leave my brain when looking at something I hadn't seen before. I really just need to stop posting and do more "thinking".
Was This Post Helpful? 0
  • +
  • -

#5 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Using an operator inside an array variable

Posted 14 June 2013 - 05:42 AM

Meh, you're a much better poster than some others asking questions on this forum.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4462
  • View blog
  • Posts: 7,776
  • Joined: 08-June 10

Re: Using an operator inside an array variable

Posted 14 June 2013 - 08:09 AM

These are the kinds of posts we actually like. Trust us, we're used to people who don't care about learning begging us to do their work for us. Most of us joined these forums because we like helping people; you're the kind of person we like helping. One who actually wants to learn.

Keep on posting!
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • Joined: 29-May 08

Re: Using an operator inside an array variable

Posted 14 June 2013 - 10:29 AM

It can be any expression that evaluates to an Integer.
Was This Post Helpful? 0
  • +
  • -

#8 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Using an operator inside an array variable

Posted 15 June 2013 - 08:05 AM

View PostCurtis Rutland, on 14 June 2013 - 08:09 AM, said:

These are the kinds of posts we actually like. Trust us, we're used to people who don't care about learning begging us to do their work for us. Most of us joined these forums because we like helping people; you're the kind of person we like helping. One who actually wants to learn.

Keep on posting!


Thank you, I continue to be impressed with the amount of knowledge here on DIC and plan to be here for quite some time :bigsmile:


This was my first thread on DIC...

First Thread

I can honestly look back on that and say I have made progress.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3548
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Using an operator inside an array variable

Posted 15 June 2013 - 07:33 PM

And we also applaud your progress! It's is very satisfying to see students learn and grow. I suspect that is what keeps us coming back to this site to help others. I would not be surprised if in a few months you'll also be helping and guiding others.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1