473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

array maths

82
if I have an 2 dimensional array eg

int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };

how do I add 8 to the first item of the third row eg 5 to make

[3, 2] { {1, 2}, {3, 4}, {13, 6} };

thnx
mrcw
Jan 7 '10 #1
7 1744
tlhintoq
3,525 Expert 2GB
Your initializer creates the following:
numbers[0,0] = 1
numbers[0,1] = 2
numbers[1,0] = 3
numbers[1,1] = 4
numbers[2,0] = 5
numbers[2,1] = 6

So to add 8 to the first item of the third row:
numbers[2,0] = numbers[2,0] + 8;
Jan 7 '10 #2
GaryTexmo
1,501 Expert 1GB
You need to access the appropriate index for that item. In C#, array indexes start at zero, so according to your setup the third row would be index 2, and the first item of that row would be index 0.

Expand|Select|Wrap|Line Numbers
  1. numbers[2,0] += 8;
  2. // or...
  3. numbers[2,0] = numbers[2][0] + 8;
Here's the MSDN page on multidimensional arrays...
http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx

*Edit: Ahhh look at me, getting caught up by syntax. Fixed!
Jan 7 '10 #3
mrcw
82
can I use that code in a windows form application ? It works fine as a console application, but as a wpa it gives me lots of nonsense errors

mrcw
Jan 12 '10 #4
tlhintoq
3,525 Expert 2GB
It should work fine. Please provide a copy of the code from your app along with the error message
Jan 12 '10 #5
mrcw
82
int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } }; is fine -no errors


numbers[2, 0] = numbers[2, 0] + 8; red wriggly line under both 2, = and +

Error 1 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

Error 2 Invalid token '=' in class, struct, or interface member declaration

Error 3 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

Error 4 Invalid token '+' in class, struct, or interface member declaration
Jan 12 '10 #6
tlhintoq
3,525 Expert 2GB
As was earlier suggested, try it as numbers[2][0]
Jan 12 '10 #7
mrcw
82
hi

I am trying the examples from http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

This gives me 11 errors that are copies of the ones I gave before
I still qustion "can I use arrays in wpa? It does appear not.

mrcw
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10.  
  11. namespace Arrays1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.  
  21.          int[,] numbers = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
  22.          numbers[2, 0] = numbers[2][0] + 8;
  23.  
  24.         //int[,] myArray = new int[4,2];
  25.         // int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
  26.  
  27.          int[,] myArray = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
  28.          myArray[1,0] = myArray[1,3] + 5;
  29.  
  30.          int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
  31.          array2D[2 ,1 ] = 25;
  32.  
  33.  
  34.          //int[,] array5;
  35.          //array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; 
  36.          //array5[2, 1] = 25;
  37.  
  38.          int[,] array6 = new int[10, 10];
  39.  
  40.     }
  41. }[
Jan 13 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Treetop | last post by:
I cannot get this array to work. I want to have the game listed until the day after the event, then come off the list. function events() { var today = new Date(); var dayarray=new...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
4
by: bg_ie | last post by:
Hi, I have an array as follows - var MultiArray = new Array(2); MultiArray = new Array(num); MultiArray = new Array(num); I've tried randomizing this array using the following but its...
39
by: hpy_awad | last post by:
I know that group is for C not for C++ but please try to help me , How can I declare size of an array as variable value came from screen? #include "iostream.h" #include "iomanip.h" #include...
5
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag =...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
3
by: mdh_2972 | last post by:
I have an array of over 1000 links in a .JS file. I do not want to put the whole thing on my page because it would take to long to render the page. So how can I randomly pick 1 element from the...
9
by: dennis.sam | last post by:
Hi, Is there away to define a multi-dimensional array with respect to the number of dimensions the array has? For example, given a user spec of "a b c d", I want to create a 4 dimensional array...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.