#include<iostream>
using namespace std;
int main() {
int a[] = {1,2,3};
int *pt;
int x = a[0];
return 0;
}
|
apta[1]xa+1pt+2cout << a << endl;
is 0012FEC4, what will be the output of the following program?
#include<iostream>
using namespace std;
int main() {
int a[] = {2,4,6,8,10};
int *pt = &a[0];
cout << a << endl;
cout << &a[0] << endl;
cout << pt << endl;
cout << *pt << endl;
cout << *(pt+1) << endl;
cout << *pt+1 << endl;
cout << pt[1] << endl;
cout << a[1] << endl;
cout << *(a+1) << endl;
cout << *(pt+2) << endl;
cout << pt[2] << endl;
cout << a[2] << endl;
cout << *(a+2) << endl;
return 0;
}
|
sum in the following program so that
no [ and ] are used in the source code.
(You shouldn't change the main function).
#include<iostream>
using namespace std;
int sum(int a[], int size); // remove [ and ]
int main() {
int a[] = {2,4,6,8,10};
cout << sum(a,5) <<endl;
return 0;
}
// remove [ and ]
int sum(int a[], int size) {
int result=0;
for(int i = 0; i < size; i++) {
result+=a[i];
}
return result;
}
|
int a[] = {0,1,2,3,4}, do you agree that a is a pointer? a point to?a+1 point to?a+2 point to?a+i points to a[i]?a[i] is same as *(a+i)?b=a+1, what is b[0], what is *b, what is *(b+1)?
#include<iostream>
using namespace std;
void printArray(int a[], int size);
int main() {
int a[] = {0,1,2,3,4};
int* pt = a;
printArray(a,5);
printArray(pt,5);
pt[2] = 7;
a[3] = 9;
printArray(a,5);
printArray(pt,5);
*pt = 3;
*(a+4) = 0;
printArray(a,5);
printArray(pt,5);
// tricky
pt=a+2;
printArray(pt,3);
printArray(a+1,2);
}
// print elements of an array in a row
void printArray(int a[], int size) {
for(int i=0; i < size; i++) {
cout << a[i] << " ";
}
cout << endl;
}
|
printArray(pt,5) and printArray(a,5)
always have the same anser? int *pt = new int and (ii) int *pt = new int[5]
#include<iostream>
using namespace std;
int main() {
char *pt, ch1='a', ch2='b';
pt=&ch1;
cout << ch1 << "," << ch2 << "," << *pt << endl;
pt=&ch2;
*pt='c';
cout << ch1 << "," << ch2 << "," << *pt << endl;
pt=new char;
*pt='d';
cout << ch1 << "," << ch2 << "," << *pt << endl;
ch2=*pt;
*pt='e';
cout << ch1 << "," << ch2 << "," << *pt << endl;
pt = new char[3];
*pt='a';
*(pt+1)='b';
*(pt+2)=ch2;
cout << *pt << "," << *(pt+1) << "," << *(pt+2) << endl;
return 0;
}
|
#include<iostream>
using namespace std;
void printArray(int a[], int size);
int main() {
int a[] = {0,1,2,3,4,5,6};
int b[] = {0,-1,-2,-3,-4,-5,-6};
int *pt;
pt = a;
printArray(a,7);
printArray(b,7);
printArray(pt,7);
cout << endl;
*pt=3;
a[2]=4;
b[3]=9;
printArray(a,7);
printArray(b,7);
printArray(pt,7);
cout << endl;
pt=b;
*pt=1;
a[4]=7;
b[5]=6;
printArray(a,7);
printArray(b,7);
printArray(pt,7);
cout << endl;
pt=pt+1;
*(a+3)=2;
*(b+5)=100;
*(pt+3)=6;
printArray(a,7);
printArray(b,7);
printArray(pt,3);
cout << endl;
pt=a;
*(pt+2) = 200;
pt=b;
*(pt+2)=300;
printArray(a,7);
printArray(b,7);
printArray(pt,7);
cout << endl;
pt = new int[7];
for(int i=0; i < 7; i++) {
pt[i] = i + 1;
}
*(pt+3) = 100;
*(a+1) = *(pt+1);
b[3] = pt[3];
printArray(a,7);
printArray(b,7);
printArray(pt,7);
cout << endl;
return 0;
}
void printArray(int a[], int size) {
for(int i=0; i < size; i++) {
cout << a[i] << " ";
}
cout << endl;
}
|
typedef ? typedef ?
#include<iostream>
using namespace std;
int main() {
float a[5] = {1,2,3,4,3.7f};
float *pt;
pt = a;
cout << pt[1];
a = pt;
cout << pt[1];
return 0;
}
|
#include<iostream>
using namespace std;
int main() {
float a[5] = {1,2,3,4,3.7f};
float *pt;
pt = a + 2;
cout << pt[5];
return 0;
}
|
#include<iostream>
using namespace std;
int main() {
float *pt;
cout << pt;
float *pt2 = new float;
cout << pt2[3];
return 0;
}
|
#include<iostream>
using namespace std;
int main() {
int num;
cin >> num;
char array[num];
for(int i=0; i < num; i++) {
cin >> array[i];
}
return 0;
}
|
int* doubler(int a[], int size) {
int temp[size];
for(int i = 0; i < size; i++) {
temp[i] = 2*a[i];
}
return temp;
}
|
double* multiply(double array[], int size, int num);. The function
returns a new array with the same size, the i-th element is the i-th element of array
multiply by num.
#include <iostream>
using namespace std;
double* multiply(double array[], int size, int num);
int main() {
double a[] = {1.1, 2.2, 0.5, 0.5, 1.3};
int size = 5;
double* b = multiply(a,5,4);
for(int i=0; i < size; i++) {
cout << b[i] << " ";
}
cout << endl;
b = multiply(a,5,3);
for(int i=0; i < size; i++) {
cout << b[i] << " ";
}
cout << endl;
return 0;
}
double* multiply(double array[], int size, int num) {
// your code
}
|
4.4 8.8 2 2 5.2 3.3 6.6 1.5 1.5 3.9 |
int* positive(int array[], int size, int& positiveSize)The function stores the number of positive numbers of
array in positiveSize
and returns a new array consists of all the positive numbers of array.
When the function is properly implemented, the output of the following program will be
#include <iostream>
using namespace std;
int* positive(int array[], int size, int& poitiveSize);
int main() {
int a[] = {1,-2,-3,4,5,6,0,-9,7,3};
int size = 10;
int positiveSize;
int* b = positive(a,10,positiveSize);
cout << "There are " << positiveSize << " positive numbers.\n";
for(int i=0; i < positiveSize; i++) {
cout << b[i] << " ";
}
cout << endl;
return 0;
}
int* positive(int array[], int size, int& positiveSize) {
// your code
}
|
There are 6 positive numbers. 1 4 5 6 7 3 |