Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 4th, 2011, 12:10 PM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 6 Ch06Ex2

Hello,i am trying to learn basic of C# using this book,and i have one question

for example: int maxval=intArray[0],author says that maxval returns the higest number in array.Generally,i want to now what number in brackets means [0]?
What,if for example i put in brackets other number (1,2,4,5...) ?.
Thanks in advance
complete code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch06Ex02
{
class Program
{
static int MaxValue(int[] intArray)
{
int maxVal = intArray[0];
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
maxVal = intArray[i];
}
return maxVal;
}

static void Main(string[] args)
{
int[] myArray = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };
int maxVal = MaxValue(myArray);
Console.WriteLine("The maximum value in myArray is {0}", maxVal);
Console.ReadKey();
}
}
}
 
Old November 4th, 2011, 04:49 PM
Registered User
 
Join Date: Nov 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The number in square brackets identifies the position in the array.

[0] is position 1
[1] is position 2
[7] is position 8 etc.

Run this to see what I mean.

Cheers

Globox

Code:
static void Main(string[] args)
        {
            int[] intArray = { 1, 3, 13, 7, 9 };

            int maxValue = 0;

            for (int i = 0; i < intArray.Length; i++)
            {
                maxValue = (intArray[i] > maxValue) ? intArray[i] : maxValue;

                Console.WriteLine("");
                Console.WriteLine("i = " + i.ToString() + "      intArray[i] = " + intArray[i]);

            }

            Console.WriteLine("");
            Console.WriteLine("Maximum value in intArray = " + maxValue.ToString());
            Console.ReadLine();
        }

Quote:
Originally Posted by dampyr View Post
Hello,i am trying to learn basic of C# using this book,and i have one question

for example: int maxval=intArray[0],author says that maxval returns the higest number in array.Generally,i want to now what number in brackets means [0]?
What,if for example i put in brackets other number (1,2,4,5...) ?.
Thanks in advance
complete code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch06Ex02
{
class Program
{
static int MaxValue(int[] intArray)
{
int maxVal = intArray[0];
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
maxVal = intArray[i];
}
return maxVal;
}

static void Main(string[] args)
{
int[] myArray = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };
int maxVal = MaxValue(myArray);
Console.WriteLine("The maximum value in myArray is {0}", maxVal);
Console.ReadKey();
}
}
}
 
Old November 5th, 2011, 03:17 PM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Solved

I googled the answer:

In the heap, the array is a block of memory made of all the elements together. Each element in the array is identified by a zero-based index number: 0, 1, 2, ... and so on. An attempt to access an index outside the 0..length-1 range will fail with a runtime exception. When first created, all of the elements in the array are set to zero

This is from Java language,but principle is the same:
http://codingbat.com/doc/array.html
 
Old November 5th, 2011, 03:19 PM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Lock

Regarding me,a lock can be placed





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 6 - Code Download Missing for this Chapter dbaechtel BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 0 August 11th, 2009 11:02 AM
Chapter 2 - End of chapter exercises whizzkid1892 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 July 30th, 2008 12:02 PM
Generics chapter 12 difficult chapter i found ...? Larryz C# 2005 1 July 4th, 2007 09:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.