It's UWAweek 47

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.
Displaying the 11 articles in this topic
Showing 11 of 828 articles.
Currently 97 other people reading this forum.


 UWA week 34 (2nd semester, week 5) ↓
SVG not supported

Login to reply

👍?
helpful
2:09pm Mon 19th Aug, ANONYMOUS

for line C, it is: for(int i=0 ; i<N ++i){ ... } seems the answer should be line A. But is it okay that it doesn't have a semicolon after i<N?


SVG not supported

Login to reply

👍?
helpful
4:31pm Mon 19th Aug, ANONYMOUS

Line A is valid; the syntax char array[N]; declares an array named "array" and allocates enough memory for N-many chars to be stored in the array, (see here).

There's definitely a syntax error in line C: there's no semicolon after i<N, so the compiler considers i<N ++i to be a whole expression, which is invalid.


SVG not supported

Login to reply

👍?
helpful
4:37pm Mon 19th Aug, Christopher M.

ANONYMOUS wrote:

Line A is valid; the syntax char array[N]; declares an array named "array" and allocates enough memory for N-many chars to be stored in the array, (see here).

There's definitely a syntax error in line C: there's no semicolon after i<N, so the compiler considers i<N ++i to be a whole expression, which is invalid.

I'm afraid both of your points are incorrect. Please see: https://secure.csse.uwa.edu.au/run/help2002?p=np&opt=B72


SVG not supported

Login to reply

👍?
helpful
4:52pm Mon 19th Aug, ANONYMOUS

Oh, I see, there's a semicolon after the N... oops. I'll be mindful of that in future.

When you say both your points, are you saying that for(int i=0 ; i<N ++i){ is valid syntax? Or are you talking about the response above mine?

Thanks for the response.


SVG not supported

Login to reply

👍x1
helpful
4:55pm Mon 19th Aug, ANONYMOUS
✅ content verified by our teaching staff  

N is replaced with "10;" - so the syntax becomes valid.. for(int i=0; i<10; ++i)


SVG not supported

Login to reply

👍x1
helpful
4:55pm Mon 19th Aug, Christopher M.

ANONYMOUS wrote:
> When you say both your points, are you saying that `for(int i=0 ; i<N ++i){` *is* valid syntax?
It's certainly an error if you correctly perform the textual substitution for N . A couple of students have asked if this is a trick question - not really, misunderstandings about how the preprocessor works are reasonably common.


SVG not supported

Login to reply

👍?
helpful
8:13am Wed 21st Aug, ANONYMOUS

How can the answer still be A when line A is valid and line C contains a syntax error?


SVG not supported

Login to reply

👍?
helpful
8:15am Wed 21st Aug, Christopher M.

ANONYMOUS wrote:
> How can the answer still be A when line A is valid and line C contains a syntax error?
Line A is not valid, and line C does not contain a syntax error. Cut-paste-compile the code if you need to.


SVG not supported

Login to reply

👍?
helpful
1:51pm Wed 21st Aug, ANONYMOUS

Wouldn't this mean that A and B are incorrect. Since we define N as 10; // which includes the semicolon. That means for A, Substitution: char array[10;]; The semicolon causes a syntax error here. For B, int x = 2 * 10;; This results in a double semicolon (;;), which is a syntax error. For C, I think that is okay for the semicolon to be before ++i Thanks!


SVG not supported

Login to reply

👍x1
helpful
2:01pm Wed 21st Aug, ANONYMOUS
✅ content verified by our teaching staff  

Not sure if I am correct but the reason why B (int x = 2 * 10;;) is because it would take the second semicolon as an empty statement so it'll be read as int x = 2 * 10; ; which would not affect the program itself but will just look weird and isn't good practice


SVG not supported

Login to reply

👍?
helpful
2:21pm Wed 21st Aug, Christopher M.

ANONYMOUS wrote:
> Not sure if I am correct but the reason why B (int x = 2 * 10;;) is because it would take the second semicolon as an empty statement so it'll be read as > int x = 2 * 10; > ; > which would not affect the program itself but will just look weird and isn't good practice
This is the correct reasoning.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  8:08AM Aug 25 2024
Privacy policy