Index out of Range exception in C# when iterating through an array - c#

I have this code I'm using to create a program that takes a a range and outputs to the Console the prime numbers. I have one problem, I'm trying to iterate through the array I built so the loop should only write to the console the values that are prime using my method's return value. The problem I'm having is that I have the second condition set to numArray.Length but it seems to give me the Index out of Range Exception. I just want the loop to iterate through all values in the numArray and stop when it's done figuring out whether the last value is prime or not.
public struct Prime
{
public int x;
// constructor for Prime
public Prime(int x1)
{
x = x1;
}
public int IsPrime(int number)
{
int i;
for (i = 2; i * i <= number; i++)
{
if (number % i == 0) return 0;
}
return 1;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter an Integer");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter a Second Integer of Greater Value");
// int num2 = 0;
int num2 = Convert.ToInt32(Console.ReadLine());
/* if (num2temp > num1)
{
num2temp = num2;
}
else
{
Console.WriteLine("You Did Not Enter An Integer Greater Than the First Integer, Please Enter Your Integers Again.");
Environment.Exit(0);
}
*/ int index = 1;
int[] numArray = new int[num2];
for (int i = num1; i <= num2; i++)
{
numArray[index] = i;
Console.WriteLine(" index: {0} assignment: {1}", index, i);
index++;
Console.WriteLine("index: {0}",index);
}
Console.WriteLine("value: {0}", numArray[40]);
/* Prime myprime = new Prime();
if (myprime.IsPrime(numArray[12]) == 1)
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("False");
} */
Prime myprime = new Prime();
int value = 0;
for (int y = 1; y <= num2; y++)
{
if (myprime.IsPrime(numArray[y]) == 1)
{
value = numArray[y];
Console.Write("{0} \t", value);
}
}

You're currently trying to iterate up to and including the size of the array. Arrays are 0-indexed in C#. So this:
int[] numArray = new int[num2];
for (int i = num1; i <= num2; i++)
should be:
for (int i = num1; i < num2; i++)
And note that to get at the first element array, num1 would have to be 0, not 1.
Likewise, your initial assignment of index as 1 should be 0 instead. Basically you need to go through all your code (it's confusing at the moment with lots of bits commented out) and check everywhere that you're assuming arrays are 1-based, and instead change your code as they're 0-based.
(In some cases you may just want to make the array one bigger, of course. If you want an array which logically contains the values 1 to x inclusive, you can either create an array of size x and subtract one from each index all the time, or create an array of size x + 1.)

Your index starts from 1 but should start from 0:
int index = 0; //CHANGE HERE
int[] numArray = new int[num2];
for (int i = num1; i <= num2; i++)
{
numArray[index] = i;
Console.WriteLine(" index: {0} assignment: {1}", index, i);
index++;
Console.WriteLine("index: {0}",index);
}
and then here y should also be 0 and check should if it is less than num2:
for (int y = 0; y < num2; y++)
{
if (myprime.IsPrime(numArray[y]) == 1)
{
value = numArray[y];
Console.Write("{0} \t", value);
}
}
because array indexing in C# start from 0.

Related

Write a program that determines whether two or more consecutive numbers are equal

Write a program that determines whether, in a sequence of N numbers entered by the user, if two or more consecutive numbers are equal. Print out, if any, the position of the first elements of the sequence of equal numbers.
This is what i got so far but it isn't working for some reason. What am I doing wrong?
using System;
public class Exercises
{
static void Main()
{
Console.WriteLine("Insert the length of the sequence of numbers:");
int n = Convert.ToInt32(Console.ReadLine());
List<int> seq = new List<int>();
int equalSeqStart = -1;
for (int i = 0; i < n; i++)
{
Console.WriteLine("Insert the number in position" + (i + 1) + ":");
seq.Add(i);
if ((seq[i] == seq[i - 1]) && (equalSeqStart == -1))
{
equalSeqStart = i - 1;
}
}
if (equalSeqStart != -1)
{
Console.WriteLine("The sequence of equal numbers starts at" + (equalSeqStart));
}
else
{
Console.WriteLine("There is no sequence of equal numbers");
}
}
}
All you have to do is to compare prior item with current one; you have no need in collections:
static void Main() {
Console.WriteLine("Insert the length of the sequence of numbers:");
//TODO: int.TryParse is a better choice
int n = int.Parse(Console.ReadLine());
int equalSeqStart = -1;
for (int i = 0, prior = 0; i < n; ++i) {
Console.WriteLine($"Insert the number in position {i + 1}:");
//TODO: int.TryParse is a better choice
int current = int.Parse(Console.ReadLine());
if (i > 0 && equalSeqStart < 0 && current == prior)
equalSeqStart = i;
prior = current;
}
if (equalSeqStart != -1)
Console.WriteLine($"The sequence of equal numbers starts at {equalSeqStart}");
else
Console.WriteLine("There is no sequence of equal numbers");
}

Calculate average grades, excluding the entered values that are the lowest and greatest

The task is to allow the user to enter values between 0 and 100. If the user enters -99 the program should stop. Once the program has exited, the average must be computed. However, the average should include all the entered values, except the lowest value entered (minimum) and the greatest value entered (maximum). I have written a fairly good-looking code, but it throws an IndexOutOfRangeException.
Here is that code:
class Program
{
static void Main(string[] args)
{
DisplayApp();
Calculate();
}
static void DisplayApp()
{
Console.WriteLine("Grade Calculator: ");
Console.WriteLine();
}
static double Calculate()
{
Console.WriteLine("Enter grades (-99 to exit): ");
string input = Console.ReadLine();
int[] array1 = new int[] { };
int iInput = int.Parse(input);
int min = 100;
int max = 0;
int i = 0;
int sum = 0;
double average = 0;
while(iInput != 99)
{
if(iInput < min)
{
array1[i] = min;
}
else if(iInput > max)
{
array1[i] = max;
}
sum += iInput - (min + max);
i++;
}
average = (double)sum / i;
return average;
}
}
What do you think could be improved in order for the program to work?
As I mentioned in the comments, you create an array of size zero. I don't see why you need an array at all, since you are only summing the values:
static double Calculate()
{
Console.WriteLine("Enter grades (-99 to exit): ");
int min = Int32.MaxValue;
int max = Int32.MinValue;
int sum = 0;
int i = 0;
while (true)
{
// TODO: Change to TryParse and handle input errors.
int iInput = int.Parse(Console.ReadLine());
if (iInput == -99) break;
if (iInput > 0 && iInput < 100) {
if (iInput < min)
{
min = iInput;
}
if (iInput > max)
{
max = iInput;
}
sum += iInput;
i += 1;
}
}
// TODO: Ensure no division by zero
return (double)(sum - max - min) / (i - 2);
}

Display sum of 10 input numbers but take out highest and lowest number

I want to input 10 numbers and get the total sum of them if you take out the highest and lowest number that was input. So basically it'll be 8 numbers that i get the sum of when i take out the highest and lowest number that was input out of the 10. So far i can only count the total sum out of the 10 numbers, but not sure how to take out the highest and lowest. What approach could i take?
static void Main(string[] args)
{
int number, sum = 0, n;
for (number = 1; number < 11; number++)
{
Console.WriteLine("Enter a number");
n = Convert.ToInt32(Console.ReadLine());
sum += n;
}
Points(sum);
Console.ReadLine();
}
static void Points(int sum)
{
Console.WriteLine("Totalpoint is " + sum);
}
Addition is additive, so you can simply remove them on the end:
int sum = 0;
int min = int.MaxValue;
int max = int.MinValue;
for (int i = 0; i < 10; ++i)
{
Console.WriteLine("Enter a number");
int n = Convert.ToInt32(Console.ReadLine());
sum += n;
min = Math.Min(min, n);
max = Math.Max(max, n);
}
sum -= min;
sum -= max;
Points(sum);
Console.ReadLine();
An easy implementation to explain my thought process, could be the following:
static void Main(string[] args)
{
int number, sum = 0, n;
List<int> inputNumbers = new List<int>();
for(number = 0; number < 10; number++)
{
inputNumbers.Add(Convert.ToInt32(Console.ReadLine()));
}
// Obtain maximum and minimum values
var maximum = inputNumbers.Max();
var mimimum = inputNumbers.Min();
foreach (var item in inputNumbers)
{
if(item == maximum)
{
inputNumbers.Remove(item);
break;
}
}
foreach(var item in inputNumbers)
{
if(item == mimimum)
{
inputNumbers.Remove(item);
break;
}
}
Console.WriteLine("Sum: "+inputNumbers.Sum());
Console.ReadKey();
}
The solution is rather simple, since after populating the List with the specified 10 values, you query it to obtain the minimum and maximum values.
When you know them, you can simply loop through the list and remove both the maximum and minimum (break is completely necessary after those operations).
Downside? In this simple implementation, you would be iterating through the List two times, which should definitely be optimized, even if for clean code sake.
But it might the best first step to you!
Just have two more variables to track max and min values as you read them:
min = Math.Min(min, n);
max = Math.Max(max, n);
Then, as you exit the loop, you can simply subtract those from the total sum before printing.
sum -= min + max;
You can try this:
List<int> testNum = new List<int>();
int num = 0;
int sum = 0;
try
{
// This will loop until you have 10 inputs
while(testNum.Count < 10)
{
Console.WriteLine("Enter a number:");
num = Convert.ToInt32(Console.ReadLine()); // Need to have the Try/Catch in case user doesn't input a number
testNum.Add(num);
}
// Get the min and max values (you will need System.Linq for this)
int minVal = testNum.Min();
int maxVal = testNum.Max();
// Remove the min and max values from the List
testNum.Remove(testNum.IndexOf(minVal));
testNum.Remove(testNum.IndexOf(maxVal));
// Sum the remaining values up
foreach(int n in testNum)
{
sum = sum + n;
}
return sum;
}
catch
{
throw;
}
Add the input numbers in the List and sum their values excluding min and max values
public static void Main(string[] args)
{
var list = new List<int>();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Enter number " + i);
int num = Convert.ToInt32(Console.ReadLine());
list.Add(num); //adding the input number in list
}
Sum(list);
}
private static void Sum(List<int> list)
{
int max = list.Max();
int min = list.Min();
int sum = list.Where(x => x != max && x != min).Sum(); //taking the sum of all values exlcuding min and max
Console.WriteLine("Sum is " + sum);
}

In my class i have to creating a method to find multiple maximum numbers in an array C#

In my class i have to create a method that will find multiple maximum numbers within an array. the user will input how many maximum numbers they are wanting and then the code should display all those numbers. if i select 2 this code will show me the 2 maximum numbers but if im wanting more it just doesnt work. i was trying to search the array for the maximum number and then have it display the outcome and then change that element to 0 and than repeat the loop until it hits that number (userinput). im soo frustrated can someone help me please
public static int FindingMaxNum(int[] arr, int max)
{
int userinput;
Console.Write("Enter the number of maximum values: ");
userinput = Convert.ToInt32(Console.Read());
int i = 0;
int c = 0;
for (i = 0; i < arr.Length; i++)
{
if (arr[i] >= max)
{
max = arr[i];
c++;
while (c <= userinput)
{
Console.WriteLine(max);
arr[i] = 0;
break;
}
}
}
return -1;
}
If I understand correctly, you want to get some number of items from an array whose values are greater than all the others in the array.
If so, something like this modified version of your method may do the trick:
public static void WriteMaxNum(int[] arr)
{
if (arr == null)
{
Console.WriteLine("The array is null");
return;
}
if (arr.Length == 0)
{
Console.WriteLine("The array is empty");
return;
}
int count = arr.Length;
int numMaxValues;
// Get number of max values to return from user
do
{
Console.Write("Enter the number of maximum values (1 - {0}): ", count);
} while (!int.TryParse(Console.ReadLine(), out numMaxValues) ||
numMaxValues < 1 ||
numMaxValues > count);
// Output the max values
Console.Write("The {0} max values are: ", numMaxValues);
Console.WriteLine(string.Join(", ", arr.OrderByDescending(i => i).Take(numMaxValues)));
}
Usage
static void Main(string[] args)
{
var myArray = new[] { 1, 9, 4, 8, 2, 5, 0, 7, 6, 3 };
WriteMaxNum(myArray);
Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
}
Output
Simple solution with average complexity O(nlogn):
Sort the array first then print the last N nos.
int[] arr = new int[]{-5,-69,1250,24,-96,32578,11,124};
Array.Sort(arr);
int n=3;
for(int i=arr.Length-1;i>=0 && n>0 ;--i){
n--;
Console.WriteLine(arr[i]);
}
Now coming to your code, there are multiple problems. First there is no need to take 'max' as parameter in your function. Second you are looping only arr.Length times once. There should be nested loop, outer one of which has to run userInput times and inner on has to iterate over all the values. Third you should initialize to extracted value position to minimum value so that the method works for negative numbers too.
Refined code:
for(int i=0;i<userinput;++i){
int max = int.MinValue,pos=-1;
for(int j=0;j<arr.Length;++j){
if(max<arr[j]){
pos = j;
max = arr[j];
}
}
arr[pos] = int.MinValue;
Console.Write(max+",");
}

C# Arrays in a Bowling Program. Wont use Lowest Score

I am trying to create a bowling program that when you enter in your name followed by your score, it will take the average, lowest, and highest scores of the players and print them. However for some reason I cannot get the lowest score to print, as when I hit enter twice, it will use the blank value instead of the lowest entered value and name. How can I fix this so that it will display the lowest score?
{
class Program
{
static void Main(string[] args)
{
const int SIZE = 10;
int i;
// create an array with 10 elements
string[] scoreInfo = new string[SIZE];
string[] names = new string[SIZE];
int[] scores = new int[SIZE];
for (i = 0; i < SIZE; i++)
{
// Prompt the user
Console.Write("Enter your first name and score on one line");
Console.WriteLine(" separated by a space.");
// Read one line of data from the file and save it in inputStr
string inputStr = Console.ReadLine( );
// if statement to break when the user enters a zero
if (inputStr == String.Empty)
{
break;
}
// The Split method creates an array of two strings
scoreInfo = inputStr.Split();
// Parse each element of the array into the correct data type
names[i] = scoreInfo[0];
scores[i] = int.Parse(scoreInfo[1]);
}
Console.WriteLine("The avarage score is {0}", AverageScore(scores, i));
Console.WriteLine("{0} scored the lowest at {1}", names[LowScore(scores, i--)], scores[LowScore(scores, i--)]);
Console.WriteLine("{0} scored the highest at {1}", names[HighScore(scores)], scores[HighScore(scores)]);
Console.ReadLine();
Console.ReadLine();
}
static int LowScore(int[] scores, int j)
{
int min = scores.Min();
return Array.IndexOf(scores, min);
}
static int HighScore(int[] scores)
{
int max = scores.Max();
return Array.IndexOf(scores, max);
}
static double AverageScore(int[] numbers, int j)
{
double average = 0;
for (int i = 0; i < j--; i++)
{
int product = 1;
product = numbers[i] * product;
average = product / j;
}
return average;
}
}
}
Use a different data structure and make less work for yourself.
Start with a dictionary that maps names to scores so that you don't have to mess around with indexes.
Then, LINQ is your friend, as you've already noticed. You don't need to create functions for things that already exist (like min/max/average).
eg.
Dictionary<string, int> ranking = new Dictionary<string, int>();
ranking.Add("adam", 20);
ranking.Add("bill", 10);
ranking.Add("carl", 30);
double avg = ranking.Average(kvp => (double)kvp.Value);
var sorted = ranking.OrderBy(kvp => kvp.Value);
var min = sorted.First();
var max = sorted.Last();
Console.WriteLine("Average: {0}", avg);
Console.WriteLine("Lowest: {0} with {1}", min.Key, min.Value);
Console.WriteLine("Highest: {0} with {1}", max.Key, max.Value);
Modify your Average function like this:-
static double AverageScore(int[] numbers, int j)
{
double sum = 0;
for (int i = 0; i < j; i++)
{
sum += numbers[i];
}
return (double)sum / j;
}
I am not sure why you were taking product of items for finding average of scores.
And Your Min function like this:-
static int LowScore(int[] scores, int j)
{
int min = scores.Where((v, i) => i < j).Min();
return Array.IndexOf(scores, min);
}
Here, you are passing the complete array of integer, so if only 3 players entered values, rest of the values will be initialized to default, i.e. 0 for int, Thus you were getting 0 as Min value.
Also, You need to change the method invocation like this:-
Console.WriteLine("{0} scored the lowest at {1}", names[LowScore(scores, i)], scores[LowScore(scores, i)]);

Categories

Resources