array behaviour with auto and decltype - c++

Can someone please explain me what's actually happening in the following code:-
#include<iostream>
using std::cout;
using std::endl;
int main(){
int ia[]= {2,1,2,3,4,6,6,7,8,9};
auto ia2(ia);
*ia2= 19;
for(auto i : ia){
cout<<ia[i] << " " ;
}
//now my new ia = {5,1,2,3,4,6,6,7,8,9};
decltype(ia) ia3= {0,1,2,3,4};
for(auto i : ia3){
cout<<ia[i] << endl ;
}
return 0;
}
My output is coming as follows :-
21949 1 2 3 4 6 6 7 8 9 19
1
2
3
4
19
19
19
19
19
I am all confused, first of all I created an array ia with 2,1,2,3,4,6,6,7,8,9
then I learned with auto you're actually creating pointer to the first element
then I changed the value at first element by using *ia2= 19 which should have changed the value of 2 to 19 then I used for range loop to display the elements in ia array I used decltype as it doesn't create pointers but instead create an array. I again used for range loop to display elements but of ia instead of ia3 and I don't understand how I am getting output like this. Can somebody please let me understand this code.

Related

Print integer from array A based on array B integer?

New to C++. I am looking for advise on the approach to this problem.
Given the following array:
Array A
1 21 43 54 99
Array B
1 4 5
What I want to achieve:
Array B integer is use to find the position of the value in array A. The end result, using the above two array, will be
End result
1 54 99
Where array B 1 will extract 1 from array A, array B 4 will extract out 54 from array A and so on. How should I approach this problem?
So you can iterate over arrB and get the desired values by doing the following:
#include <iostream>
int main(){
int arrA[] = {1, 21, 43, 54, 99};
int arrB[] = {1, 4, 5};
// iterating over arrB
for (int i = 0; i < 3; ++i)
std::cout << arrA[arrB[i] - 1] << ' ';
return 0;
}
Output :
1 54 99
Note :
Don't forget to add the required conditions (ensure accessing array within bounds) to escape from the undefined behavior.

Is there any way to add numbers to a "buffer" in a while-loop?

I'm pretty new to c++, and at the moment i am trying to make a calculator that calculates a Euklid's Algorithm.
Anyways, what i need help with is how i can add the final number to some kind of array for each loop.
Lets for example say i put in the numbers 1128 and 16. my program will then give this output
1128 % 16 = 70 + 8
70 % 16 = 4 + 6
4 % 16 = 0 + 4
theese three lines is printed, one at the time, for each loop. What i want is to add the last numbers (8, 6 and 4) to an array. How would i do this?
Use Vector instead of array, Hope this Helps!
#include <iostream>
#include<vector>
using namespace std;
int main()
{
int a=1128,b=16,i;
vector<int>arr;
while(a>b)
{
cout<<a/b<<" "<<a%b<<endl;
arr.push_back(a%b);
a/=b;
}
cout<<a/b<<" "<<a%b<<endl;
arr.push_back(a%b); // Case: When a<=b in Vector
for(i=0;i<arr.size();i++)
cout<<arr[i]<<" "; // Array i.e 8 6 4
return 0;
}
Output:
70 8
4 6
0 4
8 6 4 // Array

What does this vector array code do? (C++)

Having difficulty finding an explanation to this.
What does this code do? I understand it creates an array of vector but that's about it.
How can I print the vector array and access elements to experiment with it?
#define MAXN 300009
vector<int>dv[MAXN];
int main()
{
for(int i=1;i<MAXN;i++)
for(int j=i;j<MAXN;j+=i)
dv[j].push_back(i);
}
The code is easy enough to instrument. The reality of what it ends up producing is a very simple (and very inefficient) Sieve of Eratosthenes. Understanding that algorithm, you'll see what this code does to produce that ilk.
Edit: It is also a factor-table generator. See Edit below.
Instrumenting the code and dumping output afterward, and reducing the number of loops for simplification we have something like the following code. We use range-based-for loops for enumerating over each vector in the array of vectors:
#include <iostream>
#include <vector>
#define MAXN 20
std::vector<int>dv[MAXN];
int main()
{
for(int i=1;i<MAXN;i++)
{
for(int j=i;j<MAXN;j+=i)
dv[j].push_back(i);
}
for (auto const& v : dv)
{
for (auto x : v)
std::cout << x << ' ';
std::cout << '\n';
}
}
The resulting output is:
1
1 2
1 3
1 2 4
1 5
1 2 3 6
1 7
1 2 4 8
1 3 9
1 2 5 10
1 11
1 2 3 4 6 12
1 13
1 2 7 14
1 3 5 15
1 2 4 8 16
1 17
1 2 3 6 9 18
1 19
Now, note each vector that only has two elements (1 and an additional number). That second number is prime. In our test case those two-element vectors are:
1 2
1 3
1 5
1 7
1 11
1 13
1 17
1 19
In short, this is a very simple, and incredibly inefficient way of finding prime numbers. A slight change to the output loops to only output the second element of all vectors of length-two-only will therefore generate all the primes lower than MAXN. Therefore, using:
for (auto const& v : dv)
{
if (v.size() == 2)
std::cout << v[1] << '\n';
}
We will get all primes from [2...MAXN)
Edit: Factor Table Generation
If it wasn't obvious, each vector has an ending element (that not-coincidentally also lines up with the subscripts of the outer array). All preceding elements make up the positive factors of that number. For example:
1 2 5 10
is the dv[10] vector, and tells you 10 has factors 1,2,5,10. Likewise,
1 2 3 6 9 18
is the dv[18] vector, and tells you 18 has factors 1,2,3,6,9,18.
In short, if someone wanted to know all the factors of some number N that is < MAXN, this would be a way of putting all that info into tabular form.

Weird issue with range based for loop

I am working on learning vectors in my C++ object oriented 1 class and we have been introduced the concept of range based for loops. I decided to practice the range based for-loops separately so that I could get used to the syntax but I came across a weird issue.
#include<iostream>
using namespace std;
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
for ( auto i: a)
{
cout << a[i] << " ";
}
return 0;
}
When I run the above code my output is the following.
2 3 4 5 6 7 8 9 0 1 Press any key to continue...
My output should read
1 2 3 4 5 6 7 8 9 0 Press any key to continue...
Can anyone tell me why my first index is skipped? I have visual studio 2013 professional.
You get the weird output because i in the range loop is the value from the array, not an index. That is,
for (auto i : a)
loops through the values of a. In your code you're effectively printing the sequence a[a[0]], a[a[1]], etc.
The code you probably want is
for (auto i : a) {
std::cout << i << std::endl;
}

Mystifying For-Loop Error

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 :/)