Array Question, Assistance Needed please

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.)
You can declare a 2D array like this: int array2D[x][y]; where x and y are two positive integers known at compile time.

To fill it with random numbers, use the rand() function (and be sure to make it generate numbers between 100 and 999, inclusive)

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/
I tried like this but it just infinite loops, any assistance on how to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

  int main ()
} 
  int array2D[10][10];
	for (int y=100; y<999; y++) {
		for(int z=100; z<999; z++){
			int a=rand()%900+100;
		cout << array2D[a][a];
		}
	}
	return 0;
}
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.
That was fun!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <stdlib.h>
using namespace 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 << ",";
  }
  else if (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;
}
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.