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 <=.
Related
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];
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 2 years ago.
Improve this question
I have this really simple program, with a for loop designed to run 3 times.
For every iteration, it will ask the user to enter in the weather. Each time, the user's input is stored in an integer array called C.
Once the loop concludes, I have another for loop that print's out the user's input. This next loop works fine for the 2 values, but gives off some weird messed up value once it reaches the third iteration.
int main(){
//Variable declaration:
int days;
int C[2];
int F[2];
for(days=0; days<3; days++){
cout << "What is the temperature in celsius for day " << days + 1 << ":" << endl;
cin >> C[days];
F[days] = (C[days] * 9/5) + 32;
}
cout << "\nCelsius\t\t Farenheit\n-------\t\t ---------" << endl;
for(days =0; days <3; days++){
cout << C[days] << "\t\t " << F[days] << endl;
}
return 0;
}
This next loop works fine for the 2 values but gives off some weird messed up value once it reaches the third iteration.
Your definition of C states it has 2 entries, but your loop runs for
three iterations (0, 1, and 2).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
I'm new to programming and i have problem with some items
i would appreciate any help
first i started initializing the vector as followed but i couldn't end the loop with Ctrl+Z
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector <double> temps;
double temp;
cout << "Enter a sequence of tempreatures : " << "\n" ;
while (cin >> temp){
temps.push_back(temp);
}
double sum = 0;
for (int i = 0; i< temps.size(); ++i)
sum += temps[i];
cout << "Mean temprature : " << sum / temps.size() << "\n";
sort(temps.begin(), temps.end());
cout << "Median temprature : " << temps[temps.size() / 2];
then i changed the while into this format :
cout << "ENter a sequence of tempreatures ending in 1500 : " << "\n" ;
while (cin >> temp){
if (temp == 1500)
break;
temps.push_back(temp);
}
now i have this error
"vector subscript out of range"
apparently break does not work properly here
what should i do?
Your issue is in the check condition of for loop.
for (int i = 0; i, temps.size(); ++i)
sum += temps[i];
It should be
for (int i = 0; i < temps.size(); ++i)
i, temps.size() will evaluate and then ignore the part before , and are left with temps.size() as check condition which will always be greater than 0 if you push_back at least one element and your loop will never end.You might want to read how ,(comma) works.
If you switch to std::getline into a string instead of std::cin into a double, you can check whether the input is empty:
std::string input;
std::getline(std::cin, input);
while (!input.empty()){
temps.push_back(atof(input.c_str()));
std::getline(std::cin, input);
}
If you also fix the for-loop as mentioned by Gaurav Sehgal, it works fine (Enter all numbers then hit enter without any input).
If you are on windows then you have to do
CTRL + Z
If you are on Unix based(Linux/Mac) then you have to do
CTRL + D
This will give the end of file signal and you will be able to break the loop
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";
}
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