Series problems forloop - c#

i have a problem in my series number that will output the 1,2,4,7,11 and etc. i have my forloop that handle the 0,1,2,3,4,5 but im having trouble to progress the 1,2,4,7,11 output please help me this is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for (int v = 0; v <= 5; v++)
{
for (int x = 1; x <= 5; x++)
{
int c = v + x;
}
}
Console.ReadKey();
}
}
}

ok try this...
class Program
{
static void Main(string[] args)
{
int c = 1;
for (int v = 0; v <= 5; v++)
{
c = c + v;
Console.Write("{0} ", c);
}
Console.ReadKey();
}
}
I hope it will help you...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var iterations = 50;
var result = 0;
for (int i = 0; i < iterations; i++)
{
result += i;
}
Console.WriteLine(result);
Console.ReadKey();
}
}
}

You can try this to control number to calculate from console not by code:
static void Main(string[] args)
{
Console.WriteLine("Enter a number to calculate: ");
int num = Convert.ToInt32(Console.ReadLine());
Fib(0, 1, 1, num);
}
public static void Fib(int i, int j, int count, int num)
{
Console.WriteLine(i);
if (count < num) Fib(j, i+j, count+1, num);
}

I got it.
int x = 1;
for (int v = 0; v <= 5; v++)
{
int c = x + v;
x = c;
Console.Write(c);
}
Console.ReadKey();

It seems something like this :
var f1 = 0;
var f2 = 1;
for (int i = 1; i < 7; i++)
{
Console.WriteLine(f1);
f1 = f2;
f2 = f2 + i;
}
Output :
0, 1, 2, 4, 7, 11

This should work.
int i=1;
int j=0;
while(i<50)
{
i+=j;
j+=1;
Console.Writeln(i)
}

I hope , it should be solve the problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace While_loop
{
class Program
{
static void Main(string[] args)
{
int i = 1;
Console.WriteLine(i);
while (i < 10)
{
for (int j = 1; j < 5; j++)
{
i = i + j;
Console.WriteLine(i);
}
i++;
}
Console.ReadKey();
}
}
}

Related

Problem with UnitTest(Method doesn`t work properly)

I have a matrix calculator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp11
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Введите размерность первой матрицы: ");
int[,] A = new int[Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine())];
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
{
Console.Write("A[{0},{1}] = ", i, j);
A[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("Введите размерность второй матрицы: ");
int[,] B = new int[Convert.ToInt32(Console.ReadLine()), Convert.ToInt32(Console.ReadLine())];
for (int i = 0; i < B.GetLength(0); i++)
{
for (int j = 0; j < B.GetLength(1); j++)
{
Console.Write("B[{0},{1}] = ", i, j);
B[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("\nМатрица A:");
Print(A);
Console.WriteLine("\nМатрица B:");
Print(B);
Console.WriteLine("\nМатрица C = A * B:");
int[,] C = Multiplication(A, B);
Print(C);
}
static int[,] Multiplication(int[,] a, int[,] b)
{
if (a.GetLength(1) != b.GetLength(0)) throw new Exception("Матрицы нельзя перемножить");
int[,] r = new int[a.GetLength(0), b.GetLength(1)];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
for (int k = 0; k < b.GetLength(0); k++)
{
r[i, j] += a[i, k] * b[k, j];
}
}
}
return r;
}
static void Print(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("{0} ", a[i, j]);
}
Console.WriteLine();
}
}
static int[,] Multiplication_test(int[,] a_test, int[,] b_test) // типо забыл поменять что-то
{
int[,] r = new int[a_test.GetLength(0), b_test.GetLength(1)];
for (int i = 0; i < a_test.GetLength(0); i++)
{
for (int j = 0; j < b_test.GetLength(1); j++)
{
for (int k = 0; k < b_test.GetLength(0); k++)
{
r[i, j] -= a_test[i, k] * b_test[k, j];
}
}
}
return r;
}
}
}
And I have a UnitTest Program
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ConsoleApp11;
using System.Threading.Tasks;
namespace UnitTestProject2
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
int[,] A = { { 1, 2, 3 }, { 3, 4, 5 } };
int[,] B = { { 3, 4 }, { 1, 2 }, { 3, 5 } };
int[,] expected = { { 14, 23 }, { 28, 45 } };
int [,] actual = Multiplication_test(A, B);
Assert.AreEqual(expected, actual, "Multiplication_test1 isn`t correct ");
}
}
}
All in all is fine , but i have a problem
Severity Code Description Project File Line Suppression State
Error CS0103 The name 'Multiplication_test' does not exist in the current context UnitTestProject
I did more things but nothing changed...
I tried change static method to public but nothing changed. Please help me !!!
Multiplication_test is a static method on Program, so it should be Program.Multiplication_test(...)

Memory error NZEC when using C# on HackerEarth

I am solving a question on HackerEarth. The test case passes successfully, but the rest of the cases show a NZEC error. I know NZEC stands for Non Zero Exit Code but I am not sure why this is happening here.
Any insight about why this happens and how I can optimize my code (preferably without changing the logic) will help.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int t = int.Parse(Console.ReadLine());
int[] arr = new int[3];
for(int m = 0; m < t; m++)
{
arr[m] = int.Parse(Console.ReadLine());
}
for (int n = 0; n < t; n++)
{
int i, j;
for (i = 0; i < arr[n]; i++)
{
for (j = 0; j < 2 * arr[n]; j++)
{
if (j <= i || j >= 2 * arr[n] - 1 - i)
{
Console.Write("*");
}
else
{
Console.Write("#");
}
}
Console.WriteLine("\n");
}
Console.WriteLine("\n");
}
}
}

Ask 10 numbers then add the together

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int tulos = 0;
for (int i = 0; i < 10; i++)
{
Console.Write("Anna kokonaisluku: ");
String Luku = Console.ReadLine();
int annettu = int.Parse(Luku);
tulos = laske_pluslasku(annettu);
}
Console.WriteLine("Lukujen summa on " + tulos);
Console.ReadKey();
}
static int laske_pluslasku(int luku)
{
int lasku = 0;
lasku += luku;
return lasku;
}
}
}
The program should ask 10 numbers in a loop and then add them together in "static it". When return the sum and print it.
I should get a print like this
My problem is that it won't add all the 10 numbers together. It only displays the last given number. I think that is because of "int lasku = 0;".
The problem is that your adding a number to 0 then setting your final variable to the number you just sum.
Change your code to (If you really need to sum it on a method):
static void Main(string[] args)
{
int tulos = 0;
for (int i = 0; i < 10; i++)
{
Console.Write("Anna kokonaisluku: ");
string luku = Console.ReadLine();
int annettu = int.Parse(luku);
tulos = laske_pluslasku(tulos, annettu);
}
Console.WriteLine("Lukujen summa on " + tulos);
Console.ReadKey();
}
static int laske_pluslasku(int tulos , int annettu)
{
return tulos + annettu;
}
Or a simplier way
static void Main(string[] args)
{
int tulos = 0;
for (int i = 0; i < 10; i++)
{
Console.Write("Anna kokonaisluku: ");
string luku = Console.ReadLine();
int annettu = int.Parse(luku);
tulos += annettu;
}
Console.WriteLine("Lukujen summa on " + tulos);
Console.ReadKey();
}
List<int> sumList = new List<int>();
for (int i = 0; i < 10; i++)
{
Console.Write("Anna kokonaisluku: ");
String Luku = Console.ReadLine();
int annettu = int.Parse(Luku);
sumList.Add(annettu);
}
int result = sumList.Sum();

While cycle and inputs

I have code like this, how can i add 4 more inputs? (5 in total) and program would return the square of them(num*num, dont know how i should be in english, maybe correct?) Thank you very much for your time!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter number to get square");
int num = Convert.ToInt16(Console.ReadLine());
int ruut = num * num;
while (num!=1000)
{
Console.WriteLine(ruut);
break;
}
Console.ReadLine();
}
}
}
`
use a for loop and an array. Here's an example in the simplest code possible:
int[] num = new int[5];
for(int i = 0; i < 5; i++)
{
num[i] = Convert.ToInt16(Console.ReadLine());
}
for(int i = 0; i < 5; i++)
{
Console.WriteLine(num[i] * num[i]);
}

Calculating Mahalanobis distance with C#

I'm trying to calculate the mahalanobis distance with c#. I can't find any real good examples online and I'm new to C#. I am especially having trouble getting the covariance matrix to run right. Any help would be appreciated. Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathNet.Numerics.LinearAlgebra.Double;
namespace MahalanobisDistance
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
DenseVector vector1 = new DenseVector(4);
DenseVector vector2 = new DenseVector(4);
DenseMatrix matrix1 = new DenseMatrix(vector1.Count/2);
vector1[0] = 1;
vector1[1] = 2;
vector1[2] = 3;
vector1[3] = 4;
vector2[0] = 2;
vector2[1] = 12;
vector2[2] = 14;
vector2[3] = 18;
matrix1 = p.twoPassCovariance(vector1, vector2);
for(int i = 0; i < matrix1.RowCount; i++)
{
for(int k = 0; k < matrix1.ColumnCount; k++)
{
Console.Write(matrix1[k, i] + " ");
}
//Mahalanobis2(v1, v2, covariance);
Console.Write("\n");
}
}
public DenseMatrix twoPassCovariance(DenseVector data1, DenseVector data2)
{
int n = data1.Count;
double mean1 = data1.Average();
double mean2 = data2.Average();
DenseMatrix covariance = new DenseMatrix(data1.Count);
double x;
for(int i = 0; i < 2; i++)
{
for (int k = 0; k < n; k++)
{
double a = data1[i] - mean1;
double b = data2[k] - mean2;
x = a*b;
covariance[i, k] = x;
}
}
covariance.Multiply(1/n);
return covariance;
}
}}
In the i loop in the twoPassCovariance method, I believe you should loop to i < n rather than i < 2.

Categories

Resources