// -- C ++ shortcut tutorial -- Chapter 7 -- function, Part 1: Basic knowledge (Part 1)
// -- Chapter 7 -- function, Part 1: Basic knowledge
// -- 11/14/2005 mon.
// -- Computer lab
// -- Liwei
// -- Program #1 Scope
# Include <iostream>
Using namespace STD;
Void F1 ();
Int main ()
{
Char STR [] = "this is str in main ().";
Cout <STR <Endl;
F1 ();
Cout <STR <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void F1 ()
{
Char STR [80];
Cout <"enter something :";
Cin> STR;
Cout <STR <Endl;
}
// -- Program #2 this program will explain how the variable type is restricted in the code block
# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main ()
{
Int choice;
Cout <"1. Add numbers or:/N ";
Cout <"2. concatenate strings? :/N ";
Cin> choice;
If (choice = 1 ){
Int A, B;
Cout <"enter two numbers :";
Cin> A> B;
Cout <"sum is" <a + B <Endl;
}
Else {
Char S1 [80], S2 [80];
Cout <"enter two strings :";
Cin> S1> S2;
// CIN> S2;
Strcat (S1, S2 );
Cout <"concatenation is" <S1 <Endl;
} // End if-Else
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #3 incorrect Program
# Include <iostream>
# Include <cstring>
Using namespace STD;
Int main ()
{
Int choice;
Cout <"1. Add numbers or:/N ";
Cout <"2. concatenate strings? :/N ";
Cin> choice;
If (choice = 1 ){
Int A, B;
Cout <"enter two numbers :";
Cin> A> B;
Cout <"sum is" <a + B <Endl;
}
Else {
Char S1 [80], S2 [80];
Cout <"enter two strings :";
Cin> S1> S2;
// CIN> S2;
Strcat (S1, S2 );
Cout <"concatenation is" <S1 <Endl;
} // End if-Else
A = 10; // Error
Cout <S1; // Error
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #4 internal variables overwrite external variables
# Include <iostream>
// # Include <cstring>
Using namespace STD;
Int main ()
{
Int I, J;
I = 10;
J = 100;
If (j> 0 ){
Int I;
I = J/2;
Cout <"inner I:" <I <Endl;
} // End if
Cout <"outer I:" <I <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #5 position of variable Declaration
# Include <iostream>
// # Include <cstring>
Using namespace STD;
Int main ()
{
Cout <"enter a number :";
Int;
Cin>;
Cout <"enter a second number :";
Int B;
Cin> B;
Cout <"product:" <a * B <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #6 position of variable Declaration
# Include <iostream>
// # Include <cstring>
Using namespace STD;
Int main ()
{
For (INT I = 0; I <10; I ++ ){
Cout <I <"";
Cout <"squared is" <I * I <Endl;
}
I = 100; // error standard C ++ is not like this
Cout <I <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #7 a simple addition exercise program
# Include <iostream>
# Include <cstdlib>
Using namespace STD;
Void drill ();
Int count;
Int num_right;
Int main ()
{
Cout <"How many practice problems :";
Cin> count;
Num_right = 0;
Do {
Drill ();
Count --;
} While (count );
Cout <"you got" <num_right <"right .";
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void drill ()
{
Int count;
Int A, B, ans;
A = rand () % 100;
B = rand () % 100;
For (COUNT = 0; count <3; count ++ ){
Cout <"Your have 3 times chance to answer." <"now is:" <count + 1 <Endl;
Cout <"what is" <A <"+" <B <"? ";
Cin> ans;
If (ANS = a + B ){
Cout <"right./N ";
Num_right ++;
Return;
} // If
} //
Cout <"you have used up all your tries./N ";
Cout <"the answer is" <a + B <Endl;
}
// -- Program #8 pass a pointer to function 7
# Include <iostream>
Using namespace STD;
Void F (int * j );
Int main ()
{
Int I;
Int * P;
P = & I;
F (p );
Cout <I <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void F (int * j)
{
* J = 100;
}
// -- Program #9 pass a pointer to function 7
# Include <iostream>
Using namespace STD;
Void F (int * j );
Int main ()
{
Int I;
F (& I );
Cout <I <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void F (int * j)
{
* J = 100;
}
// -- Program #10 call a function using an array
# Include <iostream>
Using namespace STD;
// Void display (INT num [10]);
Void display (INT num []);
// Void display (int * num );
Int main ()
{
Int T [10], I;
For (I = 0; I <10; ++ I)
T [I] = I;
Display (t );
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void display (int * num)
{
Int I;
For (I = 0; I <10; I ++)
Cout <num [I] <'';
}
// -- Program #11 call a function with array elements
# Include <iostream>
Using namespace STD;
Void display (INT num );
Int main ()
{
Int T [10], I;
For (I = 0; I <10; ++ I)
T [I] = I;
For (I = 0; I <10; I ++)
Display (T [I]);
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void display (INT num)
{
Cout <num <"";
}
// -- Program #12 call a function with an Array
# Include <iostream>
Using namespace STD;
Void cube (int * n, int num );
Int main ()
{
Int I, Nums [10];
For (I = 0; I <10; I ++)
Nums [I] = I + 1;
Cout <"original contents :";
For (I = 0; I <10; I ++)
Cout <Nums [I] <'';
Cout <Endl;
Cube (Nums, 10 );
Cout <"altered contents :";
For (I = 0; I <10; I ++)
Cout <Nums [I] <'';
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void cube (int * n, int num)
{
// While (Num)
While (* n ){
* N = * n ** N;
// Num --;
N ++;
}
}
// -- Program #13 pass a string to the Function
# Include <iostream>
# Include <cstring>
# Include <cctype>
Using namespace STD;
Void stringupper (char * Str );
Int main ()
{
Char STR [80];
Strcpy (STR, "this is a test. TTT ");
Stringupper (STR );
Cout <STR <Endl;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void stringupper (char * Str)
{
While (* Str ){
* STR = toupper (* Str );
STR ++;
}
}
// -- Program # Another version of the 14 function strlen ()
# Include <iostream>
Using namespace STD;
Int mystrlen (char * Str );
Int main ()
{
Cout <"length of Hello there is :";
Cout <mystrlen ("Hello there .");
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Int mystrlen (char * Str)
{
Int I;
For (I = 0; STR [I]; I ++ );
Return I;
}