Documente Academic
Documente Profesional
Documente Cultură
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.
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.
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.
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);
}