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
}
#include <iostream>
#include<cstdlib> //strand & rand
#include<ctime>//time function
usingnamespace 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;
}
elseif (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.
#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
constexprunsigned 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_tfor (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 (constauto& 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-forfor (size_t count { }; auto & itr : vec)
{
std::cout << itr << '\t';
if (++count % 8 == 0)
{
std::cout << '\n';
}
}
std::cout << '\n';
}