Sunteți pe pagina 1din 8

1.

(25 points) Suppose you are provided with a 10x10 two-dimensional


array of integers named data and a one-dimensional integer array of size 10
named averages. Assume that data has been filled with integer values (all
array locations contain valid integers). Write a function that does the following:
Compute the average of values contained in each row in data and store
these values in the corresponding location in the averages array. For example,
store the average value of row 0 of data in averages[0], and so on.
You are given this function header:
void compute(int data[][], int averages[], int rows, int
column);
void compute(int data[][], int averages, int rows, int
column) {
for (int i=0; i<rows; i++) {
averages[i] = 0;
for (int j=0; j<column; j++) {
averages[i] += data[i][j];
}
averages[i] = averages[i] / column;
}
}

2.
(5 points) Can two elements of a multi-dimensional array (same array)
be of different data types? Explain why. Can I use a double variable for array
subscripting? Explain why.

Can two elements of a multi-dimensional array (same array) be of different
data types? Explain why. No. All array elements must be of same
type.

Can I use a double variable for array subscripting? Explain why. No. For
array subscripts we can only use discrete types, such as
integers, enums, etc.

































3.

(10 points) Given:


int arr[3][4];
int *ptr = &arr[0][0];
What value is added to ptr to reach the array location arr[1][2]?


6 must be added. ptr+6 points to arr[1][2].




































4.
(10 points) Describe what the following function is doing. Assume that
the arguments to myfunction have the same array size.
void myfunction(char *s, char *t)
{
int i = 0, n = 0;
char p;

n = strlen(s);
p = *s;
while ( (p != \0 )
{
t[n-i-1] = p;
i++;
s++;
p = *s;
}

return;
}

Reverses the string given in s and t holds the new
reversed string.






















5.

(10 points) Consider the following function:


void myFunction(int num, int *ptr)
{
*ptr = num;
printf(%d\n, *ptr);
num = 10;
printf(%d\n, *ptr);
}


What is the output to the screen when the following code executes?

int numberOne = 1;
int *aPointer = &numberOne;
int numberFive = 5;
myFunction(numberFive, aPointer);
printf(%d\n, *aPointer);
printf(%d\n, numberfive);
The output would be:
5
5
5
5

6.

(10 points) Describe what lines 5 through 10 each do.

1: int i;
2: char name [5];
3: int *iptr;
4: char *cptr;
5: iptr = &i; // assigns the address of i to iptr
so that iptr points to i
6: cptr = name; // assigns the address of character
array name, so that cptr points to name, or another way
it points to name[0]
7: i = 4; // assigns 4 to variable i. now i equals
to 4.
8: name[2] = b; // assigns third character of name
to b
9: *iptr = *iptr + 1; // adds 1 to i (remember iptr
points to i), therefore i becomes 5.
10: cptr = cptr + 1; // cptr is incremented by 1 to
point to name[1].

7.
(30 points) Write a function, called starIter, that use a loop to print the
pattern below when n = 4. Also, Write the recursive version of the function
called starRec.
void starIter(int n); // Iterative
void starRec(int n); // Recursive
****
***
**
*
*
**
***
****


void starIter(int n) { // iterative
for (int i=n; i>0; i++) {
for (int j=0; j<i; j++)
printf(*);
printf(\n);
}
for (int i=1; i<=n; i++) {
for (int j=0; j<i; j++)
printf(*);
printf(\n);
}
}
void starRec(int n) { // Recursive
if (n == 0) return;
for (int j=0; j<n; j++)
printf(*);
printf(\n);
starRec(n - 1)
for (int j=0; j<n; j++)
printf(*);
printf(\n);
}

S-ar putea să vă placă și