Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

Students are expected to attempt all of the questions in the Multi choice, Short answer and Lab quesitons BEFORE the lab time.

Multi choice

Q1: Which of the following statements is false?

1) Every variable in C has an address regardless of its type (except variables that are just stored in registers - which is really more of a compiler optimization)
2) For a variable defined as: int x; you can modify its address by saying &x = 1234;
3) On a 64-bit processor the statement: sizeof(int*) would resolve to the number 8
4) The statement *x = 0; is valid for a variable defined: int x[5];

A) 1B) 2C) 3D) 4E) None of the above

Q2: How do you open a file called "data.txt" such that it is write only, created if needed, and truncated to an empty file?

A) int fd = open("data.txt",O_CREATO_WRONLYO_TRUNC, 00666);B) int fd = open("data.txt", O_WRONLYO_RDONLY, 00666);

Q3: Which of the following statements are true about a struct?

1) A struct can store void typed variables
2) Calling sizeof() on a struct won't nececarrily return the exact sum of bytes used by variables within the struct
3) Structs can contain function definitions

A) 1 onlyB) 2 onlyC) 3 onlyD) 1 and 3

Q4: When a unix program starts executing, standard input, standard output, and standard error are like _____

your program already has ______.

A) files, openB) files, closedC) memory, allocatedD) memory, freed

Q5: Given:

int val = 7;int *ptr;ptr = &val;

Which of the following statements would print the value of "val" ?

A) printf("%d",ptr);B) printf("%d",*ptr); | C) printf("%d",&val); | D) printf("%d",&ptr);

Q6: Given:

int val = 7;int *ptr;

Which of the following statements would make the pointer "ptr" point to the variable "val"?

A) val = val;B) val = ptr;C) *ptr = val; | D) ptr = &val;

Q7: What would the below output to standard out?

    int val=5;    int *pointer;    pointer = &val;    *pointer = 7;    printf("%d",val);
A) 0B) 7C) 5D) -1

Q8: The "read" system call ______________ of data from a file.

A) reads 0 or more bytesB) only ever reads a byteC) always reads 4KiBD) maps into memory all

Q9: Given:

struct rec {   int a;   int b;};struct rec r1;

To obtain the value of "b" within "r1" you would use the expression __________

A) r1.bB) b.r1C) r1[b]D) r1->b

Short answer questions

Q10: Explain what the following statements mean. Also say if any variables should be pointers for the statement to be legal.

    <li>a * b</li><li>*a = b</li><li>a = &amp;b</li><li>int b[5]; a = *(b + 4)</li>

Q11: In a Unix-based operating systems, how does an application program interact with files? What are some advantages and disadvantages in the approach taken by unix systems?

Q12: What are the two different ways (in terms of syntax) to get an element from a struct (assuming you have a pointer to the struct)?

Lab Excercises

Q13 (Excercise)Write a program which opens a file which has a list of names and people's heights and stores all the peoples in an array of "struct".

The input file format contains names and heights on each line and the name and height is space separated (assume the name length is at most 30 characters and has no spaces in it i.e. it is just a persons first name, also the height is given in cm and is just an integer). You may assume there is at most 100 entires in the file. Once loaded simply print the list of names along with height to standard out The file name can be fixed to "data.txt". A typical interaction with the program would be:
    $ cat data.txt    Eric 170    Bill 151    Jill 193    Mill 167    $ ./loadandlist    Eric 170    Bill 151    Jill 193    Mill 167  

Q14 (Excercise)

Modify your "loadandlist" program such that it finds and reports the tallest person in the list of people. The interaction would now be:
    $ cat data.txt    Eric 170    Bill 151    Jill 193    Mill 167    $ ./loadandlist    Eric 170    Bill 151    Jill 193    Mill 167    Jill is the tallest.  

Q15 (Excercise)

Write a program the takes a list of numbers from standard input and stores them in a linked list data structure. Once loaded the program sums the numbers and outputs this sum to standard out. Note you must create your own linked list data structure (hint use struct and malloc). A typical interaction would be:
    $ echo "2 7 8 12" | ./linkedlist    29  

Q16 (Excercise)(COMP6300 question, optional for students in COMP2300) Modify the above program so that it stores the data in a linked list of structs. Use malloc to dynamically allocate memory as entries are added. Consider the performance, in terms of speed, difference between this and the previous version. Can you measure the difference?

In-class Group Task

Q17:

The goal of this group exercise is to gain some more practice and experience in programming c. To this end you are required to write a program that controls a car on a race track. The cars will race against each other over the network. However, to start with you will get your car going by itself.

This exercise may be completed in groups of 2 or 3 people.


Write a client:
  1. Download
  2. In the file client/control.c, implement the logic to accelarate and steer a car based on input recieved from the five sensors you can see in the image below.
  3. Compile it by opening a terminal in the client folder and typing make. Do this each time you make a change.
Test the client:
  1. Open a terminal in the server folder and type java -jar server.jar track1.png 12345 (Note: The track and port number can be different)
  2. Open another terminal in the client folder and type ./client localhost 12345
  3. Press Ctrl+C to stop the client
If you look in the client.h file you will find all the input and output values for your car:
InputPacket:Each sensor is 30 pixels long.
These are the surface types:OutputPacket:

Use tintr/tintg/tingb to change your car's colour and make it unique from everyone else's car.
At the end of the lesson you will be able to connect to the tutor's server and race your car against others.
Hint: Look at client.h for the possible input/output values.


Press 'r' to reset all the cars back to the start line.

Once you have complete a few races your tutor will get you together and as a group you can share the different approaches groups took in implmenting their car.