How to end program in a do-while (C++) - c++

Here is my code. I am trying to get the entire program to end if it goes into the second if statement inside the do-while loop. But every time I run it, it crashes. I am not sure what I am doing wrong.
#include <iostream>
using namespace std;
int main() {
int myData[10];
for(int i=0;i<10;i++){
myData[i] = 1;
cout<<myData[i];
}
do{
int i;
cout<<endl<<"Input index: ";
cin>> i;
int v;
cout<<endl<<"Input value: ";
cin>>v;
if(i>=0||i<10){
myData[i]=v;
for(int i=0;i<10;i++){
cout<<myData[i]<<" ";
}
}
if (i<0||i>=10){
cout<<"Index out of range. Exit.";
return 0;
}
}while(1);
}

if(i>=0||i<10){
Think about which numbers are either greater than zero or less than ten. I'm sure you realise that is true of all numbers. What you meant to write is
if(i>=0&&i<10){
This explains your crash, you are accessing the myData array with an index that is outside the array bounds.
It's very common for beginners to get && and || confused especially where there is negation involved as well.

Other people have told you the problem:
if(i>=0||i<10){
I'm going to explain why you hit this. You hit it because you didn't use any whitespace.
if (i >= 0 && i < 10) {
Please, please, please, for all that is good in the world, use whitespace liberally. You absolutely will have fewer bugs, because my version is far easier to read. Furthermore if you work with older developers (like me) we can actually read your code far more easily.

Related

i'm getting an error in for loop and if statements:

#include <iostream>
using namespace std;
int main() {
int i,t,km,sum=0;
std::cin >> t;
for( i=0;i<t;i++){
cin>>km;
}
for(i=0;i<t;i++){
if(km>300){
sum=km*10;
cout<<sum;
}
else if(km<=300){
sum=300*10;
cout<<sum;
}
else{
cout<<"wrong!";
}
}
return 0;
}
i'm not getting what's wrong with the code, when i'm entering number of test cases(t) as 1 it's running. but afterwars it's only executing the else block.
We'll start with a mini code review:
#include <iostream>
using namespace std; // Bad practice; avoid
/* Generally poor formatting throughout */
int main() {
int i,t,km,sum=0; // Prefer each variable declared on its own line; i is unnecessary
std::cin >> t;
for( i=0;i<t;i++){ // Overwrites `km` t times
cin>>km;
}
for(i=0;i<t;i++){ // Only does work on the last number entered
if(km>300){
sum=km*10; // You likely want to be adding on, not overwriting
cout<<sum;
}
else if(km<=300){
sum=300*10;
cout<<sum;
}
else{ // Impossible to reach given your two other conditions
cout<<"wrong!";
}
}
return 0;
}
The comments have spelled a lot of this out. You overwrite km t times. You only end up running your check on the last value of km that was entered, t times. You likely want to process t inputs, and the way to do this is with a single loop instead of the two that you have.
You overwrite your sum instead of (I assume) adding on to it. You wanted to sum += and not just sum =. The computer is actually pretty dumb, but it's very good at doing exactly what you told it to do. It is incapable of guessing your intent. That's supposed to be comforting, but it can be interpreted many ways.
I would also recommend taking the time to come up with better variable names. It looks like you're doing some calculations, possibly having to do with legs of a trip, but it's unclear. Making your code harder to read by choosing horrible names like t won't allow others to easily help you, and it will make your own code seem like a foreign language after just a couple days. Help yourself and others out by choosing good names.
As I stated, you might have been able to figure this out on your own if the code was indented properly. Consistent and proper formatting is extremely important for readability. If you don't want to be bothered doing it yourself, tools like clang-format exist.
Here's your code again, touched up a bit. I took a couple guesses at intended behavior.
#include <iostream>
int main() {
int t;
int km;
int sum = 0;
std::cin >> t;
for (int i = 0; i < t; i++) {
std::cin >> km;
if (km > 300) {
sum += km * 10;
std::cout << sum << '\n';
} else if (km <= 300 && km > 0) {
sum += 300 * 10;
std::cout << sum << '\n';
} else {
std::cout << "wrong!";
}
}
return 0;
}

why is the sort() changing my input array?

i am stuck on a problem where, after taking input of an array and sorting it and not doing any operation on it at all, the output shows a different array?
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
int n;
cin>>n;
long long int c[n],h[n],a[n];
for(int i=0;i<n;i++){
cin>>c[i];
}
for(int i=0;i<n;i++){
cin>>h[i];
}
sort(h,h+n);
for(int i=0;i<n;i++){
a[i]=0;
}
int i=0;
int begin=(i+1)-c[i];
int end = (i+1)+c[i];
int j=begin;
while(i<n){
a[j-1]++;
j++;
if(j>end){
i++;
begin=(i+1)-c[i];
end= (i+1)+c[i];
j=begin;
}
}
sort(a,a+n);
for(int i=0;i<n;i++){
cout<<h[i]<<" ";
}
}
return 0;
}
input for h[]={8,8,8,8,8}..n=5
output h[]={10,10,9,9,8}
Here is a version of your code written in reasonably decent C++. I didn't touch the loop in the middle because I have no clue what it's doing. You're using obscure variable names and no comments and doing all kinds of bizarre things with indexes and mixing them up with user input.
Now, reading indexes from user input and using them isn't bad, though in a real program you'd want to be doing lots of bounds checking on that input to make sure people weren't feeding you bad data. But doing all that stuff with such poorly named variables with no explanation is going to leave anybody looking at it scratching their head. Don't do that.
Also, avoid the use of begin and end as variable names, especially if they hold indexes. In most cases it will confuse things terribly as begin and end are important identifiers in the standard library and always refer to iterators, which are sort of like indexes, but most definitely not indexes, adding greatly to the confusion. beginidx and endidx could be acceptable substitutes in this case.
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using ::std::vector;
using ::std::sort;
using ::std::copy_n;
using ::std::copy;
using ::std::fill;
using ::std::istream_iterator;
using ::std::ostream_iterator;
using ::std::cin;
using ::std::cout;
int main() {
// your code goes here
using vec_el_t = long long int;
int t;
cin >> t;
while (t--) {
int const n = []() { int n; cin >> n; return n; }();
vector<vec_el_t> c{n}, h{n}, a{n};
copy_n(istream_iterator<vec_el_t>{cin}, n, c.begin());
copy_n(istream_iterator<vec_el_t>{cin}, n, h.begin());
// Suggested debugging code:
// cout << "h before sort: "
// copy(h.begin(), h.end(), ostream_iterator<vec_el_t>{cout, " "});
// cout << '\n';
sort(h.begin(), h.end());
// Suggested debugging code:
// cout << "h after sort: "
// copy(h.begin(), h.end(), ostream_iterator<vec_el_t>{cout, " "});
// cout << '\n';
fill(a.begin(), a.end(), 0);
// Weird, unexplained algorithm begins here
int i = 0;
int begin = (i + 1) - c[i];
int end = (i + 1) + c[i];
int j = begin;
while (i < n) {
a[j - 1]++;
j++;
if (j > end){
i++;
begin = (i + 1) - c[i];
end = (i + 1) + c[i];
j = begin;
}
}
// Weird unexplained algorithm ends here
sort(a.begin(), a.end());
copy(h.begin(), h.end(), ostream_iterator<vec_el_t>{cout, " "});
}
return 0;
}
Changes made... Use vector not a variable length array, which isn't valid C++ anyway and will only work in g++ and clang. Don't use explicit loops if there is an algorithm that will do the job. Try to make as many things const as you can so you can make sure that the compiler catches it if you try to change things you didn't mean to change. Avoid using std; and if you want to import names from ::std import exactly the ones you need. Don't use compiler or library implementation specific header files and use the ones from the standard instead (i.e. no bits/stdc++.h).
As for your problem, I have no idea. I suspect that the index manipulation combined with looping isn't doing what you expect. If you print out the arrays before and after sorting, you will discover that sort only alters order, and not content.
As a general rule, always suspect your own code first and make absolutely sure it's correct. And if you really think it's the library code, prove it beyond a shadow of a doubt before coming here to ask why the library isn't doing what it says it does.
The complicated code I didn't touch looks rife with opportunities for out-of-bounds access, and that results in undefined behavior, which means your program might do absolutely anything in that case. You might change uses of operator [] with calls to the at function (one of the many perks of using vector) instead. That way, attempts at out-of-bounds access will throw an exception.
Within these lines you are accessing a outside its limits:
int i=0;
int begin=(i+1)-c[i]; // begin = 1 - c[0]; <<-- this could be arbitrarily small!
int end = (i+1)+c[i]; // unrelated
int j=begin; // also equal to 1-c[0]
while(i<n){
a[j-1]++; // increment a[-c[0]] which is UB unless c[0]==0
This means undefined behavior (UB), i.e., it could do nothing, it could segfault, or (what apparently happened in your case) access elements of an adjacent data structure.

Error in program for modified bubble sort

Codeforces problem 339A-http://codeforces.com/problemset/problem/339/A
I have tried to sort the values stored at even places of the array(starting from 0).I get an error while running the program or program returns a junk value.What is wrong with my solution?
My solution:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s[101],temp;
int i,j;
cin>>s;
for(i=0;i<strlen(s);i+=2) //Bubble sorting values at even values of i.'+' is stored at odd values of i.
{
for(j=0;j<(strlen(s)-i-2);j+=2)
{
if(s[j]>s[j+2])
{
temp=s[j];
s[j]=s[j+2];
s[j+2]=temp;
}
}
}
cout<<s;
}
Your compiler should have warned you about the problem (you did switch on all warnings, yes? always do that!): Once i==strlen(s)-1, the loop for j is essentially unbounded, by the magic of arithmetic rules for signed/unsigned values.
for(unsigned j=0; j+2+i < strlen(s); j+=2)
does not have this problem. (i should be unsigned as well.)
Or stop the loop for i earlier. The problem in your code is still there then, but you won’t run into it. But I believe that is the worse route to take – fix the bug, and then optimize by observing i doesn’t need to go as far up, because the last character already forms a sorted sequence.
For odd lengths len of s, the outer loop runs until i==len-1. The inner loop then terminates at len - len - 1 - 2. Since strlen returns an unsigned type, this evaluates to a very large unsigned number, causing the inner loop to read way beyond the end of s. Eventually you'll reach memory you don't have access to read or write, causing the crash.
You can fix this by ending the outer loop sooner
int len = strlen(s);
for(i=0;i<len-2;i+=2) {
for(j=0;j<(len-i-2);j+=2)
Change this:
for(i=0;i<strlen(s);i+=2)
Into this:
for(i=0;i<strlen(s) - 2;i+=2)
Otherwise the s value be handled beyond its end-point
here is my code
void bubble(int a[], int n){
for(int i=0; i<n; i++){
int swaps=0;
for(int j=0; j<n-i-1; j++){
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
swaps++;
}
}
if(swaps==0)
break;
}
}

Why is this code showing segmentation error on codechef?

Please i am stuck at this question for half an hour and can't find why the error comes?
Problem code : test
Life, Universe and Everything
#include<iostream>
using namespace std;
int main()
{
int a[20],i;
cin>>a[0];
for(i=1;a[i-1]!=42;i++)
{
cout<<a[i]<<"\n";
cin>>a[i];
}
return(0);
}
Your code tries to access non-existing array elements, which causes segfault. You should stop your loop before the array index gets larger than the length of the array minus 1:
int a[20];
for (i = 1; i < 20 && a[i - 1] != 42; i++)
{
// ...
}
Apart from limit problem, your printing elements without initializing them
//for i = 1, a[i] is uninitialized
cout<<a[i]<<"\n";
On accessing a local variable (like this), you're likely to get garbage value.
This might be better substitute for what you are trying to do:
int a[20],i;
for(i=0;i < 20;i++)
{
cin>>a[i];
if (a[i] == 42)
break;
cout<<a[i]<<"\n";
}
You are trying to print the uninitialized data...
#include<iostream>
using namespace std;
int main()
{
int a[20],i;
cin>>a[0]; // a[0] uninitialized
for(i=1;a[i-1]!=42;i++)
{
cout<<a[i]<<"\n";
cin>>a[i];
}
return(0);
}
In the for loop get the data first and then print it.Your array size is 20 but you are trying to write upto 42.
You use array values before initializing them. C++ doesn't initialize non-static arrays for you unless you tell it to, so $DEITY knows what's in there. And technically, whatever's in there could cause an exception...or any number of other things. (For ints, on an x86 machine, that's actually highly unlikely. But elsewhere, it's possible.)
The user can enter more than 20 numbers. That's really just a special case of the more general problem, though: You allow unknown number of entries, but aren't able to accept them all without crashing.
If you don't know beforehand how many objects there will be, use a vector.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector<int> a;
int entry;
cin>>entry;
// Oh, yeah. This. You really, *really* want to check `cin`.
// Otherwise, if the user decided to type "i like cheese", you'd loop
// forever getting zeros while cin tried to parse a number.
// With the array, that'd typically cause a segfault near instantly.
// With a vector, it'll just eat up all your CPU and memory. :P
while (cin && entry != 42) {
a.push_back(entry);
cout << entry << "\n";
cin >> entry;
}
return 0;
}

Beginner C++: Strange Behaviour

thanks for taking the time to read this question!
The program is to find the smallest prime number after one billion. at the end of int main() i included a console input cin>>x;with the intention of preventing the command prompt from closing too quickly so i can see the result. however, i realised that i must first enter something before it shows me the result i want.
SO the question is: why is this so even though the console output statement cout<<i;is before the input statement cin>>x;?
#include <iostream>
#include <math.h>
using namespace std;
int is_prime(int x);
int main()
{
for (int i=100000000;;i++){
if(is_prime(i)){
cout<<i;
break;}
int x;
cin>>x;
}
}
int is_prime(int x)
{
double maxvalue = sqrt(static_cast<double>(x));
for ( int i=2;i<=maxvalue;i++){
if (x%i == 0 ) return false; }
return true;
}
It seems like the cin >> x is inside the for loop. So, every iteration of the loop, you will try to read something from the stream. So, you need to enter some numbers before the i becomes prime.
EDIT: Apparently, 1000003 is prime, so you don't have to enter a lot of numbers.
why is this so even though the console output statement cout<<i; is before the input statement cin>>x;?
Because the break changes the order of execution, letting the control skip over cin >> x once the prime is found. You need to move the cin >> x out of the loop.
There are several things that you can do to optimize things quite a bit: rather than trying to divide by every number 1 through sqrt(N), you should divide out only the prime numbers that you have found so far. This would speed things up a lot. You can also drop the call of sqrt by using i*i < x as your exit condition.