Replying to do the for loop exercises and some more, the first one was just a demonstration of knowledge, and math skills. They wanted you to look at a loop that summed all values from -100 to 100, and tell what it did, and what the result of sum would be, in this case, because all those numbers cancel out. It is 0, because sum is initially declared as 0.
Then they wanted me to rewrite, the while loops as for loops. Made them a bit shorter when I did, because all of the while loops can be reduced to the same pattern that for was invented to deal with easier anyway.
#include <iostream>
void print50To100() {
int value = 50;
for (int i = 50; i <= 100; ++i){
value += i;
}
std::cout << "The sum of 50 to 100 is: " << value << '\n';
}
void printDownFrom10() {
for (int i = 10; i > 0; --i) {
std::cout << i << ' ';
}
}
void printUserSpecifiedRange() {
int grab1 = 0;
int grab2 = 0;
std::cout << std::endl;
std::cout << "Enter 2 numbers seperated by a space." << '\n';
std::cin >> grab1;
std::cin >> grab2;
for (int i = grab1; i <= grab2; ++i) {
std::cout << i << ' ';
}
}
int main(){
print50To100();
printDownFrom10();
printUserSpecifiedRange();
system("pause");
return 0;
}
Done and done.
Then it wanted you to compare and contrast for and while, and provide the advantages and disadvantages of using either. And I will say, I find for loops easier to deal with, because the logic of the loop in so far as how much to repeat itself is contained in the initial loop declaration, where as with the while, some of the logic is normally within the loop.
Then it wanted me to make a bunch of errors again, generally common ones to deal with cout. But I'm skipping that, because I've made those mistakes before and don't want to deliberately make them again. Namely missing semicolon, missing quotes around a string literal, and missing semi-colon on a return statement.
I chose to modify the next bits exercises a bit, to have a little fun, and because I don't want to have the user input ctrl-z to end input, but rather I wanted them to hit enter to end the input and moreover I know how to do that. So blagh. And it doesn't specify that I have to use the while(std::cin >> input1) structure. So I'll be using getline and stringstream instead. So after trying that, and failing, I decided to just say fuck it, and make it with just the cin. I wanted to try and use stringstreams because I could then not worry about a wrong input(IE them inputting a non-integer value) fucking everything up. But meh, that leads to you not having an easy way to terminate the loop. And every way I tried to end it when a condition is met, either added a restriction on and/or an amount of numbers that had to be met, or was buggy, in the infinite loop sort of way, or was suboptimal compared to just using cin, because it suffered the same issues and just added 6 extra lines of code for type conversions through sstreams. I have learned not to be Fancy just for the sake of sounding like a pop star.
Exercise 1.16: Write your own version of a program that prints the sum of
a set of integers read from cin.#include <iostream>
int main() {
int inputInt = 0;
std::cout << "Enter numbers seperated by spaces, and stop by entering a non-integer value";
int sum = 0;
while (std::cin >> inputInt){
sum += inputInt;
inputInt = 0;
std::cout << "Total so far" << sum << '\n';
}
system("pause");
return 0;
}
Anyway, I went to sleep for a little bit after doing that, waking up 2 hours later was unexpected, and as I have an appointment unwanted, I'm gonna exhaust my brain with some Pathetic Attempts at Programming.
As far as the next chapter and set of exercises which are designed to introduce the if statement, another odd departure from most books on programming, which introduce the if statement well before they attempt to teach you looping.
Exercises Section 1.4.4
Exercise 1.17: What happens in the program presented in this section if the
input values are all equal? What if there are no duplicated values?
I'm assuming that it'll work fine, with every value returning 1 for no dupes and as many as you enter if you enter all equal input values, but the next question has you test it. So I imagine my theories will be tested there.
Exercise 1.18: Compile and run the program from this section giving it only
equal values as input. Run it again giving it values in which no number is
repeated.
#include<iostream>
int main()
{
// currVal is the number we're counting; we'll read new values into val
int currVal = 0, val = 0;
// read first number and ensure that we have data to process
if (std::cin >> currVal) {
int cnt = 1; // store the count for the current value we're processing
while (std::cin >> val) { // read the remaining numbers
if (val == currVal) // if the values are the same
++cnt; // add 1 to cnt
else { // otherwise, print the count for the previous value
std::cout << currVal << " occurs "
<< cnt << " times" << std::endl;
currVal = val; // remember the new value
cnt = 1; // reset the counter
}
} // while loop ends here
// remember to print the count for the last value in the file
std::cout << currVal << " occurs "
<< cnt << " times" << std::endl;
} // outermost if statement ends here
system("pause");
return 0;
}
That was odd, it worked completely different than expected, and I think the code was a little broken. Because it wouldn't cancel the loop, and when I closed it, caused the debugger to spit a kernel32 error at me, that I forgot to screenshot. Granted that coulda been due to it not freeing the ram as it closed because I force closed it. I ran it again after that, and it worked if I canceled out of the loop by giving it improper input. Like a string rather than an int. After running it again, for both it kinda behaved as expected but the first one didn't work as completely expected, it didn't list it's value until I canceled out of the loop. And the second didn't list the last value until the very end. Ie both didn't list their values until after the loop was canceled, which I suppose could be expected.
Exercise 1.19: Revise the program you wrote for the exercises in § 1.4.1(p.
13) that printed a range of numbers so that it handles input in which the first
number is smaller than the second, I think they are talking about the counting down one, this is solved easily by checking if the first is less than the second, then placing the variables depending on the results.
Think I'm gonna work on my own for a bit tomorrow today was a break day. I may or may not post the results. Also I kinda accidentally tried Smite, which is like video game heroin for me.