Run time error when running very simple program (C++) [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I wrote a program as part of a learning exercise that takes doubles as inputs, and outputs the total sum of all the doubles, as well as the smallest, largest, and the mean. When trying to run the program, I get this run time error:
Unhandled exception at 0x74b01d4d in Hello World.exe: Microsoft C++ exception: Range_error at memory location 0x00dcf760.
When I use the Visual Studio 2010 debug option, I get vector subscript is out of range.
The code is:
#include "C:\Users\Kevin\Documents\Visual Studio 2010\Projects\std_lib_facilities.h"
int main() {
vector<double> v;
double number = 0;
cout << "Enter the distance between two cities along the route.\n";
while (cin >> number) {
double sum = 0;
v.push_back(number);
sort(v.begin(),v.end());
for (size_t i = 0; i < v.size(); ++i)
sum += v[i];
cout << "The total distance from each city is " << sum
<< ". The smallest distance is " << v[0]
<< " and the greatest distance is " << v[v.size()]
<<". The mean distance is " << sum/v.size() <<".\n";
cout << "Enter the distance between two cities along the route.\n";}
}
I had to change the type of the variable defined in the for loop because I was getting a signed/unsigned mismatch error. The code gives no errors when compiled and I am having difficulty seeing the problem.

" and the greatest distance is " << v[v.size()]
is an undefined behavior because the last index in vector is v.size() - 1 if v.size() > 0 (if v.size() = 0 there is no items and nothing to subscript)
You should thus write:
if( !v.empty())
std::cout << " and the greatest distance is " << v[ v.size() - 1];
or better;
if ( !v.empty()) {
std::cout << " and the greatest distance is " << v.back();
std::vector::back

Related

Garbage value in an array where array length and input is defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am a beginner, and I am trying to learn C++.
For now, all I am trying to do is input 3 numbers, and print them back.
#include <iostream>
using namespace std;
int main(){
int n[2];
cout << "Enter three numbers" << endl;
for (int j = 0; j <= 2; j++){
cin >> n[j];
}
cout << "Debug " << n[2] << endl;
cout << endl;
for (int i = 0; i <= 2; i++){
cout << n[i] << "\t" << i << endl;
}
return 0;
}
Every time I print them, the last value of the array is modified, and I cannot figure out why! For a test input 6,7,8, the output is in the image below.
This for
for (int j=0;j<=2;j++){
cin>>n[j];
}
expects that the array has at least three elements with indices in the range [0, 2].
However you declared an array with two elements
int n[2];
If you are going to input three elements then the array should be defined as
int n[3];

C++ For Loop Won't Run [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm beginning to learn c++ with the free online codecademy course, and I'm not sure if it's a bug with their IDE or an error in my code.
#include <iostream>
#include <string>
int main() {
for (int i = 0; i > 0; i--) {
std::cout << i << " bottles of beer on the wall\n";
std::cout << i << " bottles of beer\n.";
std::cout << "take one down and guzzle it down\n";
std::cout << i - 1 << " bottles of beer on the wall.\n\n";
}
}
This is supposed to loop those strings until the number gets down to 1. Thank you for your help.
Your variable i is never bigger than 0.
I think that you meant to write int i = 10;
#include <string>
int main() {
for (int i = 10; i > 0; i--) {
std::cout << i << " bottles of beer on the wall\n";
std::cout << i << " bottles of beer\n.";
std::cout << "take one down and guzzle it down\n";
std::cout << i - 1 << " bottles of beer on the wall.\n\n";
}
}
I am not sure what your code is supposed to loop through. but this will run 10 times. you say you have a string. so try to get a variable to assign to a function string.length() to get the length assigned to i and then the loop will run as many times as the number of characters in the string.
the above code will run 10 times though

c++ - function requires c style array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I need to use some static c-libraries in my c++ code.
There I have a lot of functions which look like this (here I use a very simple example in this library to create a rotation matrix about the z-axis with the angel alpha):
void Rz(double alpha, double r[3][3])
In the end, r should be the final matrix.
I tried to call the function like this:
double alpha = 1.234;
double R[3][3] = {};
iauRz(alpha, R);
for(int i = 0; i<3; ++i){
cout << " " << R[i][0] << " " << R[i][1] << " " << R[i][2] << "\n";
}
and I hoped to display the rotation matrix, but this doesn't work. My result is:
0 0 0
0 0 0
0 0 0
Is there a mistake how I call the function? I'm not familiar with this c-style arrays...
EDIT: The library offers some very complex functions, the rotation about the z-axis was just a example. This means it is no possibility to use a more c++ style library.
EDIT 2: indices changed
You're using indices starting from 1. Indices start from 0 in C/C++
for(int i = 0; i<3; ++i){
cout << " " << R[i][0] << " " << R[i][1] << " " << R[i][2] << "\n";
}
Indices are zero based in almost every language and this has a reason: R[i] in C++ (and C) can also be written as
*(R + i * sizeof(datatype))
which means "take the memory address of R increased i times by the size of the datatype of the array and get the value at that address", so it makes perfect sense to have the first object of the array at R[0], because this is the memory address of R plus 0 bytes, so the first element

Not entering while loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
[EDIT] - Nevermind, this is a really dumb question
I'm currently trying to create a program that will basically tell me when insertion sort takes a longer time than merge sort for given a, b and n (in this case, successive powers of 2). This is what I have so far:
int comparisonSort()
{
//prompt user for values of a and b
int a = 0;
int b = 0;
cout << "Enter the value for a" << endl;
cin >> a;
cout << "Enter a value for b" << endl;
cin >> b;
double insertionSortTime = 0;
double mergeSortTime = 0;
double n = 2;
cout << outside while loop" << endl; //check
while (insertionSortTime < mergeSortTime)
{
cout << "inside while loop" << endl; //check
//compute the insertion and merge sort execution times
insertionSortTime = a*n*n;
mergeSortTime = b*n*log2(n);
cout << "is = " << insertionSortTime << " ms = " << mergeSortTime << endl;
n = pow(n, 2); // n^2
}
cout << "value of n that insertion sort beat merge sort is: " << n << endl;
return 0;
}
when I run this, I get:
Enter the value for a
8
Enter a value for b
64
outside while loop
value of n that insertion sort beat merge sort is: 2
I have no idea why the while loop isn't getting executed... Sorry if this seems like a really simple question, but I'm new to C++ and any help would be greatly appreciated, thank you!!
The conditional in
while (insertionSortTime < mergeSortTime)
is false in the first iteration when both insertionSortTime and mergeSortTime are set to zero. That explains why the loop never got executed.
Perhaps you meant to use:
while (insertionSortTime <= mergeSortTime)
Its because you have insertionSortTime = 0 and mergeSortTime = 0 and the condition for your loop is insertionSortTime < mergeSortTime.
Of course 0 is not < 0 so it never enters the loop.
Change it to <=.

If inside while error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
int main()
{
vector<int>numbers;
int numb = 0;
int i = 0;
int j = i - 1;
while (cin >> numb)
{
if (numb == numbers[j]) HERE IS THE PROBLEM!*
{
cout << "Numbers repeated\n";
}
numbers.push_back(numb);
cout << "numbers[" << i << "] = " << numbers[i] << endl;
++i;
}
/*** I don't understand why a exception, run time error, break or whatever it names.....................................................
On the first iteration through the loop, j is -1. Accessing numbers[-1] is undefined behavior because the index is outside the bounds of the vector.
Indeed, accessing any index is out of bounds until you put something in the vector, so you cannot index numbers at all until you have called push_back on it at least once.
This code will display the message if the user enters a number already in the vector:
while (cin >> numb) {
vector<int>::iterator found = std::find(numbers.begin(), numbers.end(), numb);
if (found == numbers.end()) {
cout << "Numbers repeated" << endl;
}
// If you don't want duplicate numbers in the vector, move this statement
// into an "else" block on the last "if" block.
numbers.push_back(numb);
}
This code on the other hand will only display the message when a number was the same as the last number entered, that is, if sequential numbers are the same:
while (cin >> numb) {
if (!numbers.empty() && numb == numbers.back()) {
cout << "Numbers repeated" << endl;
}
numbers.push_back(numb);
}
You need to initialize your numbers vector with initial data, or check to make sure j is withing in vector size. A vector is essentially an array, and like any array, you cannot go out of bounds.
if (j < numbers.size() && numb == numbers[j]) HERE IS THE PROBLEM!*
{
cout << "Numbers repeated\n";
}