C++ program, Microsoft Visual Studio (codes) Write a program that creates a 2D a
ID: 663359 • Letter: C
Question
C++ program, Microsoft Visual Studio (codes)
Write a program that creates a 2D array of integers with 100 rows and 15 columns. Populate the array in the following way:
- if the product of the row and column index is even, insert a random integer value between 500 and 1000
- if the product of the row and column index is odd, insert a random integer value between -1000 and -500
Display all the values in your array in row-by-row format. Calculate the average of all 1500 values and display the result.
Please check if you are getting the right answer
Explanation / Answer
#include<iostream>
#include <cstdlib>
using namespace std;
int getrand(int min,int max)
{
return(rand()%(max-min)+min);
}
int main()
{
int arr[100][15];
int i,j;
int avg = 0;
for(i=0;i<100;i++)
{
for(j=0;j<15;j++)
{
if((i*j)%2 == 0)
{
arr[i][j] = getrand(500,1000);
avg = avg+arr[i][j];
}
else
{
arr[i][j] = getrand(-1000,-500);
avg = avg+arr[i][j];
}
}
}
for(i=0;i<100;i++)
{
for(j=0;j<15;j++)
{
cout << arr[i][j] << " ";
}
cout << " ";
}
cout << " Average of all the numbers is " ;
cout << avg/1500.0;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.