How to switch max and min elements in array? - c#

I create an array and fill it from the console.
I print that array in console.
I detect max and min elements in the array and output them in console.
Then I need to switch max and min elements and output the
updated array (without creating a new one).
Optionally I need to output new array that contains switched (max and min) elements.
private static void Main(string[] args)
{
int i;
int MaxElementIndex = 0;
int MinElementIndex = 0;
int temp = 0;
int[] t = new int[5];
for (i = 0; i < 5; i++)
{
Console.WriteLine("enter {0} element array", i + 1);
t[i] = int.Parse(Console.ReadLine());
}
int max = t[0];
for (i = 0; i < 5; i++)
{
if (max < t[i])
{
max = t[i];
MaxElementIndex = i;
}
}
Console.WriteLine("the maximum a value is equal {0} and index number is {1}", max, MaxElementIndex);
int min = t[0];
for (i = 0; i < 5; i++)
{
if (min > t[i])
{
min = t[i];
MinElementIndex = i;
}
}
Console.WriteLine("the minimun value is equal {0} and index number is {1}", min, MinElementIndex);
Console.WriteLine("Initial array: ");
foreach (var item in t)
{
Console.Write(item.ToString());
}
Console.ReadKey();
Console.WriteLine("Changed array:");
MaxElementIndex = temp;
MinElementIndex = MaxElementIndex;
temp = MinElementIndex;
foreach (var newItem in t)
{
Console.Write(newItem.ToString());
}
Console.ReadKey();
}
The problem is that I am not sure how to get the old array with switched elements. The pattern with temp variable returns me "0".

The problem is you are switching your temporary variables. But you want to switch you min and max elements in array. Your switching code should be like following:
temp = t[MaxElementIndex];
t[MaxElementIndex]=t[MinElementIndex];
t[MinElementIndex]=temp;
And then you should loop over your array:
Console.WriteLine("Switched array: ");
foreach (var newItem in t)
{
Console.Write(newItem.ToString());
}
Hope this helps.

This section is wrong in your code:
Console.ReadKey();
Console.WriteLine("Changed array:");
MaxElementIndex = temp;
MinElementIndex = MaxElementIndex;
temp = MinElementIndex;
it should be:
Console.ReadKey();
Console.WriteLine("Changed array:");
temp = t[MaxElementIndex];
t[MaxElementIndex] = t[MinElementIndex];
t[MinElementIndex] = temp;
First you store your t[maxElement] in temp variable then you replace it with t[minElement] and in the end replace minimum with maximum which is stored in temp variable.

Related

Storing values in an array and outputting the highest,lowest,average. Output flaw

This was a small problem the teacher gave us at school. We were asked to make a program which keeps asking the user to input test scores until he inputs -99 to stop the program. The values are stored in an array and the highest, lowest and average scores are displayed.
The problem I have with my code is that whenever I run it it always gives me a value of 0 for lowest scores even though there are no zeros in the input. Can anyone please point out the mistake in my code?
Code:
static void Main(string[] args)
{
int[] za = new int[100];
scores(za, 0);
}
public static void scores(int[] ar, int x)
{
Console.Write("Please enter homework score [0 to 100] (-99 to exit): ");
int a = Convert.ToInt16(Console.ReadLine());
if (a != -99)
{
ar[x] = a;
x++;
scores(ar, x);
}
else
{
Console.Clear();
Console.WriteLine("Homework App");
int[] arr = new int[x];
foreach (int l in arr)
{
arr[l] = ar[l];
}
int lowest = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (lowest > arr[i]) { lowest = arr[i]; }
}
int highest = arr[0];
for (int j = 1; j < arr.Length; j++)
{
if (highest < arr[j]) { highest = arr[j]; }
}
double sum = 0;
double nums = 0;
for (int k = 0; k < arr.Length; k++)
{
sum = sum + arr[k];
nums++;
}
double average = sum / nums;
Console.WriteLine("Highest Score: {0}", highest);
Console.WriteLine("Lowest Score: {0}", lowest);
Console.WriteLine("Average Score: {0}", average);
Console.ReadLine();
}
}
When you're copying items, don't use a foreach loop and then the element stored in the array as the index that you're inserting to. Instead, use a for loop with a counter variable.
You should change this:
int[] arr = new int[x];
foreach (int l in arr)
{
// You're using the wrong value for the index here.
// l represents the value of the item stored in `arr`, which is `0`
arr[l] = ar[l];
}
To this:
int[] arr = new int[x];
for (var counter = 0; counter < x; counter++)
{
arr[counter] = ar[counter];
}

Getting an error about variable not existing in current context

I have written out a program that declares an array of 10 integers, takes input from the user and puts them in the array and then accepts out parameters for the highest value, lowest value, sum of all values, and the average.
The main method displays all the stats. I don't know why, but I get the error
k does not exist in the current context - line 51 column 5
when I have previously declared it before that given line. Any help would be appreciated.
using System;
namespace IntegerStatistics
{
class Program
{
static void Main()
{
Console.Clear();
// Declaring variables
int[] userArray = FillArray();
int ArrayHighest = 0;
int ArrayLowest = 0;
int ArraySum = 0;
int ArrayAverage = 0;
Calculations(userArray, out ArrayHighest, out ArrayLowest, out ArraySum, out ArrayAverage);
Console.WriteLine("The lowest value in the array is {0}, while the highest is {1}.", ArrayLowest, ArrayHighest);
Console.WriteLine("The array has a sum of {0} and averages out to {1}.", ArraySum, ArrayAverage);
Console.ReadLine();
}
private static int[] FillArray()
{
int[] intArray = new int[10];
int numbersEntered = 0;
int intTemp = 0;
string strTemp = "";
for(int k = 0; k < 10; ++k)
{
Console.WriteLine("Enter a whole number or 999 to quit: ");
strTemp = Console.ReadLine();
while(!int.TryParse(strTemp, out intTemp))
{
Console.WriteLine("Input was not in the correct format");
Console.Write("Please enter a valid number: ");
strTemp = Console.ReadLine();
}
}
if(intTemp != 999)
{
intArray[k] = intTemp;
++numbersEntered;
}
else
{
k = 10;
}
Array.Resize(ref intArray, numbersEntered);
return intArray;
}
private static void Calculations(int[] intArray, out int Highest, out int Lowest, out int intSum, out int average)
{
intSum = 0;
Array.Sort(intArray);
Lowest = intArray[0];
Array.Reverse(intArray);
Highest = intArray[0];
Array.Reverse(intArray);
for(int k = 0; k < intArray.Length; ++k)
{
intSum += intArray[k];
}
average = intSum / intArray.Length;
}
}
}
The line I'm specifically having issues with is:
if (intTemp != 999)
{
intArray[k] = intTemp;
++numbersEntered;
}
else
{
k = 10;
}
I think you meant to place the if-else block inside the for-loop.
Since you want to check if the input number is 999 for each iteration, you could write your for-loop like this:
for(int k = 0; k < 10; ++k)
{
Console.WriteLine("Enter a whole number or 999 to quit: ");
strTemp = Console.ReadLine();
while(!int.TryParse(strTemp, out intTemp))
{
Console.WriteLine("Input was not in the correct format");
Console.Write("Please enter a valid number: ");
strTemp = Console.ReadLine();
}
if(intTemp != 999)
{
intArray[k] = intTemp;
++numbersEntered;
}
else
{
k = 10;
}
}
The loop variable k goes out of scope when the code leaves the for loop. So, you can use variable k only within the loop.
So, you need to move your if-else inside your for loop. Change your FillArray() method to
private static int[] FillArray()
{
int[] intArray = new int[10];
int numbersEntered = 0;
int intTemp = 0;
string strTemp = "";
for (int k = 0; k < 10; ++k)
{
Console.WriteLine("Enter a whole number or 999 to quit: ");
strTemp = Console.ReadLine();
while (!int.TryParse(strTemp, out intTemp))
{
Console.WriteLine("Input was not in the correct format");
Console.Write("Please enter a valid number: ");
strTemp = Console.ReadLine();
}
if (intTemp != 999)
{
intArray[k] = intTemp;
++numbersEntered;
}
else
break;
}
Array.Resize(ref intArray, numbersEntered);
return intArray;
}
Also, I'd suggest changing ArrayAverage to double type. e.g. 2, 3 => average 2.5
double ArrayAverage = 0; //average need not be whole number
Additionally, with Linq you can shorten your Calculations method as
//using System.Linq;
private static void Calculations(int[] intArray, out int Highest, out int Lowest, out int intSum, out double average)
{
Lowest = intArray.Min();
Highest = intArray.Max();
intSum = intArray.Sum();
average = intArray.Average();
}

storing the result of a for loop into array and revrse it?

I want to store the result of the for loop in an array then reverse it.
Example:
When I run the program and enter 5 the answer will be
54321
I want it to be
12345
int num;
int index = 0;
Console.Write("Number: ");
num = int.Parse(Console.ReadLine());
for (int x = num; x > 0; x--)
{
index = Convert.ToInt32(index + x);
Console.Write(x);
}
Console.WriteLine("\nThe sum is: {0}", index);
Console.ReadLine();
How about this?
Console.Write("Number: ");
var num = int.Parse(Console.ReadLine()); // parse console input
var range = Enumerable.Range(1, num); //generate array of values from 1 to num
var str = String.Concat(range.Select(x => x.ToString())); //concatenate array of values
Console.WriteLine(str); // write string
var sum = range.Sum(); // get sum of array
Console.WriteLine("\nThe sum is: {0}", sum); // write sum
Console.ReadLine(); // pause
Ok, none of those responses really would do what you asked for, but I mut say your example is really misleading.
Try this:
public int[] ArrayAndReverse(int Number)
{
int[] data = new int[Number];
int index =
for (int x = Number; x > 0; x--)
{
index = Convert.ToInt32(index + x);
Console.Write(x);
}
return data.Reverse().ToArray();
}
now in your console code you can do:
Console.Write("Number: ");
num = int.Parse(Console.ReadLine());
int[] data = ArrayAndReverse(number);
foreach(int i in data)
Console.Write(i);
Also, not sure why your loop is already reversed, I suppose as I said this example is really misleading, else you can just use:
int[] data = Enum.Range(1, number).ToArray();
This will give you already the array in the right order.
Try this
For auto increment to total length
Use this
Console.WriteLine("Input length");
int length;
if (!int.TryParse(Console.ReadLine(), out length))
{
Console.WriteLine("Invalid number");
}
else
{
int[] array = new int[length];
for (int i = length; i > 0; i--)
{
array[length - i] = i;
}
Console.WriteLine("Array is");
foreach (var i in array)
{
Console.WriteLine(i);
}
Console.WriteLine("Reverse Array is");
foreach (var i in array.Reverse())
{
Console.WriteLine(i);
}
}
Console.ReadKey();
To take every number from user, try this
private static void Main(string[] args)
{
Console.WriteLine("Input length");
int length;
if (!int.TryParse(Console.ReadLine(), out length))
{
Console.WriteLine("Invalid number");
}
else
{
int[] array = new int[length];
for (int i = length; i > 0; i--)
{
int input;
Console.WriteLine("Input number");
if (!int.TryParse(Console.ReadLine(), out input))
{
Console.WriteLine("Invalid number");
input = 0;
}
array[length - i] = input;
}
Console.WriteLine("Array is");
foreach (var i in array)
{
Console.WriteLine(i);
}
Console.WriteLine("Reverse Array is");
foreach (var i in array.Reverse())
{
Console.WriteLine(i);
}
}
Console.ReadKey();
}

updating variable from inside a loop?

I'm writing a mastermind game and I need to update the value of an array size using a variable inside a while loop which increments on each loop is there any way i can do this?
bool game = false;
do
{
int codeSize;
int colourSize;
int guessNumber = 1;
int userGuess;
int black = 0;
int white = 0;
int count = 1;
Console.WriteLine("Welcome to Mastermind coded by ****");
Console.Write("How many positions > ");
codeSize = Convert.ToInt32(Console.ReadLine());
Console.Write("How many colours > ");
colourSize = Convert.ToInt32(Console.ReadLine());
Random rand = new Random();
int[] code = new int[codeSize];
int[] guess = new int[codeSize];
for (int i = 0; i < codeSize; i++)
{
code[i] = rand.Next(1, colourSize + 1);//filling the secret code array
}
Console.WriteLine("O.k. - I've generated a code -- guess it!");
while (black < codeSize)
{
int[,] history = new int[count, codeSize + 2];
Console.WriteLine("Next guess please.");
for (int n = 0; n < codeSize; n++)
{
Console.Write("Position " + guessNumber + " >");
userGuess = Convert.ToInt32(Console.ReadLine());
guess[n] = userGuess;
history[count - 1, n] = guess[n];
guessNumber++;
}
for (int x = 0; x < codeSize; x++)
{
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
{
if (guess[x] == code[x])
{
black++;
break;
}
goto case 2;
}
case 2:
{
if (guess[x] == code[x])
{
break;
}
int i = 0;
while (i < codeSize)
{
if ((guess[x] == code[i]) && (guess[i] != code[i]))
{
white++;
break;
}
i++;
}
break;
}
}
}
guessNumber = 1;
if (black == codeSize)
{
white = 0;
}
history[count - 1, codeSize + 1] = white;
history[count - 1, codeSize] = black;
count++;
Debug.WriteLine("-----------\nSecret code\n-----------");
for (int x = 0; x < codeSize; x++)
{
Debug.WriteLine(code[x]);
}
Console.WriteLine("Correct positions : {0}", black);
Console.WriteLine("Correct colours : {0}\n", white);
Console.WriteLine("History");
for (int t = 1; t < codeSize + 1; t++)
{
Console.Write(t + " ");
}
Console.WriteLine("B W");
for (int g = 0; g < codeSize + 3; g++)
{
Console.Write("--");
}
Console.Write("\n");
for (int t = 0; t < count - 1; t++)
{
for (int g = 0; g < codeSize + 2; g++)
{
Console.Write("{0} ", history[t, g]);
}
Console.WriteLine("\n");
}
if (codeSize > black)//reseting values for next turn
{
black = 0;
white = 0;
}
}
int play;
Console.WriteLine("\nYou Win!\n\nPress 1 to play again or any other number to quit");
play = Convert.ToInt32(Console.ReadLine());
if (play == 1)
game = true;
} while (game == true);
Arrays have a fixed size when you declare them and you cannot change the size afterwards without creating a new array. Try using a strongly typed List instead.
List<int> MyList = new List<int>();
// Add the value "1"
MyList.Add(1);
or the following for a table:
List<List<int>> MyTable = new List<List<int>>();
// Add a new row
MyTable.Add(new List<int>());
// Add the value "1" to the 1st row
MyTable[0].Add(1);
I believe you are asking whether you can change the length property of an array from within a loop, extending it as required.
Directly, no. There are helpers and classes which provide for such functionality, allocating more memory as needed, but I doubt this is what you really need. You could try using an array of fixed dimensions (the maximum codeSize your program will tolerate or expect), and then an integer next to it to record the length/position.
Alternatively, if you really need to expand to arbitrary sizes and store all codes, just use a List.
List<int[]> theList = new List<int[]>();
theList.Add(code);
Creates a list of integer arrays (your codes) that you can keep adding onto, and index just like any simple array.
There is a way to resize array size:
Array.Resize<T>
method. Details there: http://msdn.microsoft.com/en-us/library/bb348051(v=vs.110).aspx
But it's usually a pretty bad idea to resize the arrays loop based. You need to select another data structure to save your data or i.e. create an array of bigger size filled i.e. with zeroes.
You also need to add the items to the list as so. The list will dynamically grow based on it's size.
int myInt = 6;
List<int> myList = new List<int>();
myList.Add(myInt);

for loop - highscore array

So I'm trying to make this loop that goes through every index of highscore array(scorelist) to check if the current score(playerScore) is higher than the current index. If the if statement gets a "yes", then it should push all scores down(or is that higher?) along the index and put playerScore at where it should be.
{
Console.Write("input name: ");
string playerName = Console.ReadLine();
Console.Write("input score: ");
int playerScore = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < 10; i++)
{
if (playerScore > scoreList[i])
{
int nextNo = 9;
for (int n = 10; n > 0; n--)
{
scoreList[n] = scoreList[nextNo];
nameList[n] = nameList[nextNo];
nextNo--;
}
scoreList[i] = playerScore;
nameList[i] = playerName;
}
}
}
With this current code, it just places playerScore on the first index(IF it is higher than what was previously stored there), then copies it along the rest of the index. Any suggestions on how to fix this?
It can be done much easier:
int prev = playerScore;
for (int i = 0; i < 10; i++)
{
if (playerScore > scoreList[i])
{
int temp = scoreList[i];
scoreList[i] = prev;
prev = temp;
}
}
I assumed, that you only need top 10 results (and your array already has 10 elements in it).
Change your array to a List<int> and use this code:
var scorelist = new List<int>();
//populate your scorelist;
var playerScore = someintValue;
var firstlower = scorelist.FirstOrDefault(x => x < playerScore);
if (firstlower != null)
scorelist.Insert(playerScore, scoreList.IndexOf(firstlower);
else
scorelist.add(playerScore);
Edit:
If you need to crop the list to 10 items max after inserting, just:
scorelist = scorelist.Take(10).ToList();

Categories

Resources