Users/Cricket/Downloads/la 02_multi-dimensional_arrays.pdf Project: Working with
ID: 3749921 • Letter: U
Question
Users/Cricket/Downloads/la 02_multi-dimensional_arrays.pdf Project: Working with Functions The task involves writing a C++ program that produces a multiplication table in a two-dimensional array. The first row consists of the multiplicands while the first column consists of the multipliers. For instance, for n-6, the multiplication table becomes: 2 2 468 12 31 3 69 12 15 18 41 4 8 12 16 20 24 55 10 15 20 25 38 61 6 12 18 24 30 36 The value of n should be obtained from a user, but by default it may be initialized to 10. Requirements You must use user function to perform the task. Use the main function to prompt the user and call the function. Create a two-dimensional array of integer data type with generous space, for instance, 32x32. Prompt a user for n. If the user value is less than zero or above your multidimensional array, either prompt the user for proper value or use the default value 10. . " " Populate the multidimensional array with the multiplication table. Print the multidimensional array Save the multidimensional array into a file called "mtable.txt" Your code must be properly documented and formatted When you submit the lab assignment, please package your Visual Studio project in zip format and do so ROExplanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
//user function
void generate_matrix(int n,int m[][32])
{
int i=0;
for(i=0;i<n;i++){
m[0][i]=i+1;//setting first row
m[i][0]=i+1;//setting first col
}
//generating table
int j;
for(i=1;i<n;i++)
{
for(j=1;j<n;j++)
{
m[i][j]= m[i][0]*m[0][j];
}
}
//displaying array
cout<<"Array generated: ";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<m[i][j]<<" ";
}
cout<<endl;
}
//now writing to a file
//openeing file
ofstream fout;//file variable
fout.open("out.txt");//change file name and path as per your requirement
//writing matrix to file
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
fout<<m[i][j]<<" ";
}
fout<<endl;
}
fout.close();
}
int main()
{
//matrix declaration
int m[32][32];
int n;//taking input
cout<<"Enter matrix size:";
cin>>n;
while(1)//reading input upto the correct number is entered
{
if(n<0 || n>32)
{
cout<<"Enter again matrix size:";
cin>>n;
}
else break;//if correct is entered
}
//calling user function
generate_matrix(n,m);
return 0;
}
output:
Enter matrix size:-1
Enter again matrix size:35
Enter again matrix size:6
Array generated:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.