Week 07 Tutorial Questions
-
Well done everyone for making it to the end of assignment 1! For those of you in earlier tutorials, you've almost crossed the finish line! For those that are in later ones, congratulations on completing your first programming assignment!
Take a moment to think about how you found the assignment, and what you learned in the process of working on it.
If you could go back in time to the very start of the assignment, and give one piece of advice to your past self, what would you say?
Marking will not have started on the assignment before your tutorial this week, but we expect to get most marks back to students by mid-week 9.
Your tutor will talk about how you will get feedback from the marking of assignment 1.
-
How do we use
printf
andscanf
to scan in characters? How doesscanf
signal to us that it has reached the end of input? -
How do we use
getchar
andputchar
? How doesgetchar
signal to us that it has reached the end of the input?Why does
getchar
return anint
instead of achar
? -
Write a program
sum_digits.c
which reads characters from its input. When the end of input is reached it should print a count of the number of digits in its input and their sum.The only functions you can use are
getchar()
andprintf()
.For example:
./sum_digits 1 2 3 o'clock 4 o'clock rock Input contained 4 digits which summed to 10 ./sum_digits 12 twelve 24 twenty four thirty six 36 Input contained 6 digits which summed to 18
-
What is a string?
-
Discuss the following program.
int secret_function(char *word) { int i = 0; int result = 0; while (word[i] != '\0') { if (word[i] >= 'a' && word[i] <= 'z') { result++; } i++; } return result; }
- What does the function do?
- What is
'\0'
? Why is this the condition of the while loop? - What is
char *word
mean? What's the difference between that andchar word[]
?
-
How do we use
fgets
? What are the inputs given to fgets? How doesfgets
signal to us that it has reached the end of the input? -
When
fgets
scans in a line of text, will it include a'\n'
at the end of the line? Is this always the case? -
Write a program
echo_twice.c
which reads lines from its input and prints them twice.The only functions you can use are
fgets
andprintf
. You can assume lines contain at most 4096 characters.For example:
./echo_twice My cat's breath smells like cat food. My cat's breath smells like cat food. My cat's breath smells like cat food. My eyes! The goggles do nothing! My eyes! The goggles do nothing! My eyes! The goggles do nothing!
-
So far in the course, we have only received input via functions like
scanf
,getchar
andfgets
. Another way your program can receive input is through command line arguments. How are command line arguments given to a program?What are
argc
andargv
? -
If the following command was run, what would
argc
andargv
contain?./program these are command line arguments
-
How would you create a variable,
stu
, which is astruct student?
-
How would you create a variable,
stu_pointer
, that points to this new struct? -
How would you give
stu
the following values by only using this new pointer?- zID: 5123456
- wam: 74.7
- name: Frankie
-
What is the use of the -> operator? Change the previous code to utilise it.
Looking back on assignment 1
getchar() and putchar()
Strings
fgets()
Command Line Arguments
Struct Pointers
Below is a struct definition for a student
which will be used
for the next set of questions.
struct student {
int zID;
double wam;
char name[MAX_NAME_LENGTH];
};