0

MultiDimensional Array

/* kindly correct my program - print respective elements and their indexes also sum up them using loops?*/ #include <iostream> using namespace std; int main() { /*int arrTelNum[5]={3,5,7,9,11}; int sum=0; for (int x=0; x<5; x++){ sum = sum+arrTelNum[x]; cout<<x<<" :"<<arrTelNum[x]<<endl; } cout<<"Total array indexes are: "<<arrTelNum[5]<<" and sum of values is: "<<sum<<endl; */ int array1[9]={2,4,5,-6,-8,9,-22,80,-45}; int arrSum1=0; cout<<"Index"<<" : "<<"Array Element"<<endl; for (int x=0; x<9; x++){ arrSum1 += array1[x]; cout<<x <<" : "<<array1[x]<<endl; } cout<<"Total number of indexes of first array are: "<<array1[9]<<endl; cout<<" And sum of array elements of first array is: "<<arrSum1<<endl; /*---------------------------------------------------------------------*/ /* Multidimesional Array */ int arrMulti2Dim [3][3]={{2,1,3},{3,4,5},{2,5,6}}; int i; int j; int sumMulti2Dim =0; cout<<"Element at index dimension 0x0 is: "<<arrMulti2Dim[0][0] <<endl; cout<<"Element at index dimension 0x1 is: "<<arrMulti2Dim[0][1] <<endl; cout<<"Element at index dimension 0x1 is: "<<arrMulti2Dim[0][2] <<endl; cout<<"Element at index dimension 0x1 is: "<<arrMulti2Dim[1][0] <<endl; cout<<"Element at index dimension 0x1 is: "<<arrMulti2Dim[1][1] <<endl; cout<<"Element at index dimension 0x1 is: "<<arrMulti2Dim[1][2] <<endl; cout<<"Element at index dimension 0x1 is: "<<arrMulti2Dim[2][0] <<endl; cout<<"Element at index dimension 0x1 is: "<<arrMulti2Dim[2][1] <<endl; cout<<"Element at index dimension 0x1 is: "<<arrMulti2Dim[2][2] <<endl; cout<<"Index"<<" : "<<"Array Element"<<endl; /* for (i=0 && j=0; i<3 && j<3; i++ && j++){ arrMulti2Dim += arrMulti2Dim; cout <<i , j<<" : "<<arrMulti2Dim[3][3]<<endl;

15th Apr 2019, 4:05 PM
Tahir Amin
Tahir Amin - avatar
6 Respostas
+ 3
When you are displaying the number of indexes you refer to the non existing element. E.g. 'array1[9]', has 9 elements, so the last element should have index [8].
15th Apr 2019, 8:55 PM
Aleksander Szczepura
Aleksander Szczepura - avatar
+ 1
That's how you can print all of the elements on screen. It's for 3x3 array, but obviously if you change array size, you must change loop condition too. int array[3][3]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { cin >> array[i][j]; cout << array[i][j]; } } In this example the last element of an array is [2][2]. The first element always has index 0.
18th Apr 2019, 5:49 AM
Aleksander Szczepura
Aleksander Szczepura - avatar
+ 1
This is Solution : Don't forget to Upvote ................................................... using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int[,] num = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; //your code goes here for(int i=0; i<3;i++) { for(int j=0;j<3;j++) { Console.WriteLine(num[i,j]); } } } } }
26th Aug 2021, 7:11 AM
Khan Baz Khan Jadoon
Khan Baz Khan Jadoon - avatar
0
Ok. I want to know, how lements multidimensional arrays can be print on screen? Please guide and help
17th Apr 2019, 10:52 AM
Tahir Amin
Tahir Amin - avatar
0
Thanks for the help
18th Apr 2019, 1:07 PM
Tahir Amin
Tahir Amin - avatar
0
And let me check on. I will come back to you. In case I have issue.
18th Apr 2019, 1:07 PM
Tahir Amin
Tahir Amin - avatar