# returns the highest number in the array explanation needed

Page 1 of 1

## 8 Replies - 1052 Views - Last Post: 07 January 2011 - 09:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=208653&amp;s=50f267c9879b8cb6f66b5c9aedd3a8dc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kayla23

Reputation: 0
• Posts: 21
• Joined: 19-October 10

# returns the highest number in the array explanation needed

Posted 06 January 2011 - 07:18 PM

hi i'm a c# beginner i've got this above code from the book which output the highest number from the array but i am a bit cofused interpreting some parts of the code

```namespace Ch06Ex02
{
class Program
{
static int MaxValue(int[] intArray)
{
int maxVal = intArray[0];

// i equals 1; i less than intArray.Length IS THE intArray.Length 0 ??? then the increment by 1
for (int i = 1; i < intArray.Length; i++)

{
//DOES THIS MEANS  intArray has the value of i which above the i equal 1
if (intArray[i] > maxVal)

//REPLACE THE VALUE OF maxVal WITH THE VALUE OF intArray[i]
maxVal = intArray[i];
}

return maxVal;
}
```

This is the whole 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);
}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: returns the highest number in the array explanation needed

### #2 tlhIn`toq

Reputation: 5874
• Posts: 12,808
• Joined: 02-June 10

## Re: returns the highest number in the array explanation needed

Posted 06 January 2011 - 07:34 PM

```// i equals 1; i less than intArray.Length IS THE intArray.Length 0 ??? then the increment by 1
for (int i = 1; i < intArray.Length; i++)

//DOES THIS MEANS  intArray has the value of i which above the i equal 1
if (intArray[i] > maxVal)

//REPLACE THE VALUE OF maxVal WITH THE VALUE OF intArray[i]
maxVal = intArray[i];

```

There are so many learning C# language tutorial on the 'net and books in the stores. There is a certain level of reading that the student is expected to do. And reading the explanation for
• how a "for" loop works or
• how an "if" comparrison works
• or that "variable = value" is how you assign a value to variable

is SOLIDLY part of the reading you need to do on your own. You can't really expect that people are going to invent hundred of hours answering the hundreds of threads you post for each and every term in the C# language.

If the book you bought was so bad that it doesn't explain any of the code you are typing, then you need to contact the publisher and the author.

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books.

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

• Anonymous Delegates: Demonstrates the use of unnamed delegates to reduce application complexity.
• Arrays: Shows how to use arrays.
• Attributes: Shows how to create custom attribute classes, use them in code, and query them through reflection.
• Collection Classes: Shows how to make non-generic collection classes that can be used with the foreach statement.
• COM Interop Part I: Shows how to use C# to interoperate with COM objects.
• COM Interop Part II: Shows how to a use a C# server together with a C++ COM client.
• Commandline: Demonstrates simple command-line processing and array indexing.
• Condiational Methods: Demonstrates conditional methods, which provide a powerful mechanism by which calls to methods can be included or omitted depending on whether a symbol is defined.
• Delegates: Shows how delegates are declared, mapped to static and instance methods, and combined into multicast delegates.
• Events: Shows how to declare, invoke, and configure events in C#.
• Explicit Interface: Demonstrates how to explicitly implement interface members and how to access those members from interface instances.
• Generics: Shows how to make generic collection classes that can be used with the foreach statement.
• Hello World: A Hello World application.
• Indexers Part I: Shows how C# classes can declare indexers to provide array-like access to objects.
• Indexers Part II: Shows how to implement a class that uses indexed properties. Indexed properties enable you to use a class that represents an array-like collection.
• Libraries: Shows how to use compiler options to create a DLL from multiple source files; also, how to use the library in other programs
• Named and Optional (C# 4.0): Demonstrates Named and Optional parameters, an alternative to method overloads
• Nullable: Demonstrates value types, such as double and bool, that can be set to null
• Office Sample (C# 4.0): Demonstrates how Dynamic and COM Interop make it easy to call Microsoft Office in C# 4.0
• OLEDB: Demonstrates how to use a Microsoft Access database from C# by creating a dataset and adding tables to it.
• Partial Types: Demonstrates how classes and structures can be defined in multiple C# source-code files
• PInvoke: Shows how to call exported DLL functions from C#
• Properties: Shows how properties are declared and used; also demonstrates abstract properties
• Python Sample (C# 4.0): Learn how to call a Python script by using the Dynamic feature in C# 4.0
• Security: Discusses .NET Framework security and shows how to modify security permissions in C# by using permission classes and permission attributes
• Simple Variance (C# 4.0): See how Covariance and Contravariance are supported in generic interfaces and delegates
• Structs: Shows how to use structs in C#.
• Unsafe: Shows how to use unmanaged code (code that uses pointers) in C#
• User Conversions: Shows how to define conversions to and from user-defined types
• Versioning: Demonstrates versioning in C# by using the override and new keywords
• XML Documents: Shows how to document code by using XML
• Yield: Demonstrates how to use the yield keyword to filter items in a collection

This post has been edited by tlhIn'toq: 06 January 2011 - 07:35 PM

### #3 kayla23

Reputation: 0
• Posts: 21
• Joined: 19-October 10

## Re: returns the highest number in the array explanation needed

Posted 06 January 2011 - 07:43 PM

Hi i am reading from a book c# visual 2008 beginners it gave me this code it has an explanation but i am having difficulty understand thats why i'm trying to get another explanation I can post the book explanation related to the code if needed has its a beginner code i thought a more prolific coder would be able to interprete easily and hopefully come up with a better explanation than the one provided by the book. one of the problems with the explanation is does quite say intArray.Length means from what i read on the net is the length of the intArray which i am assuming here that is 0 but i'm probably wrong.

if anyone can interpret and explain better than the book i appreaciate in advance

### #4 Tryparse

Reputation: 80
• Posts: 193
• Joined: 20-April 10

## Re: returns the highest number in the array explanation needed

Posted 06 January 2011 - 11:58 PM

First, you have to understand that the length of an array is how many elements are in it (in this case, how many integers it contains). Each element has an address, or "index", in the array. intArray[0] is referring to the first element of the array, which is 1, because 1 is the first integer stored to the array in this line:
```int[] myArray = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };
```

Now, you have to understand that array indexing is 0-based, meaning that the first element is intArray[0]. Therefore, in an array with a length of 10 (elements, or indexes), the last element would be intArray[9], because we started counting at 0. So when, in the for loop, it says intArray[i], it's simply referencing the element of the array whose index corresponds to the value of i at that point in time. If you don't understand how for loops operate with their counter variables (in this case, i), stop reading this right now and go learn more about loops.

So, first you're starting by storing the value of the first element of the array in maxVal, then you're looping through the rest of the array, comparing the value of each element to maxVal. Any time an element has a higher value than maxVal, that value gets stored into maxVal, overwriting the previous value. If an element's value is lower than maxVal, nothing happens and the loop continues on to the next iteration. The end result being that once all the elements of the array have been examined, the highest value found is stored in maxVal, and is returned by the method.

### #5 tlhIn`toq

Reputation: 5874
• Posts: 12,808
• Joined: 02-June 10

## Re: returns the highest number in the array explanation needed

Posted 07 January 2011 - 07:49 AM

kayla23, on 06 January 2011 - 06:43 PM, said:

I can post the book explanation related to the code...

Yes. Would you please post that explanation. I'd really like to see how badly the author explained these basic concepts know they are writing for beginner. It does nobody any good for the author to be writing as if the reader is already an experienced coder.

### #6 kayla23

Reputation: 0
• Posts: 21
• Joined: 19-October 10

## Re: returns the highest number in the array explanation needed

Posted 07 January 2011 - 09:21 AM

Hi i'm not trying to blame the book explanation they probably did a good job at it I think i'm the one that sometimes needs thinks explain in different ways to understand bellow is the book explanation but i think i understood more from the way "Tryparse" explained here specially: intArray[0] As i was assuming that was also Declaration of arrays when in fact was

referencing the element of the array whose index corresponds to the value of i at that point in time. Explained by "Tryparse"

Book explanation:

This code contains a function that does what the example function at the beginning of this chapter
hoped to do. It accepts an array of integers as a parameter and returns the highest number in the array.
The function definition is as follows:
```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;
}
```

The function, MaxValue() , has a single parameter defined, an int array called intArray . It also has a
return type of int . The calculation of the maximum value is simple. A local integer variable called
maxVal is initialized to the first value in the array, and then this value is compared with each of the
subsequent elements in the array. If an element contains a higher value than maxVal , then this value
replaces the current value of maxVal . When the loop finishes, maxVal contains the highest value in the
array, and is returned using the return statement.
The code in Main() declares and initializes a simple integer array to use with the
MaxValue() function:
int[] myArray = {1, 8, 3, 6, 2, 5, 9, 3, 0, 2};
e
The call to MaxValue() is used to assign a value to the int variable maxVal :
int maxVal = MaxValue(myArray);
Next, you write this value to the screen using Console.WriteLine() :
Console.WriteLine(“The maximum value in myArray is {0}”, maxVal);

### #7 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4652
• Posts: 8,208
• Joined: 08-June 10

## Re: returns the highest number in the array explanation needed

Posted 07 January 2011 - 09:31 AM

Well, since I've seen more than one of these types of posts from you, you might consider getting a different book, if you're not getting much out of this one. Try a "beginners" book from a different publisher. The other author may explain things more easily, or in a way you understand.

The sad fact is we can't teach you how to program here. We can help you fix mistakes, but eventually you'll come to an example that you don't understand, and it will be too complicated for us to explain on the internet. None of us (I think anyway) are published authors.

So I definitely suggest trying a new book, or perhaps getting direct instruction/tutoring.

### #8 tlhIn`toq

Reputation: 5874
• Posts: 12,808
• Joined: 02-June 10

## Re: returns the highest number in the array explanation needed

Posted 07 January 2011 - 09:35 AM

What I'm about to ask could be a touchy subject. I'm not trying to be rude. I just want to determine why the book's explanation gave you trouble.

Quote

Hi i'm not trying to blame the book explanation they probably did a good job at it I think i'm the one that sometimes needs thinks explain in different ways to understand bellow is the book explanation but i think i understood more from the way "Tryparse" explained here specially: intArray[0] As i was assuming that was also Declaration of arrays when in fact was referencing the element of the array whose index corresponds to the value of i at that point in tim

Is English a second language for you? Are you struggling with the English of the textbook?

Tryparse did a nice job of explaining what an array is and how to work with it. The textbook just mentions that in this exercise they are using an array. Is it possible you moved forward in the book without reading the earlier explanation of what an array is? Or perhaps you read it, but didn't *comprehend* it, but moved forward anyway?

Don't move forward in a textbook until you completely comprehend the current unit. Don't be lost and hope it will become more clear as you move forward. It won't. You'll just get more lost. Each lesson is built on the previous. If you build on a shaky foundation your building is going to fall down.

### #9 kayla23

Reputation: 0
• Posts: 21
• Joined: 19-October 10

## Re: returns the highest number in the array explanation needed

Posted 07 January 2011 - 09:45 AM

You both right i will follow both ur advise i will try go back understand at least 90% the topics before i move forward and if i dnt get it i will definetly try change books thanks alot.