# Week 07 Tutorial Questions

#### Looking back on assignment 1

1. 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.

2. #### getchar() and putchar()

3. How do we use printf and scanf to scan in characters? How does scanf signal to us that it has reached the end of input?

4. How do we use getchar and putchar? How does getchar signal to us that it has reached the end of the input?

Why does getchar return an int instead of a char?

5. 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() and printf().

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

6. #### Strings

7. What is a string?

8. 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 and char word[]?
9. #### fgets()

10. How do we use fgets? What are the inputs given to fgets? How does fgets signal to us that it has reached the end of the input?

11. When fgets scans in a line of text, will it include a '\n' at the end of the line? Is this always the case?

12. Write a program echo_twice.c which reads lines from its input and prints them twice.

The only functions you can use are fgets and printf. 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!


13. #### Command Line Arguments

14. So far in the course, we have only received input via functions like scanf, getchar and fgets. Another way your program can receive input is through command line arguments. How are command line arguments given to a program?

What are argc and argv?

15. If the following command was run, what would argc and argv contain?

./program these are command line arguments

16. #### 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];
};

17. How would you create a variable, stu, which is a struct student?

18. How would you create a variable, stu_pointer, that points to this new struct?

19. How would you give stu the following values by only using this new pointer?

• zID: 5123456
• wam: 74.7
• name: Frankie
20. What is the use of the -> operator? Change the previous code to utilise it.