Hello i have a question on an array problem that i just dont know how to go about it, please i need some assistance on what im suppose to start with. The question is below :
Create a 2-dimensional array with 10 rows and 10 columns. Fill the array with random 3 digit integers. Print out the column with the largest sum. (If two or more columns share the largest sum, print out one of them only.)
The way you create a random three-digit number is correct. However, you need to seed the random number generator with srand. You should call srand once at the beginning of the program (the link I showed you above shows an example on how to do this). Be sure to #include <stdlib.h> and #include <time.h> like the example shows.
The main problem is that you're using the three-digit numbers as your array indeces. Your array dimensions are both 10, so for each dimension the indexable range is 0 through 9. Also, you only need to generate 100 random numbers to fill the array. 810000 numbers (900 x 900) is too much.
#include <iostream>
#include <stdlib.h>
usingnamespace std;
int main()
{
int array2D[10][10];
int sum100s = 0, sum10s = 0, sum1s = 0;
//fill 10x10 array with random numbers between 100 and 1000
for (int x = 0; x<10 ; x++)
for (int y = 0; y<10 ; y++)
array2D[x][y] = rand()%900+100;
//sum the three digits
for (int x = 0; x<10 ; x++)
for (int y = 0; y<10 ; y++)
{
sum100s += array2D[x][y] / 100;
sum10s += (array2D[x][y] % 100) / 10;
sum1s += array2D[x][y] % 10;
}
//Which digit is the largest? Print the column;
if (sum100s >= sum10s && sum100s >= sum1s) //100s is largest
{
cout << "100s digit is the largest with: ";
for (int x = 0; x<10 ; x++)
for (int y = 0; y<10; y++)
cout << array2D[x][y] / 100 << ",";
}
elseif (sum10s >= sum1s) // 10s is largest
{
cout << "10s digit is the largest with: ";
for (int x = 0; x<10 ; x++)
for (int y = 0; y<10; y++)
cout << (array2D[x][y] % 100) / 10 << ",";
}
else // 1s is largest
{
cout << "1s digit is the largest with: ";
for (int x = 0; x<10 ; x++)
for (int y = 0; y<10; y++)
cout << (array2D[x][y] % 10) << ",";
}
return 0;
}
Stewbond, it is highly discouraged to provide entire coded solutions on this forum. We're here to provide help. Providing a full solution will just short change someone's learning experience.
That said, you're summing each digit, when the problem only asks to sum each column.