Array ints and replace the order of number I write [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 8 years ago.
Improve this question
I try Start the console and after i write 5 numbers is do exsepsion what wrong in code
what the console do is after I write 5 number for example 7,4,9,5,1 ---> 1,5,9,4,7
this what I wrote
static void Main(string[] args)
{
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}
Console.Read();

You missed two things. First - in order to swap two array items, you need to store item somewhere. Otherwise you just replace one item with other. Second - arrays have indexes from 0 to Length - 1. So you need to subtract 1 from second item index:
for (int i = 0; i < numbers.Length / 2; i++)
{
int x = numbers[i];
numbers[i] = numbers[numbers.Length - i - 1];
numbers[numbers.Length - i - 1] = x;
}

Related

How can I calculate numbers (for example) from 1 - 10 with the next number. (1+2+3+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 1 year ago.
Improve this question
Does it have a shorter way than doing something like this:
int[] Summe = new int[6];
Summe[0] = 1;
Summe[1] = 2;
Summe[2] = 3;
Summe[3] = 4;
Summe[4] = 5;
Summe[5] = 6;
Console.WriteLine(Summe[0] + Summe[1] + Summe[2] + Summe[3] + Summe[4] + Summe[5]);
Console.ReadKey();
Using the Enumerable.Sum() method which computes the sum of a sequence of numeric values and Enumerable.Take() which returns a specified number of contiguous elements from the start of a sequence.
Console.WriteLine(Summe.Take(10).Sum());
OR from high school
// sum of integers from 1 to n
int SumNaturalNumbers(int n)
{
return n * (n + 1) / 2;
}
Formula for the general algebraic series starting from a and difference between terms d (Arithmetic progression and series)
sum = n / 2 * (2 * a + (n - 1) * d)
I think using a foreach loop may be helpful here, since you're dealing with an array. Also, you can define your array on one line.
int[] Summe = { 1, 2, 3, 4, 5, 6 };
int total = 0;
foreach (int number in Summe)
{
total += number;
}
Console.WriteLine(total);
Console.ReadKey();
You can simplify this process by simply putting this operation into a while loop.
int i = 0; // tell your program what 'i' should be upon first running the code
while (i < 10) // if 'i' is less than 10, run this block of code. You can change 10 to anything you want
{
Console.WriteLine("i = {0}", i);
i++; // increment
}
This will print each number individually, but you want to calculate the sum of every number, so you could do something like this:
{
public static void Main()
{
int j, sum = 0;
Console.Write("\n\n");
Console.Write("Find the sum of first 10 natural numbers:\n");
Console.Write("----------------------------------------------");
Console.Write("\n\n");
Console.Write("The first 10 natural number are :\n");
for (j = 1; j <= 10; j++)
{
sum = sum + j; // add the previous number to the current one
Console.Write("{0} ",j);
}
Console.Write("\nThe Sum is : {0}\n", sum);
}
}
The above code prints the sum of the first 10 natural numbers. I added some additional lines simply to make the program more legible. Again, you can change the number 10 to whatever number you want.

plusMinus function from Hacker Rank [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
I'm trying to solve a task from HackerRank, but I'm not able to solve it. Any help is appreciated!
public static void plusMinus(List<int> arr)
{
float noOfPositive = 0;
float noOfNegative = 0;
float noOfZero = 0;
float length = arr.Count;
for(var i = 0; i < arr.Count; i++){
if(i > 0){
noOfPositive += 1;
}else if(i == 0){
noOfZero += 1;
}else{
noOfNegative += 1;
}
}
Console.WriteLine($"{noOfPositive/length}\n{noOfNegative/length}\n{noOfZero/length}");
input:
6
-4 3 -9 0 4 1
My Result:
0.8333333
0
0.1666667
Expected:
0.500000
0.333333
0.166667
You are checking the value of i not arr[i].
So your code should be:
for(var i = 0; i < arr.Count; i++){
if(arr[i] > 0){
noOfPositive += 1;
}else if(arr[i] == 0){
noOfZero += 1;
}else{
noOfNegative += 1;
}
}
And your expected values are wrong, you are putting in 7 items and expecting half of them to be positive so 3.5 items are positive?

How do you find the positive sum of an array that also has negative numbers? [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 2 years ago.
Improve this question
public static int PositiveSum(int[] arr)
{
int sum = 0;
for(int i = 0; i > arr.Length; i++)
{
sum+=arr[i];
}
return sum;
}
Example:
[1,-4,7,12] => 1 + 7 + 12 = 20
Note: if there is nothing to sum, the sum defaults to 0.
One way, with minimum changes to your code, is to add a condition inside the for loop that sums the values allowing only positive numbers to be added.
Something you must change is the condition in the loop, i > arr.Length will break the cycle immediately, even if Length is 0, it needs to be i < arr.Length.
public static int PositiveSum(int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] > 0)
sum += arr[i];
}
return sum;
}
I would try filtering the arr to only contain positive numbers, then use sum on that new list:
public static int PositiveSum(int[] arr)
{
var pos = arr.Where(x => x > 0);
int sum = pos.Sum();
return sum;
}

Program generates 50 random numbers (between 1 to 10) and tells the amount of values that are smaller than 5 and bigger than 5 (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 5 years ago.
Improve this question
Program generates 50 random numbers (between 1 to 10) and tells the amount of values that are smaller than 5 and bigger than 5 (C#)
Try this:
int n = 50
IList<int> randomNumbers = new List<int>(n);
Random ran = new Random(1);
for (int i = 0; i < n; i++)
{
randomNumbers.Add(ran.Next(1, 10));
}
int lessThan5Count = randomNumbers.Count(c => c < 5);
int greaterThan5Count = randomNumbers.Count(c => c > 5);
You could do it like that:
Random r = new Random();
int n = 50;
int smaller_than_5 = 0;
int bigger_than_5 = 0;
double[] d = new double[n];
for (int i = 0; i < n; i++)
{
d[i] = 1 + r.NextDouble() * 9;
if (d[i] < 5) smaller_than_5++;
else if (d[i] > 5) bigger_than_5++;
}
Hope this helps ...

How to compute the average for every n number in a list [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
Having a List of numbers, I want to take every n(e.g. 5 , 10 , etc.) element, compute their average and put the average in a new list. As an example lets assume we have the following list:
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8
Now we compute the average every 2 elements and we will get the following list as output:
1.5 , 3.5 , 5.5 , 7.5
How can I do that?
You could use a for-loop and Enumerable.Average:
var averages = new List<double>();
for (int i = 0; i < ints.Length; i += 2)
{
int thisInt = ints[i];
int nextInt = i == ints.Length - 1 ? thisInt : ints[i + 1];
averages.Add(new[] { thisInt, nextInt }.Average());
}
Here's a dynamic approach that works with any length:
int take = 2;
for (int i = 0; i < ints.Length; i += take)
{
if(i + take >= ints.Length)
take = ints.Length - i;
int[] subArray = new int[take];
Array.Copy(ints, i, subArray, 0, take);
averages.Add(subArray.Average());
}
This problem is just testing your use of iteration and the modulus operator. Modulus gives you the remainder of division, you can use it to check whether or not the current number should be included in the average as you iterate the array. Here is a sample method;
public float nthsAverage(int n, int[] numbers)
{
// quick check to avoid a divide by 0 error
if (numbers.Length == 0)
return 0;
int sum = 0;
int count = 0;
for (int i = 0; i < numbers.Length; i++)
{
// might want i+1 here instead to compensate for array being 0 indexed, ie 9th number is at the 8th index
if (i % n == 0)
{
sum = sum + numbers[i];
count++;
}
}
return (float)sum / count;
}
public List<double> Average(List<double> number, int nElement)
{
var currentElement = 0;
var currentSum = 0.0;
var newList = new List<double>();
foreach (var item in number)
{
currentSum += item;
currentElement++;
if(currentElement == nElement)
{
newList.Add(currentSum / nElement);
currentElement = 0;
currentSum = 0.0;
}
}
// Maybe the array element count is not the same to the asked, so average the last sum. You can remove this condition if you want
if(currentElement > 0)
{
newList.Add(currentSum / currentElement);
}
return newList;
}

Categories

Resources