C语言代写 C语言代做 C程序代写 C程序作业代写

C语言代写 CS Assigment代写 CMPUT代写

Goals

• Demonstrate knowledge of Types: wrapping arithmetic & storage size, Types: conversion & char & escape sequences, Function declarations & Definitions, Local vs static local vs global vs block

Code Quality Standards

Your code must meet the code quality standards. If you've taken CMPUT 174 before these should be familiar to you.

• Use readable indentation.

o Blocks must be indented (everything between { and })

o One line must not have more than one statement on it. However, a long statement should be split into multiple lines.

• Use only idiomatic for loops.

• Use descriptive variable names. It must be obvious to the person reading (and marking your code) what each variable does.

• Every switch case must have a break.

• Never use goto.

• Never use control flow without curly braces (if, else, do, while, for, etc.)

• Use , bool, true, and false to represent boolean values.

o Never compare with true, e.g. never == true.

• Do not leave commented-out code in your code.

• Provide comments for anything that's not totally and completely obvious.

• Always check to see if I/O functions were actually successful.

• On an unexpected error, print out a useful error message and exit the program.

o For invalid input from the user you should handle it by asking the user to try again or by exiting the program with exit(1), exit(2), etc. or returning 1 or 2 etc. from main.

o For unexpected errors, such as fgets failing to read anything, consider abort().

• Main must only return 0 if the program was successful.

• Do not use magic literals (magic numbers or magic strings).

o If a value has a particular meaning, give a meaningful name with #define or by declaring a constant with const.

o Values other than 0 and 1 with the same meaning must not appear more than once.

o 0 or 1 with a meaning other than the immediately obvious must also be given a name.

o String literals must not appear more than once.

o This includes magic numbers that appear in strings!

• Program must compile without warnings with gcc -std=c99 -pedantic -Wall -Wextra -ftrapv -ggdb3.

New Code Quality Standards

• Program must be architecture independent:

o Program must not rely on the sizes of int, long, size_t, or pointers.

o Program must compile without warnings with gcc -std=c99 -pedantic -Wall -Wextra -ftrapv -ggdb3 -m32. Note the added -m32!

o The result of this compilation must be an executable program.

o The 32-bit program must produce the same output as the 64-bit program.

Hints:

• Print 64-bit ints with the ll length modifier.

o You might need to cast as well.

o Example: printf("%llu", (long long unsigned) my_64bit_number);

o Example: printf("%lld", (long long) my_64bit_number);

• Print 16-bit ints with the h length modifier.

• You've been compiling for 64-bit computers all along.

o This is the default you compile on a 64-bit OS.

o However, you may also add -m64 if you want to be 100% sure.

Please be aware, Assignment 4 will have even more code quality standards!

Testing your Program

Correct input-output examples are provided. For example, q1a-test1-input.txt is the input to your ./question1program. If your program is correct, its output will match q1a-test1-expected-output.txt.

You can tell if your output matches exactly by saving the output of your program to a file with bash's > redirection operator. For example, ./question1 >my-output-1.txt will save the output of your question1 program into the file named my-output-1.txt instead of showing it on the screen. Be warned! It will overwrite the file, deleting anything that used to be in my-output-1.txt.

Similarly, you can give input to your program from a file instead of typing it by using bash's < redirection operator. For example, ./question1

These two can be combined. For example,

./question1 my-output-1.txt

will use the contents of q1a-test1-input.txt as input and save the output of your program in my-outputr-1.txt.

When you want to check if your output is correct, you can then use the diff command from bash to compare two files. For example,

diff -b my-output-1.txt q1a-test1-expected-output.txt

will compare the two files my-output-1.txt and q1a-test1-expected-output.txt and show you any differences. -btells diff to ignore extra spaces and tabs.

diff will only show you something if there's a difference between the two files. If diff doesn't show you anything, that means the two files were the same!

So, putting it all together, to check if your program handles one example input correctly, you can run:

./question1 my-output-1.txt

diff -b my-output-1.txt q1a-test1-expected-output.txt

If diff doesn't show you anything, that means the two files were the same, so your output is correct.

This is what the included scripts (test-q1a.sh, etc.) do.

However, the examples are just that: examples. If your code doesn't produce the correct output for other inputs it will still be marked wrong.

Questions

Question 1

Overview

Write a program that calculates the Tribonacci number sequence but with different sizes of integers and floats. Using unsigned shorts (16bit), unsigned ints (32bit), unsigned long long (64bit) calculate the tribonacci numbers effeciently!

Tribonnacci numbers are defined as a function

tribonnaci(n) = tribonnaci(n-1) + tribonnaci(n-2) + tribonnaci(n-3), where tribonnaci(0) = 0 and tribonnaci(1) = 0 and tribonnaci(2) = 1

The tribonnacci sequence is defined here: https://oeis.org/A000073

The first few values of the sequence are:

0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, ...

Example output

The program will read from the terminal using scanf or fgets a number between 0 and 1023. Any other numbers or inputs are invalid input! Return 1 on invalid input.

$ ./question1 

Tribonacci! Type in a positive tribonacci input:

-1

Invalid input!

$ ./question1 

Tribonacci! Type in a positive tribonacci input:

a

Invalid input!

$ ./question1 

Tribonacci! Type in a positive tribonacci input:

10

16bit Tribonacci 81

32bit Tribonacci 81

64bit Tribonacci 81

Float Tribonacci 8.100000e+01

Double Tribonacci 8.100000e+01

$ ./question1 

Tribonacci! Type in a positive tribonacci input:

10

16bit Tribonacci 81

32bit Tribonacci 81

64bit Tribonacci 81

Float Tribonacci 8.100000e+01

Double Tribonacci 8.100000e+01

$ ./question1 

Tribonacci! Type in a positive tribonacci input:

21

16bit Tribonacci 476

32bit Tribonacci 66012

64bit Tribonacci 66012

Float Tribonacci 6.601200e+04

Double Tribonacci 6.601200e+04

$ ./question1 

Tribonacci! Type in a positive tribonacci input:

76

16bit Tribonacci 26578

32bit Tribonacci 3047122898

64bit Tribonacci 5285690360152942546

Float Tribonacci 2.373244e+19

Double Tribonacci 2.373243e+19

Additional Requirements

• Put your C code for this question in question1.c

• You should compile the program as ./question1

• You must make a ./question1.sh to compile and run your question1.c program for 64-bit computers.

• You must make a ./question1-32.sh to compile and run your question1.c program for 32-bit computers.

o Add -m32 to the gcc options.

• Remember to use the right data type to calculate the correct values

• You must demonstrate the proper use of functions calls and defining functions

• Your functions should handle independent tasks and must have a few arguments.

• It is ok to copy and paste code between different types of tribonacci functions.

• You should use an array to make the computation faster.

• You program should take no more than 10 seconds to run each time for all inputs 0 to 1023.

Marking

•  1 Point Program output is correct for a valid input. (Examples: test-q1a.sh)

•  1 Point Program output is correct for an invalid input. (Examples: test-q1b.sh)

•  1 Point Program defines and uses functions.

•  1 Point Quality of question1.c meets the quality standards, listed above.

•  1 Point question1.sh meets the requirements above.

•  1 Point question1-32.sh meets the requirements above, and your program is architecture independent as described above. (Examples: test-q1a-32.sh test-q1b-32.sh)

• No points will be deducted for rounding errors.

Hints

• uint16_t, uint32_t, uint64_t are useful type definitions available to you!

• You can pass arrays to your functions to store previous results or work.

• You can use recursion as it will make the problem easier to handle.

• There are three different printf() format specifiers for printing out floats and doubles.

Question 2

Overview

We have a bunch of items categorized by the letters of english alphabet. We receive different amounts of them over time and we want to keep track of how much of each lettered item we have received. Maintain a running total of the number of a, b, c, ..., x, y, z items we have received.

Example output

The program will read from the terminal using scanf or fgets a single character descriptor---a key---followed by a count of the number of oberservations of that key. Then at the end of the program when the user quits or indicates they want to quit (by typing X) or mistypes something, the total of sum of each lettered item is printed if their sum is greater than 0.

The summary will be like

a - 1 

b - 1

if the user typed following input: a 1 b 1. Only positive counts will be displayed.

$ ./question2 

Welcome to variable counter! Please record your counts! Type X to end.

Input character variable a-z:

Input count variable a-z:

10

Input character variable a-z:

b

Input count variable a-z:

10

Input character variable a-z:

c

Input count variable a-z:

10

Input character variable a-z:

 

X

a - 10

b - 10

c - 10

 

$ ./question2 

Welcome to variable counter! Please record your counts! Type X to end.

Input character variable a-z:

a

Input count variable a-z:

7

Input character variable a-z:

Input count variable a-z:

8

Input character variable a-z:

 a

Input count variable a-z:

9

Input character variable a-z:

b 99

Input count variable a-z:

Input character variable a-z:

X

a - 24

b - 99

 

$ ./question2 

Welcome to variable counter! Please record your counts! Type X to end.

Input character variable a-z:

Input count variable a-z:

1

Input character variable a-z:

Input count variable a-z:

2

Input character variable a-z:

c

Input count variable a-z:

3

Input character variable a-z:

d

Input count variable a-z:

4

Input character variable a-z:

z

Input count variable a-z:

99

Input character variable a-z:

X

a - 1

b - 2

c - 3

d - 4

z - 99

 

$ ./question2 

Welcome to variable counter! Please record your counts! Type X to end.

Input character variable a-z:

X

 

$ ./question2 

Welcome to variable counter! Please record your counts! Type X to end.

Input character variable a-z:

aaaaaaaaaaa

 

$ ./question2 

Welcome to variable counter! Please record your counts! Type X to end.

Input character variable a-z:

what

 

$ ./question2 

Welcome to variable counter! Please record your counts! Type X to end.

Input character variable a-z:

a

Input count variable a-z:

what

 

$ ./question2 

Welcome to variable counter! Please record your counts! Type X to end.

Input character variable a-z:

a

Input count variable a-z:

10

Input character variable a-z:

what

a - 10

 

 

Additional Requirements

• Put your C code for this question in question2.c

• You should compile the program as ./question2

• You must make a ./question2.sh to compile and run your question2.c program for 64-bit computers.

• You must make a ./question1-32.sh to compile and run your question1.c program for 32-bit computers.

o Add -m32 to the gcc options

• Remember to use the right data type to calculate the correct values

• Your program should support the entire range of positive ints

• You should use array

• You should demonstrate the proper use of functions calls and defining functions

• You can pass arrays to your function to store previous results or work

• You should convert characters to integers

• You can compare against characters: inputChar < 'a' || inputChar > 'z'

Marking

•  1 Point Program output is correct for a valid input. (Examples: test-q2a.sh)

•  1 Point Program output is correct for an invalid input. (Examples: test-q2b.sh)

•  1 Point Program defines and uses arrays.

•  1 Point Quality of question2.c meets the quality standards, listed above.

•  1 Point question2.sh meets the requirements above.

•  1 Point question2-32.sh meets the requirements above, and your program is architecture independent as described above.

Submission

Test your program!

Always test your code on the VM or a Lab computer before submitting!

See "Testing your Program" above.

Make a 1 line (excluding the comments and header) shell script for each question that will compile and run the 64-bit C program for that question. Name the scripts question1.sh and question2.sh respectively. Make a 1 line (excluding the comments and header) shell script for each question that will compile and run the 32-bit C program for that question. Name the scripts question1-32.sh and question2-32.sh respectively. Make sure the program successfully compiles the program and then runs it. If the program doesn't compile it should not run the executable. The shell program should use 1 operator to achieve this and it should all fit on the same line. You can assume the shell script is run in the directory that contains both the source code and the executable. Run the test-q1a.sh script for question1. Run the test-q1b.sh script for question1. Run the test-q1a-32.sh script for question1. Run the test-q1b-32.sh script for question1. Run the test-q2a.sh script for question2. Run the test-q2b.sh script for question2. Run the test-q2a-32.sh script for question2. Run the test-q2b-32.sh script for question2. The scripts should produce no output.

Tar it up!

Make a tar ball of your assignment. It should not be compressed. The tar name is __YOUR__CCID__-assignment3.tar

京ICP备2025144562号-1
微信
程序代写,编程代写
使用微信扫一扫关注
在线客服
欢迎在线资讯
联系时间: 全天