UWA Logo Computer Science & Software Engineering
C Programming (CITS1210) - Labsheet 3
   Faculty Home  |  CSSE Home  |  csentry  |  CITS1210  |  help1210

Labsheet 3: Functions and 1-dimensional Arrays

For the week commencing Monday 18th August.

Most of the following tasks will require you to write functions that take one or more arguments and perform some operation on them. Typically, these arguments will be read from the command-line of the program. The name of your program may be obtained from the variable (array element) argv[0]. The first "true" command-line argument to your program may be obtained from the variable (array element) argv[1]. Subsequent command-line arguments may be obtained with the code argv[2], argv[3], and so on. Note that all command-line arguments are strings.

The functions you write should be called from the main function of your program. Your functions should receive all required values as parameters to the function and (if needed) return a result back to the calling function. For example, in the code below, the process function receives a string argument (the first command-line argument to your program) and returns an integer value of 0. The calling function (the main function) stores the return result of the process function in a variable called ans, and then prints this value to the screen via the printf function.

#include <stdio.h>
#include <stdlib.h>

static int process( char s[] )
{
    // Do something with parameter s
    ...
    return 0;
}

int main(int argc, char *argv[])
{
    /* Exit with an error if the the number of arguments (including
       the name of the executable) is less than 2
     */
    if(argc < 2) {
	printf("Too few arguments for this program.\n");
	exit(EXIT_FAILURE);
    }
    else {
	// Call the process function and collect the result
	int ans = process(argv[1]);

	// Print the result
	printf("The answer is %d\n", ans);

	// Exit indicating success
	exit(EXIT_SUCCESS);
    }
    return 0;
}

Note that the return type of the process function could be changed to any type we require (e.g. to a Boolean), depending on the task the process function is performing. Of course, the type of the ans variable and the printf specifier used to print the value would also need to change.


This week's tasks:

Remember that we're using a to indicate if a question is a bit more difficult.
First-time programmers should aim to complete all questions without chilies.

1. Write a function named my_strlen that calculates and returns the length of a string. Your function should take one argument, a character array that represents the string, and return an integer - the length of the string. The calling function (the main function) should print the integer returned by your my_strlen function.

Test your function with some string constants, and by passing to it some command-line arguments.

2. Write a function named numberOfVowels that determines the number of vowels in a string. Your function should take one argument, a character array that holds the string, and return the integer count of the number of vowels.
3. A computer password is often consider "safe" (i.e. hard to guess) if it contains a mixture of uppercase and lowercase characters and digits. Write a function named isSafe to determine if a potential password (a string) has at least two uppercase characters, two lowercase characters, and two digits. Your function should take a single character array as its argument and return a Boolean value to indicate whether the password is considered safe or not.

See the online documentation (man pages) for help with the isalpha, islower, and isupper functions. Include the appropriate header file <ctype.h> into your program.

4. Write a function named my_strcmp to determine if one string is (lexicographically, or alphabetically) less than, equal to, or greater than another string. Your function should accept the two character arrays as its arguments, and return either: -1 if the first string is less than the second string, 0 if the two strings are equal, or 1 if the first string is greater than the second string.

Call your function from the main function with the code: my_strcmp(argv[1], argv[2]).

5. A palindrome is a word that reads the same forwards as it does backwards. For example, the words noon and madam are palindromes. Write a function named isPalindrome which determines if a string, supplied as the single character array argument to the function, is a palindrome or not, returning a Boolean. Use the strlen function to determine the length of the argument string.
6. Write a program that contains three functions to calculate the minimum, maximum, and average of the (assumed to be integer) command-line arguments supplied to the program. Each of your three functions should accept two arguments: an integer array of numbers, and an integer representing the number of values in the array. Your functions that determine the minimum and maximum should return an integer, while your function that determines the average should return a floating-point value.

You may find the sample program starter.c useful in demonstrating how to convert all the command-line arguments to your program to integers (recall, all command-line arguments are strings) and store them in an array.

7.
Extend your program from Q6 by adding a function to determine the mode of the (assumed to be integer) command-line arguments supplied to the program.
The mode of a collection of numbers is defined as the most frequent number in the collection. Your function should return an integer.
8.
Write a function to sort the (assumed to be integer) command-line arguments supplied to the program. Your function should accept two arguments: an integer array of numbers, and an integer representing the number of values in the array.

You will need a temporary array that will hold the integer values of the argument array as they are sorted (define your temporary array it be of MAXSIZE size and ensure your program does not receive more than MAXSIZE number of command-line arguments). The function should step through the argument array n times, where n is the number of values in the argument array (i.e. the second argument to the function). During each pass through the argument array, your function should locate the smallest value, and store that value in the next available position in the temporary array (you will need another variable to keep track of where you are up to the temporary array).

At the completion of the function, the temporary array should contain the numbers from the argument array in sorted order from lowest to highest.

9.
Use your sorting function from the previous question to extend your program from Q7 by adding a function to determine the median of the (assumed to be integer) command-line arguments supplied to the program. The median of a collection of numbers is defined as the number that divides the collection of numbers into two equal halves - those greater than the median and those less than the median. To determine the median of a collection of numbers, sort all the numbers from lowest to highest and select the middle one.

If there are an even number of numbers in the collection, the median is defined as the mean of the two middle values.

10.

Write a function named replace that replaces all instances of one string for another string in a third string. For example:

csse2100% ./replace red blue aredrobin
abluerobin

csse2100% ./replace cat bison catocathartic
bisonobisonhartic

Ensure you have terminated your string correctly.

11.

Write a function named displayVertical that displays three strings vertically instead of horizontally. For example:

csse2100% ./displayVertical cat bison dog
c b d
a i o
t s g
  o
  n
12.


If you had to write some code that iterated through all the possibilities for three variables in the range 0 to 10 inclusive, you would probably write code similar to:

for (int a=0 ; a <= 10 ; a=a+1)
	for (int b=0 ; b <= 10 ; b=b+1)
		for (int c=0 ; c <= 10 ; c=c+1)
			loop-body using a, b, and c;

But what if you had to extend this to 4, 5, or even 10+ "nested" loops? Instead of further nesting more loops, it is possible to write a function that acts as a "superloop", performing the equivalent of n nested loops with just one loop. Write a function to do this, taking an argument n that indicates the number of nested loops your function should perform. To do this, you will need to keep a 1-dimensional array of n values that maintain the state of each loop during the execution of your function.

Top of Page
CRICOS Provider Code: 00126G