In: Computer Science
Correct C++ function and main files separately for the following question and its subparts.
1-aGiven a matrix of integers, count the amount of times each number 0-9 appears. Print out your results on one line in the following form:
0:number of zeros;1:number of ones;2:number of twos;3:number of threes;4:number of fours;5:number of fives;6:number of sixes;7:number of sevens;8:number of eights;9:number of nines;
For example, if you are passed an identify matrix, it contains 12 zeros and 4 ones and no other numbers from 0 to 9, your output would be:
0:12;1:4;2:0;3:0;4:0;5:0;6:0;7:0;8:0;9:0;
Note the colons and semi-colons.
Signature: void count_numbers(int array[4][4])
1-b Given a matrix, print out the scaled version of the
matrix, where you multiply every element in the matrix by the same
numeric value. Elements should be printed by row, with a space
separating elements on a row, and a newline character separating
rows.
Example:
int scale = 3 ; int threebythree[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ; print_scaled_matrix(threebythree,scale) ;
This should produce the following output:
0 3 6 9 12 15 18 21 24
Signature: void print_scaled_matrix(int array[3][3],int scale)
1-c Given two two-dimensional arrays, add them together and print the result as in 1-b above. What relationship needs to be between the sizes of the two matrices?
Example:
int matrix1[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ; int matrix2[3][3] = {{0,0,0},{2,2,2},{-5,-4,8}} ; print_summed_matrices(matrix1,matrix2) ;
This should produce the following output:
0 1 2 5 6 7 1 3 16
Signature: void print_summed_matrices(int array1[3][3],int array2[3][3])
#include<iostream>
using namespace std;
//1a
//Given a matrix of integers, count the amount of times each number
0-9 appears.
void Count(int m[][3],int scale)
{
int a[10];
for(int i=0;i<10;i++)a[i]=0;
for(int i=0;i<scale;i++)
for(int j=0;j<scale;j++)
a[m[i][j]]++;
for(int i=0;i<10;i++)
cout<<i<<":"<<a[i]<<";";
cout<<endl;
}
int main()
{
int matrix1[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ;
Count(matrix1,3);
return 0;
}
output:
0:1;1:1;2:1;3:1;4:1;5:1;6:1;7:1;8:1;9:0;
Process exited normally.
Press any key to continue . . .
#include<iostream>
using namespace std;
//1b
void print_scaled_matrix(int m[][3],int scale)
{
for(int i=0;i<scale;i++){
for(int j=0;j<scale;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}
int main()
{
int matrix1[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ;
cout<<"Matrix:\n";
print_scaled_matrix(matrix1,3);
return 0;
}
output:
Matrix:
0 1 2
3 4 5
6 7 8
Process exited normally.
Press any key to continue . . .
#include<iostream>
using namespace std;
//1c
//adds two matrices
//their size should be same
void print_summed_matrices(int m1[][3],int m2[][3],int s1,int
s2)
{
if(s1==s2)
{
int r[s1][s2];
for(int i=0;i<s1;i++)
for(int j=0;j<s1;j++)
r[i][j]=m1[i][j]+m2[i][j];
cout<<"After addition
:\n";
for(int i=0;i<s1;i++)
{for(int j=0;j<s1;j++)
cout<<r[i][j]<<" ";
cout<<endl;
}
}
}
int main()
{
int matrix1[3][3] = {{0,1,2},{3,4,5},{6,7,8}} ;
int matrix2[3][3] = {{0,0,0},{2,2,2},{-5,-4,8}}
;
print_summed_matrices(matrix1,matrix2,3,3);
return 0;
}
output:
After addition :
0 1 2
5 6 7
1 3 16
Process exited normally.
Press any key to continue . . .