|
Labsheet 3 - for the week commencing 12th August 2024
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 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 answer, and then prints this value to the
screen via the printf() function.
#include <stdio.h>
#include <stdlib.h>
int process_argument(char s[])
{
// DO SOMETHING WITH PARAMETER s
...
return 0;
}
int main(int argc, char *argv[])
{
// ENSURE THAT PROGRAM HAS CORRECT NUMBER OF ARGUMENTS
if (argc < 2) {
fprintf(stderr, "Usage: %s argument\n", argv[0]);
exit(EXIT_FAILURE);
}
else {
// CALL THE PROCESS FUNCTION AND COLLECT THE RESULT
int answer = process_argument(argv[1]);
// PRINT THE RESULT
printf("The answer is %d\n", answer);
// TERMINATE PROGRAM, INDICATING SUCCESS
exit(EXIT_SUCCESS);
}
return 0;
}
Note that the return datatype of the process_argument() function could
be changed to any datatype we require (e.g. to a bool), depending on
the task the process_argument() function is performing. Of course,
the datatype of the answer variable and the printf()
specifier used to print the value would also need to change.
Tasks
-
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.
-
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.
-
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]).
-
The program haversine2.c from
Workshop-3
employed a global array of structures to hold a maximium of 20 points
(latitude and longitude pairs).
This is not the best approach - firstly, unnecessary global variables should be
avoided,
and the program is limited in how many points (20) it can handle.
Take a copy of haversine2.c from the sample solution
and modify it to employ a variable-length-array that
is defined in main().
-
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.
-
Each call to the standard C11 function rand() returns a
different random integer.
Running the same program multiple times results in exactly the
same sequence of random integers.
While this is generally unexpected ("hey, they are not random"!),
it is very helpful for debugging programs without the randomness.
We can provide each execution with a more random sequence of random numbers
by seeding the random number generator with the C statement
srand( time(NULL) );
- Write a simple program to fill an array of 10 integers
with random numbers from rand().
Run the program several times, printing the contents of the array.
- Now, use srand() to seed the generation of random numbers.
Run the program several times, printing the contents of the array.
- Extend your program by passing the initialised array to another
function which finds and prints the largest value in the array.
- Finally, extend the program's function to place the array's
largest value into the array's first element (index position 0),
"pushing" all other values down in the array
(0→1, 1→2, 2→3, and so on).
-
🌶
Write a function named replace() that replaces all instances of one string for another string in a third string.
For example:
prompt> ./replace red blue aredrobin
abluerobin
prompt> ./replace cat bison catocathartic
bisonobisonhartic
A reasonable prototype for the function is:
void replace( char oldword[], char newword[], char whole_sentence[] );
Ensure you have terminated your string correctly.
-
🌶
Lecture 5
introduced C11's structures and presented an example of how
application programs can request and receive information from the
underlying operating system
The example involved the gettimeofday() system-call and the
struct timeval structure.
Similarly, applications can determine information about a file's attributes using
the stat() system-call and the
struct stat structure.
Note that we need to read Section 2 of the online manual:
prompt> man 2 stat
otherwise we'll receive the documentation from Section 1.
Write a program which accepts a number of filenames on the command-line,
and prints (just as an integer)
the modification-time of each file.
Now, extend the program to also print each file's size (in bytes)
and the (more useful string)
modification-time of each file,
using the ctime() function.
-
🌶
🌶
🌶
If you had to write some code that iterated through all the
possibilities for three variables in the range 0 to 10,
you would probably write code similar to:
for(int a = 0; a < 10; ++a) {
for(int b = 0; b < 10; ++b) {
for(int c = 0; c < 10; ++c) {
; // 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.
|