I'm on the "looping while" segment of a book named c++ programming written by Mike McGrath.
In the book it explains how a while loop works and what it can and cannot do etc. It gives an example code which I understand for the most part except for something that the book does not explain and I was wondering if any of you could explain it.
This is the code:
#include <iostream>
#include <vector>
using namespace std ;
int main(){
vector <int> vec(10) ;
int i = 0 ;
while (i < vec.size()){
i ++ ;
vec[i-1] = i ;
cout << " | " << vec.at(i-1) ;
}
}
Now, this is the way I am reading this, and I'm sure I'm reading it the wrong way because the results make no sense to me:
While i is less than 10 (vector's size) continue executing this code. So integer i is 0, but it increments to 1 at the beginning of the code. Next however, is the part I am so confused about.
It says vec[i-1], why is he subtracting 1? And then making it equal to i? If I try to make it vec[i] = i; the program crashes. So the way I am reading it is, vec[i - 0 ] would have to be 0 since 0 was just incremented to 1 on the previous step of the while loop. Then to display the results he once again calls the command vec.at() to i-1, which further confuses me. I simply do not understand what the -1 means within the vector. Shouldn't what's inside the brackets mean the position within the vector?
Because in the first line of the loop body he's incremented i (making it 1 on the first iteration), so he needed to subtract 1 to get back to valid array indexing (whereby 0 is the first index of an array, and n-1 is the last).
The program crashes when you change that, because you'd be accessing vec[1] to vec[N] (where N is the vector size) instead of vec[0] to vec[N-1], the real valid range of the vector.
He's assigning i so that each vector element contains an incrementing count. It's a bit confusing.
vec[0] = 1
vec[1] = 2
vec[2] = 3
...
vec[9] = 10
It would be better written like this:
int i = 0;
while (i < vec.size()) {
vec[i] = i+1;
cout << " | " << vec[i];
i++;
}
or:
for (int i = 0; i < vec.size(); i++) {
vec[i] = i+1;
cout << " | " << vec[i];
}
As Lightness has pointed out, he need to substract 1 to get back to 0 based indexing. The loop is same as below.
vector<int> vec(10);
int i = 0;
while(i < vec.size())
{
vec[i] = i + 1;
cout << " |" << vec.at(i);
i++;
}
that's why the output printed 1 to 10.
within the vector, [i-1] means minus current value of i by 1 then assign the value as index to vector. (note: the substracted value is not assigned back to i)
same description as above for vec.at(i-1)
Related
My goal is to have a player input a bunch of numbers for an array then that array is written into a text file. Another part of it is to be able to receive a bunch of numbers from a text file and put them into a sorted array of highest to lowest then output that array. But for some reason, I'm getting a lot of errors, ones that i feel like with some research I can fix. Unfortunately, there is one very confusing situation where I test to make sure the unsorted array is correct by outputting each element of the array. This is not a part of the final program but a test for now. I have a for loop that does so and it works perfectly, outputting each number as expected. Then in the next for loop, the exact same thing is supposed to happen but the numbers being outputted are all messed up. I do not understand how. Code below
void readFile(string fName) {
string fileName = fName + ".txt";
ifstream myFile(fileName);
char c;
string num;
int count = 0;
// Bring the array from file to int array
while (!myFile.eof()) {
myFile.get(c);
if (isspace(c) && num != "") {
int n = stoi(num);
intArray[count] = n;
count++;
num = "";
continue;
}
if (!myFile.eof()) {
num += c;
}
}
for (int i = 0; i < 10; i++) {
cout << intArray[i] << endl;
}
// Sort the array higest to lowest
for (int i = 0; i < 10; i++) {
cout << intArray[i] << " ";
for (int j = 9; j >= i; j--) {
if (j == 0) {
continue;
}
if (intArray[j] > intArray[j - 1]) {
int temp = arr[j];
intArray[j] = intArray[j - 1];
intArray[j - 1] = temp;
}
}
cout << endl;
}
}
sorry about the formatting above, its being weird so imagine the code is within the function.
This is what this outputs:
1
2
3
4
5
6
7
8
99
234
1
1
1
1
1
1
1
1
1
1
The numbers before the series of 1's is the actual array, the 1's is what is apparently the array according the cout in the last section of code where it says cout << intArray[i]
Your array does appear to be sorted. The reason for all the ones being printed is due to the location of the cout << within the outer loop.
Consider what your array looks like after your first iteration through the inner loop:
234,1,2,3,4,5,6,7,8,99
Now consider that you've incremented i to 1 in the outer loop. When you index your array intArray[i], the ith element is now 1 because you correctly moved it there. Each time, you're moving your smaller elements up one position in the array, then indexing to the position where the 1 is.
Don't try to print the sorted array while you're sorting it. Instead loop over it and print it after the sort.
I was given the integers 15, 16, 17 ,18 ,19 and 20.
I am supposed to put only the numbers divisible by 4 into a vector and then display the values in the vector.
I know how to do the problem using arrays but I'm guessing I don't know how to properly use pushback or vectors.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> arrmain; int i,j;
for (int i = 15; i <=20 ; i++)
{
//checking which numbers are divisible by 4
if (i%4 == 0)
{ //if number is divisible by 4 inserting them into arrmain
arrmain.push_back(i);
//output the elements in the vector
for(j=0; j<=arrmain.size(); j++)
{
cout <<arrmain[i]<< " "<<endl;
}
}
}
return 0;
}
wanted output: Numbers divisible by 4: 16, 20
As already mentioned in the comments, you have a couple of problems in your code.
All which will bite you in the end when writing more code.
A lot of them can be told to you by compiler-tools. For example by using -Weverything in clang.
To pick out the most important ones:
source.cpp:8:10: warning: declaration shadows a local variable [-Wshadow]
for (int i = 15; i <=20 ; i++)
and
source.cpp:6:26: warning: unused variable 'i' [-Wunused-variable]
vector arrmain; int i,j;
Beside those, you have a logical issue in your code:
for values to check
if value is ok
print all known correct values
This will result in: 16, 16, 20 when ran.
Instead, you want to change the scope of the printing so it doesn't print on every match.
Finally, the bug you are seeing:
for(j=0; j<=arrmain.size(); j++)
{
cout <<arrmain[i]<< " "<<endl;
}
This bug is the result of poor naming, let me rename so you see the problem:
for(innercounter=0; innercounter<=arrmain.size(); innercounter++)
{
cout <<arrmain[outercounter]<< " "<<endl;
}
Now, it should be clear that you are using the wrong variable to index the vector. This will be indexes 16 and 20, in a vector with max size of 2. As these indexes are out-of-bounds for the vector, you have undefined behavior. When using the right index, the <= also causes you to go 1 index out of the bounds of the vector use < instead.
Besides using better names for your variables, I would recommend using the range based for. This is available since C++11.
for (int value : arrmain)
{
cout << value << " "<<endl;
}
The main issues in your code are that you are (1) using the wrong variable to index your vector when printing its values, i.e. you use cout <<arrmain[i] instead of cout <<arrmain[j]; and (2) that you exceed array bounds when iterating up to j <= arrmain.size() (instead of j < arrmain.size(). Note that arrmain[arrmain.size()] exceeds the vector's bounds by one because vector indices are 0-based; an vector of size 5, for example, has valid indices ranging from 0..4, and 5 is out of bounds.
A minor issue is that you print the array's contents again and again while filling it up. You probably want to print it once after the first loop, not again and again within it.
int main()
{
vector<int> arrmain;
for (int i = 15; i <=20 ; i++)
{
//checking which numbers are divisible by 4
if (i%4 == 0)
{ //if number is divisible by 4 inserting them into arrmain
arrmain.push_back(i);
}
}
//output the elements in the vector
for(int j=0; j<arrmain.size(); j++)
{
cout <<arrmain[j]<< " "<<endl;
}
return 0;
}
Concerning the range-based for loop mentioned in the comment, note that you can iterate over the elements of a vector using the following abbreviate syntax:
// could also be written as range-based for loop:
for(auto val : arrmain) {
cout << val << " "<<endl;
}
This syntax is called a range-based for loop and is described, for example, here at cppreference.com.
After running your code, I found two bugs which are fixed in code below.
vector<int> arrmain; int i, j;
for (int i = 15; i <= 20; i++)
{
//checking which numbers are divisible by 4
if (i % 4 == 0)
{ //if number is divisible by 4 inserting them into arrmain
arrmain.push_back(i);
//output the elements in the vector
for (j = 0; j < arrmain.size(); j++) // should be < instead of <=
{
cout << arrmain[j] << " " << endl; // j instead of i
}
}
}
This code will output: 16 16 20, as you are printing elements of vector after each insert operation. You can take second loop outside to avoid doing repeated operations.
Basically, vectors are used in case of handling dynamic size change. So you can use push_back() if you want to increase the size of the vector dynamically or you can use []operator if size is already predefined.
The purpose of my code is to add the elements of a vector together into one integer. This is for Problem #1 on Project Euler:
https://projecteuler.net/problem=1
Here is my code:
int main()
{
int max_count = 1000; //The upper bound
//Loop for filling vector
for (int i = 1; i <= max_count; ++i){
int counter[max_count];
if (counter[i] % 3 == 0|| counter[i] % 5 == 0){
vector <int> start_point = {};
start_point.push_back (counter[i]);
for (auto& n : start_point){
int sum_of_elems = 0;
sum_of_elems += n;
cout << sum_of_elems;
}
}
}
return 0;
}
Currently, my code is outputting the following and I cannot figure out why.
32766143547943202305202750000-4646761603276630-76434810000-76434582500-464677056327662448-4646770403276632766-46467703232766327666032766230586999-970904238-95777621723084852023084852032766-970904244-46467688032766230624075-970911300230826120-1916976912327663276623063434032766230634681-957776214230826120140084992032766-970911280327660003276603276630-4646761603276623058081332766-464676440327663276632766230831712230745153065793306031200003276623074515300-191647711200023084852023074515365793360036000002308224802307451533657937207200-46467616032766000023083171232766230595552230831712032766327660-46467619232766230577342230822480230829920000-46467616032766230822480230829960-46467264032766230540223001920409600-46467247232766327661920409600-46467220832766000000000011072962560230556921230818160-4646738403276619204096000000230510592-1572142422000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001920169263100000170147416279176918919693827240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
You're reinitializing your sum variable on each loop, so you end up just printing the individual values. You're also mixing looping and summing, which is complicating your code; either skip the vector entirely (just have a summing loop), or fill it completely, then sum it.
I can't give much more useful advice because you've got a lot of associated problems here. You declare counter without initializing it, then read from uninitialized memory to populate start_point.
Point is, most of your variables need to be declared outside the loops (so they're not repeatedly reinitialized from scratch on each loop), your output should be after the loops, and counter needs real data so you're not invoking undefined behavior.
Removing vectors and other unnecessary variables, the code can be simplified to:
#include <iostream>
int main()
{
int max_count = 1000; //The upper bound
int sumOfMultiples = 0;
for (int i = 1; i < max_count; ++i)
if (i % 3 == 0 || i % 5 == 0)
sumOfMultiples = sumOfMultiples + i;
std::cout << "Sum of Multiples of 3 and 5 below 1000 is: " << sumOfMultiples << "\n";
return 0;
}
Output is:
Sum of Multiples of 3 and 5 below 1000 is: 233168
I am trying to teach myself C++ and I came across this program project in my book I am working from:
In an ancient land, the beautiful princess Eve had many suitors. She decided on the following procedure to determine which suitor she would marry. First, all of the suitors would be lined up one after the other and assigned numbers.
The first suitor would be number 1, the second number 2, and so on up to the last suitor,number n. Starting at the first suitor she would then count three suitors down the line (because of the three letters in her name) and the third suitor would be eliminated from winning her hand and removed from the line. Eve would then continue, counting three more suitors, and eliminating every third suitor. When she reached the end of the line she would continue counting from the beginning.
For example, if there were six suitors then the elimination process would proceed as follows:
123456 initial list of suitors, start counting from 1
12456 suitor 3 eliminated, continue counting from 4
1245 suitor 6 eliminated, continue counting from 1
125 suitor 4 eliminated, continue counting from 5
15 suitor 2 eliminated, continue counting from 5
1 suitor 5 eliminated, 1 is the lucky winner
Write a program that uses a vector to determine which position you should stand in to marry the princess if there are n suitors. You will find the following function from the Vector class useful:
v.erase(iter);
// Removes element at position iter
For example, to use this function to erase the fourth element from the beginning of a vector variable named theVector , use
theVector.erase(theVector.begin( ) + 3);
The number 3 is used because the first element in the vector is at index position 0.
I have some preliminary code written, but I am having a hard time figuring out how to tell the program after the first suitor (i.e the 3rd suitor) is eliminated to start counting from the fourth suitor, and so on. Perhaps a nested loop would work? I have found solutions online that use a class but it is difficult for me to understand and I feel like there is a simpler way of solving this problem, any help would be greatly appreciated.
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
int n;
vector<int> vec;
cout << "Enter the number of suitors: " << endl;
cin >> n;
// set some values (from 1 to n)
for(int i = 0; i <= n; i++){
vec.push_back(i);
}
// erase third suitor
vec.erase(vec.begin()+2);
// print vector with erased suitor
for(unsigned i = 0; i <= vec.size(); i++){
cout << vec[i] << endl;
}
}
First of all, there are two bugs in your program: you're initially putting [0, n] in the vector, which should be [1, n], and when printing the contents of the vector, you use <= where it should be <.
Now for the actual question. We want to iterate over the vector with steps of size 2 (the second person to the right of the current person):
for (int i = 0; i < vec.size(); i += 2)
However, when we reach the end of the array, we want to continue counting from the front. For this, we can use the modulo operator %:
for (int i = 0; i < vec.size(); i = (i + 2) % vec.size())
This will restrict i to the range [0, vec.size() - 1]. As such, our loop condition is now useless. Instead, we need to take care to terminate the loop when the vector's size is 1:
for (int i = 0; vec.size() > 1; i = (i + 2) % vec.size())
Putting it together, we get the following:
for (int i = 0; vec.size() > 1; i = (i + 2) % vec.size())
vec.erase(vec.begin() + i + 2);
Or equivalently:
for (int i = 2; vec.size() > 1; i = (i + 2) % vec.size())
vec.erase(vec.begin() + i);
The only element that is in the vector when this loop terminates is the number of the lucky suitor.
EDIT: To print out the contents of the vector after each elimination, try this:
for (int i = 2; vec.size() > 1; i = (i + 2) % vec.size())
{
vec.erase(vec.begin() + i);
for (int j = 0; j < vec.size(); j++)
cout << vec[j];
cout << endl;
}
Use relative positioning and take advantage of vec.erase()'s return value.
newpos = vec.erase(pos);
Here 'newpos' is pointing to the element that followed the erased one. Meaning, earsing '3' from {1, 2, 3, 4, 5, 6} gets you pointed to '4'. You can then do
pos = vec.begin();
while (pos != vec.end()) {
erasePos = // compute erasing position relative to 'pos'
// e.g. pos + 2, within bounds
pos = vec.erase(erasePos);
}
You need to do this in a loop.
For that use case where you simply iterate until the end and loop back to the beginning, I would use a list instead of a vector. The program could be :
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> l;
int n;
int i;
cout << "Enter the number of suitors: ";
cin >> n;
for (i=0; i<n; i++) l.push_back(i);
int delta = 3; // want to remove every third
i = delta;
list<int>::iterator it = l.begin();
while (l.size() > 1) { // loop until only one left
if (--i == 0) { // is it third ?
list<int>::iterator it2 = it++; // move pointer one step further (erase would destroy it)
l.erase(it2); // remove it
i = delta; // rearm counter
}
else it++; // was not third, simply goes on
if (it == l.end()) it = l.begin(); // if at end, go back to begin
}
cout << "Winner is number : " << l.front() + 1 << endl; // add one as our list was 0,1,...n-1
return 0;
}
I am reading C++ Programming In Easy Steps 4th and have read in to something I am having trouble understanding. I've only just started reading it the other day.
I had a particularly hard time getting my head around vector arrays and elements when I done that section of the book, I still don't feel confident about it despite taking a bunch of time to try and understand them, so I plan to re-visit that again. I'm now on a later part of the book however which is demonstrating loops and here is the code that I am having trouble with (Just one line):
#include <iostream>
#include <vector> // Include vector support (Vector function library)
using namespace std;
int main()
{
//Declaring an integer vector and an integer variable for loop counter
vector <int> vec ( 10 );
int i = 0;
//Now inserting a while loop to assign a counter value to an element of the vector on each iteration
while ( i < vec.size() )
{
i++; //Increment the counter
vec[ i -1 ] = i; // Assign count to element
cout << " | " << vec.at ( i -1 );
}
return 0;
}
I understand everything, but this line:
vec[ i -1 ] = i; // Assign count to element
I'm not sure what this is doing exactly particularly the i -1 part? Can someone break this down for me in an easy to understand way? I'm going to quickly re-visit the section on vector arrays and see if I can get an understanding.
Vector indicies start at 0. A vector of 10 elements is indexed from 0 to 9. Assuming your goal is to load the array consecutively with the numbers 1 to 10, and since you start i at 0 and increment it before using it in the index expression, you need to subtract 1 from it to get the proper index.
This piece of code does the same thing.
vector<int> vec(10);
int i = 0;
while (i < vec.size())
{
vec[i] = i + 1;
i++;
}
i = 0;
while (i < vec.size())
{
cout << i << ": " << vec.at(i) << endl;
i++;
}
It may help you if you break the code down to what the while loop is actually accomplishing by individual statements (you would see similar if you were running through the while loop with a debugger and also adding watch statements such as i-1):
#include <iostream>
#include <vector> // Include vector support (Vector function library)
using namespace std;
int main()
{
//Declaring an integer vector and an integer variable for loop counter
vector <int> vec ( 10 ); // vector of 10 elements: 0...9
int i = 0; // i starts with the value of 0
// replace while loop with the individual statements the loop accomplishes
// note also that vec.size() = 10
// while (i < vec.size())
// increment i to 1 (started as 0, from above)
i++; //Increment the counter
vec[ 0 ] = i; // i - 1 = 0 here - Assign count to element
cout << " | " << vec.at ( 0 );
// increment i to 2
i++; //Increment the counter
vec[ 1 ] = i; // i - 1 = 1 here - Assign count to element
cout << " | " << vec.at ( 1 );
// continue to i = 9 ...
// increment i to 10
i++; //Increment the counter
vec[ 9 ] = i; // i - 1 = 9 here - Assign count to element
cout << " | " << vec.at ( 9 );
return 0;
}
you're assigning the numbers 1 to 1o to your vector.
vec[i-1] = i
follows directly after i++ which means,for your first index you're assigning a value a count/step more than index.
Put simply,let's follow the first 4 values of i and the elements inserted into the vector:
i = 0;
i = 1;here vec[ 1 - 1] = vec[0] = 1
i = 2;here vec [2 - 1] = vec[1] = 2
i = 3;and finally vec[ 2 ] = 3
Vectors are wrapper classes for arrays (Source:Link to C++ reference ) that also do some really cool things like use templates for allocators, provide dynamic space, and use stl iterators.
So, vectors can be accessed like arrays using the [ ] operator. The two code snippets are identical:
First, the array form:
//Allocate and assign array
int* arr[5]={1,2,3,4,5};
//Print the 3rd element, note that the first index corresponds to 0.
std::cout<< arr[2] <<std::endl;
//Reassign the 2nd element:
arr[1]=54;
Now the vector version:
//Allocate and assign vector
std::vector<int> arr( {1,2,3,4,5} );
//Print the 3rd element, note that the first index corresponds to 0.
std::cout<< arr[2] <<std::endl;
//Reassign the 2nd element:
arr[1]=54;
A tip for the wise: Accessing vectors like this can be dangerous since the size of an array can be changed elsewhere in code. Instead consider the following for assignment:
std::vector<int> arr( {1,2,3,4,5} );
int index=0;
for(std::vector<int>::iterator it = arr.begin(); it!= arr.end(); ++it){
if(index++ == 3){
(*it) = 54;
}
}