How do I calculate the sum of all items in an array in Java?
440005
Pseudocode, I'm not up on my Java syntax:\n\nint sum=0;\nfor(int i=0;i<array.size;i++)\n  sum+=array[i];
34052
I do not know a preprogrammed utility to do this, but can point you in the direction that you might need to go.  See the reference below for the math and an on-line converter:
48530
Looking at...\n\n>>>>>>>>> 0\n>>>>>>>>> 1\n>>>>>>>>> 2\n>>>>>> etc.\n>>>> 999999\n\n...in the ones or 10^0 column you repeat\n\n>>>> 0+1+2+3+4+5+6+7+8+9 = 45\n\n100,000 times.  That gives you 4,500,000.\n\nIn the tens or 10^1 column you repeat\n\n>>>> 10*0 + 10*1 + 10*2 + ... + 10*9 = 10*45\n\n10,000 times.  (i.e., group ten 0's in a row, then ten 1's in a row, ..., ten 9's in a row, and repeat that 10,000 times.)  That also gives you 4,500,000.\n\nLikewise with the 10^2, 10^3, 10^4, and 10^5 columns.\n\nSo far that totals 6 times 4,500,000 = 27,000,000.\n\nAll that remains is to add in the 1 from 1,000,000 to get the final total of 27,000,001.\n\nCheck: compare to previous reply. :-)\n\n(How do you get this thing to indent a line?)
348929
it a method that simply gives you the index of the last element in the array (that is, the "upper bound").\n\nit is useful when you want to loop through the entire contents of an array, like this:\n\nDim Ary(4) As Integer\nDim i as integer            \n\nFor i = 0 to Ary.GetUpperBound(0) ‘Or UBound(Ary)\nAry(i) = i\nNext
1011128
Please post the snippet of code that does not work. In general, you need to check an array's length before yo uaccess its elements - it's called defensive programming, and good programmers should employ it.
434435
It may be easier to use an array list.\n\n  Dim al As New ArrayList()\n\n   al.Add("This")\n   al.Add("Is")\n   al.Add("My")\n   al.Add("New")\n   al.Add("ArrayList")\n   al.Add("Control")\n\n   Console.WriteLine("Before Sort:")\n   WriteElements(al)\n\n   al.Sort()\n\n   Console.WriteLine("After Sort:")\n   WriteElements(al)\n End Sub\n\n Private Sub WriteElements(ByVal al As ArrayList)\n   Dim i As Integer\n   \n   For i = 0 To al.Count - 1\n     Console.WriteLine(CStr(al.Item(i)))\n   Next
9961
0,1,1,2,3\n\nto get the next number add the previous 2 numbers
751489
I don't see how you could access elements in an array past MAXINT. (Using array syntax.)\n\nSo the practical answer is MAXINT.
368941
I can help you with this program in C (not C++).\n\n#include <stdio.h>\n\nint a;\n\nvoid main(void)\n{\n   for(a = 0; a <= 1000; a++)\n   {\n       printf("%d&#92;n", a);\n   }\n}\n\nThe above program lists numbers starting from 0 to 1000.\nYou can modify the program to add up all those numbers if that is what you want.\n\n#include <stdio.h>\n\nint a, sum = 0;\n\nvoid main(void)\n{\n   for(a = 0; a <= 1000; a++)\n   {\n      printf("%d&#92;n", a);\n      sum = sum + a;\n   }\n   printf("Sum = %d", sum);\n}\n\nThis program prints and also finds the sum to 1000 numbers starting from 0!
859662
This sounds like an assignment! Anyhow...\nCreate an array of 10. Then create a for loop of ten and each time ask for the user for an input. Store that input to the array. Then using a loop again output the array to the listbox
457195
the simple program given above if fine.. in terms of the conncept.. but there's a bug... \nthe problem is with looping.. coz when u enter a number such as 80301  or 55302 ( a no. having 0 in between), the loop breaks at the point when zero is encountered.\n\n so the output in the first eg. is  8 0 ,sum= 8 (instead of correct 8 0 3 0 1 , sum=12\n\n\nso the logic of handling the number is absolutely correct but the looping is a comparatively difficult job in this case!
962937
You will have to set the element in the array to null, then loop through the rest of the array, moving each up (loop element - 1) until you reach the end of the array.\n\nI have included a link to a site which shows you some great basic array manipulation. Scroll down to the section titled "Inserting" and you will find some code that shows you the moving up of items after an insert. The same thing will apply when you remove an item. Everything below the delete will then move up to take the space of the deleted item.\n\nJust remember that after all items are moved up that the last item is set to null so that you won't have to items that are the same and you have a null that can terminate a loop through the array.\n\nCheck out the link and I hope it helps! Good luck!
203290
Change this:\n\nsArray(0)="dog"\n\nTo this:\n\nsArray[0]="dog"
178015
It's been a while since I did anything in Java but I'm fairly certain it's just this:\n\nfile.add(new MenuItem("Print"));\nfile.add(new MenuItem("-"));\nfile.add(new MenuItem("Exit"));\n\n..give it a shot.
314740
There are a few ways to do this, depending on the behavior you want. You can make a class that contains the array (constructor will take the size of the array). Then you implement/overload all the operators you are interested in. In these operators, you can overload [] to cause the index to wrap (if the size of the array was 10 and you did [10] you would get the 0'th element. Or you could have [10] return [9] element all the time. With templates:\n\ntemplate<class T, int n> class Array{\n\npublic:\nT& operator[](int i) {\nint j = i;\nif(i < 0){\n   j = 0;\n}\nif(i > n - 1){\n   j = n - 1;\n}\n\nreturn data[j];}\n\nprivate:\nT data[n];\n};\n\nor something like that.
1053311
//created by vinyl21 for yahoo answers\n//gjfr77@yahoo.com\nimport java.io.*;\nclass matrix{\nint n,m;\nint a[][]=new int[100][100];\nint r[]=new int[10];\nint c[]=new int[10];\nDataInputStream d=new DataInputStream(System.in);\n\nvoid input() throws IOException{\nSystem.out.println("ENTER THE NO OF ROW");\nn=Integer.parseInt(d.readLine());\nSystem.out.println("ENTER THE NO OF COLUMN");\nm=Integer.parseInt(d.readLine());\n\nfor(int i=1;i<=n;i++){\nfor(int j=1;j<=m;j++){\nSystem.out.print("("+i+","+j+"):");\na[i][j]=Integer.parseInt(d.readLine());}}}\n\nvoid process(){\nfor(int i=1;i<=n;i++){\nfor(int j=1;j<=m;j++){\nr[i]+=a[i][j];}}\n\nfor(int j=1;j<=m;j++){\nfor(int i=1;i<=n;i++){\nc[j]+=a[i][j];}}\n}\n\nvoid display(){\nSystem.out.println("given matrix");\n\nfor(int i=1;i<=n;i++){\nfor(int j=1;j<=m;j++){\n\nSystem.out.print(a[i][j]+"&#92;t");\n\n}\nSystem.out.println("&#92;n");}\n\nSystem.out.println("row sum");\n\nfor(int i=1;i<=n;i++){\nfor(int j=1;j<=m;j++){\n\nSystem.out.print(a[i][j]);\nif(j<m)System.out.print("   +   ");\nelse System.out.print("   =   ");}\nSystem.out.println(r[i]);\nSystem.out.println();}\n\nSystem.out.println("column sum");\n\nfor(int i=1;i<=n;i++){\nfor(int j=1;j<=m;j++){\n\nSystem.out.print(a[i][j]+"&#92;t");}\n\nif(i<n){\nSystem.out.print("&#92;n");\nfor(int d=0;d<m;d++){System.out.print("+&#92;t");}\n}\nelse {System.out.print("&#92;n");\nfor(int d=0;d<m;d++){System.out.print("=&#92;t");}}\nif(i==n){System.out.println();\nfor(int k=1;k<=m;k++){\nSystem.out.print(c[k]+"&#92;t");}}\nSystem.out.println();}\n\n}\n\nvoid displaye() throws IOException{\nSystem.out.println("PRESS ANY KEY TO EXIT");\nString n=d.readLine();}\n\n\npublic static void main(String args[])throws IOException{\nmatrix k=new matrix();\nk.input();\nk.process();\nk.display();\nk.displaye();}}\n\n/*output\nI:&#92;>cd j\n\nI:&#92;j>cd bin\n\nI:&#92;j&#92;bin>java matrix\nENTER THE NO OF ROW\n3\nENTER THE NO OF COLUMN\n3\n(1,1):1\n(1,2):2\n(1,3):3\n(2,1):9\n(2,2):8\n(2,3):7\n(3,1):4\n(3,2):5\n(3,3):6\ngiven matrix\n1       2       3\n\n9       8       7\n\n4       5       6\n\nrow sum\n1   +   2   +   3   =   6\n\n9   +   8   +   7   =   24\n\n4   +   5   +   6   =   15\n\ncolumn sum\n1       2       3\n+       +       +\n9       8       7\n+       +       +\n4       5       6\n=       =       =\n14      15      16\nPRESS ANY KEY TO EXIT\n*/
779597
The 'unique' function will return all distinct values in an array:\n\nMyData =\n\n     8\n     2\n     5\n     4\n     8\n     7\n     4\n     1\n     7\n     4\n     5\n     7\n\n>> unique(MyData)\n\nans =\n\n     1\n     2\n     4\n     5\n     7\n     8