₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,273 members, 8,421,099 topics. Date: Friday, 05 June 2026 at 06:56 PM

Toggle theme

Using 2-dimensional Array In C++ - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingUsing 2-dimensional Array In C++ (2641 Views)

1 Reply

Using 2-dimensional Array In C++ by Vicony4real(op): 11:05pm On Mar 21, 2013
How can i get started with a program to add, multiply and sum the square of two 2-dimensional array in c++.
This is strictly for academic purpose, with monday as deadline.
Thanks in anticipation of yooour help.
thanks.
Re: Using 2-dimensional Array In C++ by blenyo11(m): 9:22am On Mar 22, 2013
I am assuming that the size of the two arrays is the same
and you are to add the corresponding elements.
you can start with this


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int array1[2][2]; //creates a 2*2 matrix or a 2d array. This array 1
int array2[2][2]; //creates a 2*2 matrix or a 2d array. This array 2

//let fill the the two arrayss with some random values. This is up to you to change since I don't know the values you are given
for(int i=0; i<2; i++) //This loops on the rows.
{
for(int j=0; j<2; j++) //This loops on the columns
{
array1[i][j] = i;
array2[i][j] = i + 3;
}
}

//Let get the addition done
for(int i=0; i<2; i++) //This loops on the rows.
{
for(int j=0; j<2; j++) //This loops on the columns
{
cout << array2[i][j] + array1[i][j] << endl;
}
}

cout << endl;
//Let get the subtraction done
for(int i=0; i<2; i++) //This loops on the rows.
{
for(int j=0; j<2; j++) //This loops on the columns
{
cout << array2[i][j] - array1[i][j] << endl;
}
}
cout << endl;
system("PAUSE" ) ;
return EXIT_SUCCESS;
}



One thing you should pick from this and then build on is the ability to read a value at a
specific index
Re: Using 2-dimensional Array In C++ by Vicony4real(op): 12:31am On Mar 23, 2013
blenyo11: I am assuming that the size of the two arrays is the same
and you are to add the corresponding elements.
you can start with this


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int array1[2][2]; //creates a 2*2 matrix or a 2d array. This array 1
int array2[2][2]; //creates a 2*2 matrix or a 2d array. This array 2

//let fill the the two arrayss with some random values. This is up to you to change since I don't know the values you are given
for(int i=0; i<2; i++) //This loops on the rows.
{
for(int j=0; j<2; j++) //This loops on the columns
{
array1[i][j] = i;
array2[i][j] = i + 3;
}
}

//Let get the addition done
for(int i=0; i<2; i++) //This loops on the rows.
{
for(int j=0; j<2; j++) //This loops on the columns
{
cout << array2[i][j] + array1[i][j] << endl;
}
}

cout << endl;
//Let get the subtraction done
for(int i=0; i<2; i++) //This loops on the rows.
{
for(int j=0; j<2; j++) //This loops on the columns
{
cout << array2[i][j] - array1[i][j] << endl;
}
}
cout << endl;
system("PAUSE" ) ;
return EXIT_SUCCESS;
}



One thing you should pick from this and then build on is the ability to read a value at a
specific index
Thanks so much, am grateful.
is it possible to readthe input from a file? as in making use of the #include<fstream>
if possible, how can that be done?
thanks again...
1 Reply

Need A Developer For A Three Dimensional StoreHelp With Fftw 3 DimensionalConverting Picture To Byte Array(blob) And Back To Picture234

Help With This Regular Expression:Has The Tech Bubble Bust?I Need Different Expression In Ms Access