C ++ shortcut tutorial-Chapter 5-array and string (Part 1)

Source: Internet
Author: User

// -- C ++ shortcut tutorial -- Chapter 5 -- array and string (Part 1)
// -- Chapter 5 -- array and string
// -- 11/10/2005 Thurs.
// -- Computer lab
// -- Liwei

// -- Program #1 Array
# Include <iostream>
Using namespace STD;

Int main ()
{
Int sample [10];
Int T;
For (t = 0; t <10; t ++)
Sample [T] = T;
For (t = 0; t <10; t ++)
Cout <sample [T] <"";
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #2 array rand () Use
# Include <iostream>
# Include <cstdlib>
Using namespace STD;

Int main ()
{
Int I, min_value, max_value;
Int list [10];
For (I = 0; I <10; I ++)
List [I] = rand ();
Cout <Endl <"=========================" <Endl;
For (I = 0; I <10; I ++)
Cout <list [I] <'';
Cout <Endl;

Min_value = list [0];
For (I = 1; I <10; I ++)
If (min_value> list [I])
Min_value = list [I];
Cout <"minimum value:" <min_value <Endl;
Max_value = list [0];
For (I = 1; I <10; I ++)
If (max_value <list [I])
Max_value = list [I];
Cout <"maximum value:" <max_value <Endl;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #3 an incorrect Program
# Include <iostream>
Using namespace STD;

Int main ()
{
Int crash [10], I;
For (I = 0; I <150; I ++)
Crash [I] = I;
For (I = 0; I <150; I ++)
Cout <crash [I] <'';

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #4 sort arrays by bubble sort
# Include <iostream>
# Include <cstdlib>
Using namespace STD;

Int main ()
{
Int Nums [100];
Int A, B, T;
Int size;
Size = 100;
For (t = 0; t <size; t ++)
Nums [T] = rand ();

Cout <"original array is:/N ";
For (t = 0; t <size; t ++)
Cout <Nums [T] <'';
Cout <Endl <"=========================" <Endl;

For (A = 1; A <size; A ++) // traverse size-1 times
For (B = size-1; B> = A; B --) // The number of traversal times for each minimum value.
{
If (Nums [b-1]> Nums [B])
{
T = Nums [b-1];
Nums [b-1] = Nums [B];
Nums [B] = T;
}
}

Cout <"sorted array is:/N ";
For (t = 0; t <size; t ++)
Cout <Nums [T] <'';
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #5 Use CIN to read strings from the keyboard
# Include <iostream>
Using namespace STD;

Int main ()
{
Char STR [80];
Cout <"enter a string :";
Cin> STR;
Cout <"here is your string :";
Cout <STR;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #6 Use gets () to read strings from the keyboard
# Include <iostream>
# Include <cstdio>
Using namespace STD;

Int main ()
{
Char STR [80];
Cout <"enter a string :";
Gets (STR );
Cout <"here is your string :";
Cout <STR;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #7 strcpy ()
# Include <iostream>
# Include <cstring>
Using namespace STD;

Int main ()
{
Char STR [80];
Strcpy (STR, "Hello! ");
Cout <STR;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #8 strcat ()
# Include <iostream>
# Include <cstring>
Using namespace STD;

Int main ()
{
Char S1 [20], S2 [10];
Strcpy (S1, "hello ");
Strcpy (S2, "there ");
Strcat (S1, S2 );
Cout <S1 <"" <S2;
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #9 strcmp ()
# Include <iostream>
# Include <cstring>
Using namespace STD;

Bool password ();
Int main ()
{
If (password ())
Cout <"logged on./N ";
Else
Cout <"Access denied./N ";
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Bool password ()
{
Char s [80];
Cout <"Enter Password :";
Gets (s );
If (strcmp (S, "password "))
{
Cout <"invalid password./N ";
Return false;
}
Return true;
}

// -- Program #10 strcmp ()
# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace STD;

Int main ()
{
Char s [80];
For (;;)
{
Cout <"enter a string :";
Gets (s );
If (! Strcmp ("quit", S) break;
}
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #11 strlen ()
# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace STD;

Int main ()
{
Char STR [80];
Cout <"enter a string :";
Gets (STR );
Cout <"length is:" <strlen (STR );
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #12 reverse string Input
# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace STD;

Int main ()
{
Char STR [80];
Int I;
Cout <"enter a string :";
Gets (STR );

For (I = strlen (STR)-1; I> = 0; I --)
Cout <STR [I];
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #13 4 string processing functions
# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace STD;

Int main ()
{
Char S1 [80], S2 [80];
Cout <"enter two strings :";
Gets (S1 );
Gets (S2 );

Cout <"lengths:" <strlen (S1) <''<strlen (S2) <Endl;

If (! Strcmp (S1, S2 ))
Cout <"the strings are equal./N ";
Else
Cout <"not equal./N ";

Strcat (S1, S2 );
Cout <"S1:" <S1 <''<" S2: "<S2 <Endl;

Strcpy (S1, S2 );
Cout <S1 <"and" <S2 <''<" are now the same./N ";

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #14 converts a string to uppercase characters
# Include <iostream>
# Include <cstring>
# Include <cctype>
Using namespace STD;

Int main ()
{
Char STR [80];
Int I;
Strcpy (STR, "this is a test. abcdefghijk ....");

For (I = 0; STR [I]; I ++)
STR [I] = toupper (STR [I]);
Cout <STR;

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #15. Two-dimensional array
# Include <iostream>
Using namespace STD;

Int main ()
{
Int T, I, num [3] [4];
For (t = 0; t <3; t ++)
{
For (I = 0; I <4; I ++)
{
Num [T] [I] = T * 4 + I + 1;
Cout <num [T] [I] <'';
}
Cout <Endl;
}
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

// -- Program #16 two-dimensional array
# Include <iostream>
Using namespace STD;

Int sqrs [10] [2] = {
{1, 1 },
{2, 4 },
{3, 9 },
{4, 16 },
{5, 25 },
{6, 36 },
{7, 49 },
{8, 64 },
{9,81 },
{10,100 },
};

Int main ()
{
Int I, J;
Cout <"enter a number between 1 to 10 :";
Cin> I;

For (j = 0; j <10; j ++)
{
If (sqrs [J] [0] = I)
{Cout <"the square of" <I <"is" <sqrs [J] [1];
Break;
}
}
Cout <"/nthe square of" <I <"is" <sqrs [J] [1];
 

Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.