Saturday, 25 February 2012

Read/Print arrays using functions

#include<iostream>
using namespace std;

void ReadArr(int arr[],int size);
void PrintArr(int arr[],int size);
void Addmatrix(int Amat[],int Bmat[],int Cmat[],int size);
void MultiScalar(int Cmat[],int num,int size);

int main()
{
const int SIZE=3;
int arr[SIZE];
int scalar_number,Amat[SIZE],Bmat[SIZE],Cmat[SIZE];
ReadArr(arr,SIZE);
cout<<endl;
PrintArr(arr,SIZE);
cout<<endl;
cout<<"Matrix A"<<endl;
ReadArr(Amat,SIZE);
cout<<endl;
cout<<"Matrix B"<<endl;
ReadArr(Bmat,SIZE);
cout<<endl;
cout<<"Addition of Matrix "<<endl;
Addmatrix(Amat,Bmat,Cmat,SIZE);
cout<<endl;
PrintArr(Cmat,SIZE);
cout<<endl;
cout<<"Enter a scalar integer value = ";
cin>>scalar_number;
MultiScalar(Cmat,scalar_number,SIZE);
cout<<endl;
PrintArr(Cmat,SIZE);
cout<<endl;
return 0;
}
//read
void ReadArr(int arr[],int size)
{
cout<<"Enter the values in array : "<<endl;
for(int i=0;i<size;i++)
{
cout<<"Enter number At "<<i<<" th index :";
cin>>arr[i];
}
return ;
}
//print
void PrintArr(int arr[],int size)
{
for(int i=0;i<size;i++)
cout<<"The value at "<<i<<" th index is "<<arr[i]<<" "<<endl;
return ;
}
//addmatrix
void Addmatrix(int Amat[],int Bmat[],int Cmat[],int size)
{
for(int i=0;i<size;i++)
Cmat[i] = Amat[i] + Bmat[i];
return ;
}
//multi
void MultiScalar(int Cmat[],int num,int size)
{
for(int i=0;i<size;i++)
Cmat[i] = num * Cmat[i];
return ;
}

No comments:

Post a Comment