Add values to an array, print all values, and the sum of all values in an array in C# -
i want create loop asks price , stores price in new object of array every time iteration occurs.
double itemprice = 0; double totalprice = 0; const double over_fifty_discount = .1; double pricesentinal = 0; console.writeline("please input price of item. enter '0' if have no more items."); itemprice = convert.todouble(console.readline()); while (itemprice != pricesentinal) { console.writeline("please input price of item."); itemprice = convert.todouble(console.readline()); double[] originalprices = itemprice.todoublearray(); if (itemprice >= 50) { itemprice = itemprice * over_fifty_discount; } totalprice = totalprice + itemprice; double[] allprices = itemprice.todoublearray(); // how place every iteration of itemprice console.writeline("your total price " + totalprice); //into new array value? } //how print every object in array onto screen? //how add of objects within array , print them onto screen? console.writeline("the prices of items before discounts " + originalprices[]); console.writeline("the total price of items before discounts " originalprices[]); console.writeline("the prices of items after discounts " + allprices[]); console.writeline("the total price of items after discounts " + allprices[]);
i don't know how add new object double array every iteration of loop. don't know how print every object within array on screen, , don't know how add of objects in array , print them onto screen. me fix code please?
tl;dr: little long-winded, read through , learn something. there lot of tools @ disposal complete these tasks, starting basics best. (hint: make sure check out links, quite helpful)
there couple of things here want , learn how use.
first, want lists.
lists
lists array-like data structures can dynamically add values to. example of using list this:
// create new list can hold integers list<int> listofints = new list<int>(); // add values list listofints.add(1); listofints.add(2);
second, want for
, foreach
loops.
loops
like while
loop, for
, foreach
loops allow repeat code. however, unlike while
, for
, foreach
loops allow repeat on set of values. allows build strings repeating string concatenation code on values:
given our code above, can create string contains each of numbers this:
string numbersstring = ""; foreach(int number in listofints) { numbersstring += number.tostring() + " "; }
(sidenote)
now, code above not best practice building strings, demonstrates how use foreach
on collection of values. (check out string.join
method easy way of building strings lists/arrays)
(end sidenote)
so, combining 2 ideas of lists , loops, can calculate sum this:
int sum = 0; foreach(int number in listofints) { sum += number; } // sum == 3
(2nd sidenote)
again, code above not write yourself. more commonly, use linq extension method sum()
calculate sum of values in collection.
(end 2nd sidenote)
so, answer question,
add values array, print values, , sum of values in array in c#
use list values:
list<double> values = new list<double>(); // inside loop somewhere values.add(avalue);
print of values either:
using loop ,
console.write()
:foreach(double value in values) { console.write("{0} ", value); }
using loop add values
string
,console.writeline()
:string finalstring = ""; foreach(double value in values) { finalstring += value.tostring() + " "; }
using
string.join()
:console.writeline(string.join(" ", values));
use either, it's choice.
and finally, sum of values using either:
- a loop , sum variable (see sum example)
the
sum()
extension method:using system.linq; //... double sum = values.sum();
Comments
Post a Comment