Mystifying For-Loop Error - c++

I am using Ubuntu 10.10, Codeblocks IDE, and gcc compiler. I noticed the program I am writing was creating some odd output. Eventually I narrowed the issue down to a for-loop in the program. I was surprised to discover that the following basic for-loop didn't perform as expected.
#include <iostream>
using namespace std;
int main()
{
for(unsigned int i = 0; i < 21; i++)
{
cout << i << endl;
}
return 0;
}
When I compile and run it, the output is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Although one would expect the output should include zero. Very surprisingly, when I change the for loop to
#include <iostream>
using namespace std;
int main()
{
for(unsigned int i = 0; i < 20; i++)
{
cout << i << endl;
}
return 0;
}
I get the expected output of:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
I can't for the life of me figure out why 21 (and all numbers greater than 21) give me this false output, while 20 (and lower numbers) don't. If anyone has run into anything like this before, I'd sure appreciate hearing how he/she worked around it.

maybe the screen just scroll?
try to redirect the output to a text file

This seemed so weird that i run your first program and got what i would expect :
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
However, i notice that you use gcc as your compiler. This one is aimed towards c programming. Better use g++ as i did for this. It works fine here. (i'm actually surprised gcc compiles that :/)

Related

C++ rand() always giving same TWO values... and then working perfectly [duplicate]

This question already has answers here:
Rand() % 14 only generates the values 6 or 13
(3 answers)
Closed 5 months ago.
First, I know the basic principle of planting a time seed, and my program's outputs are partially random. But this baffles me.
On subsequent executions of the program, the seven randomly generated values may look like this:
14 14 47 70 84 2 24
14 28 42 52 31 10 12
63 25 4 50 20 27 56
63 19 55 44 65 60 52
14 16 17 40 54 77 4
63 6 79 36 51 85 39
The rest of the values appear random, but the first value is always either 14 or 63. Why is this happening, and how can I make it completely random?
The code is supposed to draw a random Scrabble letter without replacement, with a cout statement added for debugging purposes.
#include <iostream>
using namespace std;
int main()
{
string bag = "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ";
srand(time(0));
for (int a = 0; a < 7; a++)
{
int i = rand()%bag.size();
cout << i << ' ';
bag.erase(i,1);
}
cout << endl;
return 0;
}
Compiled in MacOS Catalina 10.15 terminal
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
As has been explained in comments, it looks like your compiler's C runtime library has a bad rand function.
But you're not using C, you're using C++! Starting at C++11, you have all sorts of random-number generation facilities available in the C++ standard library.
#include <iostream>
#include <string>
#include <random>
int main()
{
std::random_device eng; // or any other type of engine
using dist_params = typename std::uniform_int_distribution<int>::param_type;
int max = 99;
std::uniform_int_distribution<int> dist (0, max);
for (int a = 0; a < 7; a++)
{
int i = dist(eng);
std::cout << i << ' ';
dist.param(dist_params{0, max});
}
std::cout << '\n';
return 0;
}
Or, what I expect you were really going for:
#include <iostream>
#include <string>
#include <random>
#include <time.h>
int main()
{
std::string bag0 = "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ";
std::random_device eng;
time_t t;
using dist_params = typename std::uniform_int_distribution<size_t>::param_type;
std::uniform_int_distribution<size_t> dist;
for (auto j = 0; j<100; ++j)
{
auto bag = bag0;
for (int a = 0; a < 7; a++)
{
dist.param(dist_params{0, (bag.length())-1});
int i = dist(eng);
std::cout << bag[i] << ' ';
bag.erase(i, 1);
}
std::cout << '\n';
}
return 0;
}
The only caveat is that random_device may not produce random numbers on your platform.
rand() or std::rand() never generates true random number. It generates pseudo-random numbers. This is because computers are unable to generate truly random numbers itself, it requires assistance. Let's say you pressed a key exactly 2.054 seconds after the previous keypress. This is truly a random number. Computers use this data to generate truly random numbers. rand() or std::rand() generates a pseudo-random number, so needs to be seeded (with srand() or std::srand()). If the number you used to seed isn't random, the output wouldn't be random too. Moreover, you are using time() (or std::time()) which returns an int holding the number of seconds passed since epoch. So if you execute the program multiple times too rapidly, the seed would be the same and the output too. It also seems that your standard library a bad rand() or std::rand() function.
Example:
Output of the program (compiled from your code) executed 10 times rapidly (environment: Ubuntu, bash):
$ for i in {0..9} ; do ./a.out ; done
50 11 3 60 36 17 42
50 11 3 60 36 17 42
50 11 3 60 36 17 42
50 11 3 60 36 17 42
50 11 3 60 36 17 42
50 11 3 60 36 17 42
50 11 3 60 36 17 42
50 11 3 60 36 17 42
50 11 3 60 36 17 42
50 11 3 60 36 17 42
What to do?
I can suggest you use another time function to seed, which outputs time in milliseconds (or even nanoseconds) or get your own random number generator. See this article to know how pseudo-random number generators work. This will also help you to build your own as it seems that your standard library gives a bad rand() or std::rand() function.

Is there a way to set a specific amount of maximum spaces for TAB character (backslash t) "\t" in C++?

I want to print a square matrix that needs to be spaced each between the elements by 3 spaces. Then I found that char '\t' might be the easiest way. But, I think the number of spaces is somehow defined in certain algorithm. Could someone give me a guide through the algorithm or is there a way to set certain spaces in C++ for '\t'?
I know how to output manually by determining the number of space character. However '\t' seems simple to code rather than looping certain algorithm.
For a simple square matrix :
for (int x = 0, num = 1; x < 5; x++) {
for (int y = 0; y < 5; y++, num++) {
cout << num << "\t";
}
cout << endl;
}
The code outputs
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
while I need
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
Is there a way to set '\t'?
But, I think the number of spaces is somehow defined in certain algorithm.
...
Is there a way to set '\t'?
Not unless your connected terminal allows you to control that in a way.
The usual way to control output formatting using I/O manipulators, for your case
std::setw()
std::left (or may be better std::right for numbers)
I'm pretty sure the size of a tab is defined by your console, so this will be a setting of your IDE.
If you want a consistent quantity of spaces, you're better off using spaces. That way you don't have to alter the console settings of everything you run your code on. Why not just do something like
" "
instead of "\t" ?
EDIT: Apparently Stack overflow doesn't appreciate multiple spaces in-text.

C++ : For loop not iterating designated number of times (1 iteration less) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I have a for loop and a variable C.
The loop begins at 0 and is expected to run C times but actually it runs C-1 times.
Here is my code :
vector<int> v(1000) //Allocated globally
int M, S, C;
cin>>M>>S>>C;
//cout<<M<<" "<<S<<" "<<C;
int fi=0, la=0;
for(int i=0; i<C; i++)
{
int f;
cin>>f;
if(i==0l){ fi = f;}
v[f] = f;
cout<<i<<" "<<f<<" "<<v[f]<<endl;
if(i==C-1){ la = f;}
}
This is my test case -
3 27 16
2
3
5
6
8
9
10
13
14
15
16
19
20
21
22
27
Output by Xcode :
0 2 2
1 3 3
2 5 5
3 6 6
4 8 8
5 9 9
6 10 10
7 13 13
8 14 14
9 15 15
10 16 16
11 19 19
12 20 20
13 21 21
14 22 22
I use Xcode on Mac if it makes a difference.
The variables fi and la are to find the first and the last element of the list.
I want to know what is wrong in my code for the for loop and why is it not iterating C times.
Thanks
Your loop is iterating C times. This is the classic Zero-Based Numbering issue.
Let me explain using your example where C is 16 and a numbered list:
2
3
5
6
8
9
10
13
14
15
16
19
20
21
22
27
So you see your lopp did iterate 16 times. To go from 0 to 16 would have actually been iterating one more time, so 17 times.

Weird __COUNTER__ behavior on LLVM

I'm trying to understand what could cause this problem because everything seems to be fine but the result seems to point out some sort of buggy behavior.
I have a custom struct defined as:
struct MyStruct {
const u16 index;
...
MyStruct(u16 index) : index(index), ... { }
}
static const MyStruct array[] = {
MyStruct(__COUNTER__,...),
MyStruct(__COUNTER__,...),
MyStruct(__COUNTER__,...)
...
}
Now, if I check the preprocessed file with XCode everything is fine and I get incremental indices as they are supposed to be.
At runtime, instead, what happens is that the index 16 becomes 23 and the successive indices are lowered by one, eg:
real index stored index
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 23
17 16
18 17
19 18
20 19
This is quite curious, especially because 16 seems to be a specific bound. Compiler is Apple Clang 4.2 (based on LLVM 3.2), on XCode 4.6.

Why would a error free code not give the expected result on one machine but yes on the other

I'm having a problem with a part of code. It should work,since it is error free, and it has no logical problems since it does work on someone else pc, but on my computer the result is the same as the input.
code(runnable):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
class RAngle{
public:
int x,y,l;
RAngle(){}
RAngle(int i,int j,int k){
x=i,y=j,l=k;
}
bool operator<(const RAngle& rhs)const{
if(l < rhs.l){
return true;
}
return 0;
}
friend ostream& operator << (ostream& out, const RAngle& ra){
out << ra.x << " " << ra.y << " " << ra.l;
return out;
}
friend istream& operator >>( istream& is, RAngle &ra){
is >> ra.x;
is >> ra.y;
is >> ra.l;
return is ;
}
};
void descrSort(vector <RAngle> &l){
for(unsigned i=0; i<l.size();i++){
cout<<l[i]<<endl;
}
cout << "==================" << endl;
sort(l.begin(),l.end());
reverse(l.begin(),l.end());
for(unsigned i=0; i<l.size();i++){
cout<<l[i]<<endl;
}
}
void readData(vector <RAngle> &l){
RAngle r;
ifstream f_in;
f_in.open("test.txt",ios::in);
for(int i=0;i<10;++i){
f_in >> r;
l.push_back(r);
}
}
int main(){
vector <RAngle> a;
readData(a);
descrSort(a);
return 0;
}
DATA:
1 7 31
2 2 2
3 3 3
4 5 1
10 5 1
1 1 9
10 3 10
4 5 7
5 4 15
2 3 25
1 7 31
The output on other machine(only print part after, the descr sort):
1 7 31
2 3 25
5 4 15
10 3 10
1 1 9
4 5 7
3 3 3
2 2 2
10 5 1
4 5 1
on my computer(hoel output):
1 7 31
2 2 2
3 3 3
4 5 1
10 5 1
1 1 9
10 3 10
4 5 7
5 4 15
2 3 25
==================
1 7 31
2 2 2
3 3 3
4 5 1
10 5 1
1 1 9
10 3 10
4 5 7
5 4 15
2 3 25
This means your code has an error. C and C++ let things compile and run that actually have errors in them. Like this:
int i;
std::cout << i << std::endl;
Will do different things on different machines. I would call it an error. The compiler won't.
Now as for where your error is, here is my "use a debugger" speech. Use a debugger. It will take you less time to use a debugger and have a decent chance of finding the error than it did for me to read your code to see if anything jumped out. Compile with -g. Google "gdb cheat sheet." Run with gdb. Follow the cheat sheet. See where your code does something unexpected.
Seems smart to do this on the machine that is giving the wrong output. And see where it's doing something wrong.
To expand on what #djechlin said, what you most likely have is "undefined behavior".
Most programming languages strictly define what can and can't be done, and compiler writers use that to generate compiler errors. Undefined behavior, on the other hand, means that it is up to the compiler writer and system; anything can happen - it can work correctly, it can erase your boot sector, zombie Alan Turing rise and feast on your brains, etc.
For example, using an uninitialized variable like so:
int i;
std::cout << i << std::endl;
on some compilers will give you a pattern like 0xCECECECE in debug builds to help find uninitialized variables. In release builds it might be set to 0 by the compiler, or it might be garbage data.