March 10, 2007
First day of C++! I had a lot of fun! This is what I've learned so far:
When first starting a C++ application, you must reference certain libraries in order for the program to understand the various commands and parts of the C++ language that you will be using. (Type: #include <library.h>) We used the following libraries today:
<stdio.h><stdlib.h>
<time.h>
In order to create a function, you have to use the void(Type:void functon () {function details}) command. Every C++ application starts with the "main" function, but other functions can be placed inside it if the need arises.
Integers (Type: int integer) and characters(Type: char character)are integral to C++. These integers and characters must first be stated as shown in the previous sentence, then defined (Type: integer = value or character = value). These need to be separated by semicolons.
We learned how to display text with the print function(Type: printf("Text", integers);). Then we learned how to include user input with the limited and easily broken scan function(Type: scanf ("Text", integers and/or characters)). In order to reference integers in print and scan functions, you type %i; to refer to characters, type %c. Then define the characters and integers referenced by typing them in their order in the text. Separate them with commas.
If statements(Type: if(condition) {result} are essential for user interactivity. They apply the conditions set in them to any input and execute the given result.
In order to make sure that the program doesn't go away right after starting, you need to use the while command (Type:while(condition)){program}. As long as the condition is met, while will keep repeating the program indefinitely.
Some notes to remember:
C++ is case sensitive. No sticky caps!
Always close parenthesis. It is hard to find mistakes in a lot of code.
New program: MyFirst.sln:
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
void
{
srand(time(0));
difficulty = -1;
par = -1;
target = rand()%difficulty;
target = rand()%difficulty;
max = difficulty-1;
user_ans = -1;
high_para = 10;
low_para = 1;
score = 1;
printf("What is the highest number that you want to be able to guess?");
scanf("%i%c", &difficulty, &throw_away);
{
difficulty = difficulty/2;
par=par+1;
}
{
printf("Enter a number between 1 and %i:", max) ;
scanf("%i%c", &user_ans, &throw_away);
{
score= score+1;
printf("Too low. Try again!\n");
}
{
score = score+1;
printf("Too high. Try again!\n");
}
{
printf("That's right!\n");
{
printf("You used %i tries! Amazing!\n", score);
}
{
printf("You used %i tries! Try harder next time!\n", score);
}
{
printf("You used %i tries! Not bad.\n", score);
}
}
PickANumber()int difficulty;int target;int user_ans;int high_para;int low_para;int par;int score;int max;char throw_away;while(difficulty > 1)while(user_ans != target)if(user_ans < target)if(user_ans > target)if(user_ans == target)if (score < par)if (score > par)if (score == par)//if(user_ans < low_para)
{
//printf("And remember, the number has to be between one and ten!");
}
//if(user_ans > high_para)
{
//printf("And remember, the number has to be between one and ten!");
}
}
//printf("You entered %i\n", user_ans);
// This is a number-guessing game.
}
void
{
{
PickANumber();
printf ("Would you like to play again? (y/n)", play_again);
scanf ("%c%c", &play_again, &throw_away);
{
printf("Thank you for playing Tricia's awesome super-special number-guessing game!");
}
}
main()char throw_away;char play_again= 'y';while(play_again == 'y')if (play_again == 'n')//while(1);
}
March 17, 2007
Whoa! I learned a lot today!
While the "int" function enables a programmer to enter and receive integers, it does not accept decimals. In order to include decimals in programming, one must use the float function(Type:float variable). In order to incorporate these decimals in your script, you can use %f in where you would normally use %i. To regulate the number of decimal places that can be inputed and produced, type a 5.(number of decimal places) in between the % and f.
It is possible to create a function within an integer or float. This function ultimately defines what the integer/float will eventually stand for. By using a return command, you can sum up the function and set it equal to your valuable.
And the easily-fooled "scanf" command is dead! Long live "gets", which can be used to input a string of characters into a program. As long as you specify the the maximum number of characters in the referenced "char" command, "gets" will allow users to type a varying number of characters as well as save you a couple keystrokes. But because strings do not exist in the libraries used so far, the wonderful "gets" command can only be used once the <string.h> library has been included.
But "gets" only works with characters, and not integers, which means some of your integers will have to masquerade as characters for a while when using this command. This is okay, until your integers have to act like numbers. Then you can redefine your string as the bunch of integers it is by using "integer=atoi(string)". This assigns an integer to your string, converting it back into numbers.
New programs: hello.sln:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main ()
{
int i;
char initial[64];
char age[4];
//char initial2;
//char initial3;
//char throw_away;
int age_value;
printf("Enter your name.\n");
//scanf("%c%c%c%c", &initial1, &initial2, &initial3, &throw_away);
gets(initial);
for(i = strlen(initial); i >= 0; i = i - 1)
{
printf("%c\n", initial[i]);
}
printf("Congratulations, %s, you have set a new highest score!\n", initial);
//printf("Enter your age.\n");
//gets(age);
//printf("%s? Wow! You're old, %s!", age, initial);
//age_value = atoi (age);
//age_value = age_value / 2;
//printf("%s, did you know that half your age is %i?", initial, age_value);
while(1);
}
count1.sln:
#include <stdio.h>
#include <stdlib.h>
void main ()
{
int count;
int add;
char throw_away;
count = -1;
add = 0;
printf("Type in a number between 10 and 20\n");
scanf("%i%c", &count, &throw_away);
for(add = 0; add < count; add = add + 1)
{
printf("%i ", add + 1);
}
while(add < count)
{
printf("%i ", add + 1);
add = add + 1;
}
while (1);
}
calc.sln:
#include <stdio.h>
#include <stdlib.h>
float add(float x, float y)
{ float result;
result = x + y;
return result;
}
float sub(float x, float y)
{ float result;
result = x - y;
return result;
}
float mult(float x, float y)
{ float result;
result = x * y;
return result;
}
float divide(float x, float y)
{
if(y == 0)
{
printf("Error: Cannot divide by zero.");
}
else
{
float result;
result = x / y;
return result;
}
}
float PromptForInt(char * msg)
{
float f;
char c;
printf(msg);
scanf("%f%c", &f, &c);
return f;
}
float PromptForFloat(char * msg)
{
float f;
char c;
printf(msg);
scanf("%f%c", &f, &c);
return f;
}
void main ()
{
float numberone;
float numbertwo;
char throw_away;
char function;
float fun;
numberone = -1;
numbertwo = -1;
fun = -1;
//printf("Enter a number.\n");
//scanf("%f%c", &numberone, &throw_away);
//printf("Enter another number.\n");
//scanf("%f%c", &numbertwo, &throw_away);
//printf("What would you like to do with your numbers?( + - * /)\n");
//scanf("%c%c", &function, &throw_away);
printf("Enter calculation.\n");
scanf("%f%c%f%c", &numberone, &function, &numbertwo, &throw_away);
if(function == '+')
{
fun = numberone + numbertwo;
printf("%5.2f", add(numberone, numbertwo));
}
if(function == '-')
{
fun = numberone - numbertwo;
printf("%5.2f", sub(numberone, numbertwo));
}
if(function == '*')
{
fun = numberone * numbertwo;
printf("%5.2f", mult(numberone, numbertwo));
}
//if(function == '/' && numbertwo != 0)
//{
// fun = numberone / numbertwo;
// printf("%5.2f", fun);
//}
if(function == '/')
{
//fun = numberone / numbertwo;
printf("%5.2f", divide(numberone, numbertwo));
}
//if(function == '/' && numbertwo == 0);
//{
// printf("Error: Cannot divide by 0");
//}
while (1);
}
A blackjack game is in the makings.
March 24, 2007
Today we worked on the blackjack game for the whole class period. A lot of it was manipulation of old knowledge, but here are some new things I learned:
In order to divide and use a remainder, substitute a % for the / sign.
The C++ equivalent of “or” is || (SHIFT + 2 * key above ENTER). Likewise, a double ampersand (&&) means "and". These can be used in if statements for situations in which more than one condition can or has to be met.
And now, instead of taking the trouble of defining two seperate variables for integers that come in sets(example: playerone, playertwo...), you can define one variable and set the number of variables needed in brackets(example: player[2] ). This indicates that though only one variable has been stated, two have been created. And though they are created together, you can still call upon them seperately by typing in the number of the variable you want in brackets after the variable name(example: player[1] , player[2]). This saves a lot of typing as sets become larger; it also keeps things organized.
Some things that I haven't clarified in previous posts that I think should be included:
">" means "greater than", of course.
"<" means "less than".
In if statements, "==" are typed together to mean "equals".
"!=" is "doesn't equal".
">=" and "<=" mean "greater than or equal to" and "less than or equal to", respectively.
That's all I can think of, for now. And no, the blackjack game isn't done. It's amazing how much code goes into the (seemingly) simplest things...
March 25, 2007
More working on blackjack... but we finally finished! Sheesh, if this is how long it takes to program blackjack, I wonder how video game developers keep themselves from going insane...
And, since today was the last day of C++(sniff...), we had to complete an assessment! Dun, dun, dun...
So, without further ado...
After typing an "if" statement, you may want to specify what you want to happen in case something else happens. In that case you should type "else" after your if statement and place your results in "{}" after else. If there is more than one scenario that you want to work with, type "else if"s and specify the requirements for each. The last part should be a simple "else" statement.
And if you want to break a while loop at a certain time, place a break command inside a while loop inside the appropriate spot in your code.
New programs: blackjack.sln(at last!):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int deck[52];
int hand[2][11];
//int dealer[11];
//int hand2[11];
void InitDeck()
{
int count;
for(count = 0; count < 52; count = count + 1)
{
deck[count] = count;
}
}
void InitHand(int hand_number)
{
int card;
for(card = 0; card < 11; card = card + 1)
{
hand[hand_number][card]= -1;
}
}
void PrintCard(int card_number)
{
int suit = deck[card_number]/13;
int value = deck[card_number]%13+1;
if(value == 1 )
printf("A%c ", 3 + suit);
else if(value == 10 )
printf("T%c ", 3 + suit);
else if(value == 11 )
printf("K%c ", 3 + suit);
else if(value == 12 )
printf("Q%c ", 3 + suit);
else if(value == 13 )
printf("J%c ", 3 + suit);
else
printf("%i%c ", value, 3 + suit);
}
int GetValue(int card_number)
{
int value = deck[card_number]%13+1;
if(value > 10)
{
value = 10;
}
return value;
}
int GetHandValue(int hand_number, int show_dealer = 0)
{
int c;
int ace = 0;
int hand_value = 0;
for(c=0; c<11; c++)
{
if(hand[hand_number][c] >= 0 && hand[hand_number][c] <=51)
{
if(GetValue( hand[hand_number][c] ) == 1)
{
ace = ace + 1;
}
if(!show_dealer && hand_number == 0 && c == 0 )
{
}
else
{
hand_value = hand_value + GetValue( hand[hand_number][c]);
}
//hand_value =hand_value + GetValue(hand[hand_number][c]);
}
}
if(ace > 0 && hand_value + 10 <= 21)
{
hand_value = hand_value = 10;
}
return hand_value;
}
void PrintHand(int hand_number, int show_dealer = 0)
{
int c;
int hand_value;
hand_value = 0;
for(c=0; c<11; c++)
{
if (hand[hand_number][c] >= 0 && hand[hand_number][c] <= 51 )
{
if( !show_dealer && hand_number == 0 && c == 0 )
{
printf("??");
}
else
{
PrintCard( hand[hand_number][c]);
hand_value = hand_value + GetValue(hand[hand_number][c]);
}
}
}
printf("%i\n", GetHandValue(hand_number, show_dealer));
}
int random(int min, int max)
{
int range;
int rval;
range = max - min + 1;
rval = rand() % range;
rval = rval + min;
return rval;
}
int PlayAHand()
{
char option[64];
int answer;
int player_count = 0;
int hand_number;
int dealer_count = 0;
srand( (unsigned int) time(0));
InitDeck();
InitHand(0);
InitHand(1);
hand[1][0] = random(0, 51);
player_count = player_count +1;
hand[0][0] = random(0, 51);
dealer_count = dealer_count +1;
hand[1][1] = random(0, 51);
player_count = player_count +1;
hand[0][1] = random(0, 51);
dealer_count = dealer_count +1;
//hand[1][2] = random(0, 51);
//player_count = player_count +1;
//hand[0][2] = random(0, 51);
//hand[1][3] = random(0, 51);
//player_count = player_count +1;
//hand[0][3] = random(0, 51);
printf("Dealer:\n");
PrintHand(0);
printf("You:\n");
PrintHand(1);
printf("(H)it or (S)tay?\n");
gets(option);
while (option[0] == 'H' && GetHandValue(1) < 21)
{
hand[1][player_count = player_count] = random(0, 51);
player_count = player_count + 1;
printf("You:\n");
PrintHand(1);
printf("Hit or Stay?\n");
gets(option);
}
if (GetHandValue(0) < 17)
{
hand[0][dealer_count] = random(0, 51);
dealer_count = dealer_count +1;
printf("Dealer:\n");
PrintHand(0,1);
}
if(GetHandValue(1) > 21 )
{
printf("You lose.\n");
return 0;
}
else if( GetHandValue(0) > 21 )
{
printf("You win!\n");
return 1;
}
else if( GetHandValue(1) > GetHandValue(0) )
{
printf("You win!\n");
return 1;
}
else if( GetHandValue(1) == GetHandValue (0) )
{
printf("Push\n");
return -1;
}
else
{
printf("You lose.\n");
return 0;
}
}
void main2()
{
int i;
int card1;
int card2;
int card3;
InitDeck();
card1 = random(0, 51);
card2 = random(0, 51);
card3 = random(0, 51);
while(card1 == card2 || card1 == card3)
{
card1 = random(0, 51);
}
while(card2 == card3)
{
card2 = random(0, 51);
}
//while (card2 == card3)
//{
// card3 = random(0, 51);
//}
PrintCard(card1);
PrintCard(card2);
PrintCard(card3);
printf(" = %i\n", GetValue(card1) + GetValue(card2) + GetValue(card3));
//GetValue(card1);
for (i=0; i<0; i++)
{
printf("deck[%2i] = ",deck[i]);
PrintCard(i);
printf("%i", deck[i]);
printf("(%i)\n", GetValue(i));
GetValue(i);
}
InitDeck();
//PrintCard(0);
for (i = 0; i < 52; i++)
{
// PrintCard(i);
}
while (1);
}
void main()
{
int winner;
int points;
char bet[64];
int wager;
points = 1000;
printf("Welcome to Blackjack Version 1.0. Please remember that all responses are \ncase-sensitive. Have fun!\n");
printf("Points:%i\n", points);
while(points > 0)
{
printf("Your wager?\n");
gets(bet);
wager = atoi(bet);
while (wager > points)
{
printf("Did you bother to check your score? Your bet should be less than %i points!", points);
printf("Your wager?\n");
gets(bet);
wager = atoi(bet);
}
if( wager == 0)
{
break;
}
winner = PlayAHand();
if (winner == 0)
{
points = points - wager;
printf("Points:%i\n\n", points);
}
if (winner == 1)
{
points = points + wager;
printf("Points:%i\n\n", points);
}
if (winner == -1)
{
points = points;
printf("Points:%i\n\n", points);
}
}
printf("You have %i points.\n", points);
while (1);
}
avg.sln(Assessment):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void main()
{
char name[64];
float averagetest;
float averagehw;
float average;
int test[2];
int hw[5];
char string[64];
printf("Enter your name:\n");
gets(name);
printf("Welcome to Grade Calculator Version 1.0, %s.\n", name);
printf("Enter your first test score:\n");
gets(string);
test[1] = atoi(string);
printf("Enter your second test score:\n");
gets(string);
test[2] = atoi(string);
printf("Enter your first homework score:\n");
gets(string);
hw[1] = atoi(string);
printf("Enter your second homework score:\n");
gets(string);
hw[2] = atoi(string);
printf("Enter your third homework score:\n");
gets(string);
hw[3] = atoi(string);
printf("Enter your fourth homework score:\n");
gets(string);
hw[4] = atoi(string);
printf("Enter your fifth homework score:\n");
gets(string);
hw[5] = atoi(string);
averagetest = test[1] + test[2];
averagetest = averagetest / 2;
averagetest = averagetest * 0.50;
averagehw = hw[1] + hw[2] + hw[3] + hw[4] + hw [5];
averagehw = averagehw / 5;
averagehw = averagehw * 0.50;
average = averagetest + averagehw;
printf("%s, your average score is %5.2f%%, and ", name, average);
if(average == 100)
{
printf("your grade is A+.");
}
else if(average >= 94)
{
printf("your grade is A.");
}
else if(average >= 90)
{
printf("your grade is A-.");
}
else if(average >= 87)
{
printf("your grade is B+.");
}
else if(average >= 84)
{
printf("your grade is B.");
}
else if(average >= 80)
{
printf("your grade is B-.");
}
else if(average >= 77)
{
printf("your grade is C+.");
}
else if(average >= 73)
{
printf("your grade is C.");
}
else if(average >= 70)
{
printf("your grade is C-.");
}
else if(average >= 67)
{
printf("your grade is D+.");
}
else if(average >= 64)
{
printf("your grade is D.");
}
else if(average >= 60)
{
printf("your grade is D-.");
}
else
{
printf("your grade is F. Sorry, but you fail.");
}
while(1);
}
I can't believe the enrichement is over. I really enjoyed it; I think that I will try to learn some more advanced stuff, build on my knowledge.
Comments (0)
You don't have permission to comment on this page.