+ 2

How do I save every value of k in the loop?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { double a = 2; double b = 3; double c = 4; double g = 2; double t = 3; double n = 4; int A = 0; while (A<10){ double d = 4/(a*b*c); double e = -4/(g*t*n); double k = d+e; if(A%2 == 0){ g+=2; t+=2; n+=2; A++; } else if(A%2 == 1){ a+=2; b+=2; c+=2; Console.WriteLine(k); A++; } } } } }

9th May 2021, 4:26 PM
Krispy
Krispy - avatar
8 Answers
+ 5
On second thought, since you said "sum of all the values of <k>", I think you can do it without using array. Add one `double` variable before the while-loop. Let's name it. <SumOfK>, and have it initialized by zero. In the while-loop, you can add value of <k> into <SumOfK>, just after <k> is calculated. I think this will do the trick.
9th May 2021, 4:56 PM
Ipang
+ 2
Save every value of <k>? into what? an array? what do you want to do with values of <k> anyway?
9th May 2021, 4:42 PM
Ipang
+ 2
because I need to know the sum of all the values of k.
9th May 2021, 4:47 PM
Krispy
Krispy - avatar
+ 1
Thank you!!
9th May 2021, 5:02 PM
Krispy
Krispy - avatar
0
can be tried through an array
9th May 2021, 4:41 PM
TouchiHe
TouchiHe - avatar
0
every time the loop happens I want to save the value of k
9th May 2021, 4:44 PM
Krispy
Krispy - avatar
0
how do I do that with an array?
9th May 2021, 4:50 PM
Krispy
Krispy - avatar
0
... int A = 0; int size = 10; //Example to use array... double[] ok = new double[size]; ... while(A<size){ ... double k = d+e; ok[A]=k; ... ... A++; ...... ....etc } //for example to read array.. foreach(var u in ok){ System.Console.WriteLine(u);
10th May 2021, 5:48 PM
Smith Welder
Smith Welder - avatar