CourseWare Wiki
Switch Term
Winter 2025 / 2026
Winter 2024 / 2025
Winter 2023 / 2024
Winter 2022 / 2023
Winter 2021 / 2022
Winter 2020 / 2021
Winter 2019 / 2020
Winter 2018 / 2019
Older
Search
Log In
b251
courses
be5b99cpl
labs
lab04
Table of Contents
Lab04 - Arrays
Task 1
Task 2
Valgrind
Documentation
Lab04 - Arrays
Task 1
A command-line argument specifies the number of integers to read (N)
Read N integers from standard input using
scanf()
to a variable-length array of size N
Print the array in the order entered
Sort the array in descending order using
qsort()
Print the sorted array
Task 2
Write a program which allows the user to enter an “infinite” number of characters and will keep appending them to an array.
When they press
P
, it will output the current array.
If they enter a character
X
, it will convert the chars to upper case with
toupper()
, print them and exit.
Use the
getchar()
function to get letters from the command line.
You will need to use
malloc()
and
realloc()
to control the memory.
Don’t forget to use
free()
at the end to free the memory.
If you have time, add also sorting the chars alphabetically and lower case before upper case.
Valgrind
use
valgrind
for memory leak detection
can detect forgotten allocated memory and the exact location of out-of-bound access
can report using uninitialized variables/memory
for better output, compile your program with
-g
flag
valgrind ./your-program
Documentation
https://en.cppreference.com/w/c
Learn to understand the function declaration (the name, return type, and parameters)
The definition (code to be executed) might not always be available
courses/be5b99cpl/labs/lab04.txt
· Last modified: 2025/10/15 10:34 by
ulricji1