Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The following program is going in infinite loop when I am trying to access a multiple line input,do you have any idea why is it not working?
namespace AlternatingCharacters
{
class Program
{
static void Main(string[] args)
{
int N = Int32.Parse(Console.ReadLine());
string[] str = new string[N];
for (int i = 0; i < N ; i++)
{
str[i] = Console.ReadLine();
}
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; j < strArray.Length; j++)
{
if (strArray[i] == strArray[i + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
}
}
}
The problem is in this line:
for (int j = 0; i < strArray.Length; j++)
Your condition is checking on i, not on j, so i will always be 0 (start value) and will never change during the loop.
The right code is:
for (int j = 0; j < strArray.Length; j++)
After that it will fail on this line:
if (strArray[i] == strArray[i + 1])
At the end, it can't find 'the last index + 1' which you can prevent by subtracting one on the end, so this (also I think you need j here):
for (int j = 0; j < strArray.Length - 1; j++)
{
if (strArray[j] == strArray[j + 1])
{
count++;
}
}
The error comes from this piece of code:
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; i < strArray.Length; j++)
{
if (strArray[i] == strArray[i + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
First your loop-condition of the inner loop is whrong. While you increment j you check if i equals the number of elements in your array. More critical than this however is that you also use the whring array-elements. I guess you should use if (strArray[j] == strArray[j + 1]) instead of using i as index.
So all in all this should work:
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; j < strArray.Length; j++)
{
if (strArray[j] == strArray[j + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
Related
This question already has an answer here:
What is an "index out of range" exception, and how do I fix it? [duplicate]
(1 answer)
Closed 4 years ago.
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[6];
doors[i] = "#";
for (int j = 1; j <=i; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
Hi guys. I need to print # one and then # twice, until i get to six times. It says System.index.out.of.range. How come?
You should try to extend your array, it's limited to 6 elements but you try to access 7 elements as you go through 0 to 6.
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[7];
doors[i] = "#";
for (int j = 1; j <=i; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
If
I need to print # one and then # twice, until i get to six times.
You don't want any array - string[] doors = new string[6];, just loops:
for (int line = 1; line <= 6; ++line) {
for (int column = 1; column <= line; ++column) {
Console.Write('#');
}
Console.WriteLine();
}
If you have to work with array (i.e. array will be used somewhere else), get rid of magic numbers:
// Create and fill the array
string[] doors = new string[6];
for (int i = 0; i < doors.Length; i++)
doors[i] = "#";
// Printing out the array in the desired view
for (int i = 0; i < doors.Length; i++) {
for (int j = 0; j < i; j++) {
Console.Write(doors[j]);
}
Console.Writeline();
}
Please, notice that arrays are zero-based (array with 6 items has 0..5 indexes for them)
because it is out of range.
change it to this:
for (int i = 0; i <= 6; i++)
{
string[] doors = new string[6];
doors[i] = "#";
for (int j = 0; j <=i.length; j++)
{
Console.Write(doors[j]);
}
Console.Writeline():
}
No need to use 2 loops. Just repeat that character
for (int i = 0; i <= 6; i++)
{
Console.Write(new String("#",i));
Console.WriteLine():
}
For this code in Visual Studio
Point[,] point = new Point[9, 10];
for (int i = 0; i < 9; i++)
{
for(int j = 0; i < 10; j++)
{
point[i, j].X = i;//mark1
point[i, j].Y = j;
}
}
at //mark1,system tell me"index exceeded the number of group boundaries"
Why?
you are doing
for (int j = 0; i < 10; j++)
so the condition i < 10 which is maybe a typo is causing your loop to go out of the range in the array (you are then trying to access the eleement 0, 10 in the array)
replace that with:
for (int j = 0; j < 10; j++)
I'm writing a program to find the largest palindromic number made from product of 3-digit numbers. Firstly,i Create a method which has ability to check if it is a palindromic number. Here is my code :
static int check(string input_number)
{
for (int i = 0; i < input_number.Length/2; i++)
if (input_number[i] != input_number[input_number.Length - i])
return 0;
return 1;
}
After that, it's my main code:
static void Main(string[] args)
{
int k = 0;
for (int i = 0; i < 999; i++)
for (int j = 0; j < 999; j++)
{
k = i * j;
if (check(k.ToString()) == 1)
Console.Write(k + " ");
}
}
But when it has a problem that the length of input_number is zero. So my code doesn't run right way. What can I do to solve the length of input_number?
You have a few bugs in your code:
1. 3-digit-numbers range from `100` to `999`, not from `0` to `998` as your loops currently do.
So your Main method should look like this:
static void Main(string[] args)
{
int k = 0;
for (int i = 100; i <= 999; i++)
for (int j = 100; j <= 999; j++)
{
k = i * j;
if (check(k.ToString()) == 1)
Console.Write(k + " ");
}
}
Now all pairs of three digit numbers are checked. But to improve performance you can let j start at i, because you already checked e.g. 213 * 416 and don't need to check 416 * 213 anymore:
for (int i = 100; i <= 999; i++)
for (int j = i; j <= 999; j++) // start at i
And since you want to find the largest, you may want to start at the other end:
for (int i = 999; i >= 100; i--)
for (int j = 999; j >= 100; j--)
But that still does not guarantee that the first result will be the largest. You need to collect the result and sort them. Here is my LINQ suggestion for your Main:
var results = from i in Enumerable.Range(100, 900)
from j in Enumerable.Range(i, 1000-i)
let k = i * j
where (check(k.ToString() == 1)
orderby k descending
select new {i, j, k};
var highestResult = results.FirstOrDefault();
if (highestResult == null)
Console.WriteLine("There are no palindromes!");
else
Console.WriteLine($"The highest palindrome is {highestResult.i} * {highestResult.j} = {highestResult.k}");
2. Your palindrome-check is broken
You compare the character at index i to input_number[input_number.Length - i], which will throw an IndexOutOfRangeException for i = 0. Strings are zero-based index, so index of the last character is Length-1. So change the line to
if (input_number[i] != input_number[input_number.Length - i - 1])
Finally, I suggest to make the check method of return type bool instead of int:
static bool check(string input_number)
{
for (int i = 0; i < input_number.Length/2; i++)
if (input_number[i] != input_number[input_number.Length - i - 1])
return false;
return true;
}
This seems more natural to me.
You can use method below. Because you are trying to find the largest number you start from 999 and head backwards, do multiplication and check if its palindrome.
private void FindProduct()
{
var numbers = new List<int>();
for (int i = 999; i > 99; i--)
{
for (int j = 999; j > 99; j--)
{
var product = i * j;
var productString = product.ToString();
var reversed = product.Reverse();
if (product == reversed)
{
numbers.Add(product);
}
}
}
Console.WriteLine(numbers.Max());
}
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testing_random
{
class Program
{
static void Main(string[] args)
{
int n = 4;
int[,] a = new int[n,n];//declaring the matrix
Random o = new Random();
a[0,0] = o.Next(n);
for (int i = 1; i < n; i++)//filling the first line
{
int d = 1;
while (d != 0)
{
a[i,0] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i,0] == a[j,0])
d++;
}
}
for (int i = 1; i < n; i++)//filing the first column
{
int d = 1;
while (d != 0)
{
a[0, i] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[0, i] == a[0, j])
d++;
}
}
for (int k = 1; k < n; k++)//filling the rest of the matrix
for (int i = 1; i < n; i++)
{
int d = 1;
while (d != 0)
{
a[i, k] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i, k] == a[j, k])
d++;
for (int j = 0; j < k; j++)
if (a[i, k] == a[i, j])
d++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write("{0} ", a[i, j]);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
The output should be a matrix of 4*4 where each column and each line contains each number once.
The problem is when i run the code, not every time i get an output, i think the problem is not every set of first line and column can give a matrix as required the i get into an unending loop.
what i want to do is limit the running time of the application to 100 ms per example,so if the matrix is not filled,the program restarts
What piece of code am i missing?
replace the while( d != 0 ) with a loop which counts up to a certain very large maximum number of iterations. (Try 1000, 100000, whatever.)
Are you trying to randomly insert numbers between 1-4 in the first line of the array? If so there is a much easier way to do it.
You can generate the 4 numbers to be inserted into the array and then just look through the first line of the array and set each value.
Random rnd = new Random();
var randomNumbers = Enumerable.Range(1, 4).OrderBy(i => rnd.Next()).ToArray();
for (int i = 0; i < n; i++)
{
a[i, 0] = randomNumbers[i];
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Currently designing an application that will compute for the sale of salesment for 4 month given an initial sale. The sale is expected to raise by 5% every month in 4 months.
ProduceSalesProjectionTable function seems to be giving me an out of bound error and i don't know why can't seem to figure it out.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace itse
{
class TempAgency
{
static string[] salesman1 = new string[4];
static double[,] sales1 = new double[4, 5];
static void DisplayInstructions()
{
Console.WriteLine("You will be asked to enter data for four salesmen.");
Console.WriteLine("For their name, enter their first name followed");
Console.WriteLine("by a space and then their las name.");
Console.WriteLine("");
Console.WriteLine("For testing purposes enter data for four <4> salesmen.");
Console.WriteLine("");
Console.WriteLine("");
}
static void GetSalesData(string[] salesman, double[,] sales)
{
for (int x = 0; x < 4; x++)
{
Console.WriteLine("Enter name: ");
salesman[x] = Console.ReadLine();
string [] split = salesman[x].Split(new Char [] {' '});
Console.WriteLine(split[0]+"'s Sales goal: ");
sales[x,0]=Convert.ToDouble(Console.ReadLine());
}
}
static void ProduceSalesProjectionTable(double[,] sales)
{
for (int i = 0; i < 4; i++)
{
double salesPercentage = 0.05;
for (int j = 0; j < 4; i++)
{
sales[i, j + 1] = sales[i, 0] + (sales[i, 0] * salesPercentage); //here lies the problem
salesPercentage += 0.05;
}
}
}
static void DisplaySalesProjections()
{
}
public static void Main(String[] args)
{
DisplayInstructions();
GetSalesData(salesman1, sales1);
//Console.WriteLine("salesman: " + salesman[0]);
//Console.WriteLine("sales: " + sales[0, 0]);
ProduceSalesProjectionTable(sales1);
//for (int i = 0; i < 4; i++)
//{
// //double salesPercentage = 0.05;
// for (int j = 0; j < 4; i++)
// {
// Console.WriteLine("salesman: " + salesman[i] + "\tsales: " + sales[i, j]);
// }
//}
//Console.WriteLine("sales:" + sales[0, 1]);
//Console.WriteLine("sales:" + sales[1, 1]);
//Console.WriteLine("sales:" + sales[2, 1]);
//Console.WriteLine("sales:" + sales[3, 1]);
for (int i = 0; i < 4; i++)
{
//double salesPercentage = 0.05;
for (int j = 0; j < 4; i++)
{
Console.WriteLine("salesman: " + salesman1[i] + "\tsales: " + sales1[i, 0]);
}
}
Console.ReadLine();
}
}
}
Look carefully at the line
for (int j = 0; j < 4; i++)
Your indexer is j but you are increasing i instead of j.
Eventually sales[i, j + 1] evaluates to sales[4, 1] which is out of bounds.
Solution: change your code to
for (int j = 0; j < 4; j++)
(You have done this in 2 places)
If you build your for-loops like this, there should be no "out of bound error":
static void ProduceSalesProjectionTable(double[,] sales)
{
for (int i = 0; i < sales1.GetLength(0); i++)
{
for (int j = 0; j < sales1.GetLength(1); i++)
{
...Do Stuff...
}
}
}
When you use 2-dimensional arrays GetLength(0) gives you the length of the first dimension of your 2-dimensional array and GetLength(1) gives you the length of he second dimension.