Saturday, May 22, 2010

Anyone good at C++? need quick help at least point me in the right direction?

Write a function that adds the elements of two 3-D arrays of size [n] [2] [2]. Create and initialize a 3-D array with n=2 where all of the elements are initialized to 2. Test the function by using it to add the array to itself. Next create a 3-D array with n= 3 where all of the elements are initialized to 3. Again test the function by using it to add the array to itself.





2: Write a function to print out the elements of arrays of size [n] [2] [2]. Use the function to print the results from the above two additions.





3: Write another function to add the elements of 3-D arrays of size [n] [2] [2], except use pointer arithmetic inside the function to implement the addition. Test the function.





I know how to create basic functions but as far as arrays, I have no clue even what arrays are. I just need guidance on this, i'm not asking anyone to do this problem. Code examples would be great though.

Anyone good at C++? need quick help at least point me in the right direction?
arrays are really a simple concept.


Arrays are basically a collection of variables which belong to the same data type.


for example this a integer array :: 1,34,54,3,45,56,67,34


and its size is eight ,since it has eight elements.


The above example is a one-dimensional array


syntax to declare a 1-dimentional array ::


%26lt;datatype%26gt; %26lt;variable name%26gt;[%26lt;size%26gt;]


example for an integer array :: int numbers[8];


example for a float array :: float numbers[8];


Now to Access a single element value in the array you can use the index number. for example if we take the array example above and use this line of code ::cout%26lt;%26lt;number[1];


the output will be 34. the elements position starts from 0 to n-1 n being the number of elements.





You can have multi dimensional arrays too.


suppose you want to represent a matrix then you can use a two dimentional array. 2 dim arrays will have two indexes like numbers[2][3]. This means the third element in the 2nd row.
Reply:You really need to just read the chapter on arrays in your text book. Arrays and pointers are fundamentals for the C++ language -- if you don't understand them you will fail at being able to master the language.
Reply:It's highly unlikely that your teacher would have given you an assignment which asks for three dimensional arrays without you having ever heard of them before. I would suggest listening in class, it'll do wonders.





Your teacher/professor will no doubt be more than happy to explain what an array is and how to use it, and will do a much better job than any online resource, so I would strongly suggest asking him or her. Nevertheless, here's a breif explanation:





Arrays are variables which store a set of values - for instance, an array of integers will let you store a set of integers all under one variable name, and then you access each individual item with [ ]. So for instance, once you've created and filled an array, you can access the first item in the list with 'varName[0]' and the second with 'varName[1]' and so on. Two dimensional arrays are an array of arrays, which you can visualize like a grid or table. And if, for instance, you wanted to access the second item in the third array, you'd say 'twoDArray[3][2]'.


No comments:

Post a Comment