Question

In: Computer Science

Correct C++ function and main files separately for the following question and its subparts. 1-aGiven a...

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])

Solutions

Expert Solution

#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 . . .



Related Solutions

Question 1: Write a program in C++ using multi filing which means 3 files ( main...
Question 1: Write a program in C++ using multi filing which means 3 files ( main file, header file, and functions file) and attached screenshots as well. Attempt the Question completely which contain a and b parts a) Write a program that takes a number from the user and checks whether the number entered validates the given format: “0322-5441576”, xxxx-xxxxxxx where “x” implies the digits only and first two digits are 0 and 3 respectively. The program also identifies the...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int main() { SinglyLinkedList<std::string> list; list.Add("Hello"); list.Print(); list.Add("Hi"); list.Print(); list.InsertAt("Bye",1); list.Print(); list.Add("Akash"); list.Print(); if(list.isEmpty()){ cout<<"List is Empty "<<endl; } else{ cout<<"List is not empty"<<endl; } cout<<"Size = "<<list.Size()<<endl; cout<<"Element at position 1 is "<<list.get(1)<<endl; if(list.Contains("X")){ cout<<"List contains X"<<endl; } else{ cout<<"List does not contain X"<<endl; } cout<<"Position of the word Akash is "<<list.IndexOf("Akash")<<endl; cout<<"Last Position of the word Akash is "<<list.LastOf("Akash")<<endl; list.RemoveElement("Akash"); cout<<"After removing Akash...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
1. Gordon Grumps is married and files separately. During 2019, he had the following capital gains...
1. Gordon Grumps is married and files separately. During 2019, he had the following capital gains and losses:                STCL                                                                  (1,000)                STCG                                                                  1,900                STCL carryover from 2018                           (200)                LTCG                                                                  600                LTCL                                                                  (10,000) Gordon’s taxable income is $6,000. What is Gordon’s capital loss deduction for 2019 and his carryover? 2. Sidney Southern owned a restaurant which was condemned on November 20, 2018. On January 15, 2019, he received a condemnation award of $280,000. The adjusted...
PART C features an essay-type question with three subparts and requires to provide a full analysis...
PART C features an essay-type question with three subparts and requires to provide a full analysis of the social cost of the output decision of a profit maximizing monopoly firm, assuming that the firm is the only producer in a market. Thoroughly discuss: 1. The concept of social cost in terms of the output decision of the monopoly firm;
PART C features an essay-type question with three subparts and requires to provide a full analysis...
PART C features an essay-type question with three subparts and requires to provide a full analysis of the social cost of the output decision of a profit maximizing monopoly firm, assuming that the firm is the only producer in a market. Thoroughly discuss: 2. Assuming that the government forces the monopoly firm to lower its price, show how the output of the firm would increase;
Place each of the subparts of § 1563(a)(1)(A) adjacent to its code label. § (A) (1)...
Place each of the subparts of § 1563(a)(1)(A) adjacent to its code label. § (A) (1) 1563 (a) a. Subsection designation. b. Section number. c. Abbreviation of "section". d. Subparagraph designation. e. Paragraph designation.
Write an entire program (with a main function and necessary header files included) that declares an...
Write an entire program (with a main function and necessary header files included) that declares an 80 byte character buffer, prompts a user to enter a single word (no spaces) from the default input device (e.g. the keyboard), which is stored in the buffer, and then reverses the string while overwriting the buffer. Print the sting before and after the reversal. The program transcript should resemble the following output: $ ./program.exe enter string: nvidia before: nvidia after: aidivn in C
This is for C++ A static main() function defined in your homework assignments means: 1.The function...
This is for C++ A static main() function defined in your homework assignments means: 1.The function is always invisible to all functions outside of the class. 2.The function cannot declare any variables. 3.The function can be called without an instance. 4.The function can access all variables and methods. 5.The function can be called by the superclass.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT