Professional Documents
Culture Documents
int sample[10];
listing 2
#include <iostream>
using namespace std;
int main()
{
int sample[10]; // this reserves 10 integer elements
int t;
return 0;
}
listing 3
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i, min_value, max_value;
int list[10];
return 0;
}
listing 4
int a[10], b[10];
// ...
a = b; // error -- illegal
listing 5
// An incorrect program. Do Not Execute!
int main()
{
int crash[10], i;
return 1;
}
listing 6
// Using the bubble sort to order an array.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int nums[10];
int a, b, t;
int size;
return 0;
}
listing 7
char str[11];
listing 8
// Using cin to read a string from the keyboard.
#include <iostream>
using namespace std;
int main()
{
char str[80];
return 0;
}
listing 9
// Using gets() to read a string from the keyboard.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
return 0;
}
listing 10
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[80];
strcpy(str, "hello");
cout << str;
return 0;
}
listing 11
#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;
return 0;
}
listing 12
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
bool password();
int main()
{
if(password()) cout << "Logged on.\n";
else cout << "Access denied.\n";
return 0;
}
listing 13
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s[80];
for(;;) {
cout << "Enter a string: ";
gets(s);
if(!strcmp("quit", s)) break;
}
return 0;
}
listing 14
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str[80];
gets(str);
return 0;
}
listing 15
// Print a string backwards.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str[80];
int i;
return 0;
}
listing 16
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s1[80], s2[80];
gets(s1); gets(s2);
if(!strcmp(s1, s2))
cout << "The strings are equal\n";
else cout << "not equal\n";
strcat(s1, s2);
cout << s1 << '\n';
strcpy(s1, s2);
cout << s1 << " and " << s2 << ' ';
cout << "are now the same\n";
return 0;
}
listing 17
// Convert a string to uppercase.
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
char str[80];
int i;
return 0;
}
listing 18
int twod[10][20];
listing 19
#include <iostream>
using namespace std;
int main()
{
int t,i, num[3][4];
return 0;
}
listing 20
int multidim[4][10][3];
listing 21
int i[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
listing 22
char str[6] = "hello";
listing 23
char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
listing 24
int sqrs[10][2] = {
1, 1,
2, 4,
3, 9,
4, 16,
5, 25,
6, 36,
7, 49,
8, 64,
9, 81,
10, 100
};
listing 25
int sqrs[10][2] = {
{1, 1},
{2, 4},
{3, 9},
{4, 16},
{5, 25},
{6, 36},
{7, 49},
{8, 64},
{9, 81},
{10, 100}
};
listing 26
#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;
// look up i
for(j=0; j<10; j++)
if(sqrs[j][0]==i) break;
cout << "The square of " << i << " is ";
cout << sqrs[j][1];
return 0;
}
listing 27
#include <iostream>
#include <cstring>
using namespace std;
void f1();
int main()
{
f1();
f1();
return 0;
}
void f1()
{
char s[80]="this is a test\n";
cout << s;
strcpy(s, "CHANGED\n"); // change s
cout << s;
}
listing 28
char e1[14] = "Divide by 0\n";
char e2[23] = "End-of-File\n";
char e3[21] = "Access Denied\n";
listing 29
char e1[] = "Divide by 0\n";
char e2[] = "End-of-File\n";
char e3[] = "Access Denied\n";
listing 30
int sqrs[][2] = {
1, 1,
2, 4,
3, 9,
4, 16,
5, 25,
6, 36,
7, 49,
8, 64,
9, 81,
10, 100
};
listing 31
char str_array[30][80];
listing 32
gets(str_array[2]);
listing 33
// Enter and display strings.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int t, i;
char text[100][80];
return 0;
}
listing 34
char name[10][80]; // this array holds employee names
char phone[10][20]; // their phone numbers
float hours[10]; // hours worked per week
float wage[10]; // wage
listing 35
// Enter information.
void enter()
{
int i;
char temp[80];
listing 36
// Display report.
void report()
{
int i;
listing 37
// A simple employee database program.
#include <iostream>
using namespace std;
int menu();
void enter(), report();
int main()
{
int choice;
do {
choice = menu(); // get selection
switch(choice) {
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default: cout << "Try again.\n\n";
}
} while(choice != 0);
return 0;
}
return choice;
}
// Enter information.
void enter()
{
int i;
char temp[80];
// Display report.
void report()
{
int i;