Create a C++ program with an array of 50 integers and store random numbers betwe
ID: 3581879 • Letter: C
Question
Create a C++ program with an array of 50 integers and store random numbers between 30 and 200 into the array. Sort the array from largest number to smallest number. Array to be displayed in 10 columns and 5 rowsCreate a C++ program with an array of 50 integers and store random numbers between 30 and 200 into the array. Sort the array from largest number to smallest number. Array to be displayed in 10 columns and 5 rows
Create a C++ program with an array of 50 integers and store random numbers between 30 and 200 into the array. Sort the array from largest number to smallest number. Array to be displayed in 10 columns and 5 rows
Sort the array from largest number to smallest number. Array to be displayed in 10 columns and 5 rows
Explanation / Answer
The program you needed is :
C++ Program:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int numbers[50] = {0}, temp, i, j;
for(i=0; i<50; i++)
{
numbers[i] = rand() % 170 + 31;
}
cout << "Array elements before sorting are: " << endl;
int pos=0;
for(i=0; i<5; i++)
{
for(j=0; j<10; j++)
{
cout << numbers[pos++] << " ";
}
cout << endl;
}
for(i=0; i<50; i++)
{
for(j=0; j<50; j++)
{
if(numbers[j] < numbers[j+1])
{
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
cout << endl << "After sorting, the elements are: " << endl;
pos=0;
for(i=0; i<5; i++)
{
for(j=0; j<10; j++)
{
cout << numbers[pos++] << " ";
}
cout << endl;
}
return 0;
}
Sample Output:
Array elements before sorting are:
194 97 108 116 114 136 187 113 110 152
53 198 51 50 94 87 161 197 173 147
32 39 118 70 113 121 73 154 198 76
150 63 143 99 190 98 34 177 52 113
170 74 152 190 135 88 119 95 126 91
After sorting, the elements are:
198 198 197 194 190 190 187 177 173 170
161 154 152 152 150 147 143 136 135 126
121 119 118 116 114 113 113 113 110 108
99 98 97 95 94 91 88 87 76 74
73 70 63 53 52 51 50 39 34 32