Programmr "Reverse Array" C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I use Programmr.com to improve my skills. I have a problem with this exercise.
Question:
Write the program which takes input 10 elements in the array and
reverse the elements in the array without using any other array.
Example :
1.If user gives input 1,2,3,4,5,5,4,7,3,6 then output should be in this format: 6 3 7 4 5 5 4 3 2 1
2.If user gives input 25,23,,26,12,45,65,58,24,27,13 then output should be in this format: 13 27 24 58 65 45 12 26 23 25
Note : Make sure the output format is same as above given example.
My code:
using System;
class ReverseArray{
static void Main(string[] args){
int[] arr = new int[10];
for(int i =0;i<10;i++)
{
Console.WriteLine("Enter the array elements["+i+"]:");
arr[i]=int.Parse(Console.ReadLine());
}
//write your logic here
for(int i = arr.Length-1; i >= 0; i--)
{
Console.Write(arr[i] + " ");
}
//end
for(int i =0;i<10;i++)
{
Console.Write(arr[i]+" ");
}
}
}
Programmr doesn't pass task and write that there are undefined errors. I also tried method Array.Reverse.

Use Linq reverse method and string.join
You can:
Using System.Linq;
Console.WriteLine(String.Join(" ", arr.Reverse()));
That will reverse the array and print the list separated by spaces to the console

Related

make dictionary that will loop every 9 numbers. C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Im new to programming and sorry if I cant explain properly. I am trying to iterate through a list that has items in it in multiples of 9. So the list can have 9,18,27.. items.
I have the code working for it to read when there is 9 using this dictionary. But I would like it work for any amount in multiples of 9.
var alphabets = new Dictionary<int, string>()
{
{1,"A2"},{2,"B2"},{3,"C2"},{4"D2"},
{5,"E2"},{6,"F2"}, {7,"G2"},{8,"H2"},
{9,"I2"}
};
So for example if there was 18 items it would like this dictionary to have this function.
var alphabets2 = new Dictionary<int, string>()
{
{1,"A2"},{2,"B2"},{3,"C2"},{4"D2"},
{5,"E2"},{6,"F2"}, {7,"G2"},{8,"H2"},
{9,"I2"},
{10,"A3"},{11,"B3"},{12,"C3"},{13"D3"},
{14,"E3"},{15,"F3"}, {16,"G3"},{17,"H3"},
{18,"I3"}
};
Thank you
As #DiplomacyNotWar commented, it sounds as if you need to input int value to convert to a correlating string value which is uniformly based on multiples of 9. If this is the case, I agree with #DiplomacyNotWar that you don't need to store anything but create a function to output the needed string value based on an int value. Here is a function that will output the pattern in your examples.
// value should be 0
string ConvertIntToSpecificString(int value)
{
// this will give you an int value 0-8
var modValue = (value - 1) % 9;
// The unicode for 'A' is 65
var firstCharValue = (char)(65 + modValue);
// This will return a whole number giving the iteration count. FE: 19 / 9 = 2
// Adding 2 to fit the pattern stated in the examples.
var secondValue = ( value / 9 ) + 2 ;
return $"{firstCharValue}{secondValue}";
}

Regular expression for value Range from 1 - 1440 (Integer) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What will be the regular expression for value Range from 1 - 1440? (Integer)
I think this will do it. I'm running tests. I'm acting under the assumption you will not have numbers padded with zero like "0999"
"^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"
breakdown:
[1-9] obvious
[1-9[0-9] allow 10 to 19
[1-9][0-9][0-9] allow 100 to 199
1[0-3][0-9][0-9] allow 1000 to 1399
14[0-3][0-9] allow 1400 to 1439
1440 obvious
Appears to work:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
for(int i = 0;i < 10000;i++)
{
if(Regex.IsMatch(i.ToString(),"^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"))
{
Console.WriteLine(i);
}
}
}
}

Write program which count sum of numbers divided by 4 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to write a program which count sum of numbers divided by 4. I'm really beginner in C#.
Could you lead me to solve this problem?
I write something like this but I don't know what next and there is one problem because I put only two numbers but numbers could be a lot of.
Console.WriteLine("number a");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("number b");
int b = int.Parse(Console.ReadLine());
int sum = a + b;
int result = sum / 4 ;
Console.WriteLine ( "result is="+ result ) ;
use
double sum = Console.ReadLine().Split().Sum(x => int.Parse(x)) / 4.0;
Than you can enter any space separated array like
1 3 5 7

How to write arrays to tab delimited text file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have two arrays -
int[] array1 = { 1, 2};
int[] array2 = { 6, 7};
I want to write them in text file delimited by tab.
Final Result -
1 6
2 7
Please suggest how to do this?
Assuming both arrays are of the same length:
File.WriteAllLines(filename,
Enumerable.Range(0, arr1.Length)
.Select(i => string.Format("{0}\t{1}", arr1[i], arr2[i]));
You could do something like this:
public void writeFile(int[] array){
using (streamWriter sw = new StreamWriter(filename, true){
for(int i = 0; i < array.Length; i++){
sw.WriteLine(array[i] + "\t");
}
}
}
Then you would just call the write method like this:
writeFile(array1);
writeFile(array2);

How to numerically sort an array of strings? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an array of strings that has a number at the beginning, followed by a name. How can I sort the array numerically; ie
40 Mike, 25 Tyson, 9 Jackson, 5 Phillip, 3 Mitchell
as opposed to
9 Jackson, 5 Phillip, 40 Mike, 3 Mitchell, 25 Tyson
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] items = {"10", "1", "30", "-5"};
Array.Sort(items, (x, y) =>
{
int xNum = int.Parse(x);
int yNum = int.Parse(y);
return Comparer<int>.Default.Compare(xNum, yNum);
});
foreach(var item in items)
Console.WriteLine(item);
}
}
var sorted = yourArray.OrderBy(x => int.Parse(x.Split()[0])).ToArray();
You can use SortedDictionary data structure to obtain an array sorted.

Categories

Resources