| |
Anven
Page history last edited by Anven Zhang 2 yrs ago
Anven Zhang
Mr. Steele
C++ enrichment
20 April 2007
C++ Notes
C++ has been something that I've wanted to learn for a long time. I like how you cant bring little pieces together to make something bigger and running. C++ is very hard or easy to understand. Some people can understand it very easily because they like to think logically. Some people can never get it. I think I can get it and I really like doing this enrichment.
-
int creates integers; no decimals. It uses %i
-
float allows for decimals %f
-
char is only one letter %c
-
string uses char but is written %s
-
if you want something to print out to the screen, you use printf
-
if you want to get something from the user, you use scanf
-
if asks a question. If ___ is true, do this
-
else is the opposite. If_____ is not true, then do else
-
for loop has 3 parts: initialization, condition, increment. End each part with ;
-
initialization creates variable, set equal to 0
-
the condition is how many times the loop is run
-
the increment is how much it goes up by. adds 1, subtract 1, etc.
-
array is written as int name[number]
the while loop is shows that while ____ is happening, the program would keep running.
loops keep repeating themselves until they cannot fit the condition.
a function is adding, subtracting, multiplying, and dividing. It is written int add(int a, int b). After this you write either a+b, a-b, etc.
Always start off your program with #include <stdio.h> and #include <stdlib.h> They include standard functions. stdio.h means you can use printf. stdlib.h means you can use random numbers.
void main() is where your main programming actions take place.
while(1) keepsthe screen on.
April 19,20 2007
During these first two days Mr. Steele got us started on C++ programming and was able to get us to think like a programmer. We learned the basics of how to make integers and characters. We were also taught how to program a game where you had to guess a random number and make points. We learned arrays and functions. We are going to learn how to program a blackjack game next class and I can't wait. Mr. Steele also taught us things like why the semicolons need to be after most lines and why you need the functions.
An example of the pickanumber game:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int r = rand() % 100;
int score = 0;
int guess = -1;
char return_key;
srand( (unsigned int) time(0));
r = rand() % 100;
while(guess != r)
{
printf("pick a number between 0 and 99: ");
scanf("%i%c",&guess, &return_key);
score = score + 1;
if(guess == r)
{
printf("correct!\n");
printf("your score is %i\n",score);
}
if(guess>r)
{
printf("guess lower!\n");
printf("your score is %i\n",score);
}
if(guess<r)
{
printf("guess higher!\n");
printf("your score is %i\n",score);
}
}
// printf("r = %i\n",r);
while(1);
}

Anven
|
|
Tip: To turn text into a link, highlight the text, then click on a page or file from the list above.
|
|
|
Comments (0)
You don't have permission to comment on this page.