debugging issue on program

Hello I am having a hard time trying to get my program to debug it keeps saying
"Unhandled exception thrown: read access violation.
**array** was 0x2410112."
Here's my code:
#include <iostream>
#include<cstdlib> //strand & rand
#include<ctime>//time function
using namespace std;
void search(int array[30]){
int max=array[0]; // Variable to store the highest number
int min=array[0]; // Variable to store the lowest number
int min_index=0; // Variable to store the index of lowest number
int max_index=0; // Variable to store the index of highest number
for(int i=0;i<30;i++){ // Iterating thorough all the numbers in the array
if(array[i]<min){ // Checking for the lowest element
min=array[i]; // Updating the lowest element
min_index=i; // Updating the index of lowest element
}
else if(array[i]>max){ // Checking for the highest element
max=array[i]; // Updating the highest element
max_index=i; // Updating the index of highest element
}
}
// Displaying the output as shown in the example
for(int i=0;i<30;i++){
if(i%5==0 && i>0)
cout<<"\n";
cout<<"\t"<<array[i];
}
cout <<"\n";
cout << "The highest number is "<<max<< " and it is at position " << max_index <<"\n";
cout << "The lowest number is "<<min<< " and it is at position " << min_index <<"\n";
}
int main() {
srand(time(0)); // srand generates random numbers by seeding rand with a starting value otherwise evertyime we run the code we get the same numbers
int array[30]; // Declaring array to store 30 integers
for(int i=0;i<30;i++) // Populating the array with 30 random integers between 1 and 100
array[i]=rand() % 100 + 1;
search(array); // Calling the search function
}


Last edited on
A couple of things to note:

1.
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either


2.
Using code tags (with some light reformatting):
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
49
#include <iostream>
#include<cstdlib> //strand & rand
#include<ctime>//time function

using namespace std;

void search(int array[30])
{
   int max = array[0];
   int min = array[0];
   int min_index = 0;
   int max_index = 0;

   for (int i = 0; 1 < 30; i++)
   {
      if (array[i] < min)
      {
         min = array[i];
         min_index = i;
      }
      else if (array[i] > max)
      {
         max = array[i];
         max_index = i;
      }
   }

   for (int i = 0; i < 30; i++)
   {
      if (i % 5 == 0 && i > 0)
         cout << "\n";
      cout << "\t" << array[i];
   }
   cout << "\n\n";

   cout << "The highest number is " << max << " and it is at position " << max_index << "\n";
   cout << "The lowest number is " << min << " and it is at position" << min_index << "\n";
}

int main()
{
   srand(time(0));
   int array[30];

   for (int i = 0; i < 30; i++)
      array[i] = rand() % 100 + 1;

   search(array);
}

3.
Line 14, the for loop. 1 (one) < 30? Your loop will never terminate until your array indexing goes WAY out of bounds.
FYI, I didn't see the problem originally even after running the code through multiple debug sessions. Only when I stopped and actually LOOKED for a few minutes at the offending line in the Visual Studio IDE when running the debugger did I notice what was wrong.

Thank you for the feedback very new to this !
would I use vector instead?
Personally I would very much suggest using a std::vector over a regular array.

Not that using a vector would have miraculously made the indexing issue with line 14 disappear, the code would still have blown up.

I'd also recommend using the C++ random number generation library, <random> instead of using the C library.

Having using namespace std; is the sign of a noob, seasoned programmers tend to not use it.

https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

Using the <random> library with a vector (requires C++20 to work):
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
49
50
51
52
53
54
55
56
57
#include <iostream>

#include <vector> // http://www.cplusplus.com/reference/vector/vector/
#include <random> // http://www.cplusplus.com/reference/random/

int main()
{
   // set the number of elements the container will hold
   constexpr unsigned num_elements { 30 };

   // create a vector with a known number of elements
   std::vector<int> vec(num_elements);

   // create a C++ random num generator, seeded with std::random_device
   std::default_random_engine prng(std::random_device { }());

   // create a distribution from 0 (zero) to 100, inclusive
   std::uniform_int_distribution dist (0, 100);

   // walking through the vector using a regular for loop
   // https://en.cppreference.com/w/cpp/types/size_t
   for (size_t i { }; i < num_elements; ++i)
   {
      vec[i] = dist(prng);
   }

   // walking through the vector using iterators
   for (auto itr { vec.begin() }; itr != vec.end(); ++itr)
   {
      std::cout << *itr << '\t';
   }
   std::cout << "\n\n";

   // walking through the vector using a range-based for loop
   // const auto& will flag if any attempt is made to alter
   // an element
   // C++11
   for (const auto& itr : vec)
   {
      std::cout << itr << '\t';
   }
   std::cout << "\n\n";

   // C++20 allows for a variable initialization statement
   // within the body of the range-based for loop
   // https://en.cppreference.com/w/cpp/language/range-for
   for (size_t count { }; auto & itr : vec)
   {
      std::cout << itr << '\t';

      if (++count % 8 == 0)
      {
         std::cout << '\n';
      }
   }
   std::cout << '\n';
}
Topic archived. No new replies allowed.