Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,395 members, 7,808,390 topics. Date: Thursday, 25 April 2024 at 11:22 AM

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

Nairaland Forum / Science/Technology / Programming / Using 2-dimensional Array In C++ (2543 Views)

Need A Developer For A Three Dimensional Store / Help With Fftw 3 Dimensional / Converting Picture To Byte Array(blob) And Back To Picture (2) (3) (4)

(1) (Reply)

Using 2-dimensional Array In C++ by Vicony4real(m): 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

1 Like

Re: Using 2-dimensional Array In C++ by Vicony4real(m): 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)

Are You Ready To Make More Money While You Work Online?..... BITCOIN / How To Get Easy Backup Gmail Emails From EML / Forget Blogging,freelancing,and Make Thousands Of Dollars Daily

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 15
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.