Escolar Documentos
Profissional Documentos
Cultura Documentos
Examples
1. Code declarations for the following:
(a) A pointer to a long integer variable
long* x;
(b) An array of 70 doubles, arranged into 5 rows and 7 columns double darr[5][7];
(c) A character array called my_name, initialized with your name. char* name = Sid;
2. Consider a 2 by 3 integer array t
a) Write a declaration for t
b) How many rows does t have?
c) How many columns does t have?
d) Write the names of all elements in second row
e) Write the names of all elements in third column
f) Write a statement which sets the element in row 1 and column 2 to equal 5
t[1][2]=5;
g) Write a nested for loop to initialize value of all the elements to equal 3
for (i=0; i<2; i++){
for (j=0; j<3; j++){
t[i][j]=3;
}
}
h) Write the statements to output all the values of a row. The choice of row is input by
the user.
cin>>rowNum;
for (j=0; j<3; j++){
t[rowNum][j]=3;
3. The array price[10][4] stores the prices for last 4 weeks for 10 different products. Write
the function mean_price which takes the 4 prices as arguments and returns the average of
the four values. The returned value is stored in an appropriate cell in the array
average_price[10].
#include <iostream.h>
// function prototype
}
// finish the program to use function and calculate the
//average_price
double mean_price (double num1, double num2, double num3, double num4)
{
double average;
average = (num1+num2+num3+num4)/4;
return average;
}
4. Draw Array A and B showing the values upon completion of the following program.
#include <iostream.h>
void main()
{
int A[5][5]={1, 2, 3, 4, 5},{6, 7, 8},{0},{9, 10, 11}};
int B[7][7]={0};
for (I=2; I<=3; I+=1)
{
for (J=2; J>=1; J-=1)
{
B[I-2][J+1] = A[I+1][J-1];
}
}
} // end of main
double *nPtr;
(c)Using two separate ways assign the starting address of array numbers of type double to the pointer
variable nPtr. Show the two separate statements
nPtr=numbers;
nPtr=&numbers[0];
(d) Show three different ways of using a for loop to print the values in array numbers using the
pointer nPtr
for(i=0; i<10; i++)
cout<<*(nptr+i);
for(i=0; i<10; i++){
cout<<*(nptr);
nptr++;
}
for(i=0; i<10; i++)
cout<<nptr[i];
(e) Show the two different ways of using a for loop to print the values in array numbers using the
array name as the pointer
for(i=0; i<10; i++)
cout<<*(numbers+i);
for(i=0; i<10; i++)
cout<<numbers[i];
(f) Assuming that nPtr points to the beginning of the array numbers, what address is referred to by
nPtr+8? What value is stored at that location?
nptr+8 is equal to &numbers[8] i.e. address of numbers[8]
value stored at that location is 8.8
(g) Assuming that nPtr points to numbers[5], what address is referred to by nPtr after nPtr-=4 is
executed? What value is stored at that location?
given: nptr = numbers+5;
so: nptr-=4
which is same as nptr = nptr 4
substituting: nptr = numbers+5
we get nptr = numbers + 5 4;
nptr = numbers + 1;
final answer: nptr = &numbers[1]
value stored is 1.1
5. For each of the following, write a single statement that performs the indicated task. Assume that
floating-point variables number1 and number2 have been declared and that number1 has been
initialized to 7.3 Also, assume that variable ptr is of type char * and arrays s1[100] and s2[100] are of
type char
d) Show two ways to print the address of number1 with and without using fPtr
cout<<fptr;
cout<<&number1;
delete [] p;
7. Draw a picture of memory allocations after these statements, show variables as boxes and pointers
as ovals with arrow to the referenced variable:
int i = 42;
int k = 80;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
*p;
i;
k;
42;
i;
&i;
k = 75;
*k = 75;
p = 75;
*p = 75;
Two or more of the answers will
change i to 75.
42
42
80
80
and
and
and
and
then
then
then
then
another 42
80
42
another 80
Answers:
A.
B.
C.
D.
1
2
3
4
Answers:
A.
B.
C.
D.
1
2
3
4
Answers:
A.
B.
C.
D.
1
2
3
4
a.
b.
c.
d.
(x = y)
(x == y)
(x != y)
(strcmp(x, y))
15. What (if anything) prints when each of the following statements is executed? If the statement
contains an error, explain the error and indicate how to correct it. Assume the following variable
declarations, and consider each statement to be independent of others:
char s1[50]= jack, s2[50]=jill, s3[50], *sptr;
cout<<strcpy(s3,s2)<<endl;
cout<<strcat( strcat( strcpy(s3, s1), and), s2)<<endl;
cout<<strlen( s1 ) + strlen( s2 );<<endl;
cout<<strlen(s3) <<endl;
jill
jackandjill
8
11 (dont know depends where null character randomly occurs in memory)
16. (a)Write a single statement or a set of statements to accomplish each of the following:
Define a structure called Part containing int variable partNumber and char array partName who
values may be as long as 25 characters.
struct Part {
int
partNumber;
char partName[25];
};
(b) Declare variable a to be of type Part, array b[10] to be of type Part and variable ptr to be of type
pointer to Part
Part a;
Part b[10];
Part* ptr;
(c)Read a part number and a part name from the keyboard into the members of variable a
cin >> a.partNumber;
cin >> a.partName; //or cin.getline(partName, 24)
(d)Assign the member values of variable a to the element 3 of array b
b[3].partNumber = a.partNumber;
strcpy(b[3].partName, a.partName);
(e)Read a part number and a part name from the keyboard assign them to the members of array b in
element 5
}
(c)
s_length = 0;
int* L_ptr = &s_length;
s_length = String_Length(string_ptr, L_ptr);
void StringLength(char* str, int* strlen)
{
while (*str != \0)
(*strlen)++;
}
20. Write a function called Swap_Strings which accepts pointers to two strings (string1 and string2)
as arguments. The function then swaps the two strings, and returns void.
void Swap_Strings( char* str1, char* str2)
{
char* temp = str1;
str1 = str2;
str2 = temp;
}
21. (a) Define a struct called e_mail which is designed to store the e-mail addresses of all the
employees within the various departments of all the subsidiaries in a company. The e-mail addresses
are of the following form:
username@dept.companyname.com
The struct is to be designed such that, these e-mail addresses can be later (if required) be sorted by
either user-name or dept. name or subsidiary name. [you are not to store @ or any of . Or com]
(b) Using the following declaration (using the above created struct e_mail):
e_mail
emp_e_mail[100];
char
complete_e_mail[100][256];
write a series of statements (not the entire program) which will convert all the data in the array
emp_e_mail to e-mail addresses and store it in the array of strings called complete_e_mail [Hint:
concatenation of the members by using strcat function along with @, , and com in appropriate
sequence will do it]
Note: a typical row in the array emp_e_mail looks like
Username
dept-code
company name
The corresponding result for this row should look like as shown above
(a) struct email {
char* username;
char dept[10];
char companyname[50];
};
10