Passing Array Into Function in C++


Assalamu'alaikum. Hari ini saya cuba kongsikan coding program yang saya buat. Ianya berkaitan dengan bahasa pengaturcraan C++. Sebagaimana tajuk di atas, program ini ingin menunjukkan bagaimana kita ingin menghantar data dari function main ke function yang lain menggunakan array. Saya menggunakan TURBO C++ compiler.

Di bawah ini adalah coding nya :

#include <iostream.h>
void InputArray(int first[][3],int second[][3]);
void Multiplication(int first[][3],int second[][3],int answer[][3]);
void displayOutput(int answer[][3]);

void main()
{
int first[2][3];
int second[2][3];
int answer[2][3];

InputArray(first, second);
Multiplication(first,second,answer);
displayOutput(answer);




}

void InputArray (int first[][3],int second[][3])
{
int x,y;

for(x=0;x<2;x++)
{
for(y=0;y<3;y++)
{
cout << "Enter first row number in column "<<y+1 <<" :";
cin >> first[x][y];
}
}
cout<<endl;
for(x=0;x<2;x++)
{
for(y=0;y<3;y++)
{
cout << "Enter second number in column "<<y+1 <<" :";
cin >> second[x][y];
}
}

}

void Multiplication(int first[][3],int second[][3],int answer[][3])
{
int x,y;
for(x=0;x<2;x++)
{
for(y=0;y<3;y++)
answer[x][y] = first[x][y]*second[x][y];
}
}

void displayOutput(int answer[][3])
{
int x,y;

for(x=0;x<2;x++)
{
for(y=0;y<3;y++)
{
cout << "The answer for row " <<x+1 <<" and column " <<y+1 <<" is:";
cout << answer[x][y];
cout << endl;

}
}

}

Sample output :



Share this

Related Posts

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
:-?
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
$-)
(y)
(f)
x-)
(k)
(h)
cheer