C++ LANGUAGE 1) Write a program that creates a 2D array with 20 rows and 50 colu
ID: 3760818 • Letter: C
Question
C++ LANGUAGE
1) Write a program that creates a 2D array with 20 rows and 50 columns. Ask the user to input a positive integer or seed value. Use the seed value to populate the array (row-by-row) with random integers between 1 and 2000.
2) Write a function that has as inputs an array of real numbers and the size of the array. The function uses the selection sort method to arrange the numbers in decreasing order.
3) Write a function that has as inputs an array of integers, the size of the array, and a search value. The function returns the largest index of the search value (and 0 if the value isn't found).
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
int search(int numbers[], int arraySize,int searchValue)
{
for(int i = arraySize - 1;i>=0;i--)
{
if(numbers[i] == searchValue)return i;
}
return 0;
}
void sort(double numbers[], int arraySize)
{
for (int i= arraySize - 1; i > 0; i--)
{
int first = 0;
for (int j=1; j<=i; j++)
{
if (numbers[j] < numbers[first])
first = j;
}
double temp = numbers[first];
numbers[first] = numbers[i];
numbers[i] = temp;
}
return;
}
int main()
{
int ROWS = 20,COLS = 50;
int grid[ROWS][COLS];
int seed = 0;
while(seed <= 0)
{
cout<<"Enter the seed value(positive integer): ";
cin>>seed;
}
srand(seed);
for(int i=0;i<ROWS;i++)
{
for(int j=0;j<COLS;j++)
{
grid[i][j] = 1 + rand() % 2000;
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
double array[] = {1,3,7,2,6,9,2,55.56,23.23,34.6,12,45,678,9.0,2.4,4.3,0.56,6.23};
cout<<" Original array: "<<endl;
for(int i=0;i<18;i++)
cout<<array[i]<<" ";
cout<<endl;
sort(array,18);
cout<<"Array after sort: "<<endl;
for(int i=0;i<18;i++)
cout<<array[i]<<" ";
cout<<endl;
int intArray[] = {1,2,4,6,8,3,5,8,3,5,8,2,23,45,657,78,23,34,67,89,-934};
cout<<" Search array: "<<endl;
for(int i=0;i<18;i++)
cout<<intArray[i]<<" ";
cout<<endl;
cout<<"Enter the number you want to search: ";
double searchValue;
cin>>searchValue;
cout<<"Search value is found at: "<<search(intArray,20,searchValue)<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.