Well im trying to find a maxValue in a array and its harder then i feel it should. Normally this code here works. If i declare an array and manually put in numbers for the array it find the max value fine. But when i put in a method to make an array with random numbers it breaks and returns the last value set as the maximum one.
static int MaxArray(int[] Array)
{
int maxVal = Array[0];
for(int i = 0; i < Array.Length; i++)
{
if(Array[i] > maxVal)
{
maxVal = Array[i];
}
}
return maxVal;
}
static void Main(string[] args)
{
Random r = new Random();
int[] myArray = new int[5];
for(int i = 0; i < myArray.Length; i++)
{
int rNumb = r.Next(0, 100);
for (int v = 0; v < myArray.Length; v++)
{
myArray[v] = rNumb;
}
Console.WriteLine(myArray[i]);
}
Console.WriteLine("Press entere to find the max value");
Console.ReadKey();
Console.Write(MaxArray(myArray));
Console.Read();
}
The inner for-loop in your Main method is useless. It fills the whole array with the current random number (so at the end the whole array will contain the last random number repeated).
The correct code is the following:
for(int i = 0; i < myArray.Length; i++)
{
int rNumb = r.Next(0, 100);
myArray[i] = rNumb;
Console.WriteLine(myArray[i]);
}
You were overwriting the values with your second for loop,
for (int v = 0; v < myArray.Length; v++)
{
myArray[v] = rNumb;
}
will write the current random number at each index of your array. The last random number will overwrite previous ones, so it will be declared as max since it's the only available number in your array.
Try this instead :
static void Main(string[] args)
{
Random r = new Random();
int[] myArray = new int[5];
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = r.Next(0, 100);
Console.WriteLine(myArray[i]);
}
Console.WriteLine("Press entere to find the max value");
Console.Write(MaxArray(myArray));
Console.Read();
}
But honestly that MaxArray method is useless, don't reinvent the wheel, use Max from LINQ instead :
Console.Write(myArray.Max());
This is easily achieved using Linq.
using System.Linq;
private static Random _random = new Random();
public static int[] GenerateRandomArray(int arrayLength)
{
return Enumerable.Range(0, arrayLength).Select(i => _random.Next(0, 100)).ToArray();
}
public static int FindMaxValue(int[] array)
{
return array.Max();
}
It's because your array initialization is completely useless. This is what you're looking for:
for(int i = 0; i < myArray.Length; i++)
{
myArray[i] = r.Next(0, 100);
Console.WriteLine(myArray[i]);
}
At the end of it, the array will look like this (with your code):
{n, n, n, n, n}
where n is the last random number.
You don't need this loop. You should change this
for (int v = 0; v < myArray.Length; v++)
{
myArray[v] = rNumb;
}
with
myArray[i] = rNumb;
By writing this loop you are overwriting all the values in the array with the last value.
The reason you get the last value set as the maximum every time is because you set every element in the array to the last random number generated on the last iteration of the outer for loop.
Your Console.WriteLine(myArray[i]); is giving you a false impression of what the values of the array you pass into MaxValue() actually are!
Related
I'm trying to fill one dimensional array with random BUT unique numbers (No single number should be same). As I guess I have a logical error in second for loop, but can't get it right.
P.S I'm not looking for a more "complex" solution - all I know at is this time is while,for,if.
P.P.S I know that it's a really beginner's problem and feel sorry for this kind of question.
int[] x = new int[10];
for (int i = 0; i < x.Length; i++)
{
x[i] = r.Next(9);
for (int j = 0; j <i; j++)
{
if (x[i] == x[j]) break;
}
}
for (int i = 0; i < x.Length; i++)
{
Console.WriteLine(x[i);
}
Here is a solution with your code.
int[] x = new int[10];
for (int i = 0; i < x.Length;)
{
bool stop = false;
x[i] = r.Next(9);
for (int j = 0; j <i; j++)
{
if (x[i] == x[j]) {
stop = true;
break;
}
}
if (!stop)
i++;
}
for (int i = 0; i < x.Length; i++)
{
Console.WriteLine(x[i]);
}
A simple trace of the posted code reveals some of the issues. To be specific, on the line…
if (x[i] == x[j]) break;
if the random number is “already” in the array, then simply breaking out of the j loop is going to SKIP the current i value into the x array. This means that whenever a duplicate is found, x[i] is going to be 0 (zero) the default value, then skipped.
The outer i loop is obviously looping through the x int array, this is pretty clear and looks ok. However, the second inner loop can’t really be a for loop… and here’s why… basically you need to find a random int, then loop through the existing ints to see if it already exists. Given this, in theory you could grab the same random number “many” times over before getting a unique one. Therefore, in this scenario… you really have NO idea how many times you will loop around before you find this unique number.
With that said, it may help to “break” your problem down. I am guessing a “method” that returns a “unique” int compared to the existing ints in the x array, may come in handy. Create an endless while loop, inside this loop, we would grab a random number, then loop through the “existing” ints. If the random number is not a duplicate, then we can simply return this value. This is all this method does and it may look something like below.
private static int GetNextInt(Random r, int[] x, int numberOfRandsFound) {
int currentRand;
bool itemAlreadyExist = false;
while (true) {
currentRand = r.Next(RandomNumberSize);
itemAlreadyExist = false;
for (int i = 0; i < numberOfRandsFound; i++) {
if (x[i] == currentRand) {
itemAlreadyExist = true;
break;
}
}
if (!itemAlreadyExist) {
return currentRand;
}
}
}
NOTE: Here would be a good time to describe a possible endless loop in this code…
Currently, the random numbers and the size of the array are the same, however, if the array size is “larger” than the random number spread, then the code above will NEVER exit. Example, if the current x array is set to size 11 and the random numbers is left at 10, then you will never be able to set the x[10] item since ALL possible random numbers are already used. I hope that makes sense.
Once we have the method above… the rest should be fairly straight forward.
static int DataSize;
static int RandomNumberSize;
static void Main(string[] args) {
Random random = new Random();
DataSize = 10;
RandomNumberSize = 10;
int numberOfRandsFound = 0;
int[] ArrayOfInts = new int[DataSize];
int currentRand;
for (int i = 0; i < ArrayOfInts.Length; i++) {
currentRand = GetNextInt(random, ArrayOfInts, numberOfRandsFound);
ArrayOfInts[i] = currentRand;
numberOfRandsFound++;
}
for (int i = 0; i < ArrayOfInts.Length; i++) {
Console.WriteLine(ArrayOfInts[i]);
}
Console.ReadKey();
}
Lastly as other have mentioned, this is much easier with a List<int>…
static int DataSize;
static int RandomNumberSize;
static void Main(string[] args) {
Random random = new Random();
DataSize = 10;
RandomNumberSize = 10;
List<int> listOfInts = new List<int>();
bool stillWorking = true;
int currentRand;
while (stillWorking) {
currentRand = random.Next(RandomNumberSize);
if (!listOfInts.Contains(currentRand)) {
listOfInts.Add(currentRand);
if (listOfInts.Count == DataSize)
stillWorking = false;
}
}
for (int i = 0; i < listOfInts.Count; i++) {
Console.WriteLine(i + " - " + listOfInts[i]);
}
Console.ReadKey();
}
Hope this helps ;-)
The typical solution is to generate the entire potential set in sequence (in this case an array with values from 0 to 9). Then shuffle the sequence.
private static Random rng = new Random();
public static void Shuffle(int[] items)
{
int n = list.Length;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
int temp = items[k];
items[k] = items[n];
items[n] = temp;
}
}
static void Main(string[] args)
{
int[] x = new int[10];
for(int i = 0; i<x.Length; i++)
{
x[i] = i;
}
Shuffle(x);
for(int i = 0; i < x.Length; i++)
{
Console.WritLine(x[i]);
}
}
//alternate version of Main()
static void Main(string[] args)
{
var x = Enumerable.Range(0,10).ToArray();
Shuffle(x);
Console.WriteLine(String.Join("\n", x));
}
You can simply do this:
private void AddUniqueNumber()
{
Random r = new Random();
List<int> uniqueList = new List<int>();
int num = 0, count = 10;
for (int i = 0; i < count; i++)
{
num = r.Next(count);
if (!uniqueList.Contains(num))
uniqueList.Add(num);
}
}
Or:
int[] x = new int[10];
Random r1 = new Random();
int num = 0;
for (int i = 0; i < x.Length; i++)
{
num = r1.Next(10);
x[num] = num;
}
Trying to build a method that will find the sum of all the values within the 2D array. I'm very new to programming and can't find a good starting point on trying to figure out how its done. Here is what I have so far (forgive me, I'm usually an english/history guy, logic isn't my forte...)
class Program
{
static void Main(string[] args)
{
int[,] myArray = new int[5,6];
FillArray(myArray);
LargestValue(myArray);
}
//Fills the array with random values 1-15
public static void FillArray(int[,] array)
{
Random rnd = new Random();
int[,] tempArray = new int[,] { };
for (int i = 0; i < tempArray.GetLength(0); i++)
{
for (int j = 0; j < tempArray.GetLength(1); j++)
{
tempArray[i, j] = rnd.Next(1, 16);
}
}
}
//finds the largest value in the array (using an IEnumerator someone
//showed me, but I'm a little fuzzy on how it works...)
public static void LargestValue(int[,] array)
{
FillArray(array);
IEnumerable<int> allValues = array.Cast<int>();
int max = allValues.Max();
Console.WriteLine("Max value: " + max);
}
//finds the sum of all values
public int SumArray(int[,] array)
{
FillArray(array);
}
}
I guess I could try to find the sum of each row or column and add them up with a for loop? Add them up and return an int? If anyone could shed any insight, it would be greatly appreciated, thanks!
Firstly, you don't need to call FillArray in the beginning of each method, you have already populated the array in the main method, you are passing a populated array to these other methods.
A loop similar to what you use to populate the array is the easiest to understand:
//finds the sum of all values
public int SumArray(int[,] array)
{
int total = 0;
// Iterate through the first dimension of the array
for (int i = 0; i < array.GetLength(0); i++)
{
// Iterate through the second dimension
for (int j = 0; j < array.GetLength(1); j++)
{
// Add the value at this location to the total
// (+= is shorthand for saying total = total + <something>)
total += array[i, j];
}
}
return total;
}
To sum an array if you know the length is easy
As a bonus code included to get the highest valeu too.
You could easily expand this to get other kinds of statistical code.
I asume below Xlength and Ylength are integers too, and known by you.
You could also replace them by a number in the code.
int total = 0;
int max=0;
int t=0; // temp valeu
For (int x=0;x<Xlength;x++)
{
for (int y=0;y<Ylength;y++)
{
t = yourArray[x,y];
total =total +t;
if(t>max){max=t;} // an if on a single line
}
}
here is a link with an MSDN sample on how to retrieve unknown array lengths.
and there is a nice site to have around when you start in c#
google ".net perls"
This is a function that makes an array and fills it from LoLim till HiLim and then shuffles the order:
static Random random = new Random(); //these two statics are to make a random number, i need the first static to be outside the function so it won't keep making the same random number
static int RandomNum(int LoLim, int HiLim, int index)
{
var nums = Enumerable.Range(LoLim,HiLim).ToArray(); //these next lines make an array from LoLim to HiLim and then shuffle it
for (int i = 0; i < nums.Length; i++)
{
int randomIndex = random.Next(nums.Length);
int temp = nums[randomIndex];
nums[randomIndex] = nums[i];
nums[i] = temp;
}
return nums[index];
Then I have this function that makes a 2 dimensional array and then prints it. It uses RandomNum but not in the way I want to, but I don't know how to make it work.
static Array Matrix(int Rows, int Columns) //this is a function to initiate and print the array
{
int[,] LotteryArray = new int[Rows, Columns];
for (int i = 0; i < LotteryArray.GetLength(0); i++) //this is a series of loops to initiate and print the array
{
for (int j = 0; j < LotteryArray.GetLength(1); j++)
{
LotteryArray[i, j] = RandomNum(1,46,j); //the numbers are supposed to be between 1 and 45, so i tell it to do it until 46 because the upper limit is exclusive
Console.Write("{0,3},", LotteryArray[i, j]); //the {0,3} the 3 is three spaces after the first variable
}
Console.WriteLine();
}
return LotteryArray;
Basically I want it to call to RandomNum every "row" so it could shuffle the array, then I want it to pull out nums[index] and print it out. The reason I want this is so I can have non repeating random numbers in every row. For example: I don't want a row to be "21, 4, 21, 5, 40, 30".
There are various problems with your code, even if it worked. The "logical" problems are:
You are using a biased shuffling algorithm. You should use Fisher-Yates to do correct shuffling (see http://blog.codinghorror.com/the-danger-of-naivete/)
If you really want to do a "good" shuffling, you shouldn't use the Random number generator, and instead use the RNGCryptoServiceProvider (see http://www.cigital.com/papers/download/developer_gambling.php, but note that this is a much lesser problem than the other other one... we can survive with "a little less random" shuffles, if are the generated shuffles are all equiprobable)
You are using wrongly the arguments of Enumerable.Range()
Your coding problem is different: you have to save the shuffled row somewhere, and then take the first column values.
Here I'm using a class to encapsulate a row with shuffling
public class ShuffledRow
{
public static readonly Random Random = new Random();
public readonly int[] Row;
/// <summary>
/// Generates and shuffles some numbers
/// from min to max-1
/// </summary>
/// <param name="min"></param>
/// <param name="max">Max is excluded</param>
public ShuffledRow(int min, int max)
{
int count = max - min;
Row = Enumerable.Range(min, count).ToArray();
Shuffle(Row);
}
private static void Shuffle<T>(T[] array)
{
// Fisher-Yates correct shuffling
for (int i = array.Length - 1; i > 0; i--)
{
int j = Random.Next(i + 1);
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
Another problem: don't ever use multidimensional arrays in C# unless you know what you are doing. Sadly they are a bastard-and-forgotten child of .NET. Use jagged arrays (arrays of arrays).
public static int[][] Matrix(int rows, int columns)
{
int[][] lottery = new int[rows][];
for (int i = 0; i < lottery.Length; i++)
{
ShuffledRow sr = new ShuffledRow(1, 46);
lottery[i] = sr.Row;
Array.Resize(ref lottery[i], columns);
Console.WriteLine(
string.Join(",", lottery[i].Select(
x => string.Format("{0,3}", x))));
}
return lottery;
}
public static int[][] Matrix(int rows, int columns)
{
int[][] lottery = new int[rows][];
for (int i = 0; i < lottery.Length; i++)
{
ShuffledRow sr = new ShuffledRow(1, 46);
lottery[i] = new int[columns];
for (int j = 0; j < columns; j++)
{
lottery[i][j] = sr.Row[j];
Console.Write("{0,3},", lottery[i][j]);
}
Console.WriteLine();
}
return lottery;
}
I have prepared two versions of the Matrix function, one that is more similar to the one you are using, one that is more LINQ and "advanced".
to generate random numbers really do not use random function, use this:
private static RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
// Main method.
public static int[] generateTrueRandomOK()
{
const int totalRolls = 2500;
int[] results = new int[50]; //Number of random ints that you need
// Roll the dice 25000 times and display
// the results to the console.
for (int x = 0; x < totalRolls; x++)
{
byte roll = RollDice((byte)results.Length);
results[roll - 1]++;
}
return results;
}
// This method simulates a roll of the dice. The input parameter is the
// number of sides of the dice.
public static byte RollDice(byte numberSides)
{
if (numberSides <= 0)
throw new ArgumentOutOfRangeException("numberSides");
// Create a byte array to hold the random value.
byte[] randomNumber = new byte[1];
do
{
// Fill the array with a random value.
rngCsp.GetBytes(randomNumber);
}
while (!IsFairRoll(randomNumber[0], numberSides));
// Return the random number mod the number
// of sides. The possible values are zero-
// based, so we add one.
return (byte)((randomNumber[0] % numberSides) + 1);
}
private static bool IsFairRoll(byte roll, byte numSides)
{
// There are MaxValue / numSides full sets of numbers that can come up
// in a single byte. For instance, if we have a 6 sided die, there are
// 42 full sets of 1-6 that come up. The 43rd set is incomplete.
int fullSetsOfValues = Byte.MaxValue / numSides;
// If the roll is within this range of fair values, then we let it continue.
// In the 6 sided die case, a roll between 0 and 251 is allowed. (We use
// < rather than <= since the = portion allows through an extra 0 value).
// 252 through 255 would provide an extra 0, 1, 2, 3 so they are not fair
// to use.
return roll < numSides * fullSetsOfValues;
}
You could use jagged arrays:
Random random = new Random();
int[] RandomNum(int LoLim, int HiLim)
{
var nums = Enumerable.Range(LoLim,HiLim).ToArray();
for (int i = 0; i < nums.Length; i++)
{
int randomIndex = random.Next(nums.Length);
int temp = nums[randomIndex];
nums[randomIndex] = nums[i];
nums[i] = temp;
}
return nums;
}
int[][] Matrix(int Rows, int Columns)
{
int[][] LotteryArray = new int[Rows][];
for (int i = 0; i < LotteryArray.Length; i++)
{
LotteryArray[i] = RandomNum(1,46);
for (int j = 0; j < LotteryArray[i].Length; j++)
{
Console.Write("{0,3},", LotteryArray[i][j]);
}
Console.WriteLine();
}
return LotteryArray;
}
I am wanting to create multiple arrays of ints(in C#). However they all must have a unique number in the index, which no other array has that number in that index. So let me try show you what I mean:
int[] ints_array = new int[30];
for (int i = 0; i < ints_array.Count(); i++)
ints_array[i] = i;
//create a int array with 30 elems with each value increment by 1
List<int[]> arrayList = new List<int[]>();
for(int i = 0; i < ints_array.Count(); i++)
arrayList.Add(ints_array[i]. //somehow sort the array here randomly so it will be unique
So I am trying to get the arrayList have 30 int[] arrays and each is sorted so no array has the same int in the same index as another.
Example:
arrayList[0] = {5,2,3,4,1,6,7,8,20,21... etc }
arrayList[1] = {1,0,5,2,9,10,29,15,29... etc }
arrayList[2] = {0,28,4,7,29,23,22,17... etc }
So would this possible to sort the array in this unique kind of way? If you need anymore information just ask and ill fill you in :)
Wouldn't it be easier to create the arrays iteratively using an offset pattern?
What I mean is that if you created the first array using 1-30 where 1 is at index 0, the next array could repeat this using 2-30 where 2 is at index 0 and then wrap back to 1 and start counting forward again as soon as you go past 30. It would be an easy and repeatable way to make sure no array shared the same value/index pair.
You can do it like that:
List<int[]> arrayList = new List<int[]>();
Random rnd = new Random();
for (int i = 0; i < ints_array.Length; i++)
{
ints_array = ints_array.OrderBy(x => rnd.Next()).ToArray();
var isDuplicate = arrayList.Any(x => x.SequenceEqual(ints_array));
if (isDuplicate)
{
while (arrayList.Any(x => x.SequenceEqual(ints_array)))
{
ints_array = ints_array.OrderBy(x => rnd.Next()).ToArray();
}
}
arrayList.Add(ints_array);
}
I think, this wouldn't be so efficient for bigger numbers than 30.But in this case it shouldn't be a problem, in my machine it takes 7 milliseconds.
Jesse's idea would be best unless you needed a pure random pattern. In that case I would recommend generating a random number, checking all your previous arrays, and then placing it in an array if it did not match any other arrays current index. Otherwise, generate a new random number until you find a fresh one. Put that into a loop until all your arrays are filled.
Use a matrix (2D-array). It is easier to handle than a list of arrays. Create a random number generator. Make sure to initialize it only once, otherwise random number generator may create bad random numbers, if created in too short time intervals, since the slow PC-clock might not have ticked in between. (The actual time is used as seed value).
private static Random random = new Random();
Create two helper arrays with shuffeled indexes for rows and columns:
const int N = 30;
int[] col = CreateUniqueShuffledValues(N);
int[] row = CreateUniqueShuffledValues(N);
Then create and initialize the matrix by using the shuffeled row and column indexes:
// Create matrix
int[,] matrix = new int[N, N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrix[row[i], col[j]] = (i + j) % N;
}
}
The code uses these two helper methods:
private static int[] CreateUniqueShuffledValues(int n)
{
// Create and initialize array with indexes.
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = i;
}
// Shuffel array using one variant of Fisher–Yates shuffle
// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#The_modern_algorithm
for (int i = 0; i < n; i++) {
int j = random.Next(i, n);
Swap(array, i, j);
}
return array;
}
private static void Swap(int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
int size = 10;
// generate table (no duplicates in rows, no duplicates in columns)
// 0 1 2
// 1 2 0
// 2 0 1
int[,] table = new int[size, size];
for (int y = 0; y < size; y++)
for (int x = 0; x < size; x++)
table[y, x] = (y + x) % size;
// shuffle rows
Random rnd = new Random();
for (int i = 0; i < size; i++)
{
int y1 = rnd.Next(0, size);
int y2 = rnd.Next(0, size);
for (int x = 0; x < size; x++)
{
int tmp = table[y1, x];
table[y1, x] = table[y2, x];
table[y2, x] = tmp;
}
}
// shuffle columns
for (int i = 0; i < size; i++)
{
int x1 = rnd.Next(0, size);
int x2 = rnd.Next(0, size);
for (int y = 0; y < size; y++)
{
int tmp = table[y, x1];
table[y, x1] = table[y, x2];
table[y, x2] = tmp;
}
}
// sample output
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
Console.Write("{0} ", table[y, x]);
Console.WriteLine();
}
I am still having slight troubles with functions and arrays. I have created an array that has been filled with random numbers. I am trying to create a function that sets up the array and returns the highest value. I am not to sure on how to approach this but this and what I have written so far has not been able to compile.
namespace Task_1._13
{
class Program
{
static void Main(string[] args)
{
gettingMaximum(int i);
}
public int gettingMaximum(int i);
{
int maximum = 0;
int[] myArray = new int[10];
Random rand = new Random();
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = rand.Next(19);
}
for (int i = 0; i < 10; i++)
{
if (i == 0)
maximum = myArray[i];
else
if (myArray[i] < maximum) maximum = myArray[i];
int result = i;
return result;
}
}
}
}
That's what I have gotten so far. I am not very experienced in programming so help would be appreciated.
for (int i = 0; i < myArray.Length; i++)
{
if(myArray[i] > maximum)
{
maximum = myArray[i];
}
}
return maximum;
OR you can just use the Max function like this
return myArray.Max();
If you want to return the largest value, you can use System.Linq and use the Max function.
int largestValue = myArray.Max();
Enunerable.Max Method
public int gettingMaximum();
{
int maximum = 0;
int[] myArray = new int[10];
Random rand = new Random();
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = rand.Next(19);
}
for (int i = 0; i < myArray.Length; i++)
{
if (i == 0)
maximum = myArray[i];
else
if (myArray[i] > maximum) maximum = myArray[i];
}
return maximum;
}
This should work.
Updated w/ comments
namespace Task_1._13
{
class Program
{
static void Main(string[] args)
{
// gettingMaximum(int i); This won't work because the int can't be declared while it's being passed into the method
int i = 0; // You need to declare the i before you pass it to a method. Although look closely at your method and the reason you are passing in a value. Do you need it or use it?
Console.WriteLine(gettingMaximum(i)); // I'm outputting the results to the screen so that you can verify that at least something happened
}
public static int gettingMaximum(int z)//; You don't need the semicolon here
// Also, I renamed this to z to demonstrate that you never use it. You could just as easily remove it
// Also, this method must be marked static which is outside the scope of this question
{
int maximum = 0;
int[] myArray = new int[10];
Random rand = new Random();
// This looks good. You create the array and loop through the items and set the value
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = rand.Next(19);
}
// This code counts to 10. During each step, it compares the current step with 0.
// If the step (i) is 0, you set the maximum to to that value.
// Otherwise, you see if the array value at that step is LESS than the current maximum, and if it is
// you set the maximum to be that value.
//for (int i = 0; i < 10; i++)
//{
// if (i == 0)
// maximum = myArray[i];
// else
// if (myArray[i] < maximum) maximum = myArray[i];
// int result = i; // Here, you're actually setting the result to the current step number
// return result; // I think you meant to set it to maximum, but you can leave it out completely and just return maximum.
//}
for (int i = 0; i < 10; i++)
{
if (i == 0)
{
maximum = myArray[i];
}
else
{ // Since you have nested if's, i'm using braces to illustrate the paths of the branches
if (myArray[i] > maximum) // What i think you mean is, is the value at this item in the array MORE than my current maximum
{
maximum = myArray[i];
}
}
}
return maximum;
}
}
}