Question

Write a C++ program to scale and average the values in a two-dimensional array. Write the...

Write a C++ program to scale and average the values in a two-dimensional array. Write the following functions:

          void randomize2DArray(int arr[ROW_SIZE][COL_SIZE]) – places random values between 1 and 100 in a two-dimensional array .

          void scale2DArray(int arr[ROW_SIZE][COL_SIZE], double scale) – multiplies every value in the two-dimensional array by scale.

          double average2DArray(int arr[ROW_SIZE][COL_SIZE]) – calculates the average value over all elements in the two-dimensional array.

Randomize and print the values in the two-dimensional array of size 7 x 10. Then determine and print the average value. Then prompt for and get a scale value in the range 0.5 and 1.5 from the user. Scale and print the array. Create global constants for the array row and column sizes.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:-

#include <iostream>
#include <ctime>
#include <cstdlib>
#define ROW_SIZE 7
#define COL_SIZE 10
using namespace std;
int arr[ROW_SIZE][COL_SIZE];//a global array

void randomize2DArray(int arr[ROW_SIZE][COL_SIZE])
{
    srand ((unsigned)time(NULL));

    int i,j;// loop counters
    for(i=0; i<ROW_SIZE; i++)
    {
        for(j=0; j<COL_SIZE; j++)
            arr[i][j] = rand()%100 + 1;
    }
}

void scale2DArray(int arr[ROW_SIZE][COL_SIZE],double scale)
{
    int i,j;
    for(i=0; i<ROW_SIZE; i++)
    {
        for(j=0; j<COL_SIZE; j++)
            arr[i][j] = scale*arr[i][j];
    }
}

double average2DArray(int arr[ROW_SIZE][COL_SIZE])
{
    double avg;
    double total = 0.0;
    int i,j;
    for(i=0; i<ROW_SIZE; i++)
    {
        for(j=0; j<COL_SIZE; j++)
            total = total + arr[i][j] ;
    }
    avg = total / (double)(ROW_SIZE * COL_SIZE);
    return avg;

}


int main()
{

    randomize2DArray(arr);
    int i,j;
    cout<<"\nArray is"<<endl;
    for(i=0; i<ROW_SIZE; i++)
    {
        for(j=0; j<COL_SIZE; j++)
            {
                cout<<arr[i][j]<<" ";
            }
            cout<<endl;
    }
    double scale;
    cout<<"\nEnter the scaling value between 0.5 and 1.5 ";
    cin>>scale;
    while(scale>1.5 || scale<0.5)
    {
        cout<<"\nyou have entered out of range scaling factor \nplease enter the correct scaling factor:";
        cin>>scale;
    }
    scale2DArray(arr,scale);
    cout<<"\nScaled array is"<<endl;
    for(i=0; i<ROW_SIZE; i++)
    {
        for(j=0; j<COL_SIZE; j++)
            {
                cout<<arr[i][j]<<" ";
            }
            cout<<endl;
    }
    double avg = average2DArray(arr);
    cout<<"\nThe average of all the values of 2D array is "<<avg<<endl;


    return 0;
}


output:-

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to scale and average the values in a two-dimensional array. Write the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a main program that will randomly fill a 2 dimensional array with integer values in...

    Write a main program that will randomly fill a 2 dimensional array with integer values in the range of 0 to 50. Use the Rand_Int function and constants to do this. These values represent the height of a terrain. Write a function that will determine what part of the terrain will flood. A. Create a global constant called MAX and assign it the value of 10. B. Prompt the user in main, at what height do they consider it to...

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

  • 1.Write a Java program that computes the average of the values of a row in a...

    1.Write a Java program that computes the average of the values of a row in a twodimensional array. You should create a method with the following header: public static double averageRow(double[][] array, int row) Write a test program that reads a matrix from the user and a row index to calculate the average on. Print the result. 2.A square matrix is said to be an upper triangular matrix if the value of the cell is 0 when row > column....

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Assignment: Write a program with each of the following methods. You can assume this program is...

    Assignment: Write a program with each of the following methods. You can assume this program is saved in a file called multiArrays.java. A sample main method and sample output is provided at the end of this section. All arrays created are of integer-type. Write a method to declare, initialize, and populate a two-dimensional array. Using the following header:                         public static int[ ][ ] declare2D(Scanner scnr) The user will indicate the size of the array. Write a method to declare,...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • Part 2) write a C++ program that declares a 2 dimensional array: int CSIIAssign3 [10][12]; Initialize...

    Part 2) write a C++ program that declares a 2 dimensional array: int CSIIAssign3 [10][12]; Initialize your array using a text file, user input or assign value while declaring the array, you can choose the method. Then, a. Write a loop to print first row of the array on one line, using cout. b. Write a loop to print first column of the array on one line, using cout. c. Write a loop to print first five rows of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT