Member Avatar for sonicx2218

Hey, I'm kinda crappy with arrays.

int [] myArray = new int[5];
				myArray[0] = num1;   // initialize # of cols
				myArray[1] = num2;   // in each row
				myArray[2] = num3;
				myArray[3] = num4;
				myArray[4] = num5;

I want to use a for statement, to add these 5 things together, and then divide that by 5. Any idea how I can do that?

Member Avatar for sonicx2218
int[] ar = {num1, num2, num3, num4, num5};
				sum = 0;
				for (int d : ar)
				{  // d gets successively each value in ar.
				    sum += d;
				    avg = sum / 5;

got it myself. Just changed the style to this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.