CS 2123 Data Structures Summer 2015 - Midterm 2- July 23, 2015 You have 100 min.
ID: 3735275 • Letter: C
Question
CS 2123 Data Structures Summer 2015 - Midterm 2- July 23, 2015 You have 100 min. Good luck You can use the 2-page C reference card posted in the class web page Score: /85 1. (10pt) Review Questions a. (2pt) suppose p and q are defined as integer pointers (i.e, int *p, *q;) . Then what are the purposes of the following statements? Are they equivalent? if (p printf("...aa... are the same") if (*p ) printf (".. .bb.. . are the same" b. (2pt) What is the difference between the typedef's in the left and right columns? RE-WRITE each statement based on the declaration in the right column typedef struct myData i typedef struct myData f int x; int yi ) myDataTl; int x; int y; *myDataT2; myDataT1 *p, q myDataTl r; pmalloc (sizeof (myDataT1));Explanation / Answer
Please find solution for each point
2 a)
if (p == q) printf("...aa... are same ")
compares the address of p & q not the value.
and
if (*p == *q) printf("...bb... are same ");
compares the values at p & q
and above two statements are totaly different
Here is a code sample
int main()
{
int *p,*q;
int x =10;
int y = 10;
p = &x;
q = &x;
if (p == q) printf("...aa... are same ");
if (*p == *q) printf("...bb... are same ");
}
p & q both are pointing to x so both if statements will be true.
if we change
q = &y;
then p == q will be false as x & y are stored at different addresses. But *p == *q will be true as x & y both have value 10.
2 b)
Difference between those two typedef is first one declares a variable myDataT1 of type myData
and second one declares a pointer variable myDataT2 for type myData.
For pointer types you have to explicitly allocate space using malloc but for original type you don't need to.
Equivalent lines for second type of declaration are
myDataT2 p, q;
struct myData r;
p = malloc(sizeof(myDataT2));
p->x = 5;
r.x=7;
2 c)
int check_if_unique(int *arr, int n) {
int i,j;
int result = 1;
for(i=0;i <n;i++) {
for (j=i+1;j<n;j++) {
if (arr[i] == arr[j]) {
result = 0;
break;
}
}
if (result == 0) break;
}
return result;
}
2 d)
int arr_sum(int *arr, int n) {
if (n == 1) {
return arr[0];
} else {
return arr_sum(arr, n-1) + arr[n-1];
}
}
And here is working code for all parts. Uncomment the code that you want to run
#include <stdio.h>
#include <stdlib.h>
/**
typedef struct myData {
int x;
int y;
} myDataT1;
int main()
{
myDataT1 *p, *q;
myDataT1 r;
p = malloc(sizeof(myDataT1));
p->x = 5;
r.x=7;
printf("%d ",p->x);
printf("%d ",r.x);
}
**/
/**
typedef struct myData {
int x;
int y;
} *myDataT2;
int main()
{
myDataT2 p, q;
struct myData r;
p = malloc(sizeof(myDataT2));
p->x = 5;
r.x=7;
printf("%d ",p->x);
printf("%d ",r.x);
}**/
int arr_sum(int *arr, int n) {
if (n == 1) {
return arr[0];
} else {
return arr_sum(arr, n-1) + arr[n-1];
}
}
int check_if_unique(int *arr, int n) {
int i,j;
int result = 1;
for(i=0;i <n;i++) {
for (j=i+1;j<n;j++) {
if (arr[i] == arr[j]) {
result = 0;
break;
}
}
if (result == 0) break;
}
return result;
}
int main()
{
int arr[7] = {2,3,4,5,6,8,9};
printf("unique=%d ",check_if_unique(arr, 7));
printf("sum=%d ",arr_sum(arr, 7));
}