Created
September 25, 2012 23:41
-
-
Save hfuller/3785122 to your computer and use it in GitHub Desktop.
KH Assignment 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int n, i, j; //We initialize the variables. | |
//we don't give a shit what is in there (garbage atm) | |
int sum=0; //this time we DO care what it is. our sum starts at 0. | |
//I think main has to take parameters for it to be legal. | |
//I haven't coded C++ in a while, sorry. | |
//Maybe double check this from examples. | |
int main() { | |
cout << "Enter a positive integer: "; | |
cin >> n; | |
//So n is now our user's number. | |
j = n; | |
//Now j=n, but that will change in our loop. | |
for ( i=1; i<=n; /*do nothing*/) | |
//Don't know how familiar you are with for loops yet. But basically, | |
//this is what the for loop definition line means: | |
// - At the beginning of the loop, i is set to 1. | |
// - The loop only runs while i <= n. | |
// - Every time the loop finishes running, we do nothing. | |
// (I could put something instead of "do nothing" but I put it in the loop.) | |
{ | |
//i starts at 0 and increases by one every iteration. | |
//j starts at whatever the user enters (n) and decreases by one each iteration. | |
//so this follows the form of the assignment, | |
// 1*n + 2*(n-1) + 3*(n-2) + ... + (n-1)*2 + n*1 | |
cout << i*j << endl; | |
sum = sum + i*j; | |
//here we increase i and decrease j, in preparation for the next iteration. | |
i++; j--; | |
} | |
//once we reach here we are done iwth the loop. so we print the sucker. | |
cout << "The sum is " << sum << endl; | |
//I did not write a loop to allow the user to go again. | |
//If you need help with that then let me know. | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment