I seek guidance. (>0-0)>

Hello, need some help. my task is this:
Write a function that finds the smallest number of a given number. Make a program that generates 400 five digit numbers. By using the fuction find the smallest number of all the generated numbers. Programs given answers put into a text file.

So far my code looks like this, but it doesn't work properly. Where did I make a mistake ?

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
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <fstream>

using namespace std;

void create_random_numbers(int ran[]) 
{
    ofstream failas("text.txt");
    
    unsigned seed = time(0);

    srand(seed);
    int random_integer;
    for (int index = 10000; index<99999; index++)
    {
        ran[index] =  (rand()%89999)+10000;
        failas << index << " : " << ran[index] << endl << endl;
    }
}

int main(){
  int ran_array[400]; 
  create_random_numbers(ran_array);
  
  
  cin.get();
  failas.close();
  return 0;
}
Last edited on
Look at the size of your array ran_array.

Look at the values of the index you're using to try and access elements of that array.

See a problem?
The loop on line 16 generates 89999 values while you want just 400.

Note that an index within an array starts with 0. So in ran_array only indexes from 0 to 399 are valid. Everything else is out of bounds.
Topic archived. No new replies allowed.