Você está na página 1de 10

IDS 201

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

double mean_price (double, double, double, double);


________________________________________
void main()
{
double price[10][4];
double average_price[10];
int product, week;
for (product=0; product<10; product+=1)
{
cout<<endl<<Enter prices for product<<product+1<<endl;

for (week=0; week<4; week+=1)


{
cout<<endl<<For week <<week+1<<:
cin>>price[product][week];
}

}
// finish the program to use function and calculate the
//average_price

for (j=0; j<10; j+=1)


{
average_price[j]=mean_price(price[j][0], price[j][1] price[j][2]
price[j][3]);
}
} // end of main

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

I=2, J=2: B[0][3] = A[3][1] (=10)


J=1: B[0][2] = A[3][0] (=9)
I=3, J=2: B[1][3] = A [4][1] (=0)
J=1: B[1][2] = A[4][0] (=0)

4. Answer each of the following.


(a)Declare an array of type double called numbers with 10 elements, and initialize the elements to the
values 0.0, 1.1, 2.2, ., 9.9
double numbers[10]={0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9};
(b)Declare a pointer nPtr that points to a variable of type double

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

a) Declare a variable fPtr to be a pointer to an object of type double


double *fptr;

b) Assign the address of variable number1 to the pointer variable fptr


fPtr = &number1;

c) Assign the value of the variable pointed to by fPtr to variable number2


number2 = *fPtr1;

d) Show two ways to print the address of number1 with and without using fPtr
cout<<fptr;
cout<<&number1;

e) Print the value of value of number1 using fPtr


cout<<*fptr;
6. Suppose that p is an int* variable. Write several lines of code that will allocate memory for an
array of size 100 and make p point to this array (of 100 integers). Place the numbers 0 through 99
into the array cells using p.
Write the code to display the values in the array.
Deallocate the storage for the array
p = new int [100];
for (i=0; i<100; i++)
*(p+i)=i; //alternatively p[i]=i;
for (i=0; i<100; i++)
cout << p[i] << ;

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;

8. Consider the following statements:


int
int
int
i =
k =
p =

*p;
i;
k;
42;
i;
&i;

After these statements, which of the following


statements will change the value of i to 75?
a)
b)
c)
d)
e)

k = 75;
*k = 75;
p = 75;
*p = 75;
Two or more of the answers will
change i to 75.

9. Consider the following statements:


int i = 42;
int j = 80;
int *p1;
int *p2;
p1 = &i;
p2 = &j;
*p1 = *p2;
cout << i << j << endl;

What numbers are printed by the output


statement?
A.
B.
C.
D.

42
42
80
80

and
and
and
and

then
then
then
then

another 42
80
42
another 80

10. What is printed by these statements?


int i = 1;
int k = 2;
int *p1;
int *p2;
p1 = &i;
p2 = &k;
p1 = p2;
*p1 = 3;
*p2 = 4;
cout << i;

Answers:
A.
B.
C.
D.

1
2
3
4

Answers:
A.
B.
C.
D.

1
2
3
4

11. What is printed by these statements?


int i = 1;
int k = 2;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
p1 = p2;
*p1 = 3;
*p2 = 4;
cout << k;

12. What is printed by these statements?


int i = 1;
int k = 2;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
*p1 = *p2;
*p1 = 3;
*p2 = 4;
cout << i;

Answers:
A.
B.
C.
D.

1
2
3
4

13. Here is a function declaration:

Suppose that a is an int* variable pointing to


some integer, and *a is equal to zero. What is
printed if you print *a after the function call
goo(a)?
A. 0
B. 1
C. address of a
D. address of x
E. None of the above
14. Suppose that x and y are two C++ strings. Which expression will return a value of 0
whenever x and y contain the same sequence of characters?
void goo(int* x)
{
*x = 1;
}

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

17. Find the error in each of the following:


Assume that struct Card has been defined containing two pointers to type char, namely face and suit.
Also, the array hearts[13] has been declared to be of type Card. The following statement should print
the member face of element 10 of the array
(a) cout<<hearts.face<<endl;
cout << hearts[10].face <, endl;
(b) struct Person {
char lastName[15];
char firstName[15];
int age;
}
Assume a variable p has been declared as type Person (above) and that variable c has been declared as
type Card
p = c;

18. Provide the definition for each of the following structures:


(a)Structure Inventory containing character array PartName[30], integer partNumber, floating-point
price, integer stock, and integer reorder.
(b)A structure called Address that contains character arrays streetAddress[25], city[20], state[3], and
zipCode[6].
(c)Structure called Student that contains arrays firstName[15], and lastName[15], and variable
homeAddress of type Address
19. Write a program which declares a string of maximum 100 characters, and a variable s_length
whose value is to be the length of the string.
a) Main() assigns a pointer string_ptr to reference to the beginning of a string. Main() then
passes the pointer to a function called String_Length. The function calculates the length of
the string to which pointer refers to by actually incrementing the value of pointer, and returns
the length of the string to main. Main() assigns the returned value to the variable s_length.
b) Main() passes the constant pointer to String_Length. The pointer can not to be incremented
within the function. The function returns the value for length of the string to main(). Main()
assigns this to s_length.
[Hint: while (*(ptr+L) != \0) { L++; } ]
(c) In addition to earlier declarations, in main() s_length is initialized to zero and a pointer L_Ptr is
declared such that it refers to s_length. Main() passes both the pointer to the string and L_Ptr to the
function String_Length. The function String_Length does not return any value to main() but
indirectly updates the value of s_length to equal the length of the string (you may use either method a
or b to calculate length of string).
(a)
char mystr[100];
int s_length;
char* string_ptr = mystr;
s_length = String_Length(string_ptr);
int String_Length(char* str)
{
int len = 0;
while (*str != \0)
len++;
return len;
}
(b)
int String_Length(const char* str)
{
int len = 0;
while (*(str + len)!= \0)
len++;
return len;

}
(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];
};

(b) char tstr[256];


int i = 0;
while ( emp_email[i] ) {
strcpy( tstr, emp_email[I].username);
strcat (tstr, @);
strcat(tstr, dept);
strcat(tstr, .);
strcat(tstr, companyname);
strcat(tstr, .com);
strcpy( complete_e_mail[i], tstr);
i++;
}

10

Você também pode gostar