G H 2-1 Twe Dimensional Arrays one nased UM F the mumber of regions s for the nu
ID: 3874914 • Letter: G
Question
G H 2-1 Twe Dimensional Arrays one nased UM F the mumber of regions s for the number of products (3). (with value 4) and the odher named M OF PRODUcr columns for the products. Hit: Uve the conotants froen question 1 Write coo inoer" loops to initialize the ies array to o two integer 2. Declare a ona ne are named inlea with 4 rows for the regkons and 3 413 Write a C++ code segment so use cin to read in integer values representing the sales for each product for each region. Assume alll sales values for the first region are read first, then the second region, and so forth. Assume that exactly the right number of values are available as inpt 4. n value s Wrie code to find and priát the overall total sales, given the sales array above. einclude ciostream> using nanespace stdi 6. Write a C+ function named printSales. It is given the an array named the_array, as a parameter, this array is passed using [NUM_OF_REGIONS] INUM_ OF_ PRODUCTS] function prints the contents of the array, with each region on a separate line. Each sales number should be right justified in a space 10 characters wide.Explanation / Answer
Here assuming the values as per the previous parts
Two constants NUM_OF_REGIONS and NUM_OF_PRODUCTS has been declared in part1
One 2D array named sales with rows equals to 4(NUM_OF_REGIONS) and colums equals to 3(NUM_OF_PRODUCTS) has been defined in part 2
4)
for(int r = 0;r<NUM_OF_REGIONS;r++)
{
for(int p = 0;p<NUM_OF_PRODUCTS;p++)
{
cout<<"Please enter the product value of product "<<p+1<<" for region "<<r+1;
cin>>sales[r][p];
}
}
Here we need value of each product for each region. Hence we have a outer for loop which will iterate for row representing regions and an inner for loop representing products. So for one value of outer loop all the iterations of inner loop will execute which means for region 1st we will get value of all three products and similarly for 2, 3 and 4th.
5)
int total = 0;
for(int r = 0;r<NUM_OF_REGIONS;r++)
{
for(int p = 0;p<NUM_OF_PRODUCTS;p++)
{
total = total+sales[r][p];
}
}
cout<<”Total sales is : “<<total;
Here we need to know the total sales which mean total products hence we will sum up the values of sales array.
6)
void printSales(int the_array[][], int region, int product)
{
for(int r = 0;r<region;r++)
{
for(int p = 0;p<product;p++)
{
cout<<the_array[r][p]<<” “;
}
cout<<endl;
}
}
Here we have printSales which is taking an array as input and two other parameters defining region and product which hold the value of NUM_OF_REGIONS and NUM_OF_PRODUCTS which will be passed from calling function.
7) printSales(sales,NUM_OF_REGIONS,NUM_OF_PRODUCTS);
8) In the first prototype we are declaring the array as const int the_array which means this will be a constant array and if we try to do any modification in array contents then it will result in compilation error.
With second type of declaration there is no such constraint.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.