· web view(3. continued )parts a, b, and c are independent. when doing part b, assume the...

21
ECS 102 - Spring 2017 Baruch Exam 3 (modified)

Upload: vuongnga

Post on 18-Apr-2018

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

ECS 102 - Spring 2017Baruch

Exam 3 (modified)

Page 2:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

1. What is the output? (It may help to draw memory.)

#include <stdio.h>#include <string.h>int main(){ int colorCode[]={1,2,2,3,1}; char colorChoice[][7]={"red","blue","purple"}; char townColor[5][7]; int i, loc; for (i=0; i<5; i++) { loc=colorCode[i]-1; strcpy(townColor[i],colorChoice[loc]); } for (i=0; i<5; i++) printf("%s\n",townColor[i]); return 0;}

Page 3:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

2. What is the output? (It may help to draw memory. Circle the output.)#include <stdio.h>#define RMAX 5#define CMAX 6int main(){ char sb[RMAX][CMAX]; int r,c; for (r=0; r<RMAX; r++) for (c=0; c<CMAX; c++) sb[r][c]=' '; for (r=0; r<4; r++) sb[r][1]='X'; for (c=0; c<CMAX; c++) { sb[0][c]='-'; sb[RMAX-1][c]='-'; } for (r=0; r<RMAX; r++) sb[r][r]='M'; for (r=0; r<RMAX; r++) { for(c=0; c<CMAX; c++) printf("%c", sb[r][c]); printf("\n"); } return 0;}

Page 4:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

3. Here is a program with 3 function calls commented out. The questions appear on the next page.

#include <stdio.h>

int myRemove(int list[], int* sizeP,int index);

int main(){ int years[20] = {2016, 1925, 1999, 2004, 2006}; int currentSize=5; int k; int value; //part A //value = myRemove(years, &currentSize, 2);

//part B // value = myRemove(years, &currentSize, 7);

//part C //value = myRemove(years, &currentSize, 0); for(k=0; k<currentSize; k++) printf("%d %d\n",k, years[k]); if (value>=0) printf("the value %d was removed\n", value); else printf(" nothing removed\n"); return 0;}

int myRemove(int list[], int* sizeP,int index){ int i; int value; if (index>=*sizeP || index<0) return -1; value = list[index]; for (i=index; i<(*sizeP)-1; i++) list[i]=list[i+1]; (*sizeP)--; return value;}

Page 5:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the function calls used in parts A and B are commented out.A) What is the output when we uncomment only the function call under Part A, that isvalue = myRemove(years, &currentSize, 2);

B) What is the output when we uncomment only the function call under Part B, that isvalue = myRemove(years, &currentSize, 7);

C) What is the output when we uncomment only the function call under Part C, that isvalue = myRemove(years, &currentSize, 0);

Page 6:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

4. Write the function definition forvoid printLegal(int ages[], int IDs[], int size, int legalAge)ages and IDs are parallel arrays, both with size elements. printLegal should print the ID of each person whose age is at least legalAge. (Use a space between each age.) It should print nothing else.

5. Consider the following incomplete function:

int longWords(char words[][40], int size){ int count=0; int i; for (i=0; i<size; i++) { // code goes here } return count;}The function is intended to return the number of strings in the array words (with size elements) that are longer than 10 letters. Which of the following code segments could be used to replace "//code goes here" ?(Circle all that will work.)

A) while (strlen(words[i]>10)) count++;B) if (strlen(words[i]>10)) count++;

C) if (i>10)) count++;

Page 7:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

6. Consider the following incomplete function:void sumDown(int numbers[][20], int numrows, int numcols,int sums[]){ //code goes here}The function is intended to sum each column of the array numbers and store each column sum in the array sums. numrows is the number of rows of the array numbers and numcols is the number of columns in the array numbers. You may assume thata sums is big enough to hold the necessary sums. Which of the following code segments could be used to replace "//code goes here" ? (Circle all that will work.)A) int r,c; for (c=0; c<numcols; c++) sums[c]=0; for (c=0; c<numcols; c++) { for (r=0; r<numrows; r++) { sums[c]+=numbers[r][c]; } }

B) int r,c; for (c=0; c<numcols; c++) sums[c]=0; for (r=0; r<numrows; r++) { for (c=0; c<numcols; c++) { sums[c]+=numbers[r][c]; } }

C) int r,c; for (r=0; r<numrows; r++) sums[r]=0; for (c=0; c<numcols; c++) { for (r=0; r<numrows; r++) { sums[r]+=numbers[r][c]; } }

Page 8:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

7. Which of the following can be used to initialize the array a to all zeros? (Circle all that will work.)A) int a[5]=0;B) int a[5]={0};C) int a[5]={0,0,0,0,0};

8. For each of the following, what is the output? If the output is an address, just write "an address".A) int x[]={10,20,30,40}; int* ptr; ptr=&x[0]; printf("%d",*ptr);

B) int x[]={10,20,30,40}; int* ptr; ptr=x; printf("%d",*ptr);

C) int x[]={10,20,30,40}; int* ptr; ptr=&x[1]; (*ptr)++; printf("%d",*ptr);

D) int x[]={10,20,30,40}; int* ptr; ptr=&x[1]; *(ptr++); printf("%d",*ptr);

Page 9:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

9. In main we have the following declarations:double ages[50];int size; //the number of items in agesint biglocation;double bigvalue;//assume there is other code that stores values in ages and size.

Refer to this code for each of the following. Use the declared variables.A) int findBig(double list[], int size)size is the number of items in the array list.findBig returns, as the function return value, the index of the largest element in list.

Write a code segment for main that calls findBig and uses the result to print the both the index of the largest value in ages and the largest value of ages.Sample output:The largest value is 47.5 at index 13.

B) void big(double list[], int size, double * bigValP)size is the the number of items in the array list.big finds the largest value in list and puts it in *bigValP.

Write a code segment for main that calls big and uses it to print the largest value of list.

C) void changeValue(double * d)*d is both an input and output parameter. changeValue changes the value of *d.

Write a code segment for main that calls changeValue and uses it to modify the value of ages[2].

D) double someCalculation(double x, double y)x and y are input parameters.someCalculation does a calculation with x and y and returns the result as the function's return value.

Write a code segment for main that calls someCalculation with values ages[0] and ages[1], and prints the result.

Page 10:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

10. Consider the array of double

10.9 2.4 5.1 1.4 3.2 8.7 6.1

A) If selection sort is used to order the array from smallest to largest values, which of the following represents a possible state of the array at some point during the selection sort process? (Circle a, b, c, or d.)

a) 5.1 2.4 1.4 3.2 6.1 8.7 10.9

b) 2.4 5.1 1.4 3.2 6.1 10.9 8.7

c) 2.4 5.1 1.4 10.9 3.3 8.7 6.1

d) 6.1 2.4 5.1 1.4 3.2 8.7 10.9

B) If bubble sort is used to order the array from smallest to largest values, which of the following represents a possible state of the array at some point during the bubble sort process? (Circle a, b, c, or d.)

a) 5.1 2.4 1.4 3.2 6.1 8.7 10.9

b) 2.4 5.1 1.4 3.2 6.1 10.9 8.7

c) 2.4 5.1 1.4 10.9 3.3 8.7 6.1

d) 6.1 2.4 5.1 1.4 3.2 8.7 10.9

11. Consider the array of char

C O M P U T E R

If we do a shuffle, based on selection sort as we did in class, after two passes the state of the array is

E O M P R T C U

What was the first random number picked? (This refers to an index.)

What was the second random number picked? (This refers to an index.)

Page 11:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

12. Consider the file foods.txt

and the declarationschar a[20], b[20], c[20], d[20], e[20], f[20], g[20], h[20], j[20];FILE* myin;

Show the memory for each of the following. (Use \0 for the null character, \n for newline, and leave a blank for a space. ) Consider each code segment separately.

a) infile = fopen("foods.txt", "r");fscanf(infile, "%s", a);fscanf(infile, "%s", b);fscanf(infile, "%s", c);fclose(infile);

abc

b) infile = fopen("foods.txt", "r");fgets(d, 4, infile);fgets(e, 5, infile);fgets(f, 15, infile);fclose(infile);

def

c) infile = fopen("foods.txt", "r");fgets(g, 15, infile);fgets(h, 9, infile);fscanf(infile, "%s",j);fclose(infile);

ghj

fried eggpine nut rib steak

Page 12:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

13. Given this input file, what is the output? smallStore.txt

#include <stdio.h>int main(){ char itemName[20][50]; int quantity[20]; double unitprice[20]; FILE* infile; int i, finalCount; infile=fopen("smallStore.txt","r"); i=0; while(fscanf(infile,"%s",itemName[i])!=EOF) { fscanf(infile,"%d%lf",&quantity[i],&unitprice[i]); i++; } finalCount=i; printf("number of items: %d\n",finalCount); for(i=0; i<finalCount; i++) printf("%s %.2lf\n",itemName[i], quantity[i]*unitprice[i]); fclose(infile); return 0;}

wrench4 5.00pliers1 10.00mailbox0 15.66fencepost2 100.00

Page 13:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

14. Each part below uses this array.char words[][10]={"left", "right", "Left"};

A) What is the output?

if (strcmp(words[0] , words[1]) < 0 ) printf("lemon");else printf("raisin");

B) What is the output?

if (strcmp(words[0] , words[2]) == 0 ) printf("turn");else printf("march");

C) What is the output?

char build[20];sprintf(build,"%s or %s", words[0], words[1]);printf("%s",build);

Page 14:  · Web view(3. Continued )Parts A, B, and C are independent. When doing part B, assume the function call used in part A is commented out. When doing part C, assume the ... The function

15. Assume we have the following struct:typedef struct { int hour; int minute; int second; char amPm; // a for am and p for pm} timeType

timeType now = {8,15,22, 'p'};timeType later, sometime;

a) Write a code segment that will set later to 10:45:00.

b) Write a code segment that will scan in values to the timeType sometime.

c) Write a line of code that will copy sometime to later.

d) Write a method void printAmerican(timeType t) that will print t the American way. For example, the output for now is 8:15:22 pm.

e) Use the method to print the time later.