联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2020-11-08 08:01

ECS 36A: Programming Assignment #3

Fall 2020

Contents

1 Changelog 1

2 General Submission Details 1

3 Grading Breakdown 2

4 Submitting on Gradescope 2

4.1 Regarding Autograder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

4.1.1 Number of Visible vs. Hidden Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

4.1.2 Test Cases’ Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

5 Programming Problems 3

5.1 File Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

5.2 Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

5.3 Part #1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

5.4 Part #2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

5.5 Part #3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

5.6 Part #4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

5.7 Part #5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1 Changelog

You should always refer to the latest version of this document.

• v.1: Initial version.

• v.2: For part #5, you may assume that the strings are mutable.

• v.3: Updated deadline to be Friday night, as was confirmed in a previous Canvas announcement. Added autograder

details and grading breakdown.

• v.4: Explicitly stated what the function in part #1 is supposed to return.

2 General Submission Details

Partnering on this assignment is prohibited. If you have not already, you should read the section on

academic misconduct in the syllabus.

This assignment is due the night of Friday, November 6. Gradescope will say 12:30 AM on Saturday, November 7, due

to the “grace period” (as described in the syllabus). Be careful about relying on the grace period for extra time; this could be

risky.

You should use the -Wall and -Werror flags when compiling. The autograder will use these flags when it compiles your

program.

∗This content is protected and may not be shared, uploaded, or distributed.

1

3 Grading Breakdown

The autograder score will be out of 50 points. Below is the number of points corresponding to each part:

• Part #1: 10

• Part #2: 5

• Part #3: 10

• Part #4: 10

• Part #5: 15

4 Submitting on Gradescope

You should only submit your C files with the names indicated below. You may be penalized for submitting additional

files. You have infinite submissions until the deadline.

During the 10/02 lecture, I talked about how to change the active submission, just in case that is something that you

find yourself wanting to do.

4.1 Regarding Autograder

Your output must match mine exactly.

There is a description about how to interpret some of the autograder error messages in the directions for the previous

two programming assignments. I will not repeat that description here.

Only submit prog3.c.

4.1.1 Number of Visible vs. Hidden Cases

Below is the breakdown of visible vs. hidden test cases on each part.

Part No. Cases No. Visible No. Hidden

4.1.2 Test Cases’ Inputs

For the visible test cases, the autograder uses the files part1_visible.c, part2_visible.c, part3_visible.c, part4_visible.c,

and part5_visible.c. You can find all of these on Canvas. Note that these do not use the same main() implementations that

were shown in the examples for each of the parts.

In compiling your code, the autograder first does the line gcc -Wall -Werror -c prog3.c -o prog3.o to see if your own code

has any compiler errors. If your own code has no compiler errors, then it will be converted into an object file called prog3.o;

we’ll talk more about how this works once we get to slide deck #7, but for now, you can take the command for granted.

After executing the aforementioned command, the autograder then does one of the following commands, depending on which

part is being tested:

• Part #1: gcc -Wall -Werror part1_visible.c prog3.o -o part1_visible

• Part #2: gcc -Wall -Werror part2_visible.c prog3.o -o part2_visible

• Part #3: gcc -Wall -Werror part3_visible.c prog3.o -o part3_visible

• Part #4: gcc -Wall -Werror part4_visible.c prog3.o -o part4_visible

• Part #5: gcc -Wall -Werror part5_visible.c prog3.o -o part5_visible

When running the appropriate executable based on which part is being tested, the case number is passed as a “commandline

argument”. You should look closely at each of the autograder C files to see how this is done, if you are trying to use the

autograder files to manually test your own code. Below are a few examples.

Run case #2 of part #1, using the inputs mentioned a few paragraphs below.

1 $ ./ part1_visible 2

2 Calling enterUntilN () ...

3 Entered : 15

4 Entered : -15

5 Entered : 35

2

6 Finished call to enterUntilN () ...

7 Return value of enterUntilN () : 3

Run case #1 of part #2.

1 $ ./ part2_visible 1

2 Calling sum3 () ...

3 Finished call to sum3 () ...

4 Variable referenced by fourth argument was set to ( original value -1000) : 10

Run case #3 of part #3.

1 $ ./ part3_visible 3

2 Calling getNthOccurrence () ...

3 Finished call to getNthOccurrence () ...

4 Return value of getNthOccurrence () : 8

For the hidden test cases, the autograder uses the files part3_hidden.c, part4_hidden.c, and part5_hidden.c. These files will

not be released until after the deadline.

The function for part #1, enterUntilN(), asks for user input. Below are the user inputs that the autograder uses for the

three test cases for part #1.

• Case #1:

5 Programming Problems

5.1 File Organization

Each function mentioned below will go in a file called prog3.c. Each such function is declared in the file prog3.h on Canvas.

We will eventually talk about managing C code that spans multiple files, but for now, it suffices to think of the purpose of

prog3.h as being to allow prog3.c and the tester programs (which have their own implementations of main()) to “know about”

the same functions and those functions’ information. Compiling files that involve multiple C files is not much more difficult

than the compilation you’ve done so far, and you can see how such compilation is done in the examples for each part.

You should start with the skeleton version of prog3.c that is provided on Canvas.

5.2 Restrictions

When you submit your prog3.c to Gradescope, it cannot have a main() implementation in it.

Since you will only submit prog3.c to the autograder, you are not allowed to modify prog3.h.

Below is a list of headers that you are allowed to include in prog3.c. You may not need all of these.

• prog3.h (isn’t necessary; we’ll talk about why one might do this later)

• <stdio.h>

• <stdlib.h>

• <stdbool.h>

• <ctype.h>

3

5.3 Part #1

In prog3.c, implement the enterUntilN() function that is declared in prog3.h. This function takes a single argument, an

unsigned integer we’ll call n. The function should continue to prompt the user to enter integers until the absolute value of

the sum of the integers entered by the user is at least n.

You may assume that arithmetic overflow will never be an issue, unless you do something bizarre such as use a short to

store the sum or use a char to store the user’s input.

This function should return the number of values entered by the user.

Below are examples of how your program should behave.

1 $ cat test_enterUntilN . c

2 # include < stdio .h >

3

4 # include " prog3 .h"

5

6 int main ()

7 {

8 unsigned numVals = enterUntilN (8) ;

9 printf (" numVals =% d\n" , numVals );

10 }

11 $ gcc - Wall - Werror test_enterUntilN .c prog3 .c -o test_enterUntilN

12 $ ./ test_enterUntilN

13 Entered : 5

14 Entered : 2

15 Entered : 3

16 numVals =3

17 $ ./ test_enterUntilN

18 Entered : -4

19 Entered : -4

20 numVals =2

21 $

5.4 Part #2

In prog3.c, implement the sum3() function that is declared in prog3.h. This function takes four arguments: three integers

and one pointer to an integer. This function should place the sum of the three integers into the variable referenced by the

fourth argument.

If the pointer given is NULL, then your function should do nothing (and avoid crashing).

Below are examples of how your program should behave.

1 $ cat test_sum3 .c

2 # include " prog3 .h"

3

4 # include < stdio .h >

5

6 int main ()

7 {

8 int s;

9 sum3 (4 , 10 , -1 , & s);

10 printf ("% d\n ", s);

11 sum3 (0 , 2, -10 , & s);

12 printf ("% d\n ", s);

13 }

14 $ gcc - Wall - Werror test_sum3 . c prog3 . c -o test_sum3

15 $ ./ test_sum3

16 13

17 -8

18 $

5.5 Part #3

In prog3.c, implement the function getNthOccurrence() that is defined in prog3.h. The first two arguments of this function

are an array of integers and the length of this array. The third argument is a target integer, and the fourth argument is

an integer that we’ll call n. The function should return the index of the n-th occurrence of the target in the array. If the

function is given a null pointer as the first argument or if n is not positive, then the function should return −1. If there is

no n-th occurrence of the target (i.e. if the target does not appear at least n times in the array), then the function should

return −2.

Below are examples of how your program should behave.

4

1 $ cat test_getNthOccurrence . c

2 # include < stdio .h >

3

4 # include " prog3 .h"

5

6 int main ()

7 {

8 int arr [] = {17 , 11 , 14 , 17 , 19 , 11 , 15 , 17};

9 int x = getNthOccurrence ( arr , 8, 17 , 2) ;

10 printf ("% d\n ", x); // 3

11 x = getNthOccurrence ( arr , 8 , 17 , 3) ;

12 printf ("% d\n ", x); // 7

13 x = getNthOccurrence ( arr , 8 , 11 , 1) ;

14 printf ("% d\n ", x); // 1

15 x = getNthOccurrence ( arr , 8 , 14 , 2) ;

16 printf ("% d\n ", x); // -2

17 }

18 $ gcc - Wall - Werror test_getNthOccurrence .c prog3 .c -o test_getNthOccurrence

19 $ ./ test_getNthOccurrence

20 3

21 7

22 1

23 -2

24 $

5.6 Part #4

This function is intentionally similar to the previous one.

In prog3.c, implement the function countOccurrences() that is defined in prog3.h. The first two arguments of this function

are an array of integers and the length of this array. The third argument is a target integer, and the fourth argument is a

pointer called first that references an integer. countOccurrences() should return −1 if it is given any null pointers. Otherwise,

the function should return the number of times that the target integer appears in the array. The function should place the

index of the first occurrence of the target into the integer that is referenced by first. If the target integer never appears in

the array, then the integer referenced by first should not be modified.

Below are examples of how your program should behave.

1 $ cat test_countOccurrences . c

2 # include < stdio .h >

3

4 # include " prog3 .h"

5

6 int main ()

7 {

8 int arr [] = {18 , 52 , 52 , 49 , 52 , 61};

9 int first = -1;

10 int num = countOccurrences ( arr , 6, 52 , & first );

11 printf (" num : %d \n", num );

12 printf (" first : %d\n" , first ) ;

13 }

14 $ gcc - Wall - Werror test_countOccurrences .c prog3 .c -o test_countOccurrences

15 $ ./ test_countOccurrences

16 num : 3

17 first : 1

18 $

5.7 Part #5

In prog3.c, implement the function getNumBackwards() that is defined in prog3.h. This function takes two strings as its

arguments. This function should return −1 if it is given a null pointer. Otherwise, the function should return the number of

times that the second string appears backwards in the first string.

You may assume that the given strings are both mutable.

Below are examples of how your program should behave.

1 $ cat test_getNumBackwards .c

2 # include < stdio .h >

3

4 # include " prog3 .h"

5

6 int main ()

5

7 {

8 char s1 [] = " abracadabra ";

9 char s2 [] = " arb ";

10 int num = getNumBackwards (s1 , s2 );

11 printf ("% d\n ", num ); // 2

12 char s3 [] = " apple ";

13 char s4 [] = " pa ";

14 num = getNumBackwards (s3 , s4 ) ;

15 printf ("% d\n ", num ); // 1

16 }

17 $ gcc - Wall - Werror test_getNumBackwards .c prog3 .c -o test_getNumBackwards

18 $ ./ test_getNumBackwards

19 2

20 1

21 $

6


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp