Basics in arrays, c++.

I'm a beginner in c++ and i have been given a project to do in c++. It may look easy to a person that has been doing this for a long time but I am still learning.

I have to create a two dimensional array[5][5] with random numbers from 0 to 20, i should first print the numbers in the array on the screen and then find the sum of all of them. Also i should use the srand function with #include <time.h>.
Can i have you guys' help. Thanks in advance!
Last edited on
Making static array is indeed "easy". See http://www.cplusplus.com/doc/tutorial/arrays/

To go through every element, to iterate, to loop.
See http://www.cplusplus.com/doc/tutorial/control/ (although the array tutorial has example too)


srand? Sorry, that ain't fun. This is: http://www.cplusplus.com/reference/random/
Particularly this http://www.cplusplus.com/reference/random/uniform_int_distribution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <random>
#include <chrono>

int main()
{
  // equivalent to the time
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  // kind of srand
  std::default_random_engine generator( seed );
  std::uniform_int_distribution<int> distribution( 0, 9 );

  for (int i=0; i<15; ++i) {
    std::cout << distribution(generator) << ' ';
  }
  return 0;
}


Obviously, if the teacher prefers old C to modern C++, then:
http://www.cplusplus.com/reference/cstdlib/srand/

Note though that C++ does not:
1
2
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */ 

C++ does:
1
2
#include <cstdlib>     /* srand, rand */
#include <ctime>       /* time */ 
Topic archived. No new replies allowed.