Exercise 4.1:
// Exercise 4.1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <ostream>
#include <string>
using namespace std;
int get_size()
{
return 10;
}
int main()
{
/*
Assuming get_size is a function that takes no arguments and returns an int value, which of the following definitions are illegal? Explain why.
unsigned buf_size = 1024;
(a) int ia[buf_size];
(b) int ia[get_size()];
(c) int ia[4 * 7 - 14];
(d) char st[11] = "fundamental";
*/
unsigned buf_size = 1024;
//int ia[buf_size]; // error C2057: expected constant expression
//int ia[get_size()]; // error C2057: expected constant expression
int ia[4 * 7 - 14]; // ok
//char st[11] = "fundamental"; // error C2117: 'st' : array bounds overflow
return 0;
}
Exercise 4.2:
// Exercise 4.2.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
string sa[10]; // expected all empty string
int ia[10]; //expected all zero
int main()
{
/*
What are the values in the following arrays?
string sa[10];
int ia[10];
int main() {
string sa2[10];
int ia2[10];
}
*/
string sa2[10]; // expected all non-initialized element but actually are all empty string
int ia2[10]; // expected all non-initialized element
const size_t size = 10;
size_t index = 0;
cout << "string sa[10];'s element show:" << endl;
for (index = 0; index!= size; ++index )
{
cout << index << "th's element is '" << sa[index] << "' ";
}
cout << endl;
cout << "int ia[10];'s element show:" << endl;
for (index = 0; index!= size; ++index )
{
cout << index << "th's element is '" << ia[index] << "' ";
}
cout << endl;
cout << "string sa2[10];'s element show:" << endl;
for (index = 0; index!= size; ++index )
{
cout << index << "th's element is '" << sa2[index] << "' ";
}
cout << endl;
cout << "int ia2[10];'s element show:" << endl;
for (index = 0; index!= size; ++index )
{
cout << index << "th's element is '" << ia2[index] << "' ";
}
cout << endl;
return 0;
}
Exercise 4.3:
// Exercise 4.3.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
/*
Which, if any, of the following definitions are in error?
(a) int ia[7] = { 0, 1, 1, 2, 3, 5, 8 };
(b) vector<int> ivec = { 0, 1, 1, 2, 3, 5, 8 };
(c) int ia2[ ] = ia1;
(d) int ia3[ ] = ivec;
*/
int ia[7] = { 0, 1, 1, 2, 3, 5, 8 }; // ok
//vector<int> ivec = { 0, 1, 1, 2, 3, 5, 8 }; // error C2062: type 'int' unexpected
vector<int> ivec(5,5);
//int ia2[ ] = ia; // error C2440: 'initializing' : cannot convert from 'int [7]' to 'int []'
//int ia3[] = ivec; // error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'int []'
return 0;
}
Exercise 4.4:
// Exercise 4.4.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
//How can you initialize some or all the elements of an array?
int a[] = {1,3,4,5,6,7,7,1};
const size_t size = 10;
int b[size];
for (size_t index = 0; index != size; ++index)
{
b[index] = index;
}
char c[] = {"Hello world!"};
return 0;
}
Exercise 4.5:
- fixed size
- not size variable
- over flow and out of boundary access risk
Exercise 4.6:
Exercise 4.7:
Exercise 4.8:
Exercise 4.9:
Exercise 4.10:
Exercise 4.11:
Exercise 4.12:
Exercise 4.13:
Exercise 4.14:
Exercise 4.15:
Exercise 4.16:
Exercise 4.17:
Exercise 4.18:
Exercise 4.19:
Exercise 4.20:
Exercise 4.21:
Exercise 4.22:
Exercise 4.23:
Exercise 4.24:
Exercise 4.25:
Exercise 4.26:
Exercise 4.27:
Exercise 4.28:
Exercise 4.29:
Exercise 4.30:
Exercise 4.31:
Exercise 4.32:
Exercise 4.33:
Exercise 4.34
Exercise 4.35::
Exercise 4.36:
No comments:
Post a Comment